Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
323 lines
13 KiB
TypeScript
323 lines
13 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { campaignChecks, campaignWarnings, type GiftVariantProblem } from "./campaignChecks";
|
|
import type { CampaignInput } from "@/app/lib/types";
|
|
|
|
// The rules mirror the server validator, so each test pins the problem, its field and its order.
|
|
|
|
const base: CampaignInput = {
|
|
name: "Raya sale",
|
|
customerLabel: "RAYA 20%",
|
|
benefitType: "PERCENT_OFF",
|
|
benefitValue: 20,
|
|
maxDiscountCap: 30,
|
|
claimType: "AUTO",
|
|
channelOnline: true,
|
|
channelOutlet: false,
|
|
channelEvent: false,
|
|
allProducts: true,
|
|
products: [],
|
|
ranks: [],
|
|
effectiveFrom: "2026-10-01T00:00",
|
|
effectiveTo: "2026-10-15T23:59",
|
|
};
|
|
|
|
const amountOff: CampaignInput = { ...base, benefitType: "AMOUNT_OFF", benefitValue: 10, maxDiscountCap: null };
|
|
|
|
const bxgy: CampaignInput = {
|
|
...base, benefitType: "BUY_X_GET_Y", benefitValue: 50, maxDiscountCap: null,
|
|
buyQty: 2, getQty: 1, maxRewardSets: 5,
|
|
};
|
|
|
|
const gift: CampaignInput = {
|
|
...base, benefitType: "GIFT", benefitValue: 0, maxDiscountCap: null,
|
|
giftProductCode: "BOTTLE", giftQty: 1,
|
|
};
|
|
|
|
const fixedPrice: CampaignInput = {
|
|
...base, benefitType: "FIXED_PRICE", benefitValue: 49.9, maxDiscountCap: null, allProducts: false,
|
|
products: [{ productCode: "DIAPER", variationId: null, mode: "INCLUDE" }],
|
|
};
|
|
|
|
const check = (
|
|
c: CampaignInput,
|
|
opts: { locked?: boolean; giftVariantProblem?: GiftVariantProblem | null } = {},
|
|
) => campaignChecks(c, { locked: false, giftVariantProblem: null, ...opts });
|
|
|
|
describe("campaignChecks: a valid campaign of each benefit type", () => {
|
|
it.each([
|
|
["percent off", base],
|
|
["amount off", amountOff],
|
|
["buy X get Y", bxgy],
|
|
["gift", gift],
|
|
["fixed price", fixedPrice],
|
|
])("has no problems: %s", (_name, c) => {
|
|
expect(check(c)).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("campaignChecks: name and label", () => {
|
|
it("rejects a blank name", () => {
|
|
expect(check({ ...base, name: "" })).toEqual([{ field: "name", code: "PromoFixName" }]);
|
|
});
|
|
|
|
it("rejects a whitespace-only customer label", () => {
|
|
expect(check({ ...base, customerLabel: " " })).toEqual([{ field: "customerLabel", code: "PromoFixCustomerLabel" }]);
|
|
});
|
|
});
|
|
|
|
describe("campaignChecks: benefit value", () => {
|
|
it("requires a value for percent off", () => {
|
|
expect(check({ ...base, benefitValue: null })).toEqual([{ field: "benefitValue", code: "PromoFixValue" }]);
|
|
});
|
|
|
|
it("requires a value above zero for amount off", () => {
|
|
expect(check({ ...amountOff, benefitValue: 0 })).toEqual([{ field: "benefitValue", code: "PromoFixValue" }]);
|
|
});
|
|
|
|
it("rejects a missing or negative gift price", () => {
|
|
expect(check({ ...gift, benefitValue: null })).toEqual([{ field: "benefitValue", code: "PromoFixGiftPrice" }]);
|
|
expect(check({ ...gift, benefitValue: -1 })).toEqual([{ field: "benefitValue", code: "PromoFixGiftPrice" }]);
|
|
});
|
|
|
|
it("caps percent off and buy X get Y at 100", () => {
|
|
expect(check({ ...base, benefitValue: 150 })).toEqual([{ field: "benefitValue", code: "PromoFixPercentMax" }]);
|
|
expect(check({ ...bxgy, benefitValue: 120 })).toEqual([{ field: "benefitValue", code: "PromoFixPercentMax" }]);
|
|
});
|
|
});
|
|
|
|
describe("campaignChecks: buy X get Y quantities", () => {
|
|
it("rejects a missing, zero or fractional buy quantity", () => {
|
|
for (const buyQty of [null, 0, 2.5]) {
|
|
expect(check({ ...bxgy, buyQty })).toEqual([{ field: "buyQty", code: "PromoFixBuyGetQty" }]);
|
|
}
|
|
});
|
|
|
|
it("rejects a missing, zero or fractional get quantity on the buy quantity field", () => {
|
|
for (const getQty of [null, 0, 1.5]) {
|
|
expect(check({ ...bxgy, getQty })).toEqual([{ field: "buyQty", code: "PromoFixBuyGetQty" }]);
|
|
}
|
|
});
|
|
|
|
it("reports one problem when both quantities are bad", () => {
|
|
expect(check({ ...bxgy, buyQty: null, getQty: 0 })).toEqual([{ field: "buyQty", code: "PromoFixBuyGetQty" }]);
|
|
});
|
|
|
|
it("rejects a zero or fractional reward set cap", () => {
|
|
expect(check({ ...bxgy, maxRewardSets: 0 })).toEqual([{ field: "maxRewardSets", code: "PromoFixLimitsPositive" }]);
|
|
expect(check({ ...bxgy, maxRewardSets: 2.5 })).toEqual([{ field: "maxRewardSets", code: "PromoFixLimitsPositive" }]);
|
|
});
|
|
});
|
|
|
|
describe("campaignChecks: gift", () => {
|
|
it("rejects a blank or whitespace-only gift product", () => {
|
|
expect(check({ ...gift, giftProductCode: "" })).toEqual([{ field: "giftProduct", code: "PromoFixGiftProduct" }]);
|
|
expect(check({ ...gift, giftProductCode: " " })).toEqual([{ field: "giftProduct", code: "PromoFixGiftProduct" }]);
|
|
});
|
|
|
|
it("passes the gift-variant problem through on giftProduct", () => {
|
|
expect(check(gift, { giftVariantProblem: "PromoGiftVariantChoose" })).toEqual([
|
|
{ field: "giftProduct", code: "PromoGiftVariantChoose" },
|
|
]);
|
|
});
|
|
|
|
it("ignores the gift-variant problem for other benefit types", () => {
|
|
expect(check(base, { giftVariantProblem: "PromoGiftVariantChoose" })).toEqual([]);
|
|
});
|
|
|
|
it("rejects a missing, zero or fractional gift quantity", () => {
|
|
for (const giftQty of [null, 0, 1.5]) {
|
|
expect(check({ ...gift, giftQty })).toEqual([{ field: "giftQty", code: "PromoFixGiftQty" }]);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("campaignChecks: shared code", () => {
|
|
const shared = (sharedCode: string): CampaignInput => ({ ...base, claimType: "SHARED_CODE", sharedCode });
|
|
|
|
it("accepts a code of 3 to 40 letters or digits, in any case", () => {
|
|
expect(check(shared("ABC123"))).toEqual([]);
|
|
expect(check(shared("abc1"))).toEqual([]);
|
|
});
|
|
|
|
it("rejects a blank or too short code", () => {
|
|
expect(check(shared("AB"))).toEqual([{ field: "sharedCode", code: "PromoFixCode" }]);
|
|
expect(check(shared(""))).toEqual([{ field: "sharedCode", code: "PromoFixCode" }]);
|
|
});
|
|
});
|
|
|
|
describe("campaignChecks: products", () => {
|
|
it("needs an included product when not all products apply", () => {
|
|
const blank = { ...base, allProducts: false, products: [{ productCode: "", mode: "INCLUDE" as const }] };
|
|
expect(check(blank)).toEqual([{ field: "products", code: "PromoFixProducts" }]);
|
|
});
|
|
|
|
it("does not count an exclusion as an included product", () => {
|
|
const excludeOnly = {
|
|
...base, allProducts: false,
|
|
products: [{ productCode: "DIAPER", variationId: null, mode: "EXCLUDE" as const }],
|
|
};
|
|
expect(check(excludeOnly)).toEqual([{ field: "products", code: "PromoFixProducts" }]);
|
|
});
|
|
|
|
it("does not allow fixed price on all products", () => {
|
|
expect(check({ ...fixedPrice, allProducts: true })).toEqual([{ field: "products", code: "PromoFixFixedPriceProducts" }]);
|
|
});
|
|
});
|
|
|
|
describe("campaignChecks: channels and dates", () => {
|
|
it("needs at least one channel", () => {
|
|
const none = { ...base, channelOnline: false, channelOutlet: false, channelEvent: false };
|
|
expect(check(none)).toEqual([{ field: "channels", code: "PromoFixChannel" }]);
|
|
});
|
|
|
|
it("needs a start date", () => {
|
|
expect(check({ ...base, effectiveFrom: "" })).toEqual([{ field: "effectiveFrom", code: "PromoFixStarts" }]);
|
|
});
|
|
|
|
it("needs an end date", () => {
|
|
expect(check({ ...base, effectiveTo: "" })).toEqual([{ field: "effectiveTo", code: "PromoFixEnds" }]);
|
|
});
|
|
|
|
it("needs the end after the start", () => {
|
|
expect(check({ ...base, effectiveTo: "2026-09-30T00:00" })).toEqual([{ field: "effectiveTo", code: "PromoFixEndAfterStart" }]);
|
|
});
|
|
|
|
it("rejects an end equal to the start", () => {
|
|
expect(check({ ...base, effectiveTo: base.effectiveFrom })).toEqual([{ field: "effectiveTo", code: "PromoFixEndAfterStart" }]);
|
|
});
|
|
});
|
|
|
|
describe("campaignChecks: limits", () => {
|
|
it("rejects a zero budget", () => {
|
|
expect(check({ ...base, budgetAmount: 0 })).toEqual([{ field: "budgetAmount", code: "PromoFixLimitsPositive" }]);
|
|
});
|
|
|
|
it("rejects a negative total usage limit", () => {
|
|
expect(check({ ...base, maxUsesTotal: -1 })).toEqual([{ field: "maxUsesTotal", code: "PromoFixLimitsPositive" }]);
|
|
});
|
|
|
|
it("rejects a zero per-member usage limit", () => {
|
|
expect(check({ ...base, maxUsesPerMember: 0 })).toEqual([{ field: "maxUsesPerMember", code: "PromoFixLimitsPositive" }]);
|
|
});
|
|
|
|
it("rejects a zero or negative minimum spend", () => {
|
|
expect(check({ ...base, minSpend: 0 })).toEqual([{ field: "minSpend", code: "PromoFixLimitsPositive" }]);
|
|
expect(check({ ...base, minSpend: -5 })).toEqual([{ field: "minSpend", code: "PromoFixLimitsPositive" }]);
|
|
});
|
|
|
|
it("rejects a zero or fractional minimum quantity", () => {
|
|
expect(check({ ...base, minQuantity: 0 })).toEqual([{ field: "minQuantity", code: "PromoFixMinQty" }]);
|
|
expect(check({ ...base, minQuantity: 1.5 })).toEqual([{ field: "minQuantity", code: "PromoFixMinQty" }]);
|
|
});
|
|
});
|
|
|
|
describe("campaignChecks: several problems", () => {
|
|
it("lists an empty new form's problems in form order", () => {
|
|
const empty: CampaignInput = {
|
|
...base, name: "", customerLabel: "", benefitValue: null, maxDiscountCap: null,
|
|
channelOnline: false, effectiveFrom: "", effectiveTo: "",
|
|
};
|
|
expect(check(empty).map((p) => p.code)).toEqual([
|
|
"PromoFixName",
|
|
"PromoFixCustomerLabel",
|
|
"PromoFixValue",
|
|
"PromoFixChannel",
|
|
"PromoFixStarts",
|
|
"PromoFixEnds",
|
|
]);
|
|
});
|
|
|
|
it("lists gift problems as price, product, quantity", () => {
|
|
expect(check({ ...gift, benefitValue: null, giftProductCode: "", giftQty: 0 })).toEqual([
|
|
{ field: "benefitValue", code: "PromoFixGiftPrice" },
|
|
{ field: "giftProduct", code: "PromoFixGiftProduct" },
|
|
{ field: "giftQty", code: "PromoFixGiftQty" },
|
|
]);
|
|
});
|
|
|
|
it("lists buy X get Y problems as quantities, then reward set cap", () => {
|
|
expect(check({ ...bxgy, buyQty: null, maxRewardSets: 0 })).toEqual([
|
|
{ field: "buyQty", code: "PromoFixBuyGetQty" },
|
|
{ field: "maxRewardSets", code: "PromoFixLimitsPositive" },
|
|
]);
|
|
});
|
|
|
|
it("lists end date, budget, minimum spend, minimum quantity in form order", () => {
|
|
expect(check({ ...amountOff, effectiveTo: "", budgetAmount: 0, minSpend: 0, minQuantity: 0 })).toEqual([
|
|
{ field: "effectiveTo", code: "PromoFixEnds" },
|
|
{ field: "budgetAmount", code: "PromoFixLimitsPositive" },
|
|
{ field: "minSpend", code: "PromoFixLimitsPositive" },
|
|
{ field: "minQuantity", code: "PromoFixMinQty" },
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe("campaignChecks: locked campaign", () => {
|
|
it("checks only the end date and the usage limits", () => {
|
|
const c: CampaignInput = {
|
|
...base, name: "", customerLabel: "", benefitValue: null, channelOnline: false,
|
|
effectiveFrom: "", effectiveTo: "", minSpend: 0, budgetAmount: 0,
|
|
};
|
|
expect(check(c, { locked: true })).toEqual([
|
|
{ field: "effectiveTo", code: "PromoFixEnds" },
|
|
{ field: "budgetAmount", code: "PromoFixLimitsPositive" },
|
|
]);
|
|
});
|
|
|
|
it("rejects an end before the start", () => {
|
|
expect(check({ ...amountOff, effectiveTo: "2026-09-30T00:00" }, { locked: true })).toEqual([
|
|
{ field: "effectiveTo", code: "PromoFixEndAfterStart" },
|
|
]);
|
|
});
|
|
|
|
it("lists budget, total and per-member limits in order and skips minimums", () => {
|
|
const c = { ...amountOff, minSpend: 0, minQuantity: 0, maxUsesTotal: -1, maxUsesPerMember: 0, budgetAmount: -5 };
|
|
expect(check(c, { locked: true })).toEqual([
|
|
{ field: "budgetAmount", code: "PromoFixLimitsPositive" },
|
|
{ field: "maxUsesTotal", code: "PromoFixLimitsPositive" },
|
|
{ field: "maxUsesPerMember", code: "PromoFixLimitsPositive" },
|
|
]);
|
|
});
|
|
|
|
it("ignores the gift-variant problem", () => {
|
|
expect(check(gift, { locked: true, giftVariantProblem: "PromoGiftVariantChoose" })).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("campaignWarnings", () => {
|
|
it("warns when an amount off is at least the minimum spend", () => {
|
|
expect(campaignWarnings({ ...amountOff, benefitValue: 50, minSpend: 50 })).toEqual([
|
|
{ at: "minSpend", code: "PromoWarnSpendBelowAmount", params: { amount: "50.00", minSpend: "50.00" } },
|
|
]);
|
|
});
|
|
|
|
it("does not warn when the amount off is below the minimum spend", () => {
|
|
expect(campaignWarnings({ ...amountOff, benefitValue: 40, minSpend: 50 })).toEqual([]);
|
|
});
|
|
|
|
it("does not warn about spend when there is no minimum spend", () => {
|
|
expect(campaignWarnings({ ...amountOff, benefitValue: 50, minSpend: null })).toEqual([]);
|
|
});
|
|
|
|
it("warns when a percent off has neither a cap nor a budget", () => {
|
|
expect(campaignWarnings({ ...base, maxDiscountCap: null, budgetAmount: null })).toEqual([
|
|
{ at: "maxDiscountCap", code: "PromoWarnNoCapNoBudget" },
|
|
]);
|
|
});
|
|
|
|
it("does not warn about a missing cap while the percentage is blank or zero", () => {
|
|
expect(campaignWarnings({ ...base, benefitValue: null, maxDiscountCap: null, budgetAmount: null })).toEqual([]);
|
|
expect(campaignWarnings({ ...base, benefitValue: 0, maxDiscountCap: null, budgetAmount: null })).toEqual([]);
|
|
});
|
|
|
|
it("does not warn when a percent off has a cap, a budget or both", () => {
|
|
expect(campaignWarnings({ ...base, maxDiscountCap: 30, budgetAmount: null })).toEqual([]);
|
|
expect(campaignWarnings({ ...base, maxDiscountCap: null, budgetAmount: 500 })).toEqual([]);
|
|
expect(campaignWarnings({ ...base, maxDiscountCap: 30, budgetAmount: 500 })).toEqual([]);
|
|
});
|
|
|
|
it("does not warn about a missing cap on an amount off", () => {
|
|
expect(campaignWarnings({ ...amountOff, maxDiscountCap: null, budgetAmount: null })).toEqual([]);
|
|
});
|
|
});
|