import { FileOperationState, NotificationEventType } from "@shared/types"; import { Minute } from "@shared/utils/time"; import CollectionCreatedEmail from "@server/emails/templates/CollectionCreatedEmail"; import CollectionSharedEmail from "@server/emails/templates/CollectionSharedEmail"; import CommentCreatedEmail from "@server/emails/templates/CommentCreatedEmail"; import CommentMentionedEmail from "@server/emails/templates/CommentMentionedEmail"; import CommentResolvedEmail from "@server/emails/templates/CommentResolvedEmail"; import DocumentAccessRequestEmail from "@server/emails/templates/DocumentAccessRequestEmail"; import DocumentMentionedEmail from "@server/emails/templates/DocumentMentionedEmail"; import CollectionOwnerAddedEmail from "@server/emails/templates/CollectionOwnerAddedEmail"; import DocumentOwnerAddedEmail from "@server/emails/templates/DocumentOwnerAddedEmail"; import DocumentPublishedOrUpdatedEmail from "@server/emails/templates/DocumentPublishedOrUpdatedEmail"; import DocumentSharedEmail from "@server/emails/templates/DocumentSharedEmail"; import ExportFailureEmail from "@server/emails/templates/ExportFailureEmail"; import ExportSuccessEmail from "@server/emails/templates/ExportSuccessEmail"; import { FileOperation, Notification } from "@server/models"; import type { Event, NotificationEvent } from "@server/types"; import BaseProcessor from "./BaseProcessor"; import GroupDocumentMentionedEmail from "@server/emails/templates/GroupDocumentMentionedEmail"; import GroupCommentMentionedEmail from "@server/emails/templates/GroupCommentMentionedEmail"; export default class EmailsProcessor extends BaseProcessor { static applicableEvents: Event["name"][] = ["notifications.create"]; async perform(event: NotificationEvent) { const notification = await Notification.scope([ "withTeam", "withUser", "withActor", ]).findByPk(event.modelId); if (!notification) { return; } const notificationId = notification.id; if (notification.user.isSuspended) { return; } switch (notification.event) { case NotificationEventType.UpdateDocument: case NotificationEventType.PublishDocument: { // No need to delay email here as the notification itself is already delayed await new DocumentPublishedOrUpdatedEmail( { to: notification.user.email, language: notification.user.language, userId: notification.userId, eventType: notification.event, revisionId: notification.revisionId, documentId: notification.documentId, teamUrl: notification.team.url, actorId: notification.actorId, actorName: notification.actor.name, }, { notificationId } ).schedule(); return; } case NotificationEventType.AddUserToDocument: { await new DocumentSharedEmail( { to: notification.user.email, language: notification.user.language, userId: notification.userId, documentId: notification.documentId, membershipId: notification.membershipId, teamUrl: notification.team.url, actorName: notification.actor.name, }, { notificationId } ).schedule({ delay: Minute.ms, }); return; } case NotificationEventType.AddOwnerToDocument: { await new DocumentOwnerAddedEmail( { to: notification.user.email, language: notification.user.language, userId: notification.userId, documentId: notification.documentId, teamUrl: notification.team.url, actorName: notification.actor.name, }, { notificationId } ).schedule({ delay: Minute.ms, }); return; } case NotificationEventType.AddOwnerToCollection: { await new CollectionOwnerAddedEmail( { to: notification.user.email, language: notification.user.language, userId: notification.userId, collectionId: notification.collectionId, teamUrl: notification.team.url, actorName: notification.actor.name, }, { notificationId } ).schedule({ delay: Minute.ms, }); return; } case NotificationEventType.AddUserToCollection: { await new CollectionSharedEmail( { to: notification.user.email, language: notification.user.language, userId: notification.userId, collectionId: notification.collectionId, teamUrl: notification.team.url, actorName: notification.actor.name, }, { notificationId } ).schedule({ delay: Minute.ms, }); return; } case NotificationEventType.GroupMentionedInDocument: { await new GroupDocumentMentionedEmail( { to: notification.user.email, language: notification.user.language, userId: notification.userId, documentId: notification.documentId, revisionId: notification.revisionId, groupId: notification.groupId, teamUrl: notification.team.url, actorName: notification.actor.name, }, { notificationId } ).schedule(); return; } case NotificationEventType.MentionedInDocument: { // No need to delay email here as the notification itself is already delayed await new DocumentMentionedEmail( { to: notification.user.email, language: notification.user.language, documentId: notification.documentId, revisionId: notification.revisionId, userId: notification.userId, teamUrl: notification.team.url, actorName: notification.actor.name, }, { notificationId } ).schedule(); return; } case NotificationEventType.GroupMentionedInComment: { await new GroupCommentMentionedEmail( { to: notification.user.email, language: notification.user.language, userId: notification.userId, documentId: notification.documentId, teamUrl: notification.team.url, actorName: notification.actor.name, commentId: notification.commentId, groupId: notification.groupId, }, { notificationId } ).schedule({ delay: Minute.ms, }); return; } case NotificationEventType.MentionedInComment: { await new CommentMentionedEmail( { to: notification.user.email, language: notification.user.language, userId: notification.userId, documentId: notification.documentId, teamUrl: notification.team.url, actorName: notification.actor.name, commentId: notification.commentId, }, { notificationId: notification.id } ).schedule({ delay: Minute.ms, }); return; } case NotificationEventType.CreateCollection: { await new CollectionCreatedEmail( { to: notification.user.email, language: notification.user.language, userId: notification.userId, collectionId: notification.collectionId, teamUrl: notification.team.url, }, { notificationId: notification.id } ).schedule({ delay: Minute.ms, }); return; } case NotificationEventType.CreateComment: { await new CommentCreatedEmail( { to: notification.user.email, language: notification.user.language, userId: notification.userId, documentId: notification.documentId, teamUrl: notification.team.url, actorName: notification.actor.name, commentId: notification.commentId, }, { notificationId: notification.id } ).schedule({ delay: Minute.ms, }); return; } case NotificationEventType.ResolveComment: { await new CommentResolvedEmail( { to: notification.user.email, language: notification.user.language, userId: notification.userId, documentId: notification.documentId, teamUrl: notification.team.url, actorName: notification.actor.name, commentId: notification.commentId, }, { notificationId: notification.id } ).schedule({ delay: Minute.ms, }); return; } case NotificationEventType.ExportCompleted: { const fileOperation = notification.fileOperationId ? await FileOperation.unscoped().findByPk( notification.fileOperationId ) : null; if (fileOperation?.state === FileOperationState.Complete) { await new ExportSuccessEmail( { to: notification.user.email, language: notification.user.language, userId: notification.userId, id: fileOperation.id, teamUrl: notification.team.url, teamId: notification.teamId, }, { notificationId } ).schedule({ delay: Minute.ms, }); } if (fileOperation?.state === FileOperationState.Error) { await new ExportFailureEmail( { to: notification.user.email, language: notification.user.language, userId: notification.userId, teamUrl: notification.team.url, teamId: notification.teamId, }, { notificationId } ).schedule({ delay: Minute.ms, }); } return; } case NotificationEventType.RequestDocumentAccess: { await new DocumentAccessRequestEmail( { to: notification.user.email, language: notification.user.language, userId: notification.userId, documentId: notification.documentId, actorId: notification.actorId, teamUrl: notification.team.url, }, { notificationId: notification.id } ).schedule({ delay: Minute.ms, }); return; } } } }