来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
117 lines
4.6 KiB
TypeScript
117 lines
4.6 KiB
TypeScript
import { notifyRoleUsers, notifyUser } from './notificationService';
|
||
import { listSupplierUsersForCodes } from './bidAwardService';
|
||
|
||
/** 内部角色:验收不合格需要运营/管理员/采购员跟进 */
|
||
const TARGET_ROLES = ['operator', 'admin', 'super_admin', 'purchaser'];
|
||
|
||
/**
|
||
* # 缺口修复(2026-09-16):验收不合格退回时,**供应商账号收不到任何通知**
|
||
*
|
||
* 修复前:`notifyInspectionRejected` 只发内部 4 个角色(operator/admin/super_admin/purchaser)。
|
||
* 退回后虽然订单回退 40→30、备货被 `resetSupplierPreparationForResupply` 打回「重新供货」,
|
||
* 但**供应商侧只能靠自己登录小程序看到备货状态变了** —— 没有主动推送;
|
||
* 供应商 webhook 也只支持 `preparations.allocated` 一个事件(见 supplierPrepWebhookService),
|
||
* 所以「货被退了却不知道」是真实的流程断点。
|
||
*
|
||
* 修复方式沿用本仓已有的「按供应商编码查绑定账号」模式
|
||
* (见 `mainlineEventNotificationService.notifyPreparationsCreated`):
|
||
* `supplier_code` → `listSupplierUsersForCodes` → `notifyUser` 逐个推送。
|
||
*
|
||
* 约定:
|
||
* - **只对供应商供货(`source_type !== 'purchase'`)且带 `supplier_code` 的行发**;
|
||
* 自采(purchase)没有供应商,跳过。
|
||
* - 通知类型用独立键 `inspection_rejected_to_supplier`,与内部提醒分开,
|
||
* 便于按角色分流展示,也便于分别统计。
|
||
* - 发送失败只记日志、**不阻断验收主流程**(与内部提醒一致)。
|
||
*/
|
||
export async function notifySupplierInspectionRejected(input: {
|
||
inspectionId: string;
|
||
branchCode: string | null;
|
||
orderCode?: string | null;
|
||
skuCode?: string | null;
|
||
itemName?: string | null;
|
||
sourceType?: string | null;
|
||
supplierCode?: string | null;
|
||
reason?: string;
|
||
}): Promise<{ sent: number; skipped: boolean }> {
|
||
const supplierCode = String(input.supplierCode || '').trim();
|
||
// 自采无供应商;没有 supplier_code 也无从寻址
|
||
if (!supplierCode || String(input.sourceType || '') === 'purchase') {
|
||
return { sent: 0, skipped: true };
|
||
}
|
||
|
||
const itemName = String(input.itemName || input.skuCode || '商品');
|
||
const title = `退货通知:${itemName}`;
|
||
const content =
|
||
`订单 ${input.orderCode || '-'},${itemName} 验收不合格,已退回,请尽快重新供货` +
|
||
`${input.reason ? `(原因:${input.reason})` : ''}` +
|
||
` [验收记录:${input.inspectionId}]`;
|
||
|
||
let userIds: string[] = [];
|
||
try {
|
||
const rows = await listSupplierUsersForCodes([supplierCode], input.branchCode);
|
||
userIds = [...new Set(rows.map((r) => r.user_id).filter(Boolean))];
|
||
} catch (error) {
|
||
console.error(`[inspection] 解析供应商账号失败 supplier=${supplierCode}:`, error);
|
||
return { sent: 0, skipped: false };
|
||
}
|
||
if (!userIds.length) {
|
||
// 供应商未开通账号属正常情况(不是错误),但留痕便于排查「为什么没收到」
|
||
console.warn(
|
||
`[inspection] 供应商 ${supplierCode} 无可用账号,退货通知未送出(order=${input.orderCode || '-'})`
|
||
);
|
||
return { sent: 0, skipped: false };
|
||
}
|
||
|
||
let sent = 0;
|
||
for (const userId of userIds) {
|
||
try {
|
||
await notifyUser({
|
||
userId,
|
||
notifyType: 'inspection_rejected_to_supplier',
|
||
title,
|
||
content,
|
||
relatedOrderCode: input.orderCode || null,
|
||
branchCode: input.branchCode,
|
||
});
|
||
sent += 1;
|
||
} catch (error) {
|
||
console.error(`[inspection] 供应商退货通知发送失败 user=${userId}:`, error);
|
||
}
|
||
}
|
||
return { sent, skipped: false };
|
||
}
|
||
|
||
export async function notifyInspectionRejected(input: {
|
||
inspectionId: string;
|
||
branchCode: string | null;
|
||
orderCode?: string | null;
|
||
skuCode?: string | null;
|
||
itemName?: string | null;
|
||
sourceType?: string | null;
|
||
supplierCode?: string | null;
|
||
reason?: string;
|
||
}) {
|
||
const itemName = String(input.itemName || input.skuCode || '商品');
|
||
const title = `验收不合格:${itemName}`;
|
||
const content =
|
||
`订单 ${input.orderCode || '-'},${itemName}` +
|
||
`(${input.sourceType === 'purchase' ? '自采' : input.supplierCode || '供应商供货'})` +
|
||
`验收不合格${input.reason ? `:${input.reason}` : ',请及时处理'}` +
|
||
` [验收记录:${input.inspectionId}]`;
|
||
|
||
await notifyRoleUsers({
|
||
roles: TARGET_ROLES,
|
||
notifyType: 'inspection_rejected',
|
||
title,
|
||
content,
|
||
relatedOrderCode: input.orderCode || null,
|
||
branchCode: input.branchCode,
|
||
skipIfRecent: true,
|
||
dedupWithinHours: 1,
|
||
});
|
||
|
||
// 除内部提醒外,再推给对应供应商账号(缺口修复,见上方函数头注释)
|
||
await notifySupplierInspectionRejected(input);
|
||
}
|