完善报销通知与付款进度(08-01-reimbursement-notification-progress-clarity)
这个提交包含在:
@@ -11,6 +11,7 @@ const { parseDateOnlyString } = require("../utils/date");
|
||||
const { FEATURE_FLAG_KEYS, evaluateFeatureFlag } = require("../services/featureFlags");
|
||||
const { createNotificationsWithRecords } = require("../services/notifications");
|
||||
const { NOTIFICATION_TYPES } = require("../constants/notificationTypes");
|
||||
const { resolvePermissionsForRoles, hasPermission } = require("../constants/permissions");
|
||||
const { sendNotificationEmailSafe } = require("../services/email");
|
||||
const { flushNotificationDispatchTasks } = require("../services/notificationDispatcher");
|
||||
const { enqueueReimbursementJournalExport } = require("../services/integrations/erpJournal");
|
||||
@@ -247,6 +248,118 @@ function ensureBatchReviewPermission(fastify, request) {
|
||||
}
|
||||
}
|
||||
|
||||
function roleCodesOf(employee) {
|
||||
return (employee?.roles ?? []).map((role) => role.role_code).filter(Boolean);
|
||||
}
|
||||
|
||||
function employeeHasRolePermission(employee, permissionCode) {
|
||||
return hasPermission(resolvePermissionsForRoles(roleCodesOf(employee)), permissionCode);
|
||||
}
|
||||
|
||||
async function resolvePaymentReviewRecipientIds(tx, makerId) {
|
||||
const candidates = await tx.employee.findMany({
|
||||
where: {
|
||||
status: "active",
|
||||
id: { not: makerId },
|
||||
roles: { some: { role_code: { in: ["manager", "ceo"] } } },
|
||||
},
|
||||
orderBy: [{ employee_no: "asc" }, { id: "asc" }],
|
||||
select: {
|
||||
id: true,
|
||||
roles: { select: { role_code: true } },
|
||||
},
|
||||
});
|
||||
|
||||
return candidates
|
||||
.filter((employee) => employeeHasRolePermission(employee, "payment.batch.review"))
|
||||
.map((employee) => employee.id);
|
||||
}
|
||||
|
||||
async function resolvePaymentExecutionRecipientIds(tx, { makerId, reviewerId }) {
|
||||
const candidates = await tx.employee.findMany({
|
||||
where: {
|
||||
status: "active",
|
||||
id: { not: reviewerId },
|
||||
OR: [
|
||||
{ id: makerId },
|
||||
{ roles: { some: { role_code: { in: ["finance", "finance_mgr"] } } } },
|
||||
],
|
||||
},
|
||||
orderBy: [{ employee_no: "asc" }, { id: "asc" }],
|
||||
select: {
|
||||
id: true,
|
||||
roles: { select: { role_code: true } },
|
||||
},
|
||||
});
|
||||
const eligible = candidates.filter((employee) =>
|
||||
employee.id !== reviewerId && employeeHasRolePermission(employee, "payment.batch.manage"),
|
||||
);
|
||||
const activeMaker = eligible.find((employee) => employee.id === makerId);
|
||||
if (activeMaker) {
|
||||
return [activeMaker.id];
|
||||
}
|
||||
|
||||
return eligible
|
||||
.filter((employee) => roleCodesOf(employee).some((roleCode) => ["finance", "finance_mgr"].includes(roleCode)))
|
||||
.map((employee) => employee.id);
|
||||
}
|
||||
|
||||
async function createPaymentResponsibilityNotification(tx, {
|
||||
recipientEmployeeIds,
|
||||
type,
|
||||
title,
|
||||
body,
|
||||
batchId,
|
||||
actorId,
|
||||
}) {
|
||||
const notifications = await createNotificationsWithRecords(tx, {
|
||||
recipientEmployeeIds,
|
||||
type,
|
||||
title,
|
||||
body,
|
||||
targetType: "payment_batch",
|
||||
targetId: batchId,
|
||||
});
|
||||
|
||||
return {
|
||||
notifications,
|
||||
type,
|
||||
title,
|
||||
body,
|
||||
targetType: "payment_batch",
|
||||
targetId: batchId,
|
||||
targetPath: `/payment-batches?id=${encodeURIComponent(batchId)}`,
|
||||
actorId,
|
||||
};
|
||||
}
|
||||
|
||||
async function dispatchPaymentResponsibilityNotification(fastify, request, task) {
|
||||
if (!task?.notifications?.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await flushNotificationDispatchTasks({
|
||||
prisma: fastify.prisma,
|
||||
config: fastify.config,
|
||||
logger: fastify.log,
|
||||
requestId: request.id,
|
||||
tasks: [task],
|
||||
});
|
||||
} catch (error) {
|
||||
fastify.log.error(
|
||||
{
|
||||
err: error,
|
||||
requestId: request.id,
|
||||
notificationType: task.type,
|
||||
targetType: task.targetType,
|
||||
targetId: task.targetId,
|
||||
},
|
||||
"Failed to dispatch payment responsibility notification",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function buildBatchNo() {
|
||||
const now = new Date();
|
||||
const date = now.toISOString().slice(0, 10).replace(/-/g, "");
|
||||
@@ -1057,7 +1170,7 @@ async function paymentBatchesRoutes(fastify) {
|
||||
}
|
||||
const entityIds = Array.from(new Set(body.entityIds)).sort();
|
||||
|
||||
const created = await fastify.prisma.$transaction(async (tx) => {
|
||||
const { batch: created, notificationTask } = await fastify.prisma.$transaction(async (tx) => {
|
||||
await lockPaymentEntities(tx, body.entityType, entityIds);
|
||||
const snapshots = await loadEntitySnapshots(tx, body.entityType, entityIds);
|
||||
if (snapshots.length !== entityIds.length) {
|
||||
@@ -1130,9 +1243,24 @@ async function paymentBatchesRoutes(fastify) {
|
||||
}),
|
||||
});
|
||||
|
||||
return loadBatchWithItems(tx, batch.id);
|
||||
const recipientEmployeeIds = await resolvePaymentReviewRecipientIds(tx, request.employee.id);
|
||||
const nextNotificationTask = await createPaymentResponsibilityNotification(tx, {
|
||||
recipientEmployeeIds,
|
||||
type: NOTIFICATION_TYPES.paymentBatchReviewRequested,
|
||||
title: "待付款复核",
|
||||
body: "付款安排已创建,请完成付款复核。",
|
||||
batchId: batch.id,
|
||||
actorId: request.employee.id,
|
||||
});
|
||||
|
||||
return {
|
||||
batch: await loadBatchWithItems(tx, batch.id),
|
||||
notificationTask: nextNotificationTask,
|
||||
};
|
||||
});
|
||||
|
||||
await dispatchPaymentResponsibilityNotification(fastify, request, notificationTask);
|
||||
|
||||
return reply.code(201).send({ requestId: request.id, data: { item: mapBatch(created, []) } });
|
||||
});
|
||||
|
||||
@@ -1590,7 +1718,7 @@ async function paymentBatchesRoutes(fastify) {
|
||||
const batchId = parseId(request.params?.id);
|
||||
const body = parseBody(REVIEW_SCHEMA, request.body);
|
||||
|
||||
const result = await fastify.prisma.$transaction(async (tx) => {
|
||||
const { batch: result, notificationTask } = await fastify.prisma.$transaction(async (tx) => {
|
||||
const batch = await lockBatchWithItems(tx, batchId);
|
||||
await ensureBatchSourceDepartmentsEnabled(fastify, tx, batch);
|
||||
const batchRisk = summarizeBatchRisk(batch);
|
||||
@@ -1659,9 +1787,27 @@ async function paymentBatchesRoutes(fastify) {
|
||||
}),
|
||||
});
|
||||
|
||||
return reviewed;
|
||||
const recipientEmployeeIds = await resolvePaymentExecutionRecipientIds(tx, {
|
||||
makerId: batch.created_by,
|
||||
reviewerId: request.employee.id,
|
||||
});
|
||||
const nextNotificationTask = await createPaymentResponsibilityNotification(tx, {
|
||||
recipientEmployeeIds,
|
||||
type: NOTIFICATION_TYPES.paymentBatchExecutionRequested,
|
||||
title: "待完成付款",
|
||||
body: "付款复核已通过,请完成付款并登记回单。",
|
||||
batchId,
|
||||
actorId: request.employee.id,
|
||||
});
|
||||
|
||||
return {
|
||||
batch: reviewed,
|
||||
notificationTask: nextNotificationTask,
|
||||
};
|
||||
});
|
||||
|
||||
await dispatchPaymentResponsibilityNotification(fastify, request, notificationTask);
|
||||
|
||||
return sendData(reply, request, { item: mapBatch(result, []) });
|
||||
});
|
||||
|
||||
|
||||
@@ -333,8 +333,8 @@ const PROCESSING_STAGE_DEFINITIONS = Object.freeze([
|
||||
{ key: "submitted", label: "申请提交" },
|
||||
{ key: "manager_approval", label: "主管审批" },
|
||||
{ key: "finance_review", label: "财务复核" },
|
||||
{ key: "payment_batch_review", label: "付款批次复核" },
|
||||
{ key: "payment_and_receipt", label: "付款与回单" },
|
||||
{ key: "payment_batch_review", label: "付款复核" },
|
||||
{ key: "payment_and_receipt", label: "付款完成" },
|
||||
]);
|
||||
|
||||
function isoDateTimeOrNull(value) {
|
||||
@@ -511,31 +511,34 @@ function buildProcessingProgress({ reimbursement, workflowInstance, paymentBatch
|
||||
|
||||
if (!batch) {
|
||||
Object.assign(batchReviewStage, reimbursement.status === "approved"
|
||||
? { state: "current", result: "待建批次", detail: "待财务创建付款批次" }
|
||||
: { detail: "待财务创建付款批次" });
|
||||
paymentStage.detail = "待付款批次复核后处理";
|
||||
? { state: "current", result: "待安排", detail: "待财务安排付款" }
|
||||
: { detail: "待财务安排付款" });
|
||||
paymentStage.detail = "待付款复核后由财务登记,非审批环节";
|
||||
} else if (batch.status === "draft") {
|
||||
Object.assign(batchReviewStage, {
|
||||
state: "current",
|
||||
result: "待复核",
|
||||
detail: "所在付款批次待非制单人复核",
|
||||
detail: "待非制单人复核付款",
|
||||
targetHref: batchTargetHref,
|
||||
});
|
||||
Object.assign(paymentStage, {
|
||||
detail: "待付款复核通过后由财务登记,非审批环节",
|
||||
targetHref: batchTargetHref,
|
||||
});
|
||||
Object.assign(paymentStage, { detail: "待所在付款批次复核通过", targetHref: batchTargetHref });
|
||||
} else if (["reviewed", "executed", "failed"].includes(batch.status)) {
|
||||
Object.assign(batchReviewStage, {
|
||||
state: "completed",
|
||||
actorName: paymentActorNames.get(batch.reviewed_by) ?? null,
|
||||
occurredAt: isoDateTimeOrNull(batch.reviewed_at),
|
||||
result: "通过",
|
||||
detail: batch.status === "reviewed" ? "所在付款批次已复核,待执行付款" : null,
|
||||
detail: batch.status === "reviewed" ? "付款复核已通过" : null,
|
||||
targetHref: batchTargetHref,
|
||||
});
|
||||
if (batch.status === "reviewed") {
|
||||
Object.assign(paymentStage, {
|
||||
state: "current",
|
||||
result: "待付款",
|
||||
detail: "所在付款批次已复核,待执行付款",
|
||||
detail: "待财务完成付款并登记回单,非审批环节",
|
||||
targetHref: batchTargetHref,
|
||||
});
|
||||
} else if (batch.status === "failed") {
|
||||
@@ -544,7 +547,7 @@ function buildProcessingProgress({ reimbursement, workflowInstance, paymentBatch
|
||||
actorName: paymentActorNames.get(batch.executed_by) ?? null,
|
||||
occurredAt: isoDateTimeOrNull(batch.executed_at),
|
||||
result: "付款失败",
|
||||
detail: "所在付款批次执行失败,待财务重新处理",
|
||||
detail: "付款失败,待财务重新处理,非审批环节",
|
||||
targetHref: batchTargetHref,
|
||||
});
|
||||
}
|
||||
@@ -555,11 +558,11 @@ function buildProcessingProgress({ reimbursement, workflowInstance, paymentBatch
|
||||
occurredAt: isoDateTimeOrNull(batch.cancelled_at),
|
||||
result: "批次已取消",
|
||||
comment: batch.cancelled_reason ?? null,
|
||||
detail: "所在付款批次已取消,待财务重新创建",
|
||||
detail: "付款安排已取消,待财务重新处理",
|
||||
targetHref: batchTargetHref,
|
||||
});
|
||||
Object.assign(paymentStage, {
|
||||
detail: "待财务重新创建付款批次",
|
||||
detail: "待财务重新安排付款,非审批环节",
|
||||
targetHref: batchTargetHref,
|
||||
});
|
||||
}
|
||||
@@ -571,7 +574,7 @@ function buildProcessingProgress({ reimbursement, workflowInstance, paymentBatch
|
||||
actorName: reimbursement.paid_by_employee?.name ?? paymentActorNames.get(batch?.executed_by) ?? null,
|
||||
occurredAt: isoDateTimeOrNull(reimbursement.paid_at ?? batch?.executed_at),
|
||||
result: "已付款",
|
||||
detail: receiptCount > 0 ? "付款与回单已留存" : "付款已完成,回单待留存",
|
||||
detail: receiptCount > 0 ? "付款和回单已留存" : "付款已完成,回单待留存",
|
||||
targetHref: batchTargetHref,
|
||||
});
|
||||
}
|
||||
|
||||
在新工单中引用
屏蔽一个用户