directus

Форк
0
/
notifications.ts 
60 строк · 1.8 Кб
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';
10

11
const env = useEnv();
12
const logger = useLogger();
13

14
export class NotificationsService extends ItemsService {
15
	usersService: UsersService;
16
	mailService: MailService;
17

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 });
22
	}
23

24
	override async createOne(data: Partial<Notification>, opts?: MutationOptions): Promise<PrimaryKey> {
25
		const response = await super.createOne(data, opts);
26

27
		await this.sendEmail(data);
28

29
		return response;
30
	}
31

32
	async sendEmail(data: Partial<Notification>) {
33
		if (data.recipient) {
34
			const user = await this.usersService.readOne(data.recipient, {
35
				fields: ['id', 'email', 'email_notifications', 'role.app_access'],
36
			});
37

38
			const manageUserAccountUrl = new Url(env['PUBLIC_URL'] as string)
39
				.addPath('admin', 'users', user['id'])
40
				.toString();
41

42
			const html = data.message ? md(data.message) : '';
43

44
			if (user['email'] && user['email_notifications'] === true) {
45
				this.mailService
46
					.send({
47
						template: {
48
							name: 'base',
49
							data: user['role']?.app_access ? { url: manageUserAccountUrl, html } : { html },
50
						},
51
						to: user['email'],
52
						subject: data.subject,
53
					})
54
					.catch((error) => {
55
						logger.error(error, `Could not send notification via mail`);
56
					});
57
			}
58
		}
59
	}
60
}
61

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.