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

View File

@@ -9,8 +9,14 @@ defmodule PolicyServiceWeb.QueryHelpers do
[
page: [in: :query, schema: %Schema{type: :number, default: 1}],
page_size: [in: :query, schema: %Schema{type: :number, default: 20}],
order_by: [in: :query, schema: %Schema{type: :array, items: %Schema{type: :string, enum: order_fields}}],
order_directions: [in: :query, schema: %Schema{type: :array, items: %Schema{type: :string, enum: ["asc", "desc"]}}]
order_by: [
in: :query,
schema: %Schema{type: :array, items: %Schema{type: :string, enum: order_fields}}
],
order_directions: [
in: :query,
schema: %Schema{type: :array, items: %Schema{type: :string, enum: ["asc", "desc"]}}
]
] ++ build_filter_params(filter_fields) ++ other
end
@@ -18,9 +24,11 @@ defmodule PolicyServiceWeb.QueryHelpers do
for i <- 0..(@filter_count - 1) do
[
{:"filters[#{i}][field]", [in: :query, schema: %Schema{type: :string, enum: fields}]},
{:"filters[#{i}][op]", [in: :query, schema: %Schema{type: :string, enum: Flop.Filter.allowed_operators(:all)}]},
{:"filters[#{i}][op]",
[in: :query, schema: %Schema{type: :string, enum: Flop.Filter.allowed_operators(:all)}]},
{:"filters[#{i}][value]", [in: :query, schema: %Schema{type: :string}]}
]
end |> List.flatten()
end
|> List.flatten()
end
end
end

View File

@@ -18,8 +18,104 @@ defmodule PolicyServiceWeb.Schemas.Policy do
})
end
defmodule InsuredIndividual do
require OpenApiSpex
OpenApiSpex.schema(%{
title: "InsuredIndividual",
type: :object,
required: [:type, :name, :date_of_birth, :document_id],
properties: %{
type: %Schema{type: :string, enum: ["individual"]},
name: %Schema{type: :string, example: "Juan Pérez"},
date_of_birth: %Schema{type: :string, format: :date, example: "1985-06-15"},
document_id: %Schema{type: :string, example: "8-123-456"},
email: %Schema{type: :string, format: :email, example: "juan@example.com"},
phone: %Schema{type: :string, example: "+507-1234-5678"},
address: %Schema{type: :string, example: "Calle 50, Panama City"}
}
})
end
defmodule InsuredCorporate do
require OpenApiSpex
OpenApiSpex.schema(%{
title: "InsuredCorporate",
type: :object,
required: [:type, :company_name, :ruc, :legal_rep_name, :legal_rep_document],
properties: %{
type: %Schema{type: :string, enum: ["corporate"]},
company_name: %Schema{type: :string, example: "Empresa ABC S.A."},
ruc: %Schema{type: :string, example: "123456-1-123456"},
legal_rep_name: %Schema{type: :string, example: "María García"},
legal_rep_document: %Schema{type: :string, example: "8-456-789"},
email: %Schema{type: :string, format: :email, example: "contact@empresa-abc.com"},
phone: %Schema{type: :string, example: "+507-1234-5678"},
address: %Schema{type: :string, example: "Calle 50, Panama City"}
}
})
end
defmodule Insured do
require OpenApiSpex
OpenApiSpex.schema(%{
title: "Insured",
oneOf: [InsuredIndividual, InsuredCorporate]
})
end
defmodule BuyerIndividual do
require OpenApiSpex
OpenApiSpex.schema(%{
title: "BuyerIndividual",
type: :object,
required: [:type, :name, :date_of_birth, :document_id],
properties: %{
type: %Schema{type: :string, enum: ["individual"]},
name: %Schema{type: :string, example: "María García"},
date_of_birth: %Schema{type: :string, format: :date, example: "1980-03-20"},
document_id: %Schema{type: :string, example: "8-456-789"},
email: %Schema{type: :string, format: :email, example: "maria@example.com"},
phone: %Schema{type: :string, example: "+507-8765-4321"},
address: %Schema{type: :string, example: "Calle 75, Panama City"}
}
})
end
defmodule BuyerCorporate do
require OpenApiSpex
OpenApiSpex.schema(%{
title: "BuyerCorporate",
type: :object,
required: [:type, :company_name, :ruc, :legal_rep_name, :legal_rep_document],
properties: %{
type: %Schema{type: :string, enum: ["corporate"]},
company_name: %Schema{type: :string, example: "Empresa XYZ S.A."},
ruc: %Schema{type: :string, example: "987654-1-987654"},
legal_rep_name: %Schema{type: :string, example: "Carlos López"},
legal_rep_document: %Schema{type: :string, example: "8-789-012"},
email: %Schema{type: :string, format: :email, example: "carlos@empresa-xyz.com"},
phone: %Schema{type: :string, example: "+507-8765-4321"},
address: %Schema{type: :string, example: "Calle 100, Panama City"}
}
})
end
defmodule Buyer do
require OpenApiSpex
OpenApiSpex.schema(%{
title: "Buyer",
oneOf: [BuyerIndividual, BuyerCorporate]
})
end
# ---------------------------------------------------------------------------
# Applicant — discriminated by presence of keys
# Policy details — one per policy type
# ---------------------------------------------------------------------------
defmodule ApplicantIndividual do
@@ -77,18 +173,17 @@ defmodule PolicyServiceWeb.Schemas.Policy do
:make,
:model,
:year,
:car_value,
:use_type,
:car_type,
:chassis_number,
:engine_number
:rc_limits,
:market_value,
:requested_value
],
properties: %{
plate: %Schema{type: :string, example: "ABC-1234"},
make: %Schema{type: :string, example: "Toyota"},
model: %Schema{type: :string, example: "Corolla"},
year: %Schema{type: :integer, example: 2022},
car_value: %Schema{type: :number, example: 18000},
use_type: %Schema{type: :string, enum: ["private", "commercial", "bus", "taxi", "school"]},
car_type: %Schema{
type: :string,
@@ -105,7 +200,16 @@ defmodule PolicyServiceWeb.Schemas.Policy do
]
},
chassis_number: %Schema{type: :string, example: "9BWZZZ377VT004251"},
engine_number: %Schema{type: :string, example: "1NZ-FE-1234567"}
engine_number: %Schema{type: :string, example: "1NZ-FE-1234567"},
rc_limits: %Schema{
type: :object,
properties: %{
bodily_injury: %Schema{type: :number, example: 50000},
property_damage: %Schema{type: :number, example: 25000}
}
},
market_value: %Schema{type: :number, example: 18000},
requested_value: %Schema{type: :number, example: 20000}
}
})
end
@@ -116,24 +220,63 @@ defmodule PolicyServiceWeb.Schemas.Policy do
OpenApiSpex.schema(%{
title: "LifePolicyDetails",
type: :object,
required: [:coverage_amount, :beneficiary],
required: [:coverage_type, :coverage_amount, :coverage_years, :smoker, :weight, :height],
properties: %{
coverage_type: %Schema{
type: :string,
enum: ["banking", "protection"]
},
coverage_amount: %Schema{type: :number, example: 100_000},
beneficiary: %Schema{type: :string, example: "María Pérez"}
coverage_years: %Schema{type: :integer, example: 10},
smoker: %Schema{type: :boolean, example: false},
medications: %Schema{type: :array, items: %Schema{type: :string}},
surgeries: %Schema{type: :array, items: %Schema{type: :string}},
weight: %Schema{type: :number, example: 70},
height: %Schema{type: :number, example: 175}
}
})
end
defmodule FirePolicyDetails do
defmodule FireStructurePolicyDetails do
require OpenApiSpex
OpenApiSpex.schema(%{
title: "FirePolicyDetails",
title: "FireStructurePolicyDetails",
type: :object,
required: [:property_address, :property_value],
required: [:location, :property_value, :property_use, :security_measures, :market_value],
properties: %{
property_address: %Schema{type: :string, example: "Calle 50, Panama City"},
property_value: %Schema{type: :number, example: 250_000}
location: %Schema{type: :string, example: "Calle 50, Panama City"},
property_value: %Schema{type: :number, example: 250_000},
property_use: %Schema{type: :string, example: "residential"},
security_measures: %Schema{type: :array, items: %Schema{type: :string}},
market_value: %Schema{type: :number, example: 260_000}
}
})
end
defmodule FireContentsPolicyDetails do
require OpenApiSpex
OpenApiSpex.schema(%{
title: "FireContentsPolicyDetails",
type: :object,
required: [:location, :contents_value, :property_use, :security_measures],
properties: %{
location: %Schema{type: :string, example: "Calle 50, Panama City"},
contents_value: %Schema{type: :number, example: 50_000},
property_use: %Schema{type: :string, example: "residential"},
security_measures: %Schema{type: :array, items: %Schema{type: :string}},
high_value_items: %Schema{
type: :array,
items: %Schema{
type: :object,
properties: %{
description: %Schema{type: :string},
value: %Schema{type: :number},
type: %Schema{type: :string, enum: ["electronic", "other"]}
}
}
}
}
})
end
@@ -143,7 +286,12 @@ defmodule PolicyServiceWeb.Schemas.Policy do
OpenApiSpex.schema(%{
title: "PolicyDetails",
oneOf: [CarPolicyDetails, LifePolicyDetails, FirePolicyDetails]
oneOf: [
CarPolicyDetails,
LifePolicyDetails,
FireStructurePolicyDetails,
FireContentsPolicyDetails
]
})
end
@@ -207,14 +355,15 @@ defmodule PolicyServiceWeb.Schemas.Policy do
OpenApiSpex.schema(%{
title: "CreatePolicyRequest",
type: :object,
required: [:policy_type, :applicant_info, :policy_details, :selected_providers],
required: [:policy_type, :insured, :buyer, :policy_details, :selected_providers],
properties: %{
policy_type: %Schema{
type: :string,
enum: ["car", "life", "fire"],
enum: ["car", "life", "fire_structure", "fire_contents"],
description: "Determines the shape of policy_details"
},
applicant_info: ApplicantInfo,
insured: Insured,
buyer: Buyer,
policy_details: PolicyDetails,
selected_providers: %Schema{type: :array, items: SelectedProvider, minItems: 1}
}
@@ -262,12 +411,16 @@ defmodule PolicyServiceWeb.Schemas.Policy do
type: :object,
properties: %{
application_id: %Schema{type: :string},
policy_type: %Schema{type: :string, enum: ["car", "life", "fire"]},
policy_type: %Schema{
type: :string,
enum: ["car", "life", "fire_structure", "fire_contents"]
},
status: %Schema{
type: :string,
enum: ["quote_requested", "quotes_received", "awaiting_policy", "issued"]
},
applicant_info: ApplicantInfo,
insured: Insured,
buyer: Buyer,
policy_details: PolicyDetails,
provider_policy_number: %Schema{type: :string, nullable: true},
submitted_at: %Schema{type: :string, format: :"date-time"}
@@ -285,12 +438,16 @@ defmodule PolicyServiceWeb.Schemas.Policy do
application_id: %Schema{type: :string},
org_id: %Schema{type: :string},
submitted_by: %Schema{type: :string},
policy_type: %Schema{type: :string, enum: ["car", "life", "fire"]},
policy_type: %Schema{
type: :string,
enum: ["car", "life", "fire_structure", "fire_contents"]
},
status: %Schema{
type: :string,
enum: ["quote_requested", "quotes_received", "awaiting_policy", "issued"]
},
applicant_info: ApplicantInfo,
insured: Insured,
buyer: Buyer,
policy_details: PolicyDetails,
selected_providers: %Schema{type: :array, items: %Schema{type: :string}},
quotes: %Schema{type: :object, additionalProperties: QuoteData},