add quick leads
All checks were successful
Build and Publish / build-release (push) Successful in 2m13s
All checks were successful
Build and Publish / build-release (push) Successful in 2m13s
This commit is contained in:
@@ -2,19 +2,25 @@ defmodule CustomerServiceWeb.CustomerController do
|
||||
use CustomerServiceWeb, :controller
|
||||
use OpenApiSpex.ControllerSpecs
|
||||
|
||||
alias CustomerService.Commands.{CreateCustomer, CreateCorporateCustomer}
|
||||
alias CustomerService.Commands.{
|
||||
CreateCustomer,
|
||||
CreateCorporateCustomer,
|
||||
UpdateCustomer,
|
||||
UpdateCorporateCustomer
|
||||
}
|
||||
|
||||
alias CustomerService.Customer.Queries, as: CustomerQueries
|
||||
alias CustomerServiceWeb.Schemas.Customer, as: CustomerSchemas
|
||||
alias CustomerServiceWeb.QueryHelpers
|
||||
|
||||
operation(:index,
|
||||
summary: "List customers",
|
||||
parameters: [
|
||||
"page[number]": [in: :query, type: :integer, required: false],
|
||||
"page[size]": [in: :query, type: :integer, required: false],
|
||||
"filters[0][field]": [in: :query, type: :string, required: false],
|
||||
"filters[0][op]": [in: :query, type: :string, required: false],
|
||||
"filters[0][value]": [in: :query, type: :string, required: false]
|
||||
],
|
||||
parameters:
|
||||
QueryHelpers.flop([:customer_type, :email, :phone, :document_id, :ruc, :search], [
|
||||
:last_name,
|
||||
:legal_name,
|
||||
:inserted_at
|
||||
]),
|
||||
responses: [
|
||||
ok: {"Customer list", "application/json", CustomerSchemas.CustomerListResponse}
|
||||
]
|
||||
@@ -119,6 +125,63 @@ defmodule CustomerServiceWeb.CustomerController do
|
||||
dispatch_and_return(conn, command, customer_id)
|
||||
end
|
||||
|
||||
operation(:update,
|
||||
summary: "Update individual customer",
|
||||
parameters: [
|
||||
id: [in: :path, type: :string, required: true, description: "Customer ID"]
|
||||
],
|
||||
request_body:
|
||||
{"Customer data", "application/json", CustomerSchemas.UpdateCustomer, required: true},
|
||||
responses: [
|
||||
ok: {"Customer updated", "application/json", CustomerSchemas.CustomerResponse}
|
||||
]
|
||||
)
|
||||
|
||||
def update(conn, %{"id" => id} = params) do
|
||||
command = %UpdateCustomer{
|
||||
id: id,
|
||||
first_name: params["first_name"],
|
||||
last_name: params["last_name"],
|
||||
birth_date: parse_date(params["birth_date"]),
|
||||
gender: params["gender"],
|
||||
email: params["email"],
|
||||
phone: params["phone"],
|
||||
document_id: params["document_id"],
|
||||
address: params["address"]
|
||||
}
|
||||
|
||||
dispatch_and_return(conn, command, id)
|
||||
end
|
||||
|
||||
operation(:update_corporate,
|
||||
summary: "Update corporate customer",
|
||||
parameters: [
|
||||
id: [in: :path, type: :string, required: true, description: "Customer ID"]
|
||||
],
|
||||
request_body:
|
||||
{"Corporate customer data", "application/json", CustomerSchemas.UpdateCorporateCustomer,
|
||||
required: true},
|
||||
responses: [
|
||||
ok: {"Corporate customer updated", "application/json", CustomerSchemas.CustomerResponse}
|
||||
]
|
||||
)
|
||||
|
||||
def update_corporate(conn, %{"id" => id} = params) do
|
||||
command = %UpdateCorporateCustomer{
|
||||
id: id,
|
||||
legal_name: params["legal_name"],
|
||||
commercial_name: params["commercial_name"],
|
||||
ruc: params["ruc"],
|
||||
legal_rep_name: params["legal_rep_name"],
|
||||
legal_rep_document_id: params["legal_rep_document_id"],
|
||||
email: params["email"],
|
||||
phone: params["phone"],
|
||||
address: params["address"]
|
||||
}
|
||||
|
||||
dispatch_and_return(conn, command, id)
|
||||
end
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Private
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
202
lib/customer_service_web/controllers/lead.ex
Normal file
202
lib/customer_service_web/controllers/lead.ex
Normal file
@@ -0,0 +1,202 @@
|
||||
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
|
||||
|
||||
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
|
||||
case LeadQueries.list_leads(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
|
||||
case LeadQueries.get_lead(id) do
|
||||
{:ok, lead} ->
|
||||
conn |> put_status(:ok) |> json(%{data: lead_json(lead)})
|
||||
|
||||
{:error, :not_found} ->
|
||||
conn |> put_status(:not_found) |> json(%{error: "not found"})
|
||||
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
|
||||
lead_id = Ecto.UUID.generate()
|
||||
|
||||
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
|
||||
command = %UpdateQuickLead{
|
||||
id: 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, id)
|
||||
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
|
||||
command = %UpdateLeadStatus{
|
||||
id: id,
|
||||
status: String.to_existing_atom(params["status"])
|
||||
}
|
||||
|
||||
dispatch_and_return(conn, command, id)
|
||||
end
|
||||
|
||||
defp dispatch_and_return(conn, command, lead_id) do
|
||||
case CustomerService.CommandedApp.dispatch(command, consistency: :strong) do
|
||||
:ok ->
|
||||
case LeadQueries.get_lead(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 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
|
||||
Reference in New Issue
Block a user