• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

source-academy / backend / e0330f2cf38b2d8af12bffd20f4cac2158d607fc-PR-1236

31 Mar 2025 09:12AM UTC coverage: 19.982% (-73.6%) from 93.607%
e0330f2cf38b2d8af12bffd20f4cac2158d607fc-PR-1236

Pull #1236

github

RichDom2185
Redate migrations to maintain total ordering
Pull Request #1236: Added Exam mode

12 of 57 new or added lines in 8 files covered. (21.05%)

2430 existing lines in 97 files now uncovered.

671 of 3358 relevant lines covered (19.98%)

3.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

10.71
/lib/cadet/notifications.ex
1
defmodule Cadet.Notifications do
2
  @moduledoc """
3
  The Notifications context.
4
  """
5

6
  import Ecto.Query, warn: false
7
  alias Cadet.Repo
8

9
  alias Cadet.Notifications.{
10
    NotificationType,
11
    NotificationConfig,
12
    SentNotification,
13
    TimeOption,
14
    NotificationPreference
15
  }
16

17
  @doc """
18
  Gets a single notification_type.
19

20
  Raises `Ecto.NoResultsError` if the Notification type does not exist.
21

22
  ## Examples
23

24
      iex> get_notification_type!(123)
25
      %NotificationType{}
26

27
      iex> get_notification_type!(456)
28
      ** (Ecto.NoResultsError)
29

30
  """
31
  def get_notification_type!(id), do: Repo.get!(NotificationType, id)
1✔
32

33
  @doc """
34
  Gets a single notification_type by name.any()
35

36
  Raises `Ecto.NoResultsError` if the Notification type does not exist.
37

38
  ## Examples
39

40
      iex> get_notification_type_by_name!("AVENGER BACKLOG")
41
      %NotificationType{}
42

43
      iex> get_notification_type_by_name!("AVENGER BACKLOG")
44
      ** (Ecto.NoResultsError)
45
  """
46
  def get_notification_type_by_name!(name) do
UNCOV
47
    Repo.one!(from(nt in NotificationType, where: nt.name == ^name))
×
48
  end
49

50
  def get_notification_config!(notification_type_id, course_id, assconfig_id) do
UNCOV
51
    query =
×
UNCOV
52
      from(n in Cadet.Notifications.NotificationConfig,
×
53
        join: ntype in Cadet.Notifications.NotificationType,
54
        on: n.notification_type_id == ntype.id,
55
        where: n.notification_type_id == ^notification_type_id and n.course_id == ^course_id
56
      )
57

UNCOV
58
    query =
×
59
      if is_nil(assconfig_id) do
UNCOV
60
        where(query, [c], is_nil(c.assessment_config_id))
×
61
      else
UNCOV
62
        where(query, [c], c.assessment_config_id == ^assconfig_id)
×
63
      end
64

UNCOV
65
    Repo.one(query)
×
66
  end
67

68
  @doc """
69
  Updates a notification_config.
70

71
  ## Examples
72

73
      iex> update_notification_config(notification_config, %{field: new_value})
74
      {:ok, %NotificationConfig{}}
75

76
      iex> update_notification_config(notification_config, %{field: bad_value})
77
      {:error, %Ecto.Changeset{}}
78

79
  """
80
  def update_notification_config(notification_config = %NotificationConfig{}, attrs) do
81
    notification_config
82
    |> NotificationConfig.changeset(attrs)
UNCOV
83
    |> Repo.update()
×
84
  end
85

86
  @doc """
87
  Returns an `%Ecto.Changeset{}` for tracking notification_config changes.
88

89
  ## Examples
90

91
      iex> change_notification_config(notification_config)
92
      %Ecto.Changeset{data: %NotificationConfig{}}
93

94
  """
95
  def change_notification_config(notification_config = %NotificationConfig{}, attrs \\ %{}) do
UNCOV
96
    NotificationConfig.changeset(notification_config, attrs)
×
97
  end
98

99
  @doc """
100
  Gets a single time_option.
101

102
  Raises `Ecto.NoResultsError` if the Time option does not exist.
103

104
  ## Examples
105

106
      iex> get_time_option!(123)
107
      %TimeOption{}
108

109
      iex> get_time_option!(456)
110
      ** (Ecto.NoResultsError)
111

112
  """
UNCOV
113
  def get_time_option!(id), do: Repo.get!(TimeOption, id)
×
114

115
  def get_time_options_for_assessment(assessment_config_id, notification_type_id) do
UNCOV
116
    query =
×
UNCOV
117
      from(ac in Cadet.Courses.AssessmentConfig,
×
118
        join: n in Cadet.Notifications.NotificationConfig,
119
        on: n.assessment_config_id == ac.id,
120
        join: to in Cadet.Notifications.TimeOption,
121
        on: to.notification_config_id == n.id,
122
        where: ac.id == ^assessment_config_id and n.notification_type_id == ^notification_type_id,
123
        select: to
124
      )
125

UNCOV
126
    Repo.all(query)
×
127
  end
128

129
  def get_default_time_option_for_assessment!(assessment_config_id, notification_type_id) do
UNCOV
130
    query =
×
UNCOV
131
      from(ac in Cadet.Courses.AssessmentConfig,
×
132
        join: n in Cadet.Notifications.NotificationConfig,
133
        on: n.assessment_config_id == ac.id,
134
        join: to in Cadet.Notifications.TimeOption,
135
        on: to.notification_config_id == n.id,
136
        where:
137
          ac.id == ^assessment_config_id and n.notification_type_id == ^notification_type_id and
138
            to.is_default == true,
139
        select: to
140
      )
141

UNCOV
142
    Repo.one!(query)
×
143
  end
144

145
  @doc """
146
  Creates a time_option.
147

148
  ## Examples
149

150
      iex> create_time_option(%{field: value})
151
      {:ok, %TimeOption{}}
152

153
      iex> create_time_option(%{field: bad_value})
154
      {:error, %Ecto.Changeset{}}
155

156
  """
157
  def create_time_option(attrs \\ %{}) do
158
    %TimeOption{}
159
    |> TimeOption.changeset(attrs)
160
    |> Repo.insert()
1✔
161
  end
162

163
  @doc """
164
  Deletes a time_option.
165

166
  ## Examples
167

168
      iex> delete_time_option(time_option)
169
      {:ok, %TimeOption{}}
170

171
      iex> delete_time_option(time_option)
172
      {:error, %Ecto.Changeset{}}
173

174
  """
175
  def delete_time_option(time_option = %TimeOption{}) do
UNCOV
176
    Repo.delete(time_option)
×
177
  end
178

179
  def get_notification_preference(notification_type_id, course_reg_id) do
UNCOV
180
    query =
×
UNCOV
181
      from(np in NotificationPreference,
×
182
        join: noti in Cadet.Notifications.NotificationConfig,
183
        on: np.notification_config_id == noti.id,
184
        join: ntype in NotificationType,
185
        on: noti.notification_type_id == ntype.id,
186
        where: ntype.id == ^notification_type_id and np.course_reg_id == ^course_reg_id,
187
        preload: :time_option
188
      )
189

UNCOV
190
    Repo.one(query)
×
191
  end
192

193
  @doc """
194
  Creates a notification_preference.
195

196
  ## Examples
197

198
      iex> create_notification_preference(%{field: value})
199
      {:ok, %NotificationPreference{}}
200

201
      iex> create_notification_preference(%{field: bad_value})
202
      {:error, %Ecto.Changeset{}}
203

204
  """
205
  def create_notification_preference(attrs \\ %{}) do
206
    %NotificationPreference{}
207
    |> NotificationPreference.changeset(attrs)
208
    |> Repo.insert()
1✔
209
  end
210

211
  @doc """
212
  Updates a notification_preference.
213

214
  ## Examples
215

216
      iex> update_notification_preference(notification_preference, %{field: new_value})
217
      {:ok, %NotificationPreference{}}
218

219
      iex> update_notification_preference(notification_preference, %{field: bad_value})
220
      {:error, %Ecto.Changeset{}}
221

222
  """
223
  def update_notification_preference(notification_preference = %NotificationPreference{}, attrs) do
224
    notification_preference
225
    |> NotificationPreference.changeset(attrs)
UNCOV
226
    |> Repo.update()
×
227
  end
228

229
  @doc """
230
  Deletes a notification_preference.
231

232
  ## Examples
233

234
      iex> delete_notification_preference(notification_preference)
235
      {:ok, %NotificationPreference{}}
236

237
      iex> delete_notification_preference(notification_preference)
238
      {:error, %Ecto.Changeset{}}
239

240
  """
241
  def delete_notification_preference(notification_preference = %NotificationPreference{}) do
UNCOV
242
    Repo.delete(notification_preference)
×
243
  end
244

245
  @doc """
246
  Returns an `%Ecto.Changeset{}` for tracking notification_preference changes.
247

248
  ## Examples
249

250
      iex> change_notification_preference(notification_preference)
251
      %Ecto.Changeset{data: %NotificationPreference{}}
252

253
  """
254
  def change_notification_preference(
255
        notification_preference = %NotificationPreference{},
UNCOV
256
        attrs \\ %{}
×
257
      ) do
UNCOV
258
    NotificationPreference.changeset(notification_preference, attrs)
×
259
  end
260

261
  @doc """
262
  Creates a sent_notification.
263

264
  ## Examples
265

266
      iex> create_sent_notification(%{field: value})
267
      {:ok, %SentNotification{}}
268

269
      iex> create_sent_notification(%{field: bad_value})
270
      {:error, %Ecto.Changeset{}}
271

272
  """
273
  def create_sent_notification(course_reg_id, content) do
274
    %SentNotification{}
275
    |> SentNotification.changeset(%{course_reg_id: course_reg_id, content: content})
UNCOV
276
    |> Repo.insert()
×
277
  end
278

279
  @doc """
280
  Returns the list of sent_notifications.
281

282
  ## Examples
283

284
      iex> list_sent_notifications()
285
      [%SentNotification{}, ...]
286

287
  """
288

289
  # def list_sent_notifications do
290
  #   Repo.all(SentNotification)
291
  # end
292

293
  # @doc """
294
  # Gets a single sent_notification.
295

296
  # Raises `Ecto.NoResultsError` if the Sent notification does not exist.
297

298
  # ## Examples
299

300
  #     iex> get_sent_notification!(123)
301
  #     %SentNotification{}
302

303
  #     iex> get_sent_notification!(456)
304
  #     ** (Ecto.NoResultsError)
305

306
  # """
307
  # # def get_sent_notification!(id), do: Repo.get!(SentNotification, id)
308
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc