Add checking and updating DeepL usage information

This commit is contained in:
Thelonius Kort
2023-01-10 20:06:54 +01:00
parent 33ed533a1a
commit e16710664d
4 changed files with 21 additions and 8 deletions

View File

@ -16,12 +16,10 @@ defmodule Outlook.Translators do
def get_deepl_account!(id), do: Repo.get!(DeeplAccount, id)
def get_deepl_auth_key(user_id) do
query =
from DeeplAccount,
where: [user_id: ^user_id],
select: [:auth_key]
Repo.one!(query)
def get_deepl_auth_key(user) do
deepl_account_for_user(user)
|> select([:auth_key])
|> Repo.one()
|> Map.get(:auth_key)
end
@ -45,6 +43,10 @@ defmodule Outlook.Translators do
DeeplAccount.changeset(deepl_account, attrs)
end
def increase_our_character_count(user, billed_characters) do
deepl_account_for_user(user)
|> Repo.update_all([inc: [our_character_count: billed_characters]])
end
def translate(translation, current_user) do
%{lang: target_lang,
@ -64,6 +66,11 @@ defmodule Outlook.Translators do
Task.start_link(Deepl, :translate, args)
end
defp deepl_account_for_user(user) when is_struct(user), do: deepl_account_for_user(user.id)
defp deepl_account_for_user(user_id) do
DeeplAccount |> where(user_id: ^user_id)
end
defp prepare_article(tree) do
# Logger.info "so far."
HtmlDocComponent.render_doc(%{tree: tree})