Branch
Hash :
e363af80
Author :
Thomas de Grivel
Date :
2022-06-10T10:22:11
wip
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
defmodule Kmxcrm.OrganisationManager do
import Ecto.Query, warn: false
alias Kmxcrm.IndexParams
alias Kmxcrm.OrganisationManager.Contact
alias Kmxcrm.OrganisationManager.Organisation
alias Kmxcrm.Pagination
alias Kmxcrm.Repo
alias Kmxcrm.UserManager
@preload [:contacts, :users]
def list_organisations(params \\ %IndexParams{}) do
from(org in Organisation)
|> search(params)
|> index_order_by(params)
|> Pagination.page(params, preload: [])
end
def search(query, %IndexParams{search: search}) do
expr = "%#{search}%"
query
|> where([org], ilike(org.name, ^expr) or ilike(org.slug, ^expr))
end
def index_order_by(query, %{column: "id", reverse: true}) do
order_by(query, [desc: :id])
end
def index_order_by(query, %{column: "id"}) do
order_by(query, :id)
end
def index_order_by(query, %{column: "name", reverse: true}) do
order_by(query, [org], [desc_nulls_last: fragment("lower(?)", org.name)])
end
def index_order_by(query, %{column: "name"}) do
order_by(query, [org], [asc_nulls_last: fragment("lower(?)", org.name)])
end
def index_order_by(query, %{column: "slug", reverse: true}) do
order_by(query, [org], [desc_nulls_last: fragment("lower(?)", org.slug)])
end
def index_order_by(query, %{column: "slug"}) do
order_by(query, [org], [asc_nulls_last: fragment("lower(?)", org.slug)])
end
def count_organisations do
Repo.one from org in Organisation, select: count()
end
def get_organisation(id) do
Repo.one from organisation in Organisation,
where: [id: ^id],
preload: ^@preload,
limit: 1
end
def get_organisation!(id) do
get_organisation(id) || raise Ecto.NoResultsError
end
def change_organisation(organisation \\ %Organisation{}) do
Organisation.changeset(organisation, %{})
end
def create_organisation(user, attrs \\ %{}) do
%Organisation{}
|> Organisation.changeset(attrs)
|> Ecto.Changeset.put_assoc(:users, [user])
|> Repo.insert()
end
def update_organisation(organisation, attrs \\ %{}) do
organisation
|> Organisation.changeset(attrs)
|> Repo.update()
end
def get_organisation_by_slug(slug) do
Repo.one from o in Organisation,
where: fragment("lower(?)", o.slug) == ^String.downcase(slug),
preload: ^@preload,
limit: 1
end
def add_user(%Organisation{} = org, login) do
user = UserManager.get_user_by_login(login)
if user do
users = [user | org.users]
org
|> Organisation.changeset(%{})
|> Ecto.Changeset.put_assoc(:users, users)
|> Repo.update()
else
{:error, :not_found}
end
end
def remove_user(%Organisation{} = org, login) do
user = UserManager.get_user_by_login(login)
if user do
users = Enum.reject(org.users, &(&1.id == user.id))
org
|> Organisation.changeset(%{})
|> Ecto.Changeset.put_assoc(:users, users)
|> Repo.update()
else
{:error, :not_found}
end
end
def delete_organisation(%Organisation{} = organisation) do
organisation
|> Organisation.changeset(%{})
|> Repo.delete()
end
def admin_create_organisation(attrs \\ %{}) do
%Organisation{}
|> Organisation.changeset(attrs)
|> Repo.insert()
end
# contacts
def change_contact(contact \\ %Contact{}, params \\ %{}) do
contact
|> Contact.changeset(params)
end
def create_contact(org = %Organisation{}, attrs \\ %{}) do
%Contact{}
|> Contact.changeset(attrs)
|> Ecto.Changeset.put_assoc(:organisation, org)
|> Repo.insert()
end
def get_contact(org = %Organisation{}, id) do
Repo.one from c in Contact,
where: c.id == ^id and c.organisation_id == ^org.id,
limit: 1
end
def update_contact(contact = %Contact{}, attrs \\ %{}) do
contact
|> Contact.changeset(attrs)
|> Repo.update()
end
end