1
import { useEnv } from '@directus/env';
2
import type { Notification, PrimaryKey } from '@directus/types';
3
import { useLogger } from '../logger.js';
4
import type { AbstractServiceOptions, MutationOptions } from '../types/index.js';
5
import { md } from '../utils/md.js';
6
import { Url } from '../utils/url.js';
7
import { ItemsService } from './items.js';
8
import { MailService } from './mail/index.js';
9
import { UsersService } from './users.js';
12
const logger = useLogger();
14
export class NotificationsService extends ItemsService {
15
usersService: UsersService;
16
mailService: MailService;
18
constructor(options: AbstractServiceOptions) {
19
super('directus_notifications', options);
20
this.usersService = new UsersService({ schema: this.schema });
21
this.mailService = new MailService({ schema: this.schema, accountability: this.accountability });
24
override async createOne(data: Partial<Notification>, opts?: MutationOptions): Promise<PrimaryKey> {
25
const response = await super.createOne(data, opts);
27
await this.sendEmail(data);
32
async sendEmail(data: Partial<Notification>) {
34
const user = await this.usersService.readOne(data.recipient, {
35
fields: ['id', 'email', 'email_notifications', 'role.app_access'],
38
const manageUserAccountUrl = new Url(env['PUBLIC_URL'] as string)
39
.addPath('admin', 'users', user['id'])
42
const html = data.message ? md(data.message) : '';
44
if (user['email'] && user['email_notifications'] === true) {
49
data: user['role']?.app_access ? { url: manageUserAccountUrl, html } : { html },
52
subject: data.subject,
55
logger.error(error, `Could not send notification via mail`);