2017-12-08 09:58:14 +01:00
|
|
|
defmodule Eventos.Accounts.Account do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
|
|
|
Represents an account (local and remote users)
|
|
|
|
"""
|
2017-12-08 09:58:14 +01:00
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
2018-01-13 23:33:03 +01:00
|
|
|
alias Eventos.Accounts.{Account, User}
|
|
|
|
alias Eventos.Groups.{Group, Member, Request}
|
2017-12-08 09:58:14 +01:00
|
|
|
alias Eventos.Events.Event
|
|
|
|
|
|
|
|
schema "accounts" do
|
|
|
|
field :description, :string
|
|
|
|
field :display_name, :string
|
2018-01-13 23:33:03 +01:00
|
|
|
field :domain, :string
|
2017-12-08 09:58:14 +01:00
|
|
|
field :private_key, :string
|
|
|
|
field :public_key, :string
|
|
|
|
field :suspended, :boolean, default: false
|
|
|
|
field :uri, :string
|
|
|
|
field :url, :string
|
2018-01-13 23:33:03 +01:00
|
|
|
field :username, :string
|
|
|
|
has_many :organized_events, Event, [foreign_key: :organizer_id]
|
|
|
|
many_to_many :groups, Group, join_through: Member
|
|
|
|
has_many :group_request, Request
|
2018-01-09 17:52:26 +01:00
|
|
|
has_one :user, User
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def changeset(%Account{} = account, attrs) do
|
|
|
|
account
|
|
|
|
|> cast(attrs, [:username, :domain, :display_name, :description, :private_key, :public_key, :suspended, :uri, :url])
|
2018-01-13 23:33:03 +01:00
|
|
|
|> validate_required([:username, :public_key, :suspended, :uri, :url])
|
2018-01-09 17:52:26 +01:00
|
|
|
|> unique_constraint(:username, name: :accounts_username_domain_index)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
2018-01-13 23:33:03 +01:00
|
|
|
|
|
|
|
def registration_changeset(%Account{} = account, attrs) do
|
|
|
|
account
|
|
|
|
|> cast(attrs, [:username, :domain, :display_name, :description, :private_key, :public_key, :suspended, :uri, :url])
|
|
|
|
|> validate_required([:username, :public_key, :suspended, :uri, :url])
|
|
|
|
|> unique_constraint(:username)
|
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|