Files
bodyshop/server/notifications/esignNotifications.js

137 lines
5.0 KiB
JavaScript

const { dispatchJobWatcherNotification } = require("./dispatchJobWatcherNotification");
/**
* Default notification channel preferences for e-sign document events. By default, users will receive in-app
* notifications for e-sign events, but not email or FCM notifications. These defaults can be overridden by user
* preferences or specific notification dispatch calls.
* @type {Readonly<{app: boolean, email: boolean, fcm: boolean}>}
*/
const DEFAULT_ESIGN_CHANNEL_PREFERENCES = Object.freeze({
app: true,
email: false,
fcm: false
});
/**
* Notification scenarios for e-sign document events. Each scenario includes a unique scenario key and a localization
* key for the notification message.
* @type {Readonly<{documentOpened: {scenarioKey: string, key: string}, documentCompleted: {scenarioKey: string, key: string}, documentUploadFailed: {scenarioKey: string, key: string}}>}
*/
const ESIGN_NOTIFICATION_SCENARIOS = Object.freeze({
documentOpened: {
scenarioKey: "esign-document-opened",
key: "notifications.job.esignDocumentOpened"
},
documentCompleted: {
scenarioKey: "esign-document-completed",
key: "notifications.job.esignDocumentCompleted"
},
documentUploadFailed: {
scenarioKey: "esign-document-upload-failed",
key: "notifications.job.esignDocumentUploadFailed"
}
});
/**
* Formats the document title for use in notification messages. If a title is provided, it will be wrapped in quotes;
* if not, a generic description will be used.
* @param title
* @returns {string|string}
*/
const formatDocumentTitle = (title) => (title ? `"${title}"` : "An e-sign document");
/**
* Dispatches a notification when an e-sign document is opened. The notification will include the document title and
* will be sent to the user who uploaded the document (if available) with default channel preferences for e-sign events.
* @param param0
* @param param0.jobId
* @param param0.documentId
* @param param0.title
* @param param0.uploadedBy
* @param param0.logger
* @returns {Promise<boolean>}
*/
async function dispatchEsignDocumentOpenedNotification({ jobId, documentId, title, uploadedBy, logger }) {
return dispatchJobWatcherNotification({
jobId,
scenarioKey: ESIGN_NOTIFICATION_SCENARIOS.documentOpened.scenarioKey,
key: ESIGN_NOTIFICATION_SCENARIOS.documentOpened.key,
body: `${formatDocumentTitle(title)} has been opened.`,
variables: {
documentId,
title: title || null,
uploadedBy: uploadedBy || null,
status: "OPENED"
},
extraRecipientEmails: uploadedBy ? [uploadedBy] : [],
defaultChannelPreferences: DEFAULT_ESIGN_CHANNEL_PREFERENCES,
logger
});
}
/**
* Dispatches a notification when an e-sign document is completed. The notification will include the document title and
* will be sent to the user who uploaded the document (if available) with default channel preferences for e-sign events.
* @param param0
* @param param0.jobId
* @param param0.documentId
* @param param0.title
* @param param0.uploadedBy
* @param param0.logger
* @returns {Promise<boolean>}
*/
async function dispatchEsignDocumentCompletedNotification({ jobId, documentId, title, uploadedBy, logger }) {
return dispatchJobWatcherNotification({
jobId,
scenarioKey: ESIGN_NOTIFICATION_SCENARIOS.documentCompleted.scenarioKey,
key: ESIGN_NOTIFICATION_SCENARIOS.documentCompleted.key,
body: `${formatDocumentTitle(title)} has been completed.`,
variables: {
documentId,
title: title || null,
uploadedBy: uploadedBy || null,
status: "COMPLETED"
},
extraRecipientEmails: uploadedBy ? [uploadedBy] : [],
defaultChannelPreferences: DEFAULT_ESIGN_CHANNEL_PREFERENCES,
logger
});
}
/**
* Dispatches a notification when an e-sign document upload fails. The notification will include the document title and
* will be sent to the user who uploaded the document (if available) with default channel preferences for e-sign events.
* @param param0
* @param param0.jobId
* @param param0.documentId
* @param param0.title
* @param param0.uploadedBy
* @param param0.logger
* @returns {Promise<boolean>}
*/
async function dispatchEsignDocumentUploadFailedNotification({ jobId, documentId, title, uploadedBy, logger }) {
return dispatchJobWatcherNotification({
jobId,
scenarioKey: ESIGN_NOTIFICATION_SCENARIOS.documentUploadFailed.scenarioKey,
key: ESIGN_NOTIFICATION_SCENARIOS.documentUploadFailed.key,
body: `${formatDocumentTitle(title)} was completed, but the signed PDF failed to upload to the job documents.`,
variables: {
documentId,
title: title || null,
uploadedBy: uploadedBy || null,
status: "UPLOAD_FAILED"
},
extraRecipientEmails: uploadedBy ? [uploadedBy] : [],
defaultChannelPreferences: DEFAULT_ESIGN_CHANNEL_PREFERENCES,
logger
});
}
module.exports = {
DEFAULT_ESIGN_CHANNEL_PREFERENCES,
ESIGN_NOTIFICATION_SCENARIOS,
dispatchEsignDocumentOpenedNotification,
dispatchEsignDocumentCompletedNotification,
dispatchEsignDocumentUploadFailedNotification
};