Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
75953b0787 | ||
|
|
fc993a7761 | ||
|
|
503c4b52e9 | ||
|
|
5198830edb | ||
|
|
a281fa2a64 |
@@ -59,10 +59,7 @@ const escortRecordSetTool = tool(
|
||||
update.cancelStatus = cancelStatus;
|
||||
if (cancelStatus === "requested") update.cancelRequestedAt = new Date();
|
||||
if (cancelStatus === "cancelled") update.cancelledAt = new Date();
|
||||
// 与接口层一致:先关闭在途预付单,否则取消后旧 prepay 仍可完成支付
|
||||
await HandlerPayment.closeOpenPayRecords(orderId);
|
||||
const fresh = await DBModel.EscortRecord.findById(orderId);
|
||||
effectivePay = fresh?.payment || effectivePay;
|
||||
// 关单有外部副作用,延后到全部入参校验通过之后执行。
|
||||
}
|
||||
|
||||
if (status !== undefined && status !== record.status) {
|
||||
@@ -175,6 +172,30 @@ const escortRecordSetTool = tool(
|
||||
update["payment.status"] = "refunding";
|
||||
}
|
||||
|
||||
// 所有确定性校验通过后才关闭在途预付单。关单期间若支付成功,
|
||||
// 以刷新后的支付状态复核金额/实收输入,并把正式取消收敛到退款中。
|
||||
if (update.cancelStatus) {
|
||||
await HandlerPayment.closeOpenPayRecords(orderId);
|
||||
const fresh = await DBModel.EscortRecord.findById(orderId);
|
||||
effectivePay = fresh?.payment || effectivePay;
|
||||
const refreshedPayStatus = effectivePay.status || "unpaid";
|
||||
if (["partial", "paid", "refunding", "refunded"].includes(refreshedPayStatus)) {
|
||||
const invalidStatus = payment?.status !== undefined
|
||||
&& payment.status !== refreshedPayStatus
|
||||
&& !(update.cancelStatus === "cancelled" && payment.status === "refunding");
|
||||
const invalidPaidFee = payment?.paidFee !== undefined
|
||||
&& Number(payment.paidFee) !== (Number(effectivePay.paidFee) || 0);
|
||||
const invalidTotalFee = payment?.totalFee !== undefined
|
||||
&& Number(payment.totalFee) !== (Number(effectivePay.totalFee) || 0);
|
||||
if (invalidStatus || invalidPaidFee || invalidTotalFee) {
|
||||
return { success: false, error: "Record payment state changed while updating. Re-query and retry." };
|
||||
}
|
||||
}
|
||||
if (update.cancelStatus === "cancelled" && ["partial", "paid"].includes(refreshedPayStatus)) {
|
||||
update["payment.status"] = "refunding";
|
||||
}
|
||||
}
|
||||
|
||||
update["meta.updatetime"] = Date.now();
|
||||
|
||||
// 以读取时的状态作为更新条件,避免并发下跳过中间状态;同时排除已软删除的订单
|
||||
|
||||
@@ -766,6 +766,11 @@ class HandlerEscortRecord {
|
||||
const isUserApp = requestAppId === "wxapp-escort";
|
||||
const role = isUserApp ? null : await resolveEscortAdminRole(user);
|
||||
const isOwner = String(existing.userId) === String(user?._id);
|
||||
const isAssignedAttendant = role === "attendant"
|
||||
&& String(existing.attendant?.id || "") === String(user?._id || "");
|
||||
// 陪诊员身份只赋予其名下订单的管理权。订单主人即使同时是在职陪诊员,
|
||||
// 对未分配给自己的订单也只能走用户取消流程,不能自行确认或完成服务。
|
||||
const canManageOrder = role === "admin" || isAssignedAttendant;
|
||||
const paymentStatus = existing.payment?.status || "unpaid";
|
||||
const cancelReason = String(reason || "").trim().slice(0, 200);
|
||||
let effectivePaymentStatus = paymentStatus;
|
||||
@@ -780,7 +785,7 @@ class HandlerEscortRecord {
|
||||
return ResponseUtil.badRequest(ctx, "订单已取消,不能再次操作");
|
||||
}
|
||||
|
||||
if (role !== "admin" && role !== "attendant") {
|
||||
if (!canManageOrder) {
|
||||
// 用户端只能取消:未确认订单直接取消(未收款删除、已收款退款);已确认订单提交取消申请
|
||||
if (status !== "cancelled" || !isOwner) {
|
||||
return ResponseUtil.forbidden(ctx, "不支持的状态变更");
|
||||
|
||||
+3
-1
@@ -650,13 +650,15 @@ class HandlerPayment {
|
||||
}).sort({ createdAt: -1 });
|
||||
}
|
||||
|
||||
// 已有在途流水但金额与当前订单不一致(如被绕过改价):先关单再换新号
|
||||
// 已有在途流水但金额与当前订单不一致(如被绕过改价):先关单再换新号。
|
||||
// 关单失败时旧 prepay 可能仍可支付,必须停止本次发起,不能同时放出两张有效订单。
|
||||
if (payment && Number(payment.totalFee) !== totalFeeFen) {
|
||||
try {
|
||||
await wxpay.closeOrder(payment.outTradeNo);
|
||||
await HandlerPayment.setPaymentStatus(payment._id, "closed", { closedAt: new Date() });
|
||||
} catch (err) {
|
||||
await HandlerPayment.markPaymentError(payment._id, err, "close");
|
||||
return ResponseUtil.error(ctx, "原支付订单正在确认关闭结果,请稍后重试", null, 409);
|
||||
}
|
||||
payment = null;
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ import { createEscortRecordQueryTool } from "../agent/infra/tools/db/user/escort
|
||||
import { escortRecordQueryTool } from "../agent/infra/tools/db/admin/escort_record_query.js";
|
||||
import { createAttendantEscortRecordQueryTool } from "../agent/infra/tools/db/attendant/escort_record_query.js";
|
||||
import { buildAttendantEscortRecordSetTool } from "../agent/infra/tools/db/attendant/escort_record_set.js";
|
||||
import { escortRecordSetTool } from "../agent/infra/tools/db/admin/escort_record_set.js";
|
||||
import { HandlerPayment } from "../handler/payment.js";
|
||||
import { resolveEscortAdminRole } from "../utils/permissions.js";
|
||||
|
||||
const OWNER_ID = "64b000000000000000000001";
|
||||
@@ -409,3 +411,33 @@ test("历史管理员 role 缺失仍按管理员放行", async () => {
|
||||
assert.equal(legacy, "admin");
|
||||
});
|
||||
});
|
||||
|
||||
test("管理端订单工具拒绝组合状态更新前不会关闭在途支付", async () => {
|
||||
const previousClose = HandlerPayment.closeOpenPayRecords;
|
||||
let closeCalls = 0;
|
||||
HandlerPayment.closeOpenPayRecords = async () => {
|
||||
closeCalls += 1;
|
||||
return { closed: 0, paid: 0, unconfirmed: 0 };
|
||||
};
|
||||
const record = {
|
||||
_id: OWNER_ID,
|
||||
status: "confirmed",
|
||||
cancelStatus: "none",
|
||||
payment: { status: "unpaid", totalFee: 500, paidFee: 0 },
|
||||
};
|
||||
|
||||
try {
|
||||
await withModel("EscortRecord", { findById: async () => record }, async () => {
|
||||
const result = parseResult(await escortRecordSetTool.invoke({
|
||||
orderId: OWNER_ID,
|
||||
status: "in_progress",
|
||||
cancelStatus: "cancelled",
|
||||
}));
|
||||
assert.equal(result.success, false);
|
||||
assert.match(result.error, /Do not update service status and cancel status/);
|
||||
assert.equal(closeCalls, 0);
|
||||
});
|
||||
} finally {
|
||||
HandlerPayment.closeOpenPayRecords = previousClose;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -183,6 +183,41 @@ test("切换支付渠道时嵌套 payment 摊平后不会产生路径冲突", as
|
||||
);
|
||||
});
|
||||
|
||||
test("在职陪诊员不能确认未分配给自己的本人订单", async () => {
|
||||
const record = buildRecord({ status: "pending" });
|
||||
let statusUpdates = 0;
|
||||
const recordStub = {
|
||||
...createRecordStub(record),
|
||||
updateRecordStatus: async () => {
|
||||
statusUpdates += 1;
|
||||
return record;
|
||||
},
|
||||
};
|
||||
const employeeStub = {
|
||||
findOne: () => ({
|
||||
select: () => ({
|
||||
lean: async () => ({ role: "attendant" }),
|
||||
}),
|
||||
}),
|
||||
};
|
||||
|
||||
await withModel("EscortRecord", recordStub, () =>
|
||||
withModel("Employee", employeeStub, async () => {
|
||||
const ctx = createCtx({
|
||||
user: {
|
||||
_id: OWNER_ID,
|
||||
app: { "wxapp-escort-admin": { role: ["user"] } },
|
||||
},
|
||||
body: { status: "confirmed" },
|
||||
});
|
||||
await new HandlerEscortRecord().updateStatus(ctx);
|
||||
|
||||
assert.equal(ctx.body.code, 403);
|
||||
assert.equal(statusUpdates, 0);
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
test("健康档案更新把嵌套对象摊平成点号路径", () => {
|
||||
const picked = HandlerHealthProfile.pickFields(
|
||||
{
|
||||
|
||||
+36
-1
@@ -11,7 +11,8 @@ import {
|
||||
verifyMessageSignature,
|
||||
decryptAesGcm,
|
||||
} from "../utils/wechatpay.js";
|
||||
import { createOrderQueryCache } from "../handler/payment.js";
|
||||
import { createOrderQueryCache, HandlerPayment } from "../handler/payment.js";
|
||||
import { DBModel } from "../models/index.js";
|
||||
|
||||
test("元转分按小数点拆位,不经过浮点", () => {
|
||||
assert.equal(yuanToFen("500.00"), 50000);
|
||||
@@ -178,3 +179,37 @@ test("force 可绕过轮询缓存", async () => {
|
||||
await query("T4", { force: true });
|
||||
assert.equal(calls, 2);
|
||||
});
|
||||
|
||||
test("退款成功后的迟到处理中结果不会回退支付与订单状态", async () => {
|
||||
const previous = {
|
||||
RefundRecord: DBModel.RefundRecord,
|
||||
PaymentRecord: DBModel.PaymentRecord,
|
||||
EscortRecord: DBModel.EscortRecord,
|
||||
};
|
||||
let refundFilter = null;
|
||||
let downstreamWrites = 0;
|
||||
DBModel.RefundRecord = {
|
||||
updateOne: async (filter) => {
|
||||
refundFilter = filter;
|
||||
return { matchedCount: 0 };
|
||||
},
|
||||
};
|
||||
DBModel.PaymentRecord = {
|
||||
updateOne: async () => { downstreamWrites += 1; },
|
||||
};
|
||||
DBModel.EscortRecord = {
|
||||
updateOne: async () => { downstreamWrites += 1; },
|
||||
};
|
||||
|
||||
try {
|
||||
const status = await HandlerPayment.applyRefundResult(
|
||||
{ _id: "r1", paymentId: "p1", recordId: "o1", status: "success" },
|
||||
{ status: "PROCESSING" }
|
||||
);
|
||||
assert.equal(status, "success");
|
||||
assert.deepEqual(refundFilter.status, { $ne: "success" });
|
||||
assert.equal(downstreamWrites, 0);
|
||||
} finally {
|
||||
Object.assign(DBModel, previous);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user