orderHydration.helpers.test.mjs 7.65 KB
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { buildBoutiqueOrderHydration } from "../services/orderHydration.helpers.js";

const orderTypeList = [
	{ text: "正常订单", value: 1 },
	{ text: "打折策略", value: 2 },
	{ text: "内部结算", value: 6 },
];

function buildWithoutMutation(input) {
	const snapshot = structuredClone(input);
	const result = buildBoutiqueOrderHydration(input);
	assert.deepEqual(input, snapshot);
	return result;
}

describe("orderHydration.helpers", () => {
	it("IT-WO-HYDRATION-001: a new order keeps the previously selected scene and applies customer defaults", () => {
		const result = buildWithoutMutation({
			options: {
				merchantId: "merchant-new",
				merchantName: "新门店",
				employeeName: "销售顾问",
				customer: {
					customerName: "张三",
					customerPhone: "13800138000",
				},
			},
			previousOrderObj: {
				baseSceneCode: "scene-selected",
				baseSceneName: "已选场景",
				sceneType: 1,
			},
			orderTypeList,
			isErrorOrder: false,
			associateOrderType: false,
			merchantBrands: [],
		});

		assert.deepEqual(result, {
			orderObj: {
				merchantId: "merchant-new",
				merchantName: "新门店",
				employeeName: "销售顾问",
				customer: {
					customerName: "张三",
					customerPhone: "13800138000",
					customerSex: 1,
					experienceFlag: "N",
					salesType: 0,
				},
				baseSceneCode: "scene-selected",
				baseSceneName: "已选场景",
				sceneType: 1,
				djOrderType: 1,
			},
			patch: {
				currentWordNumber: 0,
				orderTypeName: "正常订单",
				customerTagsText: "",
				showCustomerTags: true,
				showActualDeliveryDate: false,
				first: true,
				flag: true,
				isErrorOrder: false,
				associateOrderType: false,
				isAppointment: false,
			},
			effects: [],
		});
	});

	it("IT-WO-HYDRATION-002: an edited order cleans null fields, converts numeric fields and echoes its coupon", () => {
		const result = buildWithoutMutation({
			options: {
				id: "order-id-1",
				orderCode: "DJ-20260908-1",
				merchantId: "merchant-edit",
				orderType: "2",
				selectPayType: "1",
				payeeType: "0",
				orderSource: "3",
				discountType: "3",
				remark: "编辑备注",
				ignoredRootField: null,
				customer: {
					customerName: "李四",
					customerPhone: "13900139000",
					customerSex: 2,
					experienceFlag: "Y",
					salesType: 1,
					customerTags: "1,3",
					ignoredCustomerField: null,
				},
				customerCoupon: {
					id: "coupon-record-1",
					couponName: "精品满减券",
					customerCouponCode: "COUPON-88-SUFFIX",
					ignoredCouponField: null,
				},
			},
			previousOrderObj: {},
			orderTypeList,
			isErrorOrder: false,
			associateOrderType: false,
			merchantBrands: [],
		});

		assert.deepEqual(result, {
			orderObj: {
				id: "order-id-1",
				orderCode: "DJ-20260908-1",
				merchantId: "merchant-edit",
				orderType: "2",
				selectPayType: 1,
				payeeType: 0,
				orderSource: 3,
				discountType: 3,
				remark: "编辑备注",
				customer: {
					customerName: "李四",
					customerPhone: "13900139000",
					customerSex: 2,
					experienceFlag: "Y",
					salesType: 1,
					customerTags: "1,3",
				},
				customerCoupon: {
					id: "coupon-record-1",
					couponName: "精品满减券",
					customerCouponCode: "COUPON-88-SUFFIX",
				},
				djOrderType: 2,
				couponName: "精品满减券",
				customerCouponCode: "COUPON",
				customerCouponId: "coupon-record-1",
			},
			patch: {
				currentWordNumber: 4,
				orderTypeName: "打折策略",
				customerTagsText: "已触客/已交付引荐",
				showCustomerTags: true,
				showActualDeliveryDate: false,
				first: true,
				flag: true,
				isErrorOrder: false,
				associateOrderType: false,
				isAppointment: false,
			},
			effects: [],
		});
	});

	it("IT-WO-HYDRATION-003: a reorder preserves its explicit reorder markers and numeric payment snapshot", () => {
		const result = buildWithoutMutation({
			options: {
				againOrder: true,
				resetOrder: true,
				merchantId: "merchant-reorder",
				orderSource: "4",
				selectPayType: "3",
				payeeType: "0",
				djOrderType: "6",
				discountType: "3",
				customer: {
					customerName: "王五",
					customerPhone: "13700137000",
					customerSex: 1,
					experienceFlag: "N",
					salesType: 2,
				},
			},
			previousOrderObj: {},
			orderTypeList,
			isErrorOrder: false,
			associateOrderType: false,
			merchantBrands: [],
		});

		assert.deepEqual(result, {
			orderObj: {
				againOrder: true,
				resetOrder: true,
				merchantId: "merchant-reorder",
				orderSource: 4,
				selectPayType: 3,
				payeeType: 0,
				djOrderType: 6,
				discountType: 3,
				customer: {
					customerName: "王五",
					customerPhone: "13700137000",
					customerSex: 1,
					experienceFlag: "N",
					salesType: 2,
				},
			},
			patch: {
				currentWordNumber: 0,
				orderTypeName: "内部结算",
				customerTagsText: "",
				showCustomerTags: false,
				showActualDeliveryDate: false,
				first: true,
				flag: true,
				isErrorOrder: false,
				associateOrderType: false,
				isAppointment: false,
			},
			effects: [],
		});
	});

	it("IT-WO-HYDRATION-004: a correction forces DJ collection and describes both page-only effects", () => {
		const result = buildWithoutMutation({
			options: {
				associateOrderCode: "WRONG-ORDER-1",
				merchantId: "merchant-correction",
				selectPayType: 2,
				payeeType: 1,
				customer: {
					customerName: "赵六",
					customerPhone: "13600136000",
					customerSex: 1,
					experienceFlag: "N",
					salesType: 0,
				},
			},
			previousOrderObj: {},
			orderTypeList,
			isErrorOrder: false,
			associateOrderType: false,
			merchantBrands: [],
		});

		assert.deepEqual(result, {
			orderObj: {
				associateOrderCode: "WRONG-ORDER-1",
				merchantId: "merchant-correction",
				selectPayType: 5,
				payeeType: 0,
				customer: {
					customerName: "赵六",
					customerPhone: "13600136000",
					customerSex: 1,
					experienceFlag: "N",
					salesType: 0,
				},
				djOrderType: 1,
			},
			patch: {
				currentWordNumber: 0,
				orderTypeName: "正常订单",
				customerTagsText: "",
				showCustomerTags: true,
				showActualDeliveryDate: false,
				first: true,
				flag: true,
				isErrorOrder: true,
				associateOrderType: false,
				isAppointment: false,
			},
			effects: [
				{ type: "setNavigationBarTitle", title: "修正订单" },
				{
					type: "enableAlertBeforeUnload",
					title: "提示",
					message: "您需完成正确工单的信息录入",
				},
			],
		});
	});

	it("IT-WO-HYDRATION-005: an appointment uses its YQ merchant as seller merchant", () => {
		const result = buildWithoutMutation({
			options: {
				isAppointment: true,
				yqMerchantId: "yq-merchant-9",
				merchantId: "merchant-appointment",
				sellerMerchantId: "stale-seller-merchant",
				customer: {
					customerName: "钱七",
					customerPhone: "13500135000",
					customerSex: 1,
					experienceFlag: "N",
					salesType: 3,
				},
			},
			previousOrderObj: {},
			orderTypeList,
			isErrorOrder: false,
			associateOrderType: false,
			merchantBrands: [],
		});

		assert.deepEqual(result, {
			orderObj: {
				isAppointment: true,
				yqMerchantId: "yq-merchant-9",
				merchantId: "merchant-appointment",
				sellerMerchantId: "yq-merchant-9",
				customer: {
					customerName: "钱七",
					customerPhone: "13500135000",
					customerSex: 1,
					experienceFlag: "N",
					salesType: 3,
				},
				djOrderType: 1,
			},
			patch: {
				currentWordNumber: 0,
				orderTypeName: "正常订单",
				customerTagsText: "",
				showCustomerTags: true,
				showActualDeliveryDate: false,
				first: true,
				flag: true,
				isErrorOrder: false,
				associateOrderType: false,
				isAppointment: true,
			},
			effects: [],
		});
	});
});