-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathmessages.ex
More file actions
121 lines (107 loc) · 3.77 KB
/
messages.ex
File metadata and controls
121 lines (107 loc) · 3.77 KB
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
defmodule CodeCorps.Messages do
@moduledoc ~S"""
Main context for work with the Messaging feature.
"""
alias CodeCorps.{
Conversation,
ConversationPart,
Helpers.Query,
Message,
Messages,
Repo
}
alias Ecto.{Changeset, Queryable}
@reopened "reopened"
@closed "closed"
@doc ~S"""
Lists pre-scoped `CodeCorps.Message` records filtered by parameters.
"""
@spec list(Queryable.t, map) :: list(Message.t)
def list(scope, %{} = params) do
scope
|> Query.id_filter(params)
|> Repo.all()
end
@doc ~S"""
Lists pre-scoped `CodeCorps.Conversation` records filtered by parameters
"""
@spec list_conversations(Queryable.t, map) :: list(Conversation.t)
def list_conversations(scope, %{} = params) do
scope
|> Messages.ConversationQuery.project_filter(params)
|> Messages.ConversationQuery.active_filter(params)
|> Messages.ConversationQuery.status_filter(params)
|> Messages.ConversationQuery.user_filter(params)
|> Repo.all()
end
@doc ~S"""
Lists pre-scoped `CodeCorps.ConversationPart` records filtered by parameters
"""
@spec list_parts(Queryable.t, map) :: list(Conversation.t)
def list_parts(scope, %{} = _params) do
scope |> Repo.all()
end
@doc ~S"""
Gets a `CodeCorps.Conversation` record
"""
@spec get_conversation(integer) :: Conversation.t
def get_conversation(id) do
Conversation |> Repo.get(id)
end
@doc ~S"""
Updates a `CodeCorps.Conversation` record
"""
def update_conversation(conversation, %{status: @reopened} = params) do
{:ok, now} = Timex.format(Timex.now, "{ISO:Extended}")
add_part(%{"conversation_id" => conversation.id, "body" => "Reopened on " <> now , "author_id"
=> conversation.user_id, "part_type" => "reopened"})
conversation |> Conversation.update_changeset(params) |> Repo.update
end
def update_conversation(conversation, %{status: @closed} = params) do
{:ok, now} = Timex.format(Timex.now, "{ISO:Extended}")
add_part(%{"conversation_id" => conversation.id, "body" => "Closed on " <> now, "author_id"
=> conversation.user_id, "part_type" => "closed"})
conversation |> Conversation.update_changeset(params) |> Repo.update
end
def update_conversation(conversation, params) do
conversation |> Conversation.update_changeset(params) |> Repo.update
end
@doc ~S"""
Gets a `CodeCorps.ConversationPart` record
"""
@spec get_part(integer) :: ConversationPart.t
def get_part(id) do
ConversationPart |> Repo.get(id)
end
@doc ~S"""
Creates a `CodeCorps.Message` from a set of parameters.
"""
@spec create(map) :: {:ok, Message.t} | {:error, Changeset.t}
def create(%{} = params) do
with {:ok, %Message{} = message} <- %Message{} |> create_changeset(params) |> Repo.insert() do
message |> Messages.Emails.notify_message_targets()
{:ok, message}
else
{:error, %Changeset{} = changeset} -> {:error, changeset}
end
end
@spec create_changeset(Message.t, map) :: Changeset.t
defp create_changeset(%Message{} = message, %{} = params) do
message
|> Message.changeset(params)
|> Changeset.cast(params, [:author_id, :project_id])
|> Changeset.validate_required([:author_id, :project_id])
|> Changeset.assoc_constraint(:author)
|> Changeset.assoc_constraint(:project)
|> Changeset.cast_assoc(:conversations, with: &Messages.Conversations.create_changeset/2)
end
@spec add_part(map) :: {:ok, ConversationPart.t} | {:error, Changeset.t}
def add_part(%{} = params) do
with {:ok, %ConversationPart{} = conversation_part} <- params |> Messages.ConversationParts.create do
conversation_part |> Messages.Emails.notify_of_new_reply()
{:ok, conversation_part}
else
{:error, %Changeset{} = changeset} -> {:error, changeset}
end
end
end