All checks were successful
Build and Publish / build-release (push) Successful in 3m7s
253 lines
7.7 KiB
Elixir
253 lines
7.7 KiB
Elixir
defmodule CustomerServiceWeb.LeadController do
|
|
use CustomerServiceWeb, :controller
|
|
use OpenApiSpex.ControllerSpecs
|
|
|
|
alias CustomerService.Commands.{CreateQuickLead, UpdateQuickLead, UpdateLeadStatus}
|
|
alias CustomerService.Lead.Queries, as: LeadQueries
|
|
alias CustomerServiceWeb.Schemas.Lead, as: LeadSchemas
|
|
alias CustomerServiceWeb.QueryHelpers
|
|
alias CustomerService.Aggregates.LeadId
|
|
|
|
operation(:index,
|
|
summary: "List leads",
|
|
parameters:
|
|
QueryHelpers.flop([:status, :priority, :source, :assigned_to, :search], [
|
|
:name,
|
|
:company_name,
|
|
:status,
|
|
:priority,
|
|
:inserted_at
|
|
]),
|
|
responses: [
|
|
ok: {"Lead list", "application/json", LeadSchemas.LeadListResponse}
|
|
]
|
|
)
|
|
|
|
def index(conn, params) do
|
|
org_id = conn.private[CustomerServiceWeb.Plugs.ExtractOrganizationId]
|
|
|
|
case LeadQueries.list_by_org(org_id, params) do
|
|
{:ok, {leads, meta}} ->
|
|
conn
|
|
|> put_status(:ok)
|
|
|> json(%{
|
|
data: Enum.map(leads, &lead_json/1),
|
|
meta: %{
|
|
total_count: meta.total_count,
|
|
total_pages: meta.total_pages,
|
|
current_page: meta.current_page,
|
|
page_size: meta.page_size,
|
|
has_next: meta.has_next_page?,
|
|
has_prev: meta.has_previous_page?
|
|
}
|
|
})
|
|
|
|
{:error, _meta} ->
|
|
conn
|
|
|> put_status(:bad_request)
|
|
|> json(%{error: "invalid parameters"})
|
|
end
|
|
end
|
|
|
|
operation(:show,
|
|
summary: "Get lead",
|
|
parameters: [
|
|
id: [in: :path, type: :string, required: true, description: "Lead ID"]
|
|
],
|
|
responses: [
|
|
ok: {"Lead", "application/json", LeadSchemas.LeadResponse},
|
|
not_found: {"Not found", "application/json", %OpenApiSpex.Schema{type: :object}}
|
|
]
|
|
)
|
|
|
|
def show(conn, %{"id" => id}) do
|
|
org_id = conn.private[CustomerServiceWeb.Plugs.ExtractOrganizationId]
|
|
|
|
with {:ok, %LeadId{lead_id: local_id}} <- LeadId.parse(id),
|
|
{:ok, lead} <- LeadQueries.get_by_org(org_id, local_id) do
|
|
conn |> put_status(:ok) |> json(%{data: lead_json(lead)})
|
|
else
|
|
{:error, :not_found} ->
|
|
conn |> put_status(:not_found) |> json(%{error: "not found"})
|
|
|
|
:error ->
|
|
conn |> put_status(:bad_request) |> json(%{error: "invalid lead id"})
|
|
end
|
|
end
|
|
|
|
operation(:create,
|
|
summary: "Create quick lead",
|
|
request_body: {"Lead data", "application/json", LeadSchemas.CreateQuickLead, required: true},
|
|
responses: [
|
|
ok: {"Lead created", "application/json", LeadSchemas.LeadResponse}
|
|
]
|
|
)
|
|
|
|
def create(conn, params) do
|
|
org_id = conn.private[CustomerServiceWeb.Plugs.ExtractOrganizationId]
|
|
lead_uuid = Ecto.UUID.generate()
|
|
lead_id = LeadId.new(org_id, lead_uuid)
|
|
|
|
command = %CreateQuickLead{
|
|
id: lead_id,
|
|
name: params["name"],
|
|
email: params["email"],
|
|
phone: params["phone"],
|
|
company_name: params["company_name"],
|
|
status: params["status"] || :new,
|
|
priority: params["priority"] || :medium,
|
|
source: params["source"] || :other,
|
|
notes: params["notes"],
|
|
assigned_to: params["assigned_to"],
|
|
estimated_value: parse_decimal(params["estimated_value"]),
|
|
expected_close_date: parse_date(params["expected_close_date"])
|
|
}
|
|
|
|
dispatch_and_return(conn, command, lead_id)
|
|
end
|
|
|
|
operation(:update,
|
|
summary: "Update quick lead",
|
|
parameters: [
|
|
id: [in: :path, type: :string, required: true, description: "Lead ID"]
|
|
],
|
|
request_body: {"Lead data", "application/json", LeadSchemas.UpdateQuickLead, required: true},
|
|
responses: [
|
|
ok: {"Lead updated", "application/json", LeadSchemas.LeadResponse}
|
|
]
|
|
)
|
|
|
|
def update(conn, %{"id" => id} = params) do
|
|
org_id = conn.private[CustomerServiceWeb.Plugs.ExtractOrganizationId]
|
|
|
|
with {:ok, %LeadId{lead_id: local_id}} <- LeadId.parse(id),
|
|
{:ok, _lead} <- LeadQueries.get_by_org(org_id, local_id) do
|
|
command = %UpdateQuickLead{
|
|
id: LeadId.new(org_id, local_id),
|
|
name: params["name"],
|
|
email: params["email"],
|
|
phone: params["phone"],
|
|
company_name: params["company_name"],
|
|
notes: params["notes"],
|
|
assigned_to: params["assigned_to"],
|
|
estimated_value: parse_decimal(params["estimated_value"]),
|
|
expected_close_date: parse_date(params["expected_close_date"])
|
|
}
|
|
|
|
dispatch_and_return(conn, command, LeadId.new(org_id, local_id))
|
|
else
|
|
{:error, :not_found} ->
|
|
conn |> put_status(:not_found) |> json(%{error: "not found"})
|
|
|
|
:error ->
|
|
conn |> put_status(:bad_request) |> json(%{error: "invalid lead id"})
|
|
end
|
|
end
|
|
|
|
operation(:update_status,
|
|
summary: "Update lead status",
|
|
parameters: [
|
|
id: [in: :path, type: :string, required: true, description: "Lead ID"]
|
|
],
|
|
request_body:
|
|
{"Status update", "application/json", LeadSchemas.UpdateLeadStatus, required: true},
|
|
responses: [
|
|
ok: {"Lead status updated", "application/json", LeadSchemas.LeadResponse}
|
|
]
|
|
)
|
|
|
|
def update_status(conn, %{"id" => id} = params) do
|
|
org_id = conn.private[CustomerServiceWeb.Plugs.ExtractOrganizationId]
|
|
|
|
with {:ok, %LeadId{lead_id: local_id}} <- LeadId.parse(id),
|
|
{:ok, _lead} <- LeadQueries.get_by_org(org_id, local_id) do
|
|
command = %UpdateLeadStatus{
|
|
id: LeadId.new(org_id, local_id),
|
|
status: String.to_existing_atom(params["status"])
|
|
}
|
|
|
|
dispatch_and_return(conn, command, LeadId.new(org_id, local_id))
|
|
else
|
|
{:error, :not_found} ->
|
|
conn |> put_status(:not_found) |> json(%{error: "not found"})
|
|
|
|
:error ->
|
|
conn |> put_status(:bad_request) |> json(%{error: "invalid lead id"})
|
|
end
|
|
end
|
|
|
|
defp dispatch_and_return(conn, command, %LeadId{} = lead_id) do
|
|
case CustomerService.CommandedApp.dispatch(command, consistency: :strong) do
|
|
:ok ->
|
|
case LeadQueries.get_lead(LeadId.to_string(lead_id)) do
|
|
{:ok, lead} ->
|
|
conn |> put_status(:ok) |> json(%{data: lead_json(lead)})
|
|
|
|
{:error, :not_found} ->
|
|
conn
|
|
|> put_status(:internal_server_error)
|
|
|> json(%{error: "lead created but not found in projection"})
|
|
end
|
|
|
|
{:error, reason} ->
|
|
conn |> put_status(:unprocessable_entity) |> json(%{error: inspect(reason)})
|
|
end
|
|
end
|
|
|
|
defp dispatch_and_return(conn, command, lead_id_string) when is_binary(lead_id_string) do
|
|
case CustomerService.CommandedApp.dispatch(command, consistency: :strong) do
|
|
:ok ->
|
|
case LeadQueries.get_lead(lead_id_string) do
|
|
{:ok, lead} ->
|
|
conn |> put_status(:ok) |> json(%{data: lead_json(lead)})
|
|
|
|
{:error, :not_found} ->
|
|
conn
|
|
|> put_status(:internal_server_error)
|
|
|> json(%{error: "lead created but not found in projection"})
|
|
end
|
|
|
|
{:error, reason} ->
|
|
conn |> put_status(:unprocessable_entity) |> json(%{error: inspect(reason)})
|
|
end
|
|
end
|
|
|
|
defp parse_date(nil), do: nil
|
|
|
|
defp parse_date(str) do
|
|
case Date.from_iso8601(str) do
|
|
{:ok, date} -> date
|
|
_ -> nil
|
|
end
|
|
end
|
|
|
|
defp parse_decimal(nil), do: nil
|
|
|
|
defp parse_decimal(str) when is_binary(str) do
|
|
case Decimal.parse(str) do
|
|
{decimal, ""} -> decimal
|
|
_ -> nil
|
|
end
|
|
end
|
|
|
|
defp lead_json(lead) do
|
|
%{
|
|
id: lead.id,
|
|
name: lead.name,
|
|
email: lead.email,
|
|
phone: lead.phone,
|
|
company_name: lead.company_name,
|
|
status: lead.status,
|
|
priority: lead.priority,
|
|
source: lead.source,
|
|
notes: lead.notes,
|
|
assigned_to: lead.assigned_to,
|
|
estimated_value: lead.estimated_value,
|
|
expected_close_date: lead.expected_close_date,
|
|
status_history: lead.status_history,
|
|
inserted_at: lead.inserted_at,
|
|
updated_at: lead.updated_at
|
|
}
|
|
end
|
|
end
|