Skip to content

Commit 05da700

Browse files
authored
Merge pull request #295 from pentacent/feature/import-status
Allow updating contact status via API, fix nil sigments, fix MAILER_FROM_SMTP
2 parents b7f1fb2 + 0424e68 commit 05da700

File tree

9 files changed

+51
-11
lines changed

9 files changed

+51
-11
lines changed

config/config.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ config :keila, KeilaWeb.Gettext,
108108
default_locale: "en",
109109
locales: ["de", "en"]
110110

111-
config(:keila, Keila.Mailer, from_email: "keila@localhost")
111+
config(:keila, Keila.Auth.Emails, from_email: "keila@localhost")
112112

113113
# Import environment specific config. This must remain at the bottom
114114
# of this file so it overrides the configuration defined above.

lib/keila/auth/emails.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,6 @@ defmodule Keila.Auth.Emails do
109109
end
110110

111111
defp system_from_email() do
112-
Application.get_env(:keila, Keila.Mailer) |> Keyword.fetch!(:from_email)
112+
Application.get_env(:keila, __MODULE__) |> Keyword.fetch!(:from_email)
113113
end
114114
end

lib/keila/contacts/contacts.ex

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,42 @@ defmodule Keila.Contacts do
1111

1212
@doc """
1313
Creates a new Contact within the given Project.
14+
15+
16+
## Options
17+
- `:set_status` - Also sets the `status` field from `params` when `true`.
1418
"""
15-
@spec create_contact(Project.id(), map()) ::
19+
@spec create_contact(Project.id(), map(), Keyword.t()) ::
1620
{:ok, Contact.t()} | {:error, Changeset.t(Contact.t())}
17-
def create_contact(project_id, params) when is_binary(project_id) or is_integer(project_id) do
21+
def create_contact(project_id, params, opts \\ [])
22+
when is_binary(project_id) or is_integer(project_id) do
1823
params
1924
|> Contact.creation_changeset(project_id)
25+
|> maybe_update_contact_status(params, opts[:set_status])
2026
|> Repo.insert()
2127
end
2228

29+
defp maybe_update_contact_status(changeset, params, update?)
30+
31+
defp maybe_update_contact_status(changeset, params, true),
32+
do: Contact.update_status_changeset(changeset, params)
33+
34+
defp maybe_update_contact_status(changeset, _params, _), do: changeset
35+
2336
defdelegate perform_form_action(form, params, opts), to: __MODULE__.FormActionHandler
2437
defdelegate perform_form_action(form, params), to: __MODULE__.FormActionHandler
2538

2639
@doc """
2740
Updates the specified Contact.
41+
42+
## Options
43+
- `:update_status` - Also updates the `status` field from `params` when `true`.
2844
"""
29-
@spec update_contact(Contact.id(), map()) :: {:ok, Contact} | {:error, Contact}
30-
def update_contact(id, params) do
45+
@spec update_contact(Contact.id(), map(), Keyword.t()) :: {:ok, Contact} | {:error, Contact}
46+
def update_contact(id, params, opts \\ []) do
3147
get_contact(id)
3248
|> Contact.update_changeset(params)
49+
|> maybe_update_contact_status(params, opts[:update_status])
3350
|> Repo.update()
3451
end
3552

lib/keila/contacts/schemas/contact.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ defmodule Keila.Contacts.Contact do
3030
|> check_data_size_constraint()
3131
end
3232

33+
@spec update_status_changeset(t() | Ecto.Changeset.t(t()), Ecto.Changeset.data()) ::
34+
Ecto.Changeset.t(t())
35+
def update_status_changeset(struct \\ %__MODULE__{}, params) do
36+
struct
37+
|> cast(params, [:status])
38+
end
39+
3340
@spec update_changeset(t(), Ecto.Changeset.data()) :: Ecto.Changeset.t(t())
3441
def update_changeset(struct \\ %__MODULE__{}, params) do
3542
struct

lib/keila/contacts/schemas/segment.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,21 @@ defmodule Keila.Contacts.Segment do
1414
def creation_changeset(struct \\ %__MODULE__{}, params) do
1515
struct
1616
|> cast(params, [:name, :project_id, :filter])
17+
|> ensure_filter_not_empty()
1718
end
1819

1920
@spec update_changeset(t(), Changeset.data()) :: Changeset.t(t())
2021
def update_changeset(struct, params) do
2122
struct
2223
|> cast(params, [:name, :filter])
2324
|> validate_required(:name)
25+
|> ensure_filter_not_empty()
26+
end
27+
28+
defp ensure_filter_not_empty(changeset) do
29+
case get_field(changeset, :filter) do
30+
nil -> put_change(changeset, :filter, %{})
31+
_other -> changeset
32+
end
2433
end
2534
end

lib/keila_web/api/controllers/api_contact_controller.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ defmodule KeilaWeb.ApiContactController do
7373

7474
@spec create(Plug.Conn.t(), Map.t()) :: Plug.Conn.t()
7575
def create(conn, _params) do
76-
case Contacts.create_contact(project_id(conn), conn.body_params.data) do
76+
case Contacts.create_contact(project_id(conn), conn.body_params.data, set_status: true) do
7777
{:ok, contact} -> render(conn, "contact.json", %{contact: contact})
7878
{:error, changeset} -> Errors.send_changeset_error(conn, changeset)
7979
end
@@ -107,7 +107,7 @@ defmodule KeilaWeb.ApiContactController do
107107
@spec update(Plug.Conn.t(), map()) :: Plug.Conn.t()
108108
def update(conn, %{id: id}) do
109109
if Contacts.get_project_contact(project_id(conn), id) do
110-
case Contacts.update_contact(id, conn.body_params.data) do
110+
case Contacts.update_contact(id, conn.body_params.data, update_status: true) do
111111
{:ok, contact} -> render(conn, "contact.json", %{contact: contact})
112112
{:error, changeset} -> Errors.send_changeset_error(conn, changeset)
113113
end

lib/keila_web/api/schemas/contact.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ defmodule KeilaWeb.Api.Schemas.Contact.Params do
6565
use KeilaWeb.Api.Schema
6666

6767
@properties KeilaWeb.Api.Schemas.Contact.properties()
68-
@allowed_properties [:email, :first_name, :last_name, :data]
68+
@allowed_properties [:email, :first_name, :last_name, :data, :status]
6969
build_open_api_schema(@properties, only: @allowed_properties)
7070
end

lib/keila_web/controllers/contact_controller.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ defmodule KeilaWeb.ContactController do
102102

103103
defp get_sort_by(_), do: "inserted_at"
104104

105-
defp get_sort_order(%{"sort_order" => "-1"}), do: -1
106-
defp get_sort_order(_), do: 1
105+
defp get_sort_order(%{"sort_order" => "1"}), do: 1
106+
defp get_sort_order(_), do: -1
107107

108108
@spec delete(Plug.Conn.t(), map()) :: Plug.Conn.t()
109109
def delete(conn, params) do

test/keila_web/api/api_contact_controller_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ defmodule KeilaWeb.ApiContactControllerTest do
172172
assert %{"data" => %{"first_name" => "Updated Name"}} = json_response(conn, 200)
173173
end
174174

175+
test "allows changing contact status", %{authorized_conn: conn, project: project} do
176+
contact = insert!(:contact, project_id: project.id)
177+
body = %{"data" => %{"status" => "unsubscribed"}}
178+
conn = patch_json(conn, Routes.api_contact_path(conn, :update, contact.id), body)
179+
assert %{"data" => %{"status" => "unsubscribed"}} = json_response(conn, 200)
180+
end
181+
175182
@tag :api_contact_controller
176183
test "renders changeset error", %{authorized_conn: conn, project: project} do
177184
contact = insert!(:contact, project_id: project.id)

0 commit comments

Comments
 (0)