Thanks, I got api working (it receives json payload and persists it to postgres db, really cool!) Now, I want to execute some logic before_action and after_action, but not sure if my approach is correct, should I move this to a generic action??:
defmodule Eppa.Accounts.ApiServices do
use Ash.Resource,
otp_app: :eppa,
domain: Eppa.Accounts,
data_layer: AshPostgres.DataLayer,
extensions: [AshJsonApi.Resource]
alias Eppa.Correos
alias Eppa.Accounts
alias Eppa.Accounts.Credits
json_api do
type "api_services"
end
postgres do
table "apiservicios"
repo Eppa.Repo
end
actions do
create :send_email do
argument :user_email, :string, allow_nil?: false
argument :destinatario, :string, allow_nil?: false
argument :cc, {:array, :string}, default: []
argument :asunto, :string
argument :contenido_html, :string
argument :attachments, {:array, :map}, default: []
argument :plantilla, :string
before_action(fn changeset ->
# Got no console print at all
IO.inspect(changeset, label: "before_action")
user_email = Ash.Changeset.get_argument(changeset, :user_email)
user = Eppa.Accounts.get_user_by_email!(user_email, actor: nil, load: [:servicios])
service = :correos
case Credits.check_credit(user.id, service) do
{:ok, credit_service} ->
case Credits.deduct_credit(credit_service, user, 1) do
{:ok, updated_service} ->
IO.inspect(updated_service, label: "Crédito descontado")
Ash.Changeset.put_context(changeset, :credit_service, updated_service)
{:error, errors} ->
IO.inspect(errors, label: "Error al descontar crédito")
Ash.Changeset.add_error(
changeset,
:credit,
"Error al descontar crédito: #{inspect(errors)}"
)
end
{:error, reason} ->
IO.inspect(reason, label: "Créditos insuficientes")
Ash.Changeset.add_error(changeset, :credit, "Créditos insuficientes: #{reason}")
end
end)
change set_attribute(:user_email, arg(:user_email))
change set_attribute(:destinatario, arg(:destinatario))
change set_attribute(:cc, arg(:cc))
change set_attribute(:asunto, arg(:asunto))
change set_attribute(:contenido_html, arg(:contenido_html))
change set_attribute(:attachments, arg(:attachments))
change set_attribute(:plantilla, arg(:plantilla))
after_action(fn changeset, result ->
IO.inspect("Ejecutando after_action", label: "DEBUG")
IO.inspect(result, label: "Resultado antes del envío")
destinatario = result.destinatario
cc = result.cc
asunto = result.asunto
contenido_html = result.contenido_html
attachments = result.attachments
plantilla = result.plantilla
email_response =
Eppa.Correos.enviar_email!(
destinatario,
cc,
asunto,
contenido_html,
attachments,
plantilla
)
IO.inspect(email_response, label: "Respuesta del envío de correo")
updated_changeset =
result
|> Ash.Changeset.for_update(:send_email, %{email_response: email_response})
case Eppa.Repo.update(updated_changeset) do
{:ok, updated_result} ->
IO.inspect(updated_result, label: "Registro actualizado con email_response")
:ok
{:error, err} ->
IO.inspect(err, label: "Error al actualizar email_response")
end
changeset
end)
end
end
attributes do
uuid_v7_primary_key :id
attribute :user_email, :string do
allow_nil? false
public? true
end
attribute :destinatario, :string do
allow_nil? false
public? true
end
attribute :cc, {:array, :string} do
default []
public? true
end
attribute :asunto, :string do
public? true
end
attribute :contenido_html, :string do
public? true
end
attribute :attachments, {:array, :string} do
default []
public? true
end
attribute :plantilla, :string do
public? true
end
attribute :email_response, :map, default: %{}
create_timestamp :inserted_at
update_timestamp :updated_at
end
end
Api call
{
"data":{
"type":"api_services",
"attributes": {
"user_email": "knd_rt@hotmail.com",
"destinatario": "blitzlepe@gmail.com" ,
"cc":[
"cande.lepe@agustindeiturbide.com"
],
"asunto":"Prueba desde Insomnia",
"contenido_html":"Su pinche madre, si jalo !!!",
"attachments":[],
"plantilla": "UUIDPLANTILLA"
}
}
}
Response
{
"data": {
"attributes": {
"cc": [
"cande.lepe@agustindeiturbide.com"
],
"asunto": "Prueba desde Insomnia",
"attachments": [],
"contenido_html": "Su pinche madre, si jalo !!!",
"destinatario": "blitzlepe@gmail.com",
"plantilla": "UUIDPLANTILLA",
"user_email": "knd_rt@hotmail.com"
},
"id": "01963a43-ae7f-775c-b7fa-660b74aad105",
"links": {},
"meta": {},
"type": "api_services",
"relationships": {}
},
"links": {
"self": "http://localhost:4000/api/json/apiservices"
},
"meta": {},
"jsonapi": {
"version": "1.0"
}
}```






















