import FcmService = require ( '@app/core/fcm/FcmService' ) ; import NotificationRepository = require ( './NotificationRepository' ) ; import NoFcmKeyError from './error/NoFcmKeyError' ; import User from '@app/core/user/model/User' ; import Notification from './model/Notification' ; import NotificationTypeEnum from './model/NotificationTypeEnum' ; import InvalidNotificationDetailError from './error/InvalidNotificationDetailError' ; export async function sendFcmMsg ( user , title , body , author , cause ) { if ( ! user . fcmKey ) throw new NoFcmKeyError ( ) ; let destination = user . fcmKey ; let response = await FcmService . sendMsg ( destination , title , body , author , cause ) ; return response ; } export async function sendGlobalFcmMsg ( title , body , author , cause ) { let response = await FcmService . sendGlobalMsg ( title , body , author , cause ) ; return response ; } export function add ( notification ) { validateNotificationDetail ( notification ) ; return NotificationRepository . insert ( notification ) ; } function validateNotificationDetail ( notification ) { if ( notification . type === NotificationTypeEnum . LINK_ADDR && typeof notification . detail !== 'string' ) { throw new InvalidNotificationDetailError ( notification . type , notification . detail ) ; } } export function getNewestByUser ( user , offset , limit ) { return NotificationRepository . findNewestByUser ( user , offset , limit ) ; } export function countUnreadByUser ( user ) { return NotificationRepository . countUnreadByUser ( user ) ; }