import { UsersService , MailService } from '.' ; import { AbstractServiceOptions , PrimaryKey , MutationOptions } from '../types' ; import { ItemsService } from './items' ; import { Notification } from '@directus/shared/types' ; import { md } from '../utils/md' ; export class NotificationsService extends ItemsService { usersService : UsersService ; mailService : MailService ; constructor ( options ) { super ( 'directus_notifications' , options ) ; this . usersService = new UsersService ( { schema : this . schema } ) ; this . mailService = new MailService ( { schema : this . schema , accountability : this . accountability } ) ; } async createOne ( data , opts ? ) { await this . sendEmail ( data ) ; return super . createOne ( data , opts ) ; } async createMany ( data , opts ? ) { for ( const notification of data ) { await this . sendEmail ( notification ) ; } return super . createMany ( data , opts ) ; } async sendEmail ( data ) { if ( data . recipient ) { const user = await this . usersService . readOne ( data . recipient , { fields : [ 'email' , 'email_notifications' ] } ) ; if ( user . email && user . email_notifications === true ) { await this . mailService . send ( { template : { name : 'base' , data : { html : data . message ? md ( data . message ) : '' , } , } , to : user . email , subject : data . subject , } ) ; } } } }