UNPKG

@directus/api

Version:

Directus is a real-time API and App dashboard for managing SQL database content

67 lines (65 loc) 2.1 kB
import { useLogger } from "../logger/index.js"; import { fetchRolesTree } from "../permissions/lib/fetch-roles-tree.js"; import { fetchGlobalAccess } from "../permissions/modules/fetch-global-access/fetch-global-access.js"; import { ItemsService } from "./items.js"; import { Url } from "../utils/url.js"; import { md } from "../utils/md.js"; import { MailService } from "./mail/index.js"; import { UsersService } from "./users.js"; import { useEnv } from "@directus/env"; //#region src/services/notifications.ts const env = useEnv(); const logger = useLogger(); var NotificationsService = class extends ItemsService { constructor(options) { super("directus_notifications", options); } async createOne(data, opts) { const response = await super.createOne(data, opts); await this.sendEmail(data); return response; } async sendEmail(data) { if (data.recipient) { const user = await new UsersService({ schema: this.schema, knex: this.knex }).readOne(data.recipient, { fields: [ "id", "email", "email_notifications", "role" ] }); if (user["email"] && user["email_notifications"] === true) { const manageUserAccountUrl = new Url(env["PUBLIC_URL"]).addPath("admin", "users", user["id"]).toString(); const html = data.message ? md(data.message) : ""; const roles = await fetchRolesTree(user["role"], { knex: this.knex }); const { app: app_access } = await fetchGlobalAccess({ user: user["id"], roles, ip: null }, { knex: this.knex }); const mailService = new MailService({ schema: this.schema, knex: this.knex, accountability: this.accountability }); mailService.send({ template: { name: "base", data: app_access ? { url: manageUserAccountUrl, html } : { html } }, to: user["email"], subject: data.subject }, { defaultTemplateData: await mailService.getDefaultTemplateData() }).catch((error) => { logger.error(error, `Could not send notification via mail`); }); } } } }; //#endregion export { NotificationsService };