This commit is contained in:
@@ -1,309 +1,34 @@
|
||||
defmodule PolicyService.Aggregates.CarPolicyApplication do
|
||||
@moduledoc """
|
||||
Aggregate for managing car insurance policy applications.
|
||||
use PolicyService.Aggregates.PolicyApplication,
|
||||
policy_type: "car",
|
||||
commands: PolicyService.Commands.CarPolicy
|
||||
|
||||
Lifecycle:
|
||||
nil → :awaiting_quotes → :solicitation_sent → :issued
|
||||
"""
|
||||
|
||||
defstruct [
|
||||
:application_id,
|
||||
:org_id,
|
||||
:submitted_by,
|
||||
:applicant_info,
|
||||
:car_details,
|
||||
:selected_providers,
|
||||
:quotes,
|
||||
:accepted_quote_id,
|
||||
:accepted_provider_id,
|
||||
:policy_number,
|
||||
:state
|
||||
]
|
||||
|
||||
alias PolicyService.Commands.Car.{
|
||||
SubmitCarPolicyApplication,
|
||||
RecordCarProviderQuote,
|
||||
AcceptCarQuoteAndSolicit,
|
||||
RecordCarPolicyIssued
|
||||
}
|
||||
|
||||
alias PolicyService.Events.Car.{
|
||||
CarPolicyApplicationSubmitted,
|
||||
CarProviderQuoteReceived,
|
||||
AllCarQuotesReceived,
|
||||
CarQuoteAccepted,
|
||||
CarSolicitationSent,
|
||||
CarPolicyIssued,
|
||||
CarQuoteRequestSent
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Submit — establishes org ownership
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def execute(%__MODULE__{state: nil}, %SubmitCarPolicyApplication{} = cmd) do
|
||||
with :ok <- validate_org(cmd.org_id),
|
||||
:ok <- validate_user(cmd.submitted_by),
|
||||
:ok <- validate_applicant(cmd.applicant_info),
|
||||
:ok <- validate_car_details(cmd.car_details),
|
||||
:ok <- validate_providers(cmd.selected_providers) do
|
||||
quote_requests =
|
||||
Enum.map(cmd.selected_providers, fn provider ->
|
||||
%CarQuoteRequestSent{
|
||||
application_id: cmd.application_id,
|
||||
org_id: cmd.org_id,
|
||||
provider_id: provider.id,
|
||||
provider_email: provider.email,
|
||||
applicant_info: cmd.applicant_info,
|
||||
car_details: cmd.car_details,
|
||||
requested_at: DateTime.utc_now()
|
||||
}
|
||||
end)
|
||||
|
||||
[
|
||||
%CarPolicyApplicationSubmitted{
|
||||
application_id: cmd.application_id,
|
||||
org_id: cmd.org_id,
|
||||
submitted_by: cmd.submitted_by,
|
||||
applicant_info: cmd.applicant_info,
|
||||
car_details: cmd.car_details,
|
||||
selected_providers: cmd.selected_providers,
|
||||
submitted_at: DateTime.utc_now()
|
||||
}
|
||||
| quote_requests
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
def execute(%__MODULE__{state: state}, %SubmitCarPolicyApplication{}) do
|
||||
{:error, {:invalid_state, "cannot submit in state: #{state}"}}
|
||||
end
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Record provider quote — external webhook, verify org
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def execute(%__MODULE__{state: :awaiting_quotes} = agg, %RecordCarProviderQuote{} = cmd) do
|
||||
with :ok <- verify_org(agg, cmd) do
|
||||
if Map.has_key?(agg.quotes, cmd.provider_id) do
|
||||
{:error, {:duplicate_quote, "quote from provider #{cmd.provider_id} already received"}}
|
||||
else
|
||||
quote_event = %CarProviderQuoteReceived{
|
||||
application_id: cmd.application_id,
|
||||
org_id: agg.org_id,
|
||||
recorded_by: cmd.recorded_by,
|
||||
provider_id: cmd.provider_id,
|
||||
quote_id: cmd.quote_id,
|
||||
premium: cmd.premium,
|
||||
coverage_details: cmd.coverage_details,
|
||||
valid_until: cmd.valid_until,
|
||||
received_at: DateTime.utc_now()
|
||||
}
|
||||
|
||||
new_quote_count = map_size(agg.quotes) + 1
|
||||
|
||||
if new_quote_count == length(agg.selected_providers) do
|
||||
[
|
||||
quote_event,
|
||||
%AllCarQuotesReceived{
|
||||
application_id: cmd.application_id,
|
||||
org_id: agg.org_id,
|
||||
quote_count: new_quote_count
|
||||
}
|
||||
]
|
||||
else
|
||||
quote_event
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def execute(%__MODULE__{state: state}, %RecordCarProviderQuote{}) do
|
||||
{:error, {:invalid_state, "cannot record quote in state: #{state}"}}
|
||||
end
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Accept quote and solicit — internal user action, verify org
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def execute(%__MODULE__{state: :awaiting_quotes} = agg, %AcceptCarQuoteAndSolicit{} = cmd) do
|
||||
with :ok <- verify_org(agg, cmd) do
|
||||
case find_quote(agg.quotes, cmd.quote_id) do
|
||||
nil ->
|
||||
{:error, {:quote_not_found, "quote #{cmd.quote_id} not found"}}
|
||||
|
||||
{provider_id, _quote} ->
|
||||
[
|
||||
%CarQuoteAccepted{
|
||||
application_id: cmd.application_id,
|
||||
org_id: agg.org_id,
|
||||
accepted_by: cmd.accepted_by,
|
||||
quote_id: cmd.quote_id,
|
||||
provider_id: provider_id,
|
||||
accepted_at: DateTime.utc_now()
|
||||
},
|
||||
%CarSolicitationSent{
|
||||
application_id: cmd.application_id,
|
||||
org_id: agg.org_id,
|
||||
provider_id: provider_id,
|
||||
quote_id: cmd.quote_id,
|
||||
sent_at: DateTime.utc_now()
|
||||
}
|
||||
]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def execute(%__MODULE__{state: state}, %AcceptCarQuoteAndSolicit{}) do
|
||||
{:error, {:invalid_state, "cannot accept quote in state: #{state}"}}
|
||||
end
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Record policy issued — external or internal, verify org
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def execute(%__MODULE__{state: :solicitation_sent} = agg, %RecordCarPolicyIssued{} = cmd) do
|
||||
with :ok <- verify_org(agg, cmd) do
|
||||
if cmd.provider_id != agg.accepted_provider_id do
|
||||
{:error, {:provider_mismatch, "policy issued by unexpected provider"}}
|
||||
else
|
||||
%CarPolicyIssued{
|
||||
application_id: cmd.application_id,
|
||||
org_id: agg.org_id,
|
||||
recorded_by: cmd.recorded_by,
|
||||
policy_number: cmd.policy_number,
|
||||
provider_id: cmd.provider_id,
|
||||
effective_date: cmd.effective_date,
|
||||
expiry_date: cmd.expiry_date,
|
||||
issued_at: DateTime.utc_now()
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def execute(%__MODULE__{state: state}, %RecordCarPolicyIssued{}) do
|
||||
{:error, {:invalid_state, "cannot record policy in state: #{state}"}}
|
||||
end
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Apply events
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def apply(%__MODULE__{} = agg, %CarPolicyApplicationSubmitted{} = e) do
|
||||
%__MODULE__{
|
||||
agg
|
||||
| application_id: e.application_id,
|
||||
org_id: e.org_id,
|
||||
submitted_by: e.submitted_by,
|
||||
applicant_info: e.applicant_info,
|
||||
car_details: e.car_details,
|
||||
selected_providers: e.selected_providers,
|
||||
quotes: %{},
|
||||
state: :awaiting_quotes
|
||||
}
|
||||
end
|
||||
|
||||
def apply(%__MODULE__{} = agg, %CarQuoteRequestSent{}), do: agg
|
||||
|
||||
def apply(%__MODULE__{} = agg, %CarProviderQuoteReceived{} = e) do
|
||||
quote_data = %{
|
||||
quote_id: e.quote_id,
|
||||
premium: e.premium,
|
||||
coverage_details: e.coverage_details,
|
||||
valid_until: e.valid_until
|
||||
}
|
||||
|
||||
%__MODULE__{agg | quotes: Map.put(agg.quotes, e.provider_id, quote_data)}
|
||||
end
|
||||
|
||||
def apply(%__MODULE__{} = agg, %AllCarQuotesReceived{}), do: agg
|
||||
|
||||
def apply(%__MODULE__{} = agg, %CarQuoteAccepted{} = e) do
|
||||
%__MODULE__{agg | accepted_quote_id: e.quote_id, accepted_provider_id: e.provider_id}
|
||||
end
|
||||
|
||||
def apply(%__MODULE__{} = agg, %CarSolicitationSent{}) do
|
||||
%__MODULE__{agg | state: :solicitation_sent}
|
||||
end
|
||||
|
||||
def apply(%__MODULE__{} = agg, %CarPolicyIssued{} = e) do
|
||||
%__MODULE__{agg | policy_number: e.policy_number, state: :issued}
|
||||
end
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Private helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
defp verify_org(%__MODULE__{org_id: org_id}, %{org_id: cmd_org_id}) do
|
||||
if org_id == cmd_org_id,
|
||||
do: :ok,
|
||||
else: {:error, :org_mismatch}
|
||||
end
|
||||
|
||||
defp validate_org(org_id) when is_binary(org_id) and byte_size(org_id) > 0, do: :ok
|
||||
defp validate_org(_), do: {:error, :missing_org_id}
|
||||
|
||||
defp validate_user(user_id) when is_binary(user_id) and byte_size(user_id) > 0, do: :ok
|
||||
defp validate_user(_), do: {:error, :missing_user_id}
|
||||
|
||||
defp validate_applicant(%{name: name, date_of_birth: dob, document_id: doc})
|
||||
when is_binary(name) and is_binary(doc),
|
||||
do: :ok
|
||||
|
||||
defp validate_applicant(_), do: {:error, :invalid_applicant_info}
|
||||
|
||||
@valid_use_types ~w(private commercial bus taxi school)a
|
||||
@valid_car_types ~w(sedan suv hatchback coupe convertible pickup van minivan truck)a
|
||||
|
||||
defp validate_car_details(%{
|
||||
plate: plate,
|
||||
make: make,
|
||||
model: model,
|
||||
year: year,
|
||||
car_value: car_value,
|
||||
use_type: use_type,
|
||||
car_type: car_type,
|
||||
chassis_number: chassis_number,
|
||||
engine_number: engine_number
|
||||
})
|
||||
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_binary(chassis_number) and is_binary(engine_number) do
|
||||
current_year = Date.utc_today().year
|
||||
@valid_use_types ~w(private commercial bus taxi school)
|
||||
@valid_car_types ~w(sedan suv hatchback coupe convertible pickup van minivan truck)
|
||||
|
||||
def validate_details(%{
|
||||
"plate" => plate,
|
||||
"make" => make,
|
||||
"model" => model,
|
||||
"year" => year,
|
||||
"car_value" => car_value,
|
||||
"use_type" => use_type,
|
||||
"car_type" => car_type,
|
||||
"chassis_number" => chassis,
|
||||
"engine_number" => engine
|
||||
})
|
||||
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_binary(chassis) and is_binary(engine) do
|
||||
cond do
|
||||
year < 1886 ->
|
||||
{:error, :invalid_car_year}
|
||||
|
||||
year > current_year + 1 ->
|
||||
{:error, :invalid_car_year}
|
||||
|
||||
use_type not in @valid_use_types ->
|
||||
{:error, {:invalid_use_type, "must be one of: #{inspect(@valid_use_types)}"}}
|
||||
|
||||
car_type not in @valid_car_types ->
|
||||
{:error, {:invalid_car_type, "must be one of: #{inspect(@valid_car_types)}"}}
|
||||
|
||||
byte_size(chassis_number) == 0 ->
|
||||
{:error, :missing_chassis_number}
|
||||
|
||||
byte_size(engine_number) == 0 ->
|
||||
{:error, :missing_engine_number}
|
||||
|
||||
true ->
|
||||
:ok
|
||||
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}
|
||||
true -> :ok
|
||||
end
|
||||
end
|
||||
|
||||
defp validate_car_details(_), do: {:error, :invalid_car_details}
|
||||
|
||||
defp validate_providers(providers)
|
||||
when is_list(providers) and length(providers) > 0,
|
||||
do: :ok
|
||||
|
||||
defp validate_providers(_), do: {:error, :no_providers_selected}
|
||||
|
||||
defp find_quote(quotes, quote_id) do
|
||||
Enum.find(quotes, fn {_provider_id, q} -> q.quote_id == quote_id end)
|
||||
end
|
||||
def validate_details(_), do: {:error, :invalid_car_details}
|
||||
end
|
||||
|
||||
11
lib/policy_service/aggregates/fire_policy_application.ex
Normal file
11
lib/policy_service/aggregates/fire_policy_application.ex
Normal file
@@ -0,0 +1,11 @@
|
||||
defmodule PolicyService.Aggregates.FirePolicyApplication do
|
||||
use PolicyService.Aggregates.PolicyApplication,
|
||||
policy_type: "fire",
|
||||
commands: PolicyService.Commands.FirePolicy
|
||||
|
||||
def validate_details(%{property_address: addr, property_value: val})
|
||||
when is_binary(addr) and byte_size(addr) > 0 and is_number(val) and val > 0,
|
||||
do: :ok
|
||||
|
||||
def validate_details(_), do: {:error, :invalid_fire_details}
|
||||
end
|
||||
284
lib/policy_service/aggregates/policy_application.ex
Normal file
284
lib/policy_service/aggregates/policy_application.ex
Normal file
@@ -0,0 +1,284 @@
|
||||
defmodule PolicyService.Aggregates.PolicyApplication do
|
||||
@moduledoc """
|
||||
Behaviour and __using__ macro for policy application aggregates.
|
||||
Each policy type implements validate_details/1 and declares its detail fields.
|
||||
|
||||
Usage:
|
||||
defmodule PolicyService.Aggregates.CarPolicyApplication do
|
||||
use PolicyService.Aggregates.PolicyApplication,
|
||||
policy_type: "car"
|
||||
end
|
||||
"""
|
||||
|
||||
@callback validate_details(map()) :: :ok | {:error, term()}
|
||||
|
||||
defmacro __using__(opts) do
|
||||
policy_type = Keyword.fetch!(opts, :policy_type)
|
||||
commands_module = Keyword.get(opts, :commands, PolicyService.Commands.Policy)
|
||||
|
||||
quote do
|
||||
@behaviour Commanded.Aggregates.Aggregate
|
||||
|
||||
alias unquote(commands_module).SubmitPolicyApplication
|
||||
alias unquote(commands_module).RecordProviderQuote
|
||||
alias unquote(commands_module).AcceptQuoteAndSolicit
|
||||
alias unquote(commands_module).RecordPolicyIssued
|
||||
|
||||
alias PolicyService.Events.Policy.{
|
||||
PolicyApplicationSubmitted,
|
||||
QuoteRequestSent,
|
||||
ProviderQuoteReceived,
|
||||
AllQuotesReceived,
|
||||
QuoteAccepted,
|
||||
SolicitationSent,
|
||||
PolicyIssued
|
||||
}
|
||||
|
||||
@policy_type unquote(policy_type)
|
||||
|
||||
defstruct [
|
||||
:id,
|
||||
:submitted_by,
|
||||
:applicant_info,
|
||||
:policy_details,
|
||||
:selected_providers,
|
||||
:accepted_quote_id,
|
||||
:accepted_plan_id,
|
||||
:accepted_provider_id,
|
||||
:solicitation_id,
|
||||
:policy_number,
|
||||
:effective_date,
|
||||
:expiry_date,
|
||||
:state,
|
||||
quotes: %{},
|
||||
pending_endorsements: %{}
|
||||
]
|
||||
|
||||
# ── 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),
|
||||
:ok <- validate_details(cmd.policy_details),
|
||||
:ok <-
|
||||
PolicyService.Aggregates.PolicyApplication.validate_providers(
|
||||
cmd.selected_providers
|
||||
) do
|
||||
quote_requests =
|
||||
Enum.map(cmd.selected_providers, fn provider ->
|
||||
%QuoteRequestSent{
|
||||
id: cmd.id,
|
||||
provider_id: provider.provider_id,
|
||||
provider_email: provider.email,
|
||||
applicant_info: cmd.applicant_info,
|
||||
policy_details: cmd.policy_details,
|
||||
requested_at: DateTime.utc_now()
|
||||
}
|
||||
end)
|
||||
|
||||
[
|
||||
%PolicyApplicationSubmitted{
|
||||
id: cmd.id,
|
||||
submitted_by: cmd.submitted_by,
|
||||
applicant_info: cmd.applicant_info,
|
||||
policy_details: cmd.policy_details,
|
||||
selected_providers: cmd.selected_providers,
|
||||
submitted_at: DateTime.utc_now()
|
||||
}
|
||||
| quote_requests
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
def execute(%__MODULE__{state: state}, %SubmitPolicyApplication{}) do
|
||||
{:error, {:invalid_state, "cannot submit in state: #{state}"}}
|
||||
end
|
||||
|
||||
def execute(%__MODULE__{state: :awaiting_quotes} = agg, %RecordProviderQuote{} = cmd) do
|
||||
if Map.has_key?(agg.quotes, cmd.provider_id) do
|
||||
{:error, {:duplicate_quote, "quote from #{cmd.provider_id} already received"}}
|
||||
else
|
||||
quote_event = %ProviderQuoteReceived{
|
||||
id: cmd.id,
|
||||
recorded_by: cmd.recorded_by,
|
||||
provider_id: cmd.provider_id,
|
||||
quote_id: cmd.quote_id,
|
||||
valid_until: cmd.valid_until,
|
||||
plans: cmd.plans,
|
||||
received_at: DateTime.utc_now()
|
||||
}
|
||||
|
||||
new_quote_count = map_size(agg.quotes) + 1
|
||||
|
||||
if new_quote_count == length(agg.selected_providers) do
|
||||
[
|
||||
quote_event,
|
||||
%AllQuotesReceived{
|
||||
id: cmd.id,
|
||||
quote_count: new_quote_count
|
||||
}
|
||||
]
|
||||
else
|
||||
quote_event
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def execute(%__MODULE__{state: state}, %RecordProviderQuote{}) do
|
||||
{:error, {:invalid_state, "cannot record quote in state: #{state}"}}
|
||||
end
|
||||
|
||||
def execute(%__MODULE__{state: :awaiting_quotes}, %AcceptQuoteAndSolicit{}) do
|
||||
{:error, :no_quotes_received}
|
||||
end
|
||||
|
||||
def execute(%__MODULE__{state: state}, %AcceptQuoteAndSolicit{})
|
||||
when state not in [:quotes_received] do
|
||||
{:error, :invalid_state}
|
||||
end
|
||||
|
||||
def execute(%__MODULE__{} = agg, %AcceptQuoteAndSolicit{} = cmd) do
|
||||
with {:ok, quote} <-
|
||||
PolicyService.Aggregates.PolicyApplication.find_quote(agg, cmd.quote_id),
|
||||
{:ok, provider} <-
|
||||
PolicyService.Aggregates.PolicyApplication.find_provider(agg, quote.provider_id),
|
||||
{:ok, plan} <-
|
||||
PolicyService.Aggregates.PolicyApplication.find_plan(quote, cmd.plan_id) do
|
||||
%QuoteAccepted{
|
||||
id: agg.id,
|
||||
quote: quote,
|
||||
plan: plan,
|
||||
provider: provider,
|
||||
accepted_at: DateTime.utc_now()
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def execute(%__MODULE__{state: :issued}, %RecordPolicyIssued{}),
|
||||
do: {:error, :already_issued}
|
||||
|
||||
def execute(%__MODULE__{} = agg, %RecordPolicyIssued{} = cmd) do
|
||||
%PolicyIssued{
|
||||
id: agg.id,
|
||||
policy_number: cmd.policy_number,
|
||||
effective_date: cmd.effective_date,
|
||||
expiry_date: cmd.expiry_date,
|
||||
issued_at: cmd.issued_at || DateTime.utc_now()
|
||||
}
|
||||
end
|
||||
|
||||
# ── Apply ──────────────────────────────────────────────────────────────
|
||||
|
||||
@impl Commanded.Aggregates.Aggregate
|
||||
def apply(%__MODULE__{} = agg, %PolicyApplicationSubmitted{} = e) do
|
||||
%__MODULE__{
|
||||
agg
|
||||
| id: e.id,
|
||||
submitted_by: e.submitted_by,
|
||||
applicant_info: e.applicant_info,
|
||||
policy_details: e.policy_details,
|
||||
selected_providers: e.selected_providers,
|
||||
quotes: %{},
|
||||
state: :awaiting_quotes
|
||||
}
|
||||
end
|
||||
|
||||
def apply(%__MODULE__{} = agg, %QuoteRequestSent{}), do: agg
|
||||
|
||||
def apply(%__MODULE__{} = agg, %ProviderQuoteReceived{} = e) do
|
||||
quote_data = %{
|
||||
quote_id: e.quote_id,
|
||||
provider_id: e.provider_id,
|
||||
valid_until: e.valid_until,
|
||||
plans: e.plans || []
|
||||
}
|
||||
|
||||
%__MODULE__{agg | quotes: Map.put(agg.quotes, e.provider_id, quote_data)}
|
||||
end
|
||||
|
||||
def apply(%__MODULE__{} = agg, %AllQuotesReceived{}) do
|
||||
%__MODULE__{agg | state: :quotes_received}
|
||||
end
|
||||
|
||||
def apply(%__MODULE__{} = agg, %QuoteAccepted{} = e) do
|
||||
%__MODULE__{
|
||||
agg
|
||||
| accepted_quote_id: e.quote.quote_id,
|
||||
accepted_plan_id: e.plan.plan_id,
|
||||
accepted_provider_id: e.provider.provider_id,
|
||||
state: :solicitation_sent
|
||||
}
|
||||
end
|
||||
|
||||
def apply(%__MODULE__{} = agg, %SolicitationSent{} = e) do
|
||||
%__MODULE__{agg | solicitation_id: e.solicitation_id}
|
||||
end
|
||||
|
||||
def apply(%__MODULE__{} = agg, %PolicyIssued{} = e) do
|
||||
%__MODULE__{
|
||||
agg
|
||||
| policy_number: e.policy_number,
|
||||
effective_date: e.effective_date,
|
||||
expiry_date: e.expiry_date,
|
||||
state: :issued
|
||||
}
|
||||
end
|
||||
|
||||
# allow each aggregate to override any callback
|
||||
defoverridable execute: 2, apply: 2
|
||||
end
|
||||
end
|
||||
|
||||
def validate_policy_id(%PolicyService.Aggregates.PolicyId{policy_type: _}), do: :ok
|
||||
def validate_policy_id(_), do: {:error, :invalid_policy_id_format}
|
||||
|
||||
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})
|
||||
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(%{
|
||||
"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_applicant(_), do: {:error, :invalid_applicant_info}
|
||||
|
||||
def validate_providers(p) when is_list(p) and length(p) > 0, do: :ok
|
||||
def validate_providers(_), do: {:error, :no_providers_selected}
|
||||
|
||||
def find_quote(agg, quote_id) do
|
||||
case Enum.find(agg.quotes, fn {_, q} -> q.quote_id == quote_id end) do
|
||||
nil -> {:error, :quote_not_found}
|
||||
{_, quote} -> {:ok, quote}
|
||||
end
|
||||
end
|
||||
|
||||
def find_plan(quote, plan_id) do
|
||||
case Enum.find(quote.plans || [], fn p ->
|
||||
Map.get(p, :plan_id) == plan_id or Map.get(p, "plan_id") == plan_id
|
||||
end) do
|
||||
nil -> {:error, :plan_not_found}
|
||||
plan -> {:ok, plan}
|
||||
end
|
||||
end
|
||||
|
||||
def find_provider(agg, provider_id) do
|
||||
case Enum.find(agg.selected_providers || [], fn p ->
|
||||
Map.get(p, :provider_id) == provider_id
|
||||
end) do
|
||||
nil -> {:error, :provider_not_found}
|
||||
provider -> {:ok, provider}
|
||||
end
|
||||
end
|
||||
end
|
||||
48
lib/policy_service/aggregates/policy_id.ex
Normal file
48
lib/policy_service/aggregates/policy_id.ex
Normal file
@@ -0,0 +1,48 @@
|
||||
defmodule PolicyService.Aggregates.PolicyId do
|
||||
@derive Jason.Encoder
|
||||
defstruct [:org_id, :policy_type, :application_id]
|
||||
|
||||
def new(org_id, policy_type, application_id) do
|
||||
%__MODULE__{
|
||||
org_id: org_id,
|
||||
policy_type: policy_type,
|
||||
application_id: application_id
|
||||
}
|
||||
end
|
||||
|
||||
def parse(string) when is_binary(string) do
|
||||
case String.split(string, ":", parts: 3) do
|
||||
[org_id, policy_type, application_id] ->
|
||||
{:ok,
|
||||
%__MODULE__{
|
||||
org_id: org_id,
|
||||
policy_type: policy_type,
|
||||
application_id: application_id
|
||||
}}
|
||||
|
||||
_ ->
|
||||
{:error, :invalid_policy_id}
|
||||
end
|
||||
end
|
||||
|
||||
def parse!(string) do
|
||||
case parse(string) do
|
||||
{:ok, id} -> id
|
||||
{:error, reason} -> raise ArgumentError, "invalid policy id #{inspect(string)}: #{reason}"
|
||||
end
|
||||
end
|
||||
|
||||
defimpl String.Chars do
|
||||
def to_string(%PolicyService.Aggregates.PolicyId{
|
||||
org_id: org_id,
|
||||
policy_type: policy_type,
|
||||
application_id: application_id
|
||||
}) do
|
||||
org_id <> ":" <> policy_type <> ":" <> application_id
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Commanded.Serialization.JsonDecoder do
|
||||
def decode(id), do: id
|
||||
end
|
||||
end
|
||||
@@ -10,6 +10,10 @@ defmodule PolicyService.Application do
|
||||
children = [
|
||||
PolicyService.CommandedApp,
|
||||
PolicyService.Handlers.QuoteRequestHandler,
|
||||
PolicyService.Consumers.QuoteReceivedConsumer,
|
||||
PolicyService.Projectors.PolicyProjector,
|
||||
PolicyService.Consumers.PolicyIssuedConsumer,
|
||||
PolicyService.Handlers.SolicitationRequestHandler,
|
||||
PolicyServiceWeb.Telemetry,
|
||||
PolicyService.Repo,
|
||||
{DNSCluster, query: Application.get_env(:policy_service, :dns_cluster_query) || :ignore},
|
||||
|
||||
@@ -1,17 +1,28 @@
|
||||
defmodule PolicyService.Router do
|
||||
use Commanded.Commands.Router
|
||||
alias PolicyService.Commands.Car
|
||||
alias PolicyService.Aggregates
|
||||
|
||||
# Route Car commands to Car Aggregate
|
||||
dispatch(
|
||||
[
|
||||
Car.SubmitCarPolicyApplication,
|
||||
Car.RecordCarProviderQuote,
|
||||
Car.AcceptCarQuoteAndSolicit,
|
||||
Car.RecordCarPolicyIssued
|
||||
PolicyService.Commands.CarPolicy.SubmitPolicyApplication,
|
||||
PolicyService.Commands.CarPolicy.RecordProviderQuote,
|
||||
PolicyService.Commands.CarPolicy.AcceptQuoteAndSolicit,
|
||||
PolicyService.Commands.CarPolicy.RecordPolicyIssued
|
||||
],
|
||||
to: PolicyService.Aggregates.CarPolicyApplication,
|
||||
identity: :application_id
|
||||
identity: :id
|
||||
)
|
||||
|
||||
# Route Fire commands to Fire Aggregate
|
||||
dispatch(
|
||||
[
|
||||
PolicyService.Commands.FirePolicy.SubmitPolicyApplication,
|
||||
PolicyService.Commands.FirePolicy.RecordProviderQuote,
|
||||
PolicyService.Commands.FirePolicy.AcceptQuoteAndSolicit,
|
||||
PolicyService.Commands.FirePolicy.RecordPolicyIssued
|
||||
],
|
||||
to: PolicyService.Aggregates.FirePolicyApplication,
|
||||
identity: :id
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
defmodule PolicyService.Commands.Car.SubmitCarPolicyApplication do
|
||||
defstruct [
|
||||
:application_id,
|
||||
:org_id,
|
||||
:submitted_by,
|
||||
:applicant_info,
|
||||
:car_details,
|
||||
:selected_providers
|
||||
]
|
||||
end
|
||||
|
||||
defmodule PolicyService.Commands.Car.RecordCarProviderQuote do
|
||||
defstruct [
|
||||
:application_id,
|
||||
:org_id,
|
||||
:recorded_by,
|
||||
:provider_id,
|
||||
:quote_id,
|
||||
:premium,
|
||||
:coverage_details,
|
||||
:valid_until
|
||||
]
|
||||
end
|
||||
|
||||
defmodule PolicyService.Commands.Car.AcceptCarQuoteAndSolicit do
|
||||
defstruct [:application_id, :org_id, :accepted_by, :quote_id]
|
||||
end
|
||||
|
||||
defmodule PolicyService.Commands.Car.RecordCarPolicyIssued do
|
||||
defstruct [
|
||||
:application_id,
|
||||
:org_id,
|
||||
:recorded_by,
|
||||
:policy_number,
|
||||
:provider_id,
|
||||
:effective_date,
|
||||
:expiry_date
|
||||
]
|
||||
end
|
||||
8
lib/policy_service/commands/car_policy.ex
Normal file
8
lib/policy_service/commands/car_policy.ex
Normal file
@@ -0,0 +1,8 @@
|
||||
defmodule PolicyService.Commands.CarPolicy do
|
||||
alias PolicyService.Commands.Policy
|
||||
|
||||
defmodule SubmitPolicyApplication, do: use(Policy.SubmitPolicyApplication)
|
||||
defmodule RecordProviderQuote, do: use(Policy.RecordProviderQuote)
|
||||
defmodule AcceptQuoteAndSolicit, do: use(Policy.AcceptQuoteAndSolicit)
|
||||
defmodule RecordPolicyIssued, do: use(Policy.RecordPolicyIssued)
|
||||
end
|
||||
8
lib/policy_service/commands/fire_policy.ex
Normal file
8
lib/policy_service/commands/fire_policy.ex
Normal file
@@ -0,0 +1,8 @@
|
||||
defmodule PolicyService.Commands.FirePolicy do
|
||||
alias PolicyService.Commands.Policy
|
||||
|
||||
defmodule SubmitPolicyApplication, do: use(Policy.SubmitPolicyApplication)
|
||||
defmodule RecordProviderQuote, do: use(Policy.RecordProviderQuote)
|
||||
defmodule AcceptQuoteAndSolicit, do: use(Policy.AcceptQuoteAndSolicit)
|
||||
defmodule RecordPolicyIssued, do: use(Policy.RecordPolicyIssued)
|
||||
end
|
||||
65
lib/policy_service/commands/policy.ex
Normal file
65
lib/policy_service/commands/policy.ex
Normal file
@@ -0,0 +1,65 @@
|
||||
defmodule PolicyService.Commands.Policy do
|
||||
@moduledoc """
|
||||
Base templates for Policy commands.
|
||||
Use these macros to ensure all policy types share the same structure.
|
||||
"""
|
||||
|
||||
defmodule SubmitPolicyApplication do
|
||||
defmacro __using__(_opts) do
|
||||
quote do
|
||||
defstruct [
|
||||
:id,
|
||||
:submitted_by,
|
||||
:applicant_info,
|
||||
:policy_details,
|
||||
:selected_providers
|
||||
]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmodule RecordProviderQuote do
|
||||
defmacro __using__(_opts) do
|
||||
quote do
|
||||
defstruct [
|
||||
:id,
|
||||
:recorded_by,
|
||||
:provider_id,
|
||||
:quote_id,
|
||||
:premium,
|
||||
:coverage_details,
|
||||
:valid_until,
|
||||
:plans
|
||||
]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmodule AcceptQuoteAndSolicit do
|
||||
defmacro __using__(_opts) do
|
||||
quote do
|
||||
defstruct [
|
||||
:id,
|
||||
:accepted_by,
|
||||
:quote_id,
|
||||
:plan_id,
|
||||
:solicitation_fields
|
||||
]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmodule RecordPolicyIssued do
|
||||
defmacro __using__(_opts) do
|
||||
quote do
|
||||
defstruct [
|
||||
:id,
|
||||
:policy_number,
|
||||
:effective_date,
|
||||
:expiry_date,
|
||||
:issued_at
|
||||
]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,16 +0,0 @@
|
||||
defmodule PolicyService.Common.CarInfo do
|
||||
use ExConstructor
|
||||
|
||||
@derive Jason.Encoder
|
||||
defstruct [
|
||||
:plate,
|
||||
:make,
|
||||
:model,
|
||||
:year,
|
||||
:car_value,
|
||||
:use_type,
|
||||
:car_type,
|
||||
:chassis_number,
|
||||
:engine_number
|
||||
]
|
||||
end
|
||||
@@ -1,6 +0,0 @@
|
||||
defmodule PolicyService.Common.ClientInfo do
|
||||
use ExConstructor
|
||||
|
||||
@derive Jason.Encoder
|
||||
defstruct [:first_name, :last_name, :birth_date, :gender, :email, :phone, :user_id]
|
||||
end
|
||||
73
lib/policy_service/consumers/policy_issued.ex
Normal file
73
lib/policy_service/consumers/policy_issued.ex
Normal file
@@ -0,0 +1,73 @@
|
||||
defmodule PolicyService.Consumers.PolicyIssuedConsumer do
|
||||
use GenServer
|
||||
require Logger
|
||||
|
||||
alias PolicyService.CommandedApp
|
||||
alias PolicyService.Commands.CarPolicy
|
||||
alias PolicyService.Aggregates.PolicyId
|
||||
|
||||
@exchange "carrier_inbox.events"
|
||||
@queue "policy_service.policy_issued"
|
||||
@routing_key "policy.issued"
|
||||
|
||||
def start_link(_opts), do: GenServer.start_link(__MODULE__, [], name: __MODULE__)
|
||||
|
||||
def init(_) do
|
||||
{:ok, conn} = AMQP.Connection.open(amqp_url())
|
||||
{:ok, channel} = AMQP.Channel.open(conn)
|
||||
|
||||
AMQP.Exchange.topic(channel, @exchange, durable: true)
|
||||
AMQP.Queue.declare(channel, @queue, durable: true)
|
||||
AMQP.Queue.bind(channel, @queue, @exchange, routing_key: @routing_key)
|
||||
AMQP.Basic.qos(channel, prefetch_count: 10)
|
||||
{:ok, _tag} = AMQP.Basic.consume(channel, @queue)
|
||||
|
||||
{:ok, %{channel: channel}}
|
||||
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}
|
||||
|
||||
def handle_info({:basic_deliver, payload, meta}, state) do
|
||||
case Jason.decode(payload) do
|
||||
{:ok, event} ->
|
||||
process(event, meta, state)
|
||||
|
||||
{:error, _} ->
|
||||
Logger.error("PolicyIssuedConsumer: failed to decode payload")
|
||||
AMQP.Basic.reject(state.channel, meta.delivery_tag, requeue: false)
|
||||
end
|
||||
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
defp process(event, meta, state) do
|
||||
%{policy_type: policy_type} = PolicyId.parse!(event["id"])
|
||||
|
||||
command =
|
||||
case policy_type do
|
||||
"car" ->
|
||||
%CarPolicy.RecordPolicyIssued{
|
||||
id: event["id"],
|
||||
policy_number: event["policy_number"],
|
||||
effective_date: event["effective_date"],
|
||||
expiry_date: event["expiry_date"],
|
||||
issued_at: DateTime.utc_now()
|
||||
}
|
||||
end
|
||||
|
||||
case CommandedApp.dispatch(command) do
|
||||
:ok ->
|
||||
AMQP.Basic.ack(state.channel, meta.delivery_tag)
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.error("PolicyIssuedConsumer: dispatch failed: #{inspect(reason)}")
|
||||
AMQP.Basic.reject(state.channel, meta.delivery_tag, requeue: true)
|
||||
end
|
||||
end
|
||||
|
||||
defp amqp_url do
|
||||
Application.get_env(:policy_service, :amqp_url, "amqp://guest:guest@localhost:5672")
|
||||
end
|
||||
end
|
||||
120
lib/policy_service/consumers/quote_received.ex
Normal file
120
lib/policy_service/consumers/quote_received.ex
Normal file
@@ -0,0 +1,120 @@
|
||||
defmodule PolicyService.Consumers.QuoteReceivedConsumer do
|
||||
use GenServer
|
||||
|
||||
require Logger
|
||||
|
||||
alias PolicyService.CommandedApp
|
||||
alias PolicyService.Commands.CarPolicy
|
||||
alias PolicyService.Aggregates.PolicyId
|
||||
|
||||
@exchange "carrier_inbox.events"
|
||||
@queue "policy_service.quote_received"
|
||||
@routing_key "quote.received"
|
||||
|
||||
def start_link(opts \\ []) do
|
||||
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
|
||||
end
|
||||
|
||||
def init(_opts) do
|
||||
amqp_url = Application.fetch_env!(:policy_service, :amqp_url)
|
||||
|
||||
{:ok, conn} = AMQP.Connection.open(amqp_url)
|
||||
{:ok, channel} = AMQP.Channel.open(conn)
|
||||
|
||||
AMQP.Exchange.declare(channel, @exchange, :topic, durable: true)
|
||||
|
||||
AMQP.Queue.declare(channel, @queue, durable: true)
|
||||
AMQP.Queue.bind(channel, @queue, @exchange, routing_key: @routing_key)
|
||||
|
||||
AMQP.Basic.consume(channel, @queue, nil, no_ack: false)
|
||||
|
||||
Logger.info("QuoteReceivedConsumer started, listening on #{@queue}")
|
||||
|
||||
{:ok, %{conn: conn, channel: channel}}
|
||||
end
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# AMQP callbacks
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
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}
|
||||
|
||||
def handle_info({:basic_deliver, payload, %{delivery_tag: tag}}, state) do
|
||||
case process(payload) do
|
||||
:ok ->
|
||||
AMQP.Basic.ack(state.channel, tag)
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.error("Failed to process quote.received: #{inspect(reason)}")
|
||||
AMQP.Basic.nack(state.channel, tag, requeue: false)
|
||||
end
|
||||
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Processing
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
defp process(payload) do
|
||||
with {:ok, event} <- Jason.decode(payload),
|
||||
{:ok, cmd} <- build_command(event),
|
||||
:ok <- CommandedApp.dispatch(cmd, consistency: :strong) do
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
defp build_command(event) do
|
||||
case event["policy_type"] do
|
||||
"car" -> build_car_command(event)
|
||||
type -> {:error, {:unsupported_policy_type, type}}
|
||||
end
|
||||
end
|
||||
|
||||
defp build_car_command(event) do
|
||||
%{policy_type: policy_type} = PolicyId.parse!(event["id"])
|
||||
|
||||
case policy_type do
|
||||
"car" ->
|
||||
cmd = %CarPolicy.RecordProviderQuote{
|
||||
id: PolicyId.parse!(event["id"]),
|
||||
recorded_by: event["entered_by"],
|
||||
provider_id: event["provider_id"],
|
||||
quote_id: event["quote_id"],
|
||||
valid_until: parse_date(event["valid_until"]),
|
||||
plans: parse_plans(event["plans"])
|
||||
}
|
||||
|
||||
{:ok, cmd}
|
||||
end
|
||||
rescue
|
||||
e -> {:error, e}
|
||||
end
|
||||
|
||||
defp parse_plans(nil), do: []
|
||||
|
||||
defp parse_plans(plans) when is_list(plans) do
|
||||
Enum.map(plans, fn p ->
|
||||
%{
|
||||
plan_id: p["plan_id"],
|
||||
name: p["name"],
|
||||
premium: p["premium"],
|
||||
coverage_details: p["coverage_details"],
|
||||
deductible: p["deductible"],
|
||||
coverage_limit: p["coverage_limit"]
|
||||
}
|
||||
end)
|
||||
end
|
||||
|
||||
defp parse_date(nil), do: nil
|
||||
defp parse_date(%Date{} = d), do: d
|
||||
|
||||
defp parse_date(s) when is_binary(s) do
|
||||
case Date.from_iso8601(s) do
|
||||
{:ok, d} -> d
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,69 +0,0 @@
|
||||
defmodule PolicyService.Events.Car.CarPolicyApplicationSubmitted do
|
||||
@derive Jason.Encoder
|
||||
defstruct [
|
||||
:application_id,
|
||||
:org_id,
|
||||
:submitted_by,
|
||||
:applicant_info,
|
||||
:car_details,
|
||||
:selected_providers,
|
||||
:submitted_at
|
||||
]
|
||||
end
|
||||
|
||||
defmodule PolicyService.Events.Car.CarQuoteRequestSent do
|
||||
@derive Jason.Encoder
|
||||
defstruct [
|
||||
:application_id,
|
||||
:org_id,
|
||||
:provider_id,
|
||||
:provider_email,
|
||||
:applicant_info,
|
||||
:car_details,
|
||||
:requested_at
|
||||
]
|
||||
end
|
||||
|
||||
defmodule PolicyService.Events.Car.CarProviderQuoteReceived do
|
||||
@derive Jason.Encoder
|
||||
defstruct [
|
||||
:application_id,
|
||||
:org_id,
|
||||
:recorded_by,
|
||||
:provider_id,
|
||||
:quote_id,
|
||||
:premium,
|
||||
:coverage_details,
|
||||
:valid_until,
|
||||
:received_at
|
||||
]
|
||||
end
|
||||
|
||||
defmodule PolicyService.Events.Car.AllCarQuotesReceived do
|
||||
@derive Jason.Encoder
|
||||
defstruct [:application_id, :org_id, :quote_count]
|
||||
end
|
||||
|
||||
defmodule PolicyService.Events.Car.CarQuoteAccepted do
|
||||
@derive Jason.Encoder
|
||||
defstruct [:application_id, :org_id, :accepted_by, :quote_id, :provider_id, :accepted_at]
|
||||
end
|
||||
|
||||
defmodule PolicyService.Events.Car.CarSolicitationSent do
|
||||
@derive Jason.Encoder
|
||||
defstruct [:application_id, :org_id, :provider_id, :quote_id, :sent_at]
|
||||
end
|
||||
|
||||
defmodule PolicyService.Events.Car.CarPolicyIssued do
|
||||
@derive Jason.Encoder
|
||||
defstruct [
|
||||
:application_id,
|
||||
:org_id,
|
||||
:recorded_by,
|
||||
:policy_number,
|
||||
:provider_id,
|
||||
:effective_date,
|
||||
:expiry_date,
|
||||
:issued_at
|
||||
]
|
||||
end
|
||||
80
lib/policy_service/events/policy.ex
Normal file
80
lib/policy_service/events/policy.ex
Normal file
@@ -0,0 +1,80 @@
|
||||
defmodule PolicyService.Events.Policy do
|
||||
defmodule PolicyApplicationSubmitted do
|
||||
@derive Jason.Encoder
|
||||
defstruct [
|
||||
:id,
|
||||
:submitted_by,
|
||||
:applicant_info,
|
||||
:policy_details,
|
||||
:selected_providers,
|
||||
:submitted_at
|
||||
]
|
||||
end
|
||||
|
||||
defmodule QuoteRequestSent do
|
||||
@derive Jason.Encoder
|
||||
defstruct [
|
||||
:id,
|
||||
:provider_id,
|
||||
:provider_email,
|
||||
:applicant_info,
|
||||
:policy_details,
|
||||
:requested_at
|
||||
]
|
||||
end
|
||||
|
||||
defmodule ProviderQuoteReceived do
|
||||
@derive Jason.Encoder
|
||||
defstruct [
|
||||
:id,
|
||||
:recorded_by,
|
||||
:provider_id,
|
||||
:quote_id,
|
||||
:premium,
|
||||
:coverage_details,
|
||||
:valid_until,
|
||||
:plans,
|
||||
:received_at
|
||||
]
|
||||
end
|
||||
|
||||
defmodule AllQuotesReceived do
|
||||
@derive Jason.Encoder
|
||||
defstruct [:id, :org_id, :quote_count]
|
||||
end
|
||||
|
||||
defmodule QuoteAccepted do
|
||||
@derive Jason.Encoder
|
||||
defstruct [
|
||||
:id,
|
||||
:accepted_by,
|
||||
:quote,
|
||||
:plan,
|
||||
:provider,
|
||||
:accepted_at
|
||||
]
|
||||
end
|
||||
|
||||
defmodule SolicitationSent do
|
||||
@derive Jason.Encoder
|
||||
defstruct [
|
||||
:id,
|
||||
:solicitation_id,
|
||||
:provider_id,
|
||||
:template_id,
|
||||
:s3_key,
|
||||
:sent_at
|
||||
]
|
||||
end
|
||||
|
||||
defmodule PolicyIssued do
|
||||
@derive Jason.Encoder
|
||||
defstruct [
|
||||
:id,
|
||||
:policy_number,
|
||||
:effective_date,
|
||||
:expiry_date,
|
||||
:issued_at
|
||||
]
|
||||
end
|
||||
end
|
||||
@@ -3,19 +3,9 @@ defmodule PolicyService.Handlers.QuoteRequestHandler do
|
||||
application: PolicyService.CommandedApp,
|
||||
name: __MODULE__
|
||||
|
||||
alias PolicyService.Events.Car.CarQuoteRequestSent
|
||||
# alias PolicyService.Events.Life.LifeQuoteRequestSent
|
||||
# alias PolicyService.Events.Fire.FireQuoteRequestSent
|
||||
alias PolicyService.Events.Policy.QuoteRequestSent
|
||||
|
||||
def handle(%CarQuoteRequestSent{} = e, _metadata) do
|
||||
PolicyService.MessageBus.publish("carquote.requested", e)
|
||||
def handle(%QuoteRequestSent{} = e, _metadata) do
|
||||
PolicyService.MessageBus.publish("quote.requested", e)
|
||||
end
|
||||
|
||||
# def handle(%LifeQuoteRequestSent{} = e, _metadata) do
|
||||
# PolicyService.MessageBus.publish("quote.requested", e)
|
||||
# end
|
||||
|
||||
# def handle(%FireQuoteRequestSent{} = e, _metadata) do
|
||||
# PolicyService.MessageBus.publish("quote.requested", e)
|
||||
# end
|
||||
end
|
||||
|
||||
15
lib/policy_service/handlers/solicitation_request_handler.ex
Normal file
15
lib/policy_service/handlers/solicitation_request_handler.ex
Normal file
@@ -0,0 +1,15 @@
|
||||
defmodule PolicyService.Handlers.SolicitationRequestHandler do
|
||||
use Commanded.Event.Handler,
|
||||
application: PolicyService.CommandedApp,
|
||||
name: "SolicitationRequestHandler"
|
||||
|
||||
require Logger
|
||||
|
||||
alias PolicyService.Events.Policy.QuoteAccepted
|
||||
alias PolicyService.MessageBus
|
||||
|
||||
def handle(%QuoteAccepted{} = event, _metadata) do
|
||||
MessageBus.publish("quote.accepted", event)
|
||||
:ok
|
||||
end
|
||||
end
|
||||
@@ -7,7 +7,6 @@ defmodule PolicyService.MessageBus do
|
||||
:ok =
|
||||
AMQP.Basic.publish(channel(), "policy_service.events", routing_key, payload,
|
||||
content_type: "application/json",
|
||||
# survives RabbitMQ restart
|
||||
persistent: true
|
||||
)
|
||||
end
|
||||
|
||||
17
lib/policy_service/policy/filters.ex
Normal file
17
lib/policy_service/policy/filters.ex
Normal file
@@ -0,0 +1,17 @@
|
||||
defmodule PolicyService.Filters.PolicyApplicationFilters do
|
||||
import Ecto.Query
|
||||
|
||||
def search(query, %Flop.Filter{value: value}, _opts) do
|
||||
term = "%#{value}%"
|
||||
|
||||
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
|
||||
ilike(p.policy_number, ^term)
|
||||
)
|
||||
end
|
||||
end
|
||||
21
lib/policy_service/policy/queries.ex
Normal file
21
lib/policy_service/policy/queries.ex
Normal file
@@ -0,0 +1,21 @@
|
||||
defmodule PolicyService.Queries.PolicyQueries do
|
||||
import Ecto.Query
|
||||
|
||||
alias PolicyService.Repo
|
||||
alias PolicyService.Projections.PolicyApplication
|
||||
|
||||
def list_by_org(org_id, params \\ %{}) do
|
||||
base = from(p in PolicyApplication, where: p.org_id == ^org_id)
|
||||
Flop.validate_and_run(base, params, for: PolicyApplication)
|
||||
end
|
||||
|
||||
def get_by_application_id(org_id, application_id) do
|
||||
case Repo.get_by(PolicyApplication,
|
||||
application_id: application_id,
|
||||
org_id: org_id
|
||||
) do
|
||||
nil -> {:error, :not_found}
|
||||
p -> {:ok, p}
|
||||
end
|
||||
end
|
||||
end
|
||||
85
lib/policy_service/projections/policy.ex
Normal file
85
lib/policy_service/projections/policy.ex
Normal file
@@ -0,0 +1,85 @@
|
||||
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_quote_id,
|
||||
:accepted_plan_id,
|
||||
:accepted_provider_id,
|
||||
:accepted_by,
|
||||
:accepted_at,
|
||||
:solicitation_id,
|
||||
:solicitation_s3_key,
|
||||
:policy_number,
|
||||
:premium,
|
||||
:effective_date,
|
||||
:expiry_date,
|
||||
:status,
|
||||
:submitted_at,
|
||||
:solicitation_sent_at,
|
||||
:issued_at,
|
||||
:inserted_at,
|
||||
:updated_at
|
||||
]}
|
||||
|
||||
@derive {
|
||||
Flop.Schema,
|
||||
filterable: [:org_id, :policy_type, :status, :search],
|
||||
sortable: [:submitted_at, :policy_type, :status],
|
||||
default_limit: 20,
|
||||
max_limit: 100,
|
||||
custom_fields: [
|
||||
search: [
|
||||
filter: {PolicyService.Projections.PolicyApplicationFilters, :search, []},
|
||||
ecto_type: :string,
|
||||
operators: [:==]
|
||||
]
|
||||
]
|
||||
}
|
||||
|
||||
@primary_key {:id, :string, autogenerate: false}
|
||||
@timestamps_opts [type: :utc_datetime_usec]
|
||||
|
||||
schema "policy_applications" do
|
||||
field :application_id, :string
|
||||
field :org_id, :string
|
||||
field :submitted_by, :string
|
||||
field :policy_type, :string
|
||||
|
||||
field :applicant_info, :map
|
||||
field :policy_details, :map
|
||||
|
||||
field :selected_providers, {:array, :string}, default: []
|
||||
field :quotes, :map, default: %{}
|
||||
|
||||
field :accepted_quote_id, :string
|
||||
field :accepted_plan_id, :string
|
||||
field :accepted_provider_id, :string
|
||||
field :accepted_by, :string
|
||||
field :accepted_at, :utc_datetime_usec
|
||||
|
||||
field :solicitation_id, :string
|
||||
field :solicitation_s3_key, :string
|
||||
|
||||
field :policy_number, :string
|
||||
field :premium, :decimal
|
||||
field :effective_date, :date
|
||||
field :expiry_date, :date
|
||||
|
||||
field :status, :string
|
||||
field :submitted_at, :utc_datetime_usec
|
||||
field :solicitation_sent_at, :utc_datetime_usec
|
||||
field :issued_at, :utc_datetime_usec
|
||||
|
||||
timestamps()
|
||||
end
|
||||
end
|
||||
144
lib/policy_service/projectors/policy_projector.ex
Normal file
144
lib/policy_service/projectors/policy_projector.ex
Normal file
@@ -0,0 +1,144 @@
|
||||
defmodule PolicyService.Projectors.PolicyProjector do
|
||||
use Commanded.Projections.Ecto,
|
||||
application: PolicyService.CommandedApp,
|
||||
repo: PolicyService.Repo,
|
||||
name: "PolicyApplicationProjection",
|
||||
consistency: :strong
|
||||
|
||||
alias PolicyService.Events.Policy.{
|
||||
PolicyApplicationSubmitted,
|
||||
ProviderQuoteReceived,
|
||||
AllQuotesReceived,
|
||||
QuoteAccepted,
|
||||
SolicitationSent,
|
||||
PolicyIssued
|
||||
}
|
||||
|
||||
alias PolicyService.Projections.PolicyApplication
|
||||
alias PolicyService.Aggregates.PolicyId
|
||||
import Ecto.Query
|
||||
|
||||
project(%PolicyApplicationSubmitted{} = e, _meta, fn multi ->
|
||||
%{policy_type: policy_type, application_id: application_id, org_id: org_id} = e.id
|
||||
|
||||
Ecto.Multi.insert(multi, :policy_application, %PolicyApplication{
|
||||
id: to_string(PolicyId.new(org_id, policy_type, application_id)),
|
||||
application_id: application_id,
|
||||
org_id: org_id,
|
||||
submitted_by: e.submitted_by,
|
||||
policy_type: policy_type,
|
||||
applicant_info: atomize(e.applicant_info),
|
||||
policy_details: atomize(e.policy_details),
|
||||
selected_providers: Enum.map(e.selected_providers, & &1["provider_id"]),
|
||||
quotes: %{},
|
||||
status: "quote_requested",
|
||||
submitted_at: parse_datetime(e.submitted_at)
|
||||
})
|
||||
end)
|
||||
|
||||
project(%ProviderQuoteReceived{} = e, _meta, fn multi ->
|
||||
multi
|
||||
|> Ecto.Multi.run(:fetch, fn repo, _ ->
|
||||
{:ok, repo.get!(PolicyApplication, to_string(e.id))}
|
||||
end)
|
||||
|> Ecto.Multi.update(:policy_application, fn %{fetch: p} ->
|
||||
quote_data = %{
|
||||
"quote_id" => e.quote_id,
|
||||
"provider_id" => e.provider_id,
|
||||
"valid_until" => e.valid_until,
|
||||
"received_at" => parse_datetime(e.received_at),
|
||||
"plans" => e.plans || []
|
||||
}
|
||||
|
||||
Ecto.Changeset.change(p, quotes: Map.put(p.quotes, e.provider_id, quote_data))
|
||||
end)
|
||||
end)
|
||||
|
||||
project(%AllQuotesReceived{} = e, _meta, fn multi ->
|
||||
multi
|
||||
|> Ecto.Multi.run(:fetch, fn repo, _ ->
|
||||
{:ok, repo.get!(PolicyApplication, to_string(e.id))}
|
||||
end)
|
||||
|> Ecto.Multi.update(:policy_application, fn %{fetch: p} ->
|
||||
Ecto.Changeset.change(p, status: "quotes_received")
|
||||
end)
|
||||
end)
|
||||
|
||||
project(%QuoteAccepted{} = e, _meta, fn multi ->
|
||||
multi
|
||||
|> Ecto.Multi.run(:fetch, fn repo, _ ->
|
||||
{:ok, repo.get!(PolicyApplication, to_string(e.id))}
|
||||
end)
|
||||
|> Ecto.Multi.update(:policy_application, fn %{fetch: p} ->
|
||||
Ecto.Changeset.change(p,
|
||||
accepted_quote_id: e.quote.quote_id,
|
||||
accepted_plan_id: e.plan.plan_id,
|
||||
accepted_provider_id: e.provider.id,
|
||||
accepted_at: parse_datetime(e.accepted_at),
|
||||
status: "solicitation_sent"
|
||||
)
|
||||
end)
|
||||
end)
|
||||
|
||||
project(%SolicitationSent{} = e, _meta, fn multi ->
|
||||
multi
|
||||
|> Ecto.Multi.run(:fetch, fn repo, _ ->
|
||||
{:ok, repo.get!(PolicyApplication, to_string(e.id))}
|
||||
end)
|
||||
|> Ecto.Multi.update(:policy_application, fn %{fetch: p} ->
|
||||
Ecto.Changeset.change(p,
|
||||
solicitation_id: e.solicitation_id,
|
||||
solicitation_s3_key: e.s3_key,
|
||||
solicitation_sent_at: parse_datetime(e.sent_at)
|
||||
)
|
||||
end)
|
||||
end)
|
||||
|
||||
project(%PolicyIssued{} = e, _meta, fn multi ->
|
||||
multi
|
||||
|> Ecto.Multi.run(:fetch, fn repo, _ ->
|
||||
{:ok, repo.get!(PolicyApplication, to_string(e.id))}
|
||||
end)
|
||||
|> Ecto.Multi.update(:policy_application, fn %{fetch: p} ->
|
||||
Ecto.Changeset.change(p,
|
||||
policy_number: e.policy_number,
|
||||
effective_date: parse_date(e.effective_date),
|
||||
expiry_date: parse_date(e.expiry_date),
|
||||
issued_at: parse_datetime(e.issued_at),
|
||||
status: "issued"
|
||||
)
|
||||
end)
|
||||
end)
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
defp atomize(nil), do: nil
|
||||
|
||||
defp atomize(map) when is_map(map) do
|
||||
Map.new(map, fn {k, v} ->
|
||||
{if(is_atom(k), do: Atom.to_string(k), else: k), v}
|
||||
end)
|
||||
end
|
||||
|
||||
defp parse_datetime(nil), do: nil
|
||||
defp parse_datetime(%DateTime{} = dt), do: dt
|
||||
|
||||
defp parse_datetime(str) when is_binary(str) do
|
||||
case DateTime.from_iso8601(str) do
|
||||
{:ok, dt, _} -> dt
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_date(nil), do: nil
|
||||
defp parse_date(%Date{} = d), do: d
|
||||
|
||||
defp parse_date(str) when is_binary(str) do
|
||||
case Date.from_iso8601(str) do
|
||||
{:ok, d} -> d
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user