defmodule PolicyService.Aggregates.CarPolicyApplication do use PolicyService.Aggregates.PolicyApplication, policy_type: "car", commands: PolicyService.Commands.CarPolicy @valid_use_types ~w(private commercial bus taxi school) @valid_car_types ~w(sedan suv hatchback coupe convertible pickup van minivan truck) def validate_insured_object(%{ "plate" => plate, "make" => make, "model" => model, "year" => year, "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_insured_object(%{ "plate" => plate, "make" => make, "model" => model, "year" => year, "use_type" => use_type, "car_type" => car_type, "chassis_number" => chassis, "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(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(plate) == 0 -> {:error, :missing_plate} true -> :ok end end def validate_insured_object(_), do: {:error, :invalid_car_details} end