notification_message.ex 1.37 KB
Newer Older
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
defmodule NotificationDispatcher.Schema.NotificationMessage do
  use Ecto.Schema
  import Ecto.Changeset
  import Ecto.Query

  alias NotificationDispatcher.Schema.NotificationMessage

  @primary_key {:id, :binary_id, autogenerate: true}
  schema "notification_messages" do

    field :title, :string
    field :message, :string

    # 0 - Password Reset
    field :notification_type, :integer

    # 0 - Notification / 1 - Email
    field :channel, :integer
    field :locale, :string
    field :dispatch_offsets, :string

    timestamps()
  end

  @doc false
  def changeset(notification_message, attrs) do
    notification_message
    |> cast(attrs, [:title, :message, :notification_type, :channel, :locale, :dispatch_offsets])
    |> validate_required([:title, :message, :notification_type, :channel, :locale, :dispatch_offsets])
  end

  def query_main, do: from(notification in NotificationMessage, as: :notification)

  def where(query, id), do: from([notification: n] in query, where: n.id == ^id)

  def where_channel(query, channel), do: from([notification: n] in query, where: n.channel == ^channel)

  def where_type(query, type), do: from([notification: n] in query, where: n.notification_type == ^type)

  def where_locale(query, locale), do: from([notification: n] in query, where: n.locale == ^locale)

  def limit(query, limit), do: from([notification: n] in query, limit: ^limit)
end