From e322b1b59fa6d2d710cfd43e15285b0d0b70cafe Mon Sep 17 00:00:00 2001 From: Kristaps Fabians Geikins Date: Fri, 13 Sep 2024 13:19:33 +0300 Subject: [PATCH] chore(server): notifications IoC 4 - mentionedInCommentHandlerFactory --- .../services/handlers/mentionedInComment.ts | 96 ++++++++++++------- 1 file changed, 62 insertions(+), 34 deletions(-) diff --git a/packages/server/modules/notifications/services/handlers/mentionedInComment.ts b/packages/server/modules/notifications/services/handlers/mentionedInComment.ts index 91bad9f81..5b7f6091e 100644 --- a/packages/server/modules/notifications/services/handlers/mentionedInComment.ts +++ b/packages/server/modules/notifications/services/handlers/mentionedInComment.ts @@ -157,45 +157,73 @@ function buildEmailTemplateParams( /** * Notification that is triggered when a user is mentioned in a comment */ -const handler: NotificationHandler = async (msg) => { - const { - targetUserId, - data: { threadId, authorId, streamId, commentId } - } = msg +const mentionedInCommentHandlerFactory = + (deps: { + getUser: typeof getUser + getStream: typeof getStream + getComment: typeof getComment + getServerInfo: typeof getServerInfo + renderEmail: typeof renderEmail + sendEmail: typeof sendEmail + }): NotificationHandler => + async (msg) => { + const { + targetUserId, + data: { threadId, authorId, streamId, commentId } + } = msg - const isCommentAndThreadTheSame = threadId === commentId + const isCommentAndThreadTheSame = threadId === commentId - const [targetUser, author, stream, threadComment, comment, serverInfo] = - await Promise.all([ - getUser(targetUserId), - getUser(authorId), - getStream({ streamId }), - getComment({ id: threadId }), - isCommentAndThreadTheSame ? null : getComment({ id: commentId }), - getServerInfo() - ]) + const [targetUser, author, stream, threadComment, comment, serverInfo] = + await Promise.all([ + deps.getUser(targetUserId), + deps.getUser(authorId), + deps.getStream({ streamId }), + deps.getComment({ id: threadId }), + isCommentAndThreadTheSame ? null : deps.getComment({ id: commentId }), + deps.getServerInfo() + ]) - const mentionComment = isCommentAndThreadTheSame ? threadComment : comment + const mentionComment = isCommentAndThreadTheSame ? threadComment : comment - // Validate message - const state = validate({ - targetUser, - author, - stream, - threadComment, - mentionComment, - msg, - serverInfo - }) - - const templateParams = buildEmailTemplateParams(state) - const { text, html } = await renderEmail(templateParams, serverInfo, targetUser) - await sendEmail({ - to: state.targetUser.email, - text, - html, - subject: "You've just been mentioned in a Speckle comment" + // Validate message + const state = validate({ + targetUser, + author, + stream, + threadComment, + mentionComment, + msg, + serverInfo + }) + + const templateParams = buildEmailTemplateParams(state) + const { text, html } = await deps.renderEmail( + templateParams, + serverInfo, + targetUser + ) + await deps.sendEmail({ + to: state.targetUser.email, + text, + html, + subject: "You've just been mentioned in a Speckle comment" + }) + } + +/** + * Notification that is triggered when a user is mentioned in a comment + */ +const handler: NotificationHandler = async (...args) => { + const mentionedInCommentHandler = mentionedInCommentHandlerFactory({ + getUser, + getStream, + getComment, + getServerInfo, + renderEmail, + sendEmail }) + return mentionedInCommentHandler(...args) } export default handler