2021-02-02 18:49:11 +01:00
|
|
|
defmodule RecycledCloud.SupportRequest do
|
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
|
|
|
import Bamboo.Email
|
|
|
|
|
|
|
|
alias RecycledCloud.SupportRequest
|
|
|
|
alias RecycledCloud.Mailer
|
2021-02-03 09:37:00 +01:00
|
|
|
alias RecycledCloud.Captcha
|
2021-02-02 18:49:11 +01:00
|
|
|
|
|
|
|
embedded_schema do
|
|
|
|
field :name, :string
|
|
|
|
field :email, :string
|
|
|
|
field :message, :string
|
2021-02-03 09:37:00 +01:00
|
|
|
field :captcha, :integer
|
2021-02-02 18:49:11 +01:00
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
def changeset(key, attrs) do
|
2021-02-03 09:37:00 +01:00
|
|
|
expected_captcha_result = attrs |> Map.get("expected_captcha")
|
|
|
|
|
2021-02-02 18:49:11 +01:00
|
|
|
key
|
2021-02-03 09:37:00 +01:00
|
|
|
|> cast(attrs, [:name, :email, :message, :captcha])
|
|
|
|
|> validate_required([:name, :email, :message, :captcha])
|
|
|
|
|> Captcha.validate(expected_captcha_result)
|
2021-02-02 18:49:11 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def send(%SupportRequest{} = request) do
|
|
|
|
admin_email = Application.get_env(:recycledcloud, :admin_email)
|
|
|
|
host = Application.get_env(:recycledcloud, RecycledCloudWeb.Endpoint)
|
|
|
|
|> Keyword.get(:url, [])
|
|
|
|
|> Keyword.get(:host, "localhost")
|
|
|
|
|
|
|
|
body = """
|
|
|
|
A new request has been received on #{host}:
|
|
|
|
|
|
|
|
From: #{request.name} // #{request.email}
|
|
|
|
~~~
|
|
|
|
#{request.message}
|
|
|
|
"""
|
|
|
|
|
|
|
|
Mailer.template()
|
|
|
|
|> to(request.email)
|
|
|
|
|> bcc(admin_email)
|
|
|
|
|> put_header("Reply-To", request.email)
|
|
|
|
|> subject("Recycled Cloud Support Request")
|
|
|
|
|> text_body(body)
|
|
|
|
|> Mailer.deliver_now()
|
|
|
|
end
|
|
|
|
end
|