defmodule CustomerService.Aggregates.Customer do defstruct [ :id, :first_name, :last_name, :birth_date, :gender, :email, :phone ] 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 } end @impl Aggregate def apply(%Customer{} = c, %Events.CustomerCreated{} = e) do %Customer{ 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 } end end