feat(P19IM1-3,P19IM1-4): 接入业务事件与通知渠道后台
这个提交包含在:
+262
-129
@@ -9,7 +9,9 @@ const {
|
||||
createNotifications,
|
||||
resolveRecipientsForAssignee,
|
||||
} = require("../services/notifications");
|
||||
const { enqueueImNotifications } = require("../services/notifications/imEnqueue");
|
||||
const { sendNotificationEmailSafe } = require("../services/email");
|
||||
const { NOTIFICATION_TYPES } = require("../constants/notificationTypes");
|
||||
const {
|
||||
findActiveDelegationForDelegate,
|
||||
listActiveDelegationPrincipals,
|
||||
@@ -407,6 +409,201 @@ function buildReturnedCopy(entityMeta, comment) {
|
||||
};
|
||||
}
|
||||
|
||||
function uniq(values) {
|
||||
return Array.from(new Set((values ?? []).filter(Boolean)));
|
||||
}
|
||||
|
||||
async function loadLeaveNotifyContext(tx, leaveRequestId) {
|
||||
return tx.leaveRequest.findUnique({
|
||||
where: { id: leaveRequestId },
|
||||
select: {
|
||||
id: true,
|
||||
owner_id: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function loadPurchaseNotifyContext(tx, purchaseRequestId) {
|
||||
return tx.purchaseRequest.findUnique({
|
||||
where: { id: purchaseRequestId },
|
||||
select: {
|
||||
id: true,
|
||||
request_no: true,
|
||||
applicant_id: true,
|
||||
submitter_id: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function loadWorkflowNotifyContext(tx, entityType, entityId) {
|
||||
const normalizedEntityType = entityType === 'procurement' ? 'purchase_request' : entityType;
|
||||
|
||||
if (normalizedEntityType === 'reimbursement') {
|
||||
const record = await loadReimbursementNotifyContext(tx, entityId);
|
||||
return {
|
||||
record,
|
||||
recipientEmployeeIds: uniq([record?.owner_id, record?.submitter_id]),
|
||||
};
|
||||
}
|
||||
|
||||
if (normalizedEntityType === 'leave') {
|
||||
const record = await loadLeaveNotifyContext(tx, entityId);
|
||||
return {
|
||||
record,
|
||||
recipientEmployeeIds: uniq([record?.owner_id]),
|
||||
};
|
||||
}
|
||||
|
||||
if (normalizedEntityType === 'purchase_request') {
|
||||
const record = await loadPurchaseNotifyContext(tx, entityId);
|
||||
return {
|
||||
record,
|
||||
recipientEmployeeIds: uniq([record?.applicant_id, record?.submitter_id]),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
record: null,
|
||||
recipientEmployeeIds: [],
|
||||
};
|
||||
}
|
||||
|
||||
function resolveWorkflowOutcomeNotificationType(entityType, outcome) {
|
||||
const normalizedEntityType = entityType === 'procurement' ? 'purchase_request' : entityType;
|
||||
|
||||
if (outcome === 'approved') {
|
||||
if (normalizedEntityType === 'reimbursement') return NOTIFICATION_TYPES.reimbursementApproved;
|
||||
if (normalizedEntityType === 'leave') return NOTIFICATION_TYPES.leaveApproved;
|
||||
if (normalizedEntityType === 'purchase_request') return NOTIFICATION_TYPES.purchaseRequestApproved;
|
||||
}
|
||||
|
||||
if (outcome === 'rejected') {
|
||||
if (normalizedEntityType === 'reimbursement') return NOTIFICATION_TYPES.reimbursementRejected;
|
||||
if (normalizedEntityType === 'leave') return NOTIFICATION_TYPES.leaveRejected;
|
||||
if (normalizedEntityType === 'purchase_request') return NOTIFICATION_TYPES.purchaseRequestRejected;
|
||||
}
|
||||
|
||||
if (outcome === 'returned') {
|
||||
if (normalizedEntityType === 'reimbursement') return NOTIFICATION_TYPES.reimbursementReturned;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async function queueTaskAssignedNotifications({
|
||||
tx,
|
||||
config,
|
||||
logger,
|
||||
actorId,
|
||||
emailTasks,
|
||||
recipientEmployeeIds,
|
||||
title,
|
||||
body,
|
||||
taskId,
|
||||
entityType,
|
||||
entityId,
|
||||
}) {
|
||||
const recipients = uniq(recipientEmployeeIds);
|
||||
if (!recipients.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
await createNotifications(tx, {
|
||||
recipientEmployeeIds: recipients,
|
||||
type: NOTIFICATION_TYPES.workflowTaskAssigned,
|
||||
title,
|
||||
body,
|
||||
targetType: 'workflow_task',
|
||||
targetId: taskId,
|
||||
});
|
||||
|
||||
await enqueueImNotifications(tx, {
|
||||
config,
|
||||
logger,
|
||||
recipientEmployeeIds: recipients,
|
||||
type: NOTIFICATION_TYPES.workflowTaskAssigned,
|
||||
title,
|
||||
body,
|
||||
targetType: 'workflow_task',
|
||||
targetId: taskId,
|
||||
targetPath: `/workflow/tasks/${taskId}`,
|
||||
entityType,
|
||||
entityId,
|
||||
actorId,
|
||||
});
|
||||
|
||||
emailTasks.push({
|
||||
type: NOTIFICATION_TYPES.workflowTaskAssigned,
|
||||
recipientEmployeeIds: recipients,
|
||||
subject: title,
|
||||
body,
|
||||
targetPath: `/workflow/tasks/${taskId}`,
|
||||
});
|
||||
}
|
||||
|
||||
async function queueWorkflowOutcomeNotifications({
|
||||
tx,
|
||||
config,
|
||||
logger,
|
||||
actorId,
|
||||
emailTasks,
|
||||
workflowInstance,
|
||||
outcome,
|
||||
comment = null,
|
||||
}) {
|
||||
const normalizedEntityType = workflowInstance.entity_type === 'procurement' ? 'purchase_request' : workflowInstance.entity_type;
|
||||
const notificationType = resolveWorkflowOutcomeNotificationType(normalizedEntityType, outcome);
|
||||
if (!notificationType) {
|
||||
return;
|
||||
}
|
||||
|
||||
const context = await loadWorkflowNotifyContext(tx, normalizedEntityType, workflowInstance.entity_id);
|
||||
const recipients = uniq(context.recipientEmployeeIds);
|
||||
if (!recipients.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const entityMeta = getWorkflowEntityMeta(normalizedEntityType, context.record, workflowInstance.entity_id);
|
||||
const copy =
|
||||
outcome === 'approved'
|
||||
? buildApprovedCopy(entityMeta)
|
||||
: outcome === 'rejected'
|
||||
? buildRejectedCopy(entityMeta, comment)
|
||||
: buildReturnedCopy(entityMeta, comment);
|
||||
|
||||
await createNotifications(tx, {
|
||||
recipientEmployeeIds: recipients,
|
||||
type: notificationType,
|
||||
title: copy.title,
|
||||
body: copy.body,
|
||||
targetType: entityMeta.targetType,
|
||||
targetId: workflowInstance.entity_id,
|
||||
});
|
||||
|
||||
await enqueueImNotifications(tx, {
|
||||
config,
|
||||
logger,
|
||||
recipientEmployeeIds: recipients,
|
||||
type: notificationType,
|
||||
title: copy.title,
|
||||
body: copy.body,
|
||||
targetType: entityMeta.targetType,
|
||||
targetId: workflowInstance.entity_id,
|
||||
targetPath: entityMeta.detailPath,
|
||||
entityType: normalizedEntityType,
|
||||
entityId: workflowInstance.entity_id,
|
||||
actorId,
|
||||
});
|
||||
|
||||
emailTasks.push({
|
||||
type: notificationType,
|
||||
recipientEmployeeIds: recipients,
|
||||
subject: copy.title,
|
||||
body: copy.body,
|
||||
targetPath: entityMeta.detailPath,
|
||||
});
|
||||
}
|
||||
|
||||
async function createSkipHistoryRecords(tx, workflowInstanceId, actorId, ownerId, skippedSteps) {
|
||||
for (const skipped of skippedSteps) {
|
||||
await tx.workflowHistory.create({
|
||||
@@ -1069,21 +1266,18 @@ async function workflowRoutes(fastify) {
|
||||
assigneeRole: actionable.assigneeRole,
|
||||
});
|
||||
|
||||
await createNotifications(tx, {
|
||||
await queueTaskAssignedNotifications({
|
||||
tx,
|
||||
config: fastify.config,
|
||||
logger: fastify.log,
|
||||
actorId: request.employee.id,
|
||||
emailTasks,
|
||||
recipientEmployeeIds: recipients,
|
||||
type: "workflow.task_assigned",
|
||||
title: pendingCopy.title,
|
||||
body: pendingCopy.body,
|
||||
targetType: "workflow_task",
|
||||
targetId: nextTask.id,
|
||||
});
|
||||
|
||||
emailTasks.push({
|
||||
type: "workflow.task_assigned",
|
||||
recipientEmployeeIds: recipients,
|
||||
subject: pendingCopy.title,
|
||||
body: pendingCopy.body,
|
||||
targetPath: `/workflow/tasks/${nextTask.id}`,
|
||||
taskId: nextTask.id,
|
||||
entityType: workflowInstance.entity_type,
|
||||
entityId: workflowInstance.entity_id,
|
||||
});
|
||||
} catch (error) {
|
||||
fastify.log.error(
|
||||
@@ -1197,41 +1391,21 @@ async function workflowRoutes(fastify) {
|
||||
},
|
||||
});
|
||||
|
||||
if (workflowInstance.entity_type === "reimbursement") {
|
||||
try {
|
||||
const reimbursement = await loadReimbursementNotifyContext(tx, workflowInstance.entity_id);
|
||||
const entityMeta = getWorkflowEntityMeta(
|
||||
workflowInstance.entity_type,
|
||||
reimbursement,
|
||||
workflowInstance.entity_id,
|
||||
);
|
||||
const approvedCopy = buildApprovedCopy(entityMeta);
|
||||
const recipients = Array.from(
|
||||
new Set([reimbursement?.owner_id, reimbursement?.submitter_id].filter(Boolean)),
|
||||
);
|
||||
|
||||
await createNotifications(tx, {
|
||||
recipientEmployeeIds: recipients,
|
||||
type: "reimbursement.approved",
|
||||
title: approvedCopy.title,
|
||||
body: approvedCopy.body,
|
||||
targetType: entityMeta.targetType,
|
||||
targetId: workflowInstance.entity_id,
|
||||
});
|
||||
|
||||
emailTasks.push({
|
||||
type: "reimbursement.approved",
|
||||
recipientEmployeeIds: recipients,
|
||||
subject: approvedCopy.title,
|
||||
body: approvedCopy.body,
|
||||
targetPath: entityMeta.detailPath,
|
||||
});
|
||||
} catch (error) {
|
||||
fastify.log.error(
|
||||
{ err: error, taskId, reimbursementId: workflowInstance.entity_id },
|
||||
"Failed to create reimbursement approved notifications",
|
||||
);
|
||||
}
|
||||
try {
|
||||
await queueWorkflowOutcomeNotifications({
|
||||
tx,
|
||||
config: fastify.config,
|
||||
logger: fastify.log,
|
||||
actorId: request.employee.id,
|
||||
emailTasks,
|
||||
workflowInstance,
|
||||
outcome: 'approved',
|
||||
});
|
||||
} catch (error) {
|
||||
fastify.log.error(
|
||||
{ err: error, taskId, entityType: workflowInstance.entity_type, entityId: workflowInstance.entity_id },
|
||||
"Failed to create approval outcome notifications",
|
||||
);
|
||||
}
|
||||
|
||||
result = {
|
||||
@@ -1385,41 +1559,22 @@ async function workflowRoutes(fastify) {
|
||||
},
|
||||
});
|
||||
|
||||
if (workflowInstance.entity_type === "reimbursement") {
|
||||
try {
|
||||
const reimbursement = await loadReimbursementNotifyContext(tx, workflowInstance.entity_id);
|
||||
const entityMeta = getWorkflowEntityMeta(
|
||||
workflowInstance.entity_type,
|
||||
reimbursement,
|
||||
workflowInstance.entity_id,
|
||||
);
|
||||
const rejectedCopy = buildRejectedCopy(entityMeta, comment);
|
||||
const recipients = Array.from(
|
||||
new Set([reimbursement?.owner_id, reimbursement?.submitter_id].filter(Boolean)),
|
||||
);
|
||||
|
||||
await createNotifications(tx, {
|
||||
recipientEmployeeIds: recipients,
|
||||
type: "reimbursement.rejected",
|
||||
title: rejectedCopy.title,
|
||||
body: rejectedCopy.body,
|
||||
targetType: entityMeta.targetType,
|
||||
targetId: workflowInstance.entity_id,
|
||||
});
|
||||
|
||||
emailTasks.push({
|
||||
type: "reimbursement.rejected",
|
||||
recipientEmployeeIds: recipients,
|
||||
subject: rejectedCopy.title,
|
||||
body: rejectedCopy.body,
|
||||
targetPath: entityMeta.detailPath,
|
||||
});
|
||||
} catch (error) {
|
||||
fastify.log.error(
|
||||
{ err: error, taskId, reimbursementId: workflowInstance.entity_id },
|
||||
"Failed to create reimbursement rejected notifications",
|
||||
);
|
||||
}
|
||||
try {
|
||||
await queueWorkflowOutcomeNotifications({
|
||||
tx,
|
||||
config: fastify.config,
|
||||
logger: fastify.log,
|
||||
actorId: request.employee.id,
|
||||
emailTasks,
|
||||
workflowInstance,
|
||||
outcome: 'rejected',
|
||||
comment,
|
||||
});
|
||||
} catch (error) {
|
||||
fastify.log.error(
|
||||
{ err: error, taskId, entityType: workflowInstance.entity_type, entityId: workflowInstance.entity_id },
|
||||
"Failed to create rejection outcome notifications",
|
||||
);
|
||||
}
|
||||
|
||||
result = {
|
||||
@@ -1572,41 +1727,22 @@ async function workflowRoutes(fastify) {
|
||||
},
|
||||
});
|
||||
|
||||
if (workflowInstance.entity_type === "reimbursement") {
|
||||
try {
|
||||
const reimbursement = await loadReimbursementNotifyContext(tx, workflowInstance.entity_id);
|
||||
const entityMeta = getWorkflowEntityMeta(
|
||||
workflowInstance.entity_type,
|
||||
reimbursement,
|
||||
workflowInstance.entity_id,
|
||||
);
|
||||
const returnedCopy = buildReturnedCopy(entityMeta, comment);
|
||||
const recipients = Array.from(
|
||||
new Set([reimbursement?.owner_id, reimbursement?.submitter_id].filter(Boolean)),
|
||||
);
|
||||
|
||||
await createNotifications(tx, {
|
||||
recipientEmployeeIds: recipients,
|
||||
type: "reimbursement.returned",
|
||||
title: returnedCopy.title,
|
||||
body: returnedCopy.body,
|
||||
targetType: entityMeta.targetType,
|
||||
targetId: workflowInstance.entity_id,
|
||||
});
|
||||
|
||||
emailTasks.push({
|
||||
type: "reimbursement.returned",
|
||||
recipientEmployeeIds: recipients,
|
||||
subject: returnedCopy.title,
|
||||
body: returnedCopy.body,
|
||||
targetPath: entityMeta.detailPath,
|
||||
});
|
||||
} catch (error) {
|
||||
fastify.log.error(
|
||||
{ err: error, taskId, reimbursementId: workflowInstance.entity_id },
|
||||
"Failed to create reimbursement returned notifications",
|
||||
);
|
||||
}
|
||||
try {
|
||||
await queueWorkflowOutcomeNotifications({
|
||||
tx,
|
||||
config: fastify.config,
|
||||
logger: fastify.log,
|
||||
actorId: request.employee.id,
|
||||
emailTasks,
|
||||
workflowInstance,
|
||||
outcome: 'returned',
|
||||
comment,
|
||||
});
|
||||
} catch (error) {
|
||||
fastify.log.error(
|
||||
{ err: error, taskId, entityType: workflowInstance.entity_type, entityId: workflowInstance.entity_id },
|
||||
"Failed to create return outcome notifications",
|
||||
);
|
||||
}
|
||||
|
||||
result = {
|
||||
@@ -1719,21 +1855,18 @@ async function workflowRoutes(fastify) {
|
||||
workflowInstance.entity_id,
|
||||
);
|
||||
const pendingCopy = buildPendingTaskCopy(entityMeta, { transfer: true });
|
||||
await createNotifications(tx, {
|
||||
await queueTaskAssignedNotifications({
|
||||
tx,
|
||||
config: fastify.config,
|
||||
logger: fastify.log,
|
||||
actorId: request.employee.id,
|
||||
emailTasks,
|
||||
recipientEmployeeIds: [toApproverId],
|
||||
type: "workflow.task_assigned",
|
||||
title: pendingCopy.title,
|
||||
body: pendingCopy.body,
|
||||
targetType: "workflow_task",
|
||||
targetId: taskId,
|
||||
});
|
||||
|
||||
emailTasks.push({
|
||||
type: "workflow.task_assigned",
|
||||
recipientEmployeeIds: [toApproverId],
|
||||
subject: pendingCopy.title,
|
||||
body: pendingCopy.body,
|
||||
targetPath: `/workflow/tasks/${taskId}`,
|
||||
taskId,
|
||||
entityType: workflowInstance.entity_type,
|
||||
entityId: workflowInstance.entity_id,
|
||||
});
|
||||
} catch (error) {
|
||||
fastify.log.error(
|
||||
|
||||
在新工单中引用
屏蔽一个用户