defmodule NotificationDispatcher.Service.NotificationService do @moduledoc """ This module has every function responsible for sending the different types of email on the application """ alias NotificationDispatcher.Context.NotificationsContext def send_notification(user_id, to, type, params, language, device_id \\ nil) do notification = NotificationsContext.get_by_type_and_language(type, language) notification = if notification == nil do NotificationsContext.get_by_type(type) else notification end if notification != nil do offsets = Poison.decode!(notification.dispatch_offsets) Enum.each( offsets, fn offset -> time_now = NaiveDateTime.local_now time_now = Timex.shift(time_now, minutes: offset) content = Poison.encode!( %{ user_id: user_id, title: notification.title, message: notification.message, email: to, device_id: device_id, fcm_token: to, locale: language, channel: notification.channel, params: params, dispatched_at: time_now, type: type } ) NotificationDispatcher.Sender.send_message("notification_exchange", content, offset) end ) else :template_not_found_error end end end