defmodule CustomerService.Aggregates.Customer do defstruct [ :id, :first_name, :last_name, :birth_date, :gender, :email, :phone, :address, :document_id ] alias __MODULE__ alias Commanded.Aggregates.Aggregate alias CustomerService.Commands alias CustomerService.Events @behaviour Aggregate @impl Aggregate def execute(%Customer{id: nil}, %Commands.CreateCustomer{} = cmd) do %Events.CustomerCreated{ id: cmd.id, first_name: cmd.first_name, last_name: cmd.last_name, birth_date: cmd.birth_date, gender: cmd.gender, email: cmd.email, phone: cmd.phone, document_id: cmd.document_id, address: cmd.address } end @impl Aggregate def execute(%Customer{id: _id}, %Commands.UpdateCustomer{} = cmd) do %Events.CustomerUpdated{ id: cmd.id, first_name: cmd.first_name, last_name: cmd.last_name, birth_date: cmd.birth_date, gender: cmd.gender, email: cmd.email, phone: cmd.phone, document_id: cmd.document_id, address: cmd.address } end @impl Aggregate def apply(%Customer{} = c, %Events.CustomerCreated{} = e) do %__MODULE__{ c | id: e.id, first_name: e.first_name, last_name: e.last_name, birth_date: e.birth_date, gender: e.gender, email: e.email, phone: e.phone, address: e.address, document_id: e.document_id } end @impl Aggregate def apply(%Customer{} = c, %Events.CustomerUpdated{} = e) do %__MODULE__{ c | first_name: e.first_name || c.first_name, last_name: e.last_name || c.last_name, birth_date: e.birth_date || c.birth_date, gender: e.gender || c.gender, email: e.email || c.email, phone: e.phone || c.phone, address: e.address || c.address, document_id: e.document_id || c.document_id } end end