ratemyclient/lib/my_first_elixir_vibe_code/accounts/contractor.ex

34 lines
1.1 KiB
Elixir
Raw Normal View History

defmodule MyFirstElixirVibeCode.Accounts.Contractor do
use Ecto.Schema
import Ecto.Changeset
schema "contractors" do
field :name, :string
field :email, :string
field :company_name, :string
field :municipal_registration_proof, :string
field :insurance_proof, :string
field :registration_status, :string, default: "pending"
field :verified_at, :utc_datetime
timestamps(type: :utc_datetime)
end
@doc false
def changeset(contractor, attrs) do
contractor
|> cast(attrs, [:name, :email, :company_name])
|> validate_required([:name, :email, :company_name])
end
@doc false
def registration_changeset(contractor, attrs) do
contractor
|> cast(attrs, [:name, :email, :company_name, :municipal_registration_proof, :insurance_proof])
|> validate_required([:name, :email, :company_name, :municipal_registration_proof, :insurance_proof])
|> validate_format(:email, ~r/^[^\s]+@[^\s]+$/, message: "must be a valid email")
|> unique_constraint(:email)
|> put_change(:registration_status, "pending")
end
end