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

@@ -11,21 +11,52 @@ defmodule PolicyService.Aggregates.CarPolicyApplication do
"make" => make,
"model" => model,
"year" => year,
"car_value" => car_value,
"use_type" => use_type,
"car_type" => car_type,
"rc_limits" => _rc_limits,
"market_value" => market_value,
"requested_value" => requested_value
})
when is_binary(plate) and is_binary(make) and is_binary(model) and
is_integer(year) and
is_number(market_value) and market_value > 0 and
is_number(requested_value) and requested_value > 0 and
is_binary(use_type) and byte_size(use_type) > 0 and
is_binary(car_type) and byte_size(car_type) > 0 do
cond do
year < 1886 or year > Date.utc_today().year + 1 -> {:error, :invalid_car_year}
use_type not in @valid_use_types -> {:error, :invalid_use_type}
car_type not in @valid_car_types -> {:error, :invalid_car_type}
byte_size(plate) == 0 -> {:error, :missing_plate}
true -> :ok
end
end
def validate_details(%{
"plate" => plate,
"make" => make,
"model" => model,
"year" => year,
"use_type" => use_type,
"car_type" => car_type,
"chassis_number" => chassis,
"engine_number" => engine
"engine_number" => engine,
"rc_limits" => _rc_limits,
"market_value" => market_value,
"requested_value" => requested_value
})
when is_binary(plate) and is_binary(make) and is_binary(model) and
is_integer(year) and is_number(car_value) and car_value > 0 and
is_integer(year) and
is_number(market_value) and market_value > 0 and
is_number(requested_value) and requested_value > 0 and
is_binary(use_type) and byte_size(use_type) > 0 and
is_binary(car_type) and byte_size(car_type) > 0 and
is_binary(chassis) and is_binary(engine) do
cond do
year < 1886 or year > Date.utc_today().year + 1 -> {:error, :invalid_car_year}
use_type not in @valid_use_types -> {:error, :invalid_use_type}
car_type not in @valid_car_types -> {:error, :invalid_car_type}
byte_size(chassis) == 0 -> {:error, :missing_chassis_number}
byte_size(engine) == 0 -> {:error, :missing_engine_number}
byte_size(plate) == 0 -> {:error, :missing_plate}
true -> :ok
end
end

View File

@@ -0,0 +1,22 @@
defmodule PolicyService.Aggregates.FireContentsPolicyApplication do
use PolicyService.Aggregates.PolicyApplication,
policy_type: "fire_contents",
commands: PolicyService.Commands.FireContentsPolicy
def validate_details(%{
"location" => location,
"contents_value" => value,
"property_use" => use_type,
"security_measures" => measures,
"high_value_items" => items
})
when is_binary(location) and byte_size(location) > 0 and
is_number(value) and value > 0 and
is_binary(use_type) and byte_size(use_type) > 0 and
is_list(measures) and
is_list(items) do
:ok
end
def validate_details(_), do: {:error, :invalid_fire_contents_details}
end

View File

@@ -0,0 +1,22 @@
defmodule PolicyService.Aggregates.FireStructurePolicyApplication do
use PolicyService.Aggregates.PolicyApplication,
policy_type: "fire_structure",
commands: PolicyService.Commands.FireStructurePolicy
def validate_details(%{
"location" => location,
"property_value" => value,
"property_use" => use_type,
"security_measures" => measures,
"market_value" => market_value
})
when is_binary(location) and byte_size(location) > 0 and
is_number(value) and value > 0 and
is_binary(use_type) and byte_size(use_type) > 0 and
is_list(measures) and
is_number(market_value) and market_value > 0 do
:ok
end
def validate_details(_), do: {:error, :invalid_fire_structure_details}
end

View File

@@ -39,7 +39,8 @@ defmodule PolicyService.Aggregates.PolicyApplication do
defstruct [
:id,
:submitted_by,
:applicant_info,
:insured,
:buyer,
:policy_details,
:selected_providers,
:accepted_plan_id,
@@ -52,13 +53,15 @@ defmodule PolicyService.Aggregates.PolicyApplication do
endorsements: %{}
]
# ── Execute ────────────────────────────────────────────────────────────
# ── Execute ──────────────────────────────────────────────────
@impl Commanded.Aggregates.Aggregate
def execute(%__MODULE__{state: nil}, %SubmitPolicyApplication{} = cmd) do
with :ok <- PolicyService.Aggregates.PolicyApplication.validate_policy_id(cmd.id),
:ok <-
PolicyService.Aggregates.PolicyApplication.validate_applicant(cmd.applicant_info),
PolicyService.Aggregates.PolicyApplication.validate_insured(cmd.insured),
:ok <-
PolicyService.Aggregates.PolicyApplication.validate_buyer(cmd.buyer),
:ok <- validate_details(cmd.policy_details),
:ok <-
PolicyService.Aggregates.PolicyApplication.validate_providers(
@@ -70,7 +73,8 @@ defmodule PolicyService.Aggregates.PolicyApplication do
id: cmd.id,
provider_id: provider.provider_id,
provider_email: provider.email,
applicant_info: cmd.applicant_info,
insured: cmd.insured,
buyer: cmd.buyer,
policy_details: cmd.policy_details,
requested_at: DateTime.utc_now()
}
@@ -80,7 +84,8 @@ defmodule PolicyService.Aggregates.PolicyApplication do
%PolicyApplicationSubmitted{
id: cmd.id,
submitted_by: cmd.submitted_by,
applicant_info: cmd.applicant_info,
insured: cmd.insured,
buyer: cmd.buyer,
policy_details: cmd.policy_details,
selected_providers: cmd.selected_providers,
submitted_at: DateTime.utc_now()
@@ -178,7 +183,7 @@ defmodule PolicyService.Aggregates.PolicyApplication do
}
end
# ── Apply ──────────────────────────────────────────────────────────────
# ── Apply ──────────────────────────────────────────────────────
@impl Commanded.Aggregates.Aggregate
def apply(%__MODULE__{} = agg, %PolicyApplicationSubmitted{} = e) do
@@ -186,7 +191,8 @@ defmodule PolicyService.Aggregates.PolicyApplication do
agg
| id: e.id,
submitted_by: e.submitted_by,
applicant_info: e.applicant_info,
insured: e.insured,
buyer: e.buyer,
policy_details: e.policy_details,
selected_providers: e.selected_providers,
quotes: %{},
@@ -247,12 +253,17 @@ defmodule PolicyService.Aggregates.PolicyApplication do
def validate_user(id) when is_binary(id) and byte_size(id) > 0, do: :ok
def validate_user(_), do: {:error, :missing_user_id}
def validate_applicant(%{"name" => n, "date_of_birth" => _, "document_id" => d})
def validate_insured(%{
"type" => "individual",
"name" => n,
"date_of_birth" => _,
"document_id" => d
})
when is_binary(n) and is_binary(d) and byte_size(n) > 0 and byte_size(d) > 0,
do: :ok
# Match on string keys for Company
def validate_applicant(%{
def validate_insured(%{
"type" => "corporate",
"company_name" => c,
"ruc" => r,
"legal_rep_name" => rep,
@@ -262,7 +273,29 @@ defmodule PolicyService.Aggregates.PolicyApplication do
byte_size(c) > 0 and byte_size(r) > 0,
do: :ok
def validate_applicant(_), do: {:error, :invalid_applicant_info}
def validate_insured(_), do: {:error, :invalid_insured_info}
def validate_buyer(%{
"type" => "individual",
"name" => n,
"date_of_birth" => _,
"document_id" => d
})
when is_binary(n) and is_binary(d) and byte_size(n) > 0 and byte_size(d) > 0,
do: :ok
def validate_buyer(%{
"type" => "corporate",
"company_name" => c,
"ruc" => r,
"legal_rep_name" => rep,
"legal_rep_document" => rd
})
when is_binary(c) and is_binary(r) and is_binary(rep) and is_binary(rd) and
byte_size(c) > 0 and byte_size(r) > 0,
do: :ok
def validate_buyer(_), do: {:error, :invalid_buyer_info}
def validate_providers(p) when is_list(p) and length(p) > 0, do: :ok
def validate_providers(_), do: {:error, :no_providers_selected}

View File

@@ -1,9 +1,9 @@
defmodule PolicyService.Aggregates.PolicyId do
@type t :: %__MODULE__{
org_id: String.t(),
policy_type: String.t(),
application_id: String.t()
}
org_id: String.t(),
policy_type: String.t(),
application_id: String.t()
}
@derive Jason.Encoder
defstruct [:org_id, :policy_type, :application_id]

View File

@@ -13,15 +13,39 @@ defmodule PolicyService.Router do
identity: :id
)
# Route Fire commands to Fire Aggregate
# Route Life commands to Life Aggregate
dispatch(
[
PolicyService.Commands.FirePolicy.SubmitPolicyApplication,
PolicyService.Commands.FirePolicy.RecordProviderQuote,
PolicyService.Commands.FirePolicy.AcceptQuoteAndSolicit,
PolicyService.Commands.FirePolicy.RecordPolicyIssued
PolicyService.Commands.LifePolicy.SubmitPolicyApplication,
PolicyService.Commands.LifePolicy.RecordProviderQuote,
PolicyService.Commands.LifePolicy.AcceptQuoteAndSolicit,
PolicyService.Commands.LifePolicy.RecordPolicyIssued
],
to: PolicyService.Aggregates.FirePolicyApplication,
to: PolicyService.Aggregates.LifePolicyApplication,
identity: :id
)
# Route FireStructure commands to FireStructure Aggregate
dispatch(
[
PolicyService.Commands.FireStructurePolicy.SubmitPolicyApplication,
PolicyService.Commands.FireStructurePolicy.RecordProviderQuote,
PolicyService.Commands.FireStructurePolicy.AcceptQuoteAndSolicit,
PolicyService.Commands.FireStructurePolicy.RecordPolicyIssued
],
to: PolicyService.Aggregates.FireStructurePolicyApplication,
identity: :id
)
# Route FireContents commands to FireContents Aggregate
dispatch(
[
PolicyService.Commands.FireContentsPolicy.SubmitPolicyApplication,
PolicyService.Commands.FireContentsPolicy.RecordProviderQuote,
PolicyService.Commands.FireContentsPolicy.AcceptQuoteAndSolicit,
PolicyService.Commands.FireContentsPolicy.RecordPolicyIssued
],
to: PolicyService.Aggregates.FireContentsPolicyApplication,
identity: :id
)
end
@@ -31,4 +55,4 @@ defmodule PolicyService.CommandedApp do
otp_app: :policy_service
router(PolicyService.Router)
end
end

View File

@@ -1,6 +1,8 @@
defmodule PolicyService.Commands.CarPolicy do
defmodule SubmitPolicyApplication, do: use(PolicyService.Commands.Policy.SubmitPolicyApplication)
defmodule SubmitPolicyApplication,
do: use(PolicyService.Commands.Policy.SubmitPolicyApplication)
defmodule RecordProviderQuote, do: use(PolicyService.Commands.Policy.RecordProviderQuote)
defmodule AcceptQuoteAndSolicit, do: use(PolicyService.Commands.Policy.AcceptQuoteAndSolicit)
defmodule RecordPolicyIssued, do: use(PolicyService.Commands.Policy.RecordPolicyIssued)
end
end

View File

@@ -0,0 +1,8 @@
defmodule PolicyService.Commands.FireContentsPolicy do
defmodule SubmitPolicyApplication,
do: use(PolicyService.Commands.Policy.SubmitPolicyApplication)
defmodule RecordProviderQuote, do: use(PolicyService.Commands.Policy.RecordProviderQuote)
defmodule AcceptQuoteAndSolicit, do: use(PolicyService.Commands.Policy.AcceptQuoteAndSolicit)
defmodule RecordPolicyIssued, do: use(PolicyService.Commands.Policy.RecordPolicyIssued)
end

View File

@@ -1,6 +1,8 @@
defmodule PolicyService.Commands.FirePolicy do
defmodule SubmitPolicyApplication, do: use(PolicyService.Commands.Policy.SubmitPolicyApplication)
defmodule SubmitPolicyApplication,
do: use(PolicyService.Commands.Policy.SubmitPolicyApplication)
defmodule RecordProviderQuote, do: use(PolicyService.Commands.Policy.RecordProviderQuote)
defmodule AcceptQuoteAndSolicit, do: use(PolicyService.Commands.Policy.AcceptQuoteAndSolicit)
defmodule RecordPolicyIssued, do: use(PolicyService.Commands.Policy.RecordPolicyIssued)
end
end

View File

@@ -0,0 +1,8 @@
defmodule PolicyService.Commands.FireStructurePolicy do
defmodule SubmitPolicyApplication,
do: use(PolicyService.Commands.Policy.SubmitPolicyApplication)
defmodule RecordProviderQuote, do: use(PolicyService.Commands.Policy.RecordProviderQuote)
defmodule AcceptQuoteAndSolicit, do: use(PolicyService.Commands.Policy.AcceptQuoteAndSolicit)
defmodule RecordPolicyIssued, do: use(PolicyService.Commands.Policy.RecordPolicyIssued)
end

View File

@@ -0,0 +1,8 @@
defmodule PolicyService.Commands.LifePolicy do
defmodule SubmitPolicyApplication,
do: use(PolicyService.Commands.Policy.SubmitPolicyApplication)
defmodule RecordProviderQuote, do: use(PolicyService.Commands.Policy.RecordProviderQuote)
defmodule AcceptQuoteAndSolicit, do: use(PolicyService.Commands.Policy.AcceptQuoteAndSolicit)
defmodule RecordPolicyIssued, do: use(PolicyService.Commands.Policy.RecordPolicyIssued)
end

View File

@@ -11,7 +11,8 @@ defmodule PolicyService.Commands.Policy do
typedstruct do
field :id, PolicyService.Aggregates.PolicyId.t(), enforce: true
field :submitted_by, String.t(), enforce: true
field :applicant_info, map(), enforce: true
field :insured, map(), enforce: true
field :buyer, map(), enforce: true
field :policy_details, map()
field :selected_providers, list(), enforce: true
end
@@ -67,4 +68,4 @@ defmodule PolicyService.Commands.Policy do
end
end
end
end
end

View File

@@ -42,6 +42,10 @@ defmodule PolicyService.Consumers.QuoteTaskConsumer do
{:noreply, state}
end
def handle_info({:basic_consume_ok, _}, state), do: {:noreply, state}
def handle_info({:basic_cancel, _}, state), do: {:stop, :normal, state}
def handle_info({:basic_cancel_ok, _}, state), do: {:noreply, state}
defp process(payload) do
with {:ok, event} <- Jason.decode(payload),
:ok <- dispatch(event) do
@@ -49,10 +53,6 @@ defmodule PolicyService.Consumers.QuoteTaskConsumer do
end
end
def handle_info({:basic_consume_ok, _}, state), do: {:noreply, state}
def handle_info({:basic_cancel, _}, state), do: {:stop, :normal, state}
def handle_info({:basic_cancel_ok, _}, state), do: {:noreply, state}
defp dispatch(%{
"application_id" => %{
"org_id" => org_id,

View File

@@ -41,6 +41,10 @@ defmodule PolicyService.Consumers.SolicitationTaskConsumer do
{:noreply, state}
end
def handle_info({:basic_consume_ok, _}, state), do: {:noreply, state}
def handle_info({:basic_cancel, _}, state), do: {:stop, :normal, state}
def handle_info({:basic_cancel_ok, _}, state), do: {:noreply, state}
defp process(payload) do
with {:ok, event} <- Jason.decode(payload),
:ok <- dispatch(event) do
@@ -48,17 +52,13 @@ defmodule PolicyService.Consumers.SolicitationTaskConsumer do
end
end
def handle_info({:basic_consume_ok, _}, state), do: {:noreply, state}
def handle_info({:basic_cancel, _}, state), do: {:stop, :normal, state}
def handle_info({:basic_cancel_ok, _}, state), do: {:noreply, state}
defp dispatch(%{
"application_id" => %{
"org_id" => org_id,
"policy_type" => policy_type,
"application_id" => app_id
},
"task_info" => task_info,
"task_info" => _task_info,
"submission" => submission
}) do
cmd =

View File

@@ -8,7 +8,10 @@ defmodule PolicyService.Events do
defmacro __using__(_opts) do
quote do
defimpl Commanded.Serialization.JsonDecoder do
def decode(%{id: %{org_id: org_id, policy_type: policy_type, application_id: application_id}} = event) do
def decode(
%{id: %{org_id: org_id, policy_type: policy_type, application_id: application_id}} =
event
) do
%{event | id: PolicyId.new(org_id, policy_type, application_id)}
end
@@ -26,19 +29,45 @@ defmodule PolicyService.Events.Policy do
defmodule PolicyApplicationSubmitted do
use PolicyService.Events
@derive Jason.Encoder
defstruct [:id, :submitted_by, :applicant_info, :policy_details, :selected_providers, :submitted_at]
defstruct [
:id,
:submitted_by,
:insured,
:buyer,
:policy_details,
:selected_providers,
:submitted_at
]
end
defmodule QuoteRequestSent do
use PolicyService.Events
@derive Jason.Encoder
defstruct [:id, :provider_id, :provider_email, :applicant_info, :policy_details, :requested_at]
defstruct [
:id,
:provider_id,
:provider_email,
:insured,
:buyer,
:policy_details,
:requested_at
]
end
defmodule ProviderQuoteReceived do
use PolicyService.Events
@derive Jason.Encoder
defstruct [:id, :recorded_by, :provider_id, :quote_id, :premium, :coverage_details, :valid_until, :plans, :received_at]
defstruct [
:id,
:recorded_by,
:provider_id,
:quote_id,
:premium,
:coverage_details,
:valid_until,
:plans,
:received_at
]
end
defmodule AllQuotesReceived do
@@ -53,11 +82,11 @@ defmodule PolicyService.Events.Policy do
defstruct [:id, :accepted_by, :quote, :plan, :provider]
end
defmodule SolicitationRequestSent do
use PolicyService.Events
@derive Jason.Encoder
defstruct [:id, :plan, :provider_id]
end
defmodule SolicitationRequestSent do
use PolicyService.Events
@derive Jason.Encoder
defstruct [:id, :plan, :provider_id]
end
defmodule PolicyIssued do
use PolicyService.Events

View File

@@ -5,11 +5,16 @@ defmodule PolicyService.Handlers.SolicitationRequestHandler do
require Logger
alias PolicyService.Events.Policy.SolicitationRequestSent
alias PolicyService.MessageBus
alias PolicyService.Events.Policy.SolicitationRequestSent
alias PolicyService.MessageBus
def handle(%SolicitationRequestSent{} = event, _metadata) do
MessageBus.publish("policy_service.events.solicitation_requested", "solicitation.requested", event)
:ok
end
def handle(%SolicitationRequestSent{} = event, _metadata) do
MessageBus.publish(
"policy_service.events.solicitation_requested",
"solicitation.requested",
event
)
:ok
end
end

View File

@@ -18,4 +18,4 @@ defmodule PolicyService.MessageBus do
end
defp amqp_url, do: Application.fetch_env!(:policy_service, :amqp_url)
end
end

View File

@@ -7,10 +7,14 @@ defmodule PolicyService.Filters.PolicyApplicationFilters do
where(
query,
[p],
fragment("?->>'name' ilike ?", p.applicant_info, ^term) or
fragment("?->>'company_name' ilike ?", p.applicant_info, ^term) or
fragment("?->>'document_id' ilike ?", p.applicant_info, ^term) or
fragment("?->>'ruc' ilike ?", p.applicant_info, ^term) or
fragment("?->>'name' ilike ?", p.insured, ^term) or
fragment("?->>'company_name' ilike ?", p.insured, ^term) or
fragment("?->>'document_id' ilike ?", p.insured, ^term) or
fragment("?->>'ruc' ilike ?", p.insured, ^term) or
fragment("?->>'name' ilike ?", p.buyer, ^term) or
fragment("?->>'company_name' ilike ?", p.buyer, ^term) or
fragment("?->>'document_id' ilike ?", p.buyer, ^term) or
fragment("?->>'ruc' ilike ?", p.buyer, ^term) or
ilike(p.provider_policy_number, ^term)
)
end

View File

@@ -2,29 +2,30 @@ defmodule PolicyService.Projections.PolicyApplication do
use Ecto.Schema
@derive {Jason.Encoder,
only: [
:id,
:application_id,
:org_id,
:submitted_by,
:policy_type,
:applicant_info,
:policy_details,
:selected_providers,
:quotes,
:accepted_plan_id,
:accepted_by,
:provider_policy_number,
:premium,
:effective_date,
:expiry_date,
:status,
:submitted_at,
:solicitation_sent_at,
:issued_at,
:inserted_at,
:updated_at
]}
only: [
:id,
:application_id,
:org_id,
:submitted_by,
:policy_type,
:insured,
:buyer,
:policy_details,
:selected_providers,
:quotes,
:accepted_plan_id,
:accepted_by,
:provider_policy_number,
:premium,
:effective_date,
:expiry_date,
:status,
:submitted_at,
:solicitation_sent_at,
:issued_at,
:inserted_at,
:updated_at
]}
@derive {
Flop.Schema,
@@ -50,7 +51,8 @@ defmodule PolicyService.Projections.PolicyApplication do
field :submitted_by, :string
field :policy_type, :string
field :applicant_info, :map
field :insured, :map
field :buyer, :map
field :policy_details, :map
field :selected_providers, {:array, :string}, default: []
@@ -59,7 +61,7 @@ defmodule PolicyService.Projections.PolicyApplication do
field :accepted_plan_id, :string
field :accepted_by, :string
field :provider_policy_number, :string
field :provider_policy_number, :string
field :premium, :decimal
field :effective_date, :date

View File

@@ -24,7 +24,8 @@ defmodule PolicyService.Projectors.PolicyProjector do
org_id: e.id.org_id,
submitted_by: e.submitted_by,
policy_type: e.id.policy_type,
applicant_info: atomize(e.applicant_info),
insured: atomize(e.insured),
buyer: atomize(e.buyer),
policy_details: atomize(e.policy_details),
selected_providers: Enum.map(e.selected_providers, & &1["provider_id"]),
quotes: %{},

View File

@@ -34,4 +34,4 @@ defmodule PolicyService.Release do
:ok = EventStore.Tasks.Init.exec(config, [])
end
end
end