Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Public Libs
Elixir
Notification Dispatcher
Commits
793cd992
Commit
793cd992
authored
Oct 29, 2020
by
Miguel Basticioto
Browse files
chore: usando exdoc
parent
5501a21c
Changes
4
Hide whitespace changes
Inline
Side-by-side
lib/contexts/notification_context.ex
View file @
793cd992
defmodule
NotificationDispatcher
.
Context
.
NotificationContext
do
@moduledoc
"""
Th
e Notifications context.
Th
is module has several auxiliary functions to interact with the database
"""
import
Ecto
.
Query
,
warn:
false
@doc
"""
Returns the repo defined on the config.
"""
def
get_repo
(),
do
:
Application
.
fetch_env!
(
:notification_dispatcher
,
:repo
)
alias
NotificationDispatcher
.
Schema
.
{
Device
,
NotificationMessage
}
@doc
"""
Get a notification_message entry by the id
## Parameters
- id: Id of the notification_message entry
"""
def
get_notification_message!
(
id
),
do
:
get_repo
()
.
get!
(
NotificationMessage
,
id
)
@doc
"""
Get a notification_message by the channel
## Parameters
- channel: Channel of the notification_message.
"""
def
get_one_notification_message!
(
channel
)
do
NotificationMessage
.
query_main
()
|>
NotificationMessage
.
where_channel
(
channel
)
...
...
@@ -17,6 +35,14 @@ defmodule NotificationDispatcher.Context.NotificationContext do
|>
get_repo
()
.
one
()
end
@doc
"""
Get a notification_message by type and language
## Parameters
- type: Type of the notification_message entry
- language: Language of the notification_message entry
"""
def
get_by_type_and_language
(
type
,
language
)
do
NotificationMessage
.
query_main
()
|>
NotificationMessage
.
where_type
(
type
)
...
...
@@ -25,6 +51,13 @@ defmodule NotificationDispatcher.Context.NotificationContext do
|>
get_repo
()
.
one
()
end
@doc
"""
Get a notification_message by type only. Ideal in case of there is no notification_message on the requested language.
## Parameters
- type: Type of the notification_message entry
"""
def
get_by_type
(
type
)
do
NotificationMessage
.
query_main
()
|>
NotificationMessage
.
where_type
(
type
)
...
...
@@ -32,39 +65,114 @@ defmodule NotificationDispatcher.Context.NotificationContext do
|>
get_repo
()
.
one
()
end
@doc
"""
Call the changeset validation and repo to insert a notification_message on the database.
## Parameters
- attrs: All the data that will be validated by a changeset and then inserted.
## Examples
attrs = %{
"title" => "title",
"message" => "message",
"notification_type" => 0,
"channel" => 0,
"locale" => "en",
"dispatch_offsets" => "[0]"
}
alias NotificationDispatcher.Context.NotificationContext
NotificationContext.create_notification_message(attrs)
"""
def
create_notification_message
(
attrs
\\
%{})
do
%
NotificationMessage
{}
|>
NotificationMessage
.
changeset
(
attrs
)
|>
get_repo
()
.
insert
()
end
@doc
"""
Delete a notification_message on the database.
## Parameters
- notification_message: The desired notification_message to be deleted
"""
def
delete_notification_message
(%
NotificationMessage
{}
=
notification_message
)
do
get_repo
()
.
delete
(
notification_message
)
end
@doc
"""
Returns all devices of specified users.
## Parameters
- user_ids: A list of user_ids
"""
def
get_devices
(
user_ids
)
do
Device
.
query_main
()
|>
Device
.
where_user_id_in
(
user_ids
)
|>
get_repo
()
.
all
()
end
@doc
"""
Returns a specific device.
## Parameters
- id: Id of the device to return
"""
def
get_device!
(
id
)
do
Device
.
query_main
|>
Device
.
where
(
id
)
|>
get_repo
()
.
one
end
@doc
"""
Returns a specific device.
## Parameters
- id: Id of the device to return
"""
def
get_device
(
id
)
do
device
=
get_device!
(
id
)
if
is_nil
(
device
),
do
:
:not_found
,
else
:
{
:ok
,
device
}
end
@doc
"""
Updates a existing device
## Parameters
- device: Device to be updated
- attrs: Attributes and values to be changed on device
"""
def
update_device
(%
Device
{}
=
device
,
attrs
)
do
device
|>
Device
.
changeset_update
(
attrs
)
|>
get_repo
()
.
update
end
@doc
"""
Call the changeset validations and insert a new device in the database.
## Parameters
- attrs: Attributes of the new device
- user_id: Id of the user the devices refers
## Examples
attrs = %{
"device_token" => "firebase_token",
"os" => "android"
}
alias NotificationDispatcher.Context.NotificationContext
NotificationContext.create_device(attrs, user_id)
"""
def
create_device
(
attrs
\\
%{},
user_id
)
do
attrs
=
Map
.
put
(
attrs
,
"user_id"
,
user_id
)
...
...
lib/controllers/notification_message_controller.ex
View file @
793cd992
defmodule
NotificationDispatcher
.
Controller
.
NotificationMessageController
do
alias
NotificationDispatcher
.
Context
.
NotificationContext
@moduledoc
"""
Contains the method to create notification_messages in your database. You can call this method on a controller on the create method.
"""
@doc
"""
Create can return {:ok, notification_message, json} or {:error, changeset}
Create a notification_message entry.
## Parameters
- params: All the data that will be validated by a changeset and then inserted.
## Examples
params = %{
"title" => "title",
"message" => "message",
"notification_type" => 0,
"channel" => 0,
"locale" => "en",
"dispatch_offsets" => "[0]"
}
alias NotificationDispatcher.Controller.NotificationMessageController
NotificationMessageController.create(params)
"""
def
create
(
params
)
do
case
NotificationContext
.
create_notification_message
(
params
)
do
{
:ok
,
notification_message
}
->
{
:ok
,
notification_message
,
%{
data:
%{
id:
notification_message
.
id
}}}
{
:ok
,
notification_message
,
%{
data:
%{
id:
notification_message
.
id
}
}
}
{
:error
,
changeset
}
->
{
:error
,
changeset
}
end
...
...
lib/migration.ex
View file @
793cd992
defmodule
NotificationDispatcher
.
Migration
do
use
Ecto
.
Migration
@moduledoc
"""
This module has the functions necessary to create the database tables.
"""
@doc
"""
Create the notification_messages table. This tables stores notifications templates in different languages.
"""
def
create_notification_messages
()
do
create
table
(
:notification_messages
,
primary_key:
false
)
do
add
:id
,
:uuid
,
primary_key:
true
...
...
@@ -20,6 +26,9 @@ defmodule NotificationDispatcher.Migration do
create
index
(
:notification_messages
,
[
:notification_type
])
end
@doc
"""
Create the devices table. This table is used to store the firebase token of your users if your project uses push notification.
"""
def
create_devices
()
do
create
table
(
:devices
,
primary_key:
false
)
do
add
:id
,
:uuid
,
primary_key:
true
...
...
lib/services/notification_service.ex
View file @
793cd992
defmodule
NotificationDispatcher
.
Service
.
NotificationService
do
@moduledoc
"""
This module has
every
function responsible for sending the
different types of email on the application
This module has
the
function
s
responsible for sending the
notification to the sender method.
"""
alias
NotificationDispatcher
.
Context
.
NotificationContext
@doc
"""
Send the notification already formatted to the sender method
## Parameters
- user_id: Id of the user that will receive the notification.
- to: Email or firebase token of the user.
- type: Type of the desired notification, as registered in the database.
- params: Extra params to be sent on the message to be queued by rabbitmq.
- language: The desired language of the notification, as registered in the database.
- device_id: The id of the user's device on the devices table(Only if using push notification).
"""
def
send_notification
(
user_id
,
to
,
type
,
params
,
language
,
device_id
\\
nil
)
do
notification
=
NotificationContext
.
get_by_type_and_language
(
type
,
language
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment