refactor buyer and insured and add more policy types
All checks were successful
Build and Publish / build-release (push) Successful in 1m38s

This commit is contained in:
2026-04-27 14:06:28 -05:00
parent c8a58c3f58
commit 2a8f2ffc2d
27 changed files with 676 additions and 170 deletions

View File

@@ -7,6 +7,9 @@ defmodule PolicyServiceWeb.PolicyController do
alias PolicyService.Aggregates.PolicyId
alias PolicyService.Commands.CarPolicy
alias PolicyService.Commands.LifePolicy
alias PolicyService.Commands.FireStructurePolicy
alias PolicyService.Commands.FireContentsPolicy
alias PolicyServiceWeb.Schemas.Policy, as: S
alias PolicyServiceWeb.QueryHelpers
@@ -16,10 +19,11 @@ defmodule PolicyServiceWeb.PolicyController do
operation(:index,
summary: "List policies",
parameters: QueryHelpers.flop(
[:status, :policy_type, :search],
[:submitted_at, :policy_type, :status]
),
parameters:
QueryHelpers.flop(
[:status, :policy_type, :search],
[:submitted_at, :policy_type, :status]
),
responses: [
ok: {"Policy list", "application/json", S.PolicyListResponse},
bad_request: {"Invalid params", "application/json", S.ErrorResponse}
@@ -89,7 +93,8 @@ defmodule PolicyServiceWeb.PolicyController do
submitted_by = conn.assigns[:user_id] || "test"
with {:ok, policy_type} <- parse_policy_type(params["policy_type"]),
{:ok, applicant_info} <- parse_applicant_info(params["applicant_info"]),
{:ok, insured} <- parse_insured(params["insured"]),
{:ok, buyer} <- parse_buyer(params["buyer"]),
{:ok, policy_details} <- parse_policy_details(policy_type, params["policy_details"]),
{:ok, providers} <- parse_providers(params["selected_providers"]) do
command =
@@ -98,7 +103,38 @@ defmodule PolicyServiceWeb.PolicyController do
%CarPolicy.SubmitPolicyApplication{
id: PolicyId.new(org_id, policy_type, application_id),
submitted_by: submitted_by,
applicant_info: applicant_info,
insured: insured,
buyer: buyer,
policy_details: policy_details,
selected_providers: providers
}
"life" ->
%LifePolicy.SubmitPolicyApplication{
id: PolicyId.new(org_id, policy_type, application_id),
submitted_by: submitted_by,
insured: insured,
buyer: buyer,
policy_details: policy_details,
selected_providers: providers
}
"fire_structure" ->
%FireStructurePolicy.SubmitPolicyApplication{
id: PolicyId.new(org_id, policy_type, application_id),
submitted_by: submitted_by,
insured: insured,
buyer: buyer,
policy_details: policy_details,
selected_providers: providers
}
"fire_contents" ->
%FireContentsPolicy.SubmitPolicyApplication{
id: PolicyId.new(org_id, policy_type, application_id),
submitted_by: submitted_by,
insured: insured,
buyer: buyer,
policy_details: policy_details,
selected_providers: providers
}
@@ -148,6 +184,27 @@ defmodule PolicyServiceWeb.PolicyController do
accepted_by: params["accepted_by"] || "system",
accepted_plan_id: params["accepted_plan_id"]
}
"life" ->
%LifePolicy.AcceptQuoteAndSolicit{
id: PolicyId.new(org_id, policy.policy_type, application_id),
accepted_by: params["accepted_by"] || "system",
accepted_plan_id: params["accepted_plan_id"]
}
"fire_structure" ->
%FireStructurePolicy.AcceptQuoteAndSolicit{
id: PolicyId.new(org_id, policy.policy_type, application_id),
accepted_by: params["accepted_by"] || "system",
accepted_plan_id: params["accepted_plan_id"]
}
"fire_contents" ->
%FireContentsPolicy.AcceptQuoteAndSolicit{
id: PolicyId.new(org_id, policy.policy_type, application_id),
accepted_by: params["accepted_by"] || "system",
accepted_plan_id: params["accepted_plan_id"]
}
end
case CommandedApp.dispatch(command, consistency: :strong) do
@@ -176,7 +233,8 @@ defmodule PolicyServiceWeb.PolicyController do
application_id: p.application_id,
policy_type: p.policy_type,
status: p.status,
applicant_info: p.applicant_info,
insured: p.insured,
buyer: p.buyer,
policy_details: p.policy_details,
provider_policy_number: p.provider_policy_number,
submitted_at: p.submitted_at
@@ -190,7 +248,8 @@ defmodule PolicyServiceWeb.PolicyController do
submitted_by: p.submitted_by,
policy_type: p.policy_type,
status: p.status,
applicant_info: p.applicant_info,
insured: p.insured,
buyer: p.buyer,
policy_details: p.policy_details,
selected_providers: p.selected_providers,
quotes: p.quotes,
@@ -221,41 +280,90 @@ defmodule PolicyServiceWeb.PolicyController do
# Parse helpers
# ---------------------------------------------------------------------------
defp parse_policy_type(type) when type in ["car", "life", "fire"], do: {:ok, type}
defp parse_policy_type(type) when type in ["car", "life", "fire_structure", "fire_contents"],
do: {:ok, type}
defp parse_policy_type(_), do: {:error, :invalid_policy_type}
# individual — has document_id
defp parse_applicant_info(%{"document_id" => doc} = info)
when is_binary(doc) and byte_size(doc) > 0 do
case info["date_of_birth"] do
nil ->
{:error, :missing_date_of_birth}
# insured — individual
defp parse_insured(info) do
case info["type"] do
"individual" ->
case info["date_of_birth"] do
nil ->
{:error, :missing_date_of_birth}
dob ->
dob ->
{:ok,
%{
"type" => "individual",
"name" => info["name"],
"date_of_birth" => dob,
"document_id" => info["document_id"],
"email" => info["email"],
"phone" => info["phone"],
"address" => info["address"]
}}
end
"corporate" ->
{:ok,
%{
"name" => info["name"],
"date_of_birth" => dob,
"document_id" => doc,
"client_type" => "natural"
"type" => "corporate",
"company_name" => info["company_name"],
"ruc" => info["ruc"],
"legal_rep_name" => info["legal_rep_name"],
"legal_rep_document" => info["legal_rep_document"],
"email" => info["email"],
"phone" => info["phone"],
"address" => info["address"]
}}
_ ->
{:error, :invalid_insured_type}
end
end
# corporate — has ruc
defp parse_applicant_info(%{"ruc" => ruc} = info)
when is_binary(ruc) and byte_size(ruc) > 0 do
{:ok,
%{
"company_name" => info["company_name"],
"ruc" => ruc,
"legal_rep_name" => info["legal_rep_name"],
"legal_rep_document" => info["legal_rep_document"],
"client_type" => "juridico"
}}
# buyer — individual
defp parse_buyer(info) do
case info["type"] do
"individual" ->
case info["date_of_birth"] do
nil ->
{:error, :missing_date_of_birth}
dob ->
{:ok,
%{
"type" => "individual",
"name" => info["name"],
"date_of_birth" => dob,
"document_id" => info["document_id"],
"email" => info["email"],
"phone" => info["phone"],
"address" => info["address"]
}}
end
"corporate" ->
{:ok,
%{
"type" => "corporate",
"company_name" => info["company_name"],
"ruc" => info["ruc"],
"legal_rep_name" => info["legal_rep_name"],
"legal_rep_document" => info["legal_rep_document"],
"email" => info["email"],
"phone" => info["phone"],
"address" => info["address"]
}}
_ ->
{:error, :invalid_buyer_type}
end
end
defp parse_applicant_info(_), do: {:error, :invalid_applicant_info}
# individual — has document_id
# car details
defp parse_policy_details("car", nil), do: {:error, :missing_policy_details}
@@ -267,11 +375,13 @@ defmodule PolicyServiceWeb.PolicyController do
"make" => d["make"],
"model" => d["model"],
"year" => d["year"],
"car_value" => d["car_value"],
"use_type" => d["use_type"],
"car_type" => d["car_type"],
"chassis_number" => d["chassis_number"],
"engine_number" => d["engine_number"]
"engine_number" => d["engine_number"],
"rc_limits" => d["rc_limits"],
"market_value" => d["market_value"],
"requested_value" => d["requested_value"]
}}
end
@@ -281,19 +391,42 @@ defmodule PolicyServiceWeb.PolicyController do
defp parse_policy_details("life", d) do
{:ok,
%{
"coverage_type" => d["coverage_type"],
"coverage_amount" => d["coverage_amount"],
"beneficiary" => d["beneficiary"]
"coverage_years" => d["coverage_years"],
"smoker" => d["smoker"],
"medications" => d["medications"],
"surgeries" => d["surgeries"],
"weight" => d["weight"],
"height" => d["height"]
}}
end
# fire details
defp parse_policy_details("fire", nil), do: {:error, :missing_policy_details}
# fire_structure details
defp parse_policy_details("fire_structure", nil), do: {:error, :missing_policy_details}
defp parse_policy_details("fire", d) do
defp parse_policy_details("fire_structure", d) do
{:ok,
%{
"property_address" => d["property_address"],
"property_value" => d["property_value"]
"location" => d["location"],
"property_value" => d["property_value"],
"property_use" => d["property_use"],
"security_measures" => d["security_measures"],
"market_value" => d["market_value"]
}}
end
# fire_contents details
defp parse_policy_details("fire_contents", nil), do: {:error, :missing_policy_details}
defp parse_policy_details("fire_contents", d) do
{:ok,
%{
"location" => d["location"],
"contents_value" => d["contents_value"],
"property_use" => d["property_use"],
"security_measures" => d["security_measures"],
"high_value_items" => d["high_value_items"]
}}
end