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
185 lines
7.6 KiB
TypeScript
185 lines
7.6 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { resolveMessage } from "@/app/lib/i18n/t";
|
|
|
|
const mocks = vi.hoisted(() => ({ apiGet: vi.fn() }));
|
|
vi.mock("@/app/lib/clientApi", () => ({ apiGet: mocks.apiGet }));
|
|
|
|
import {
|
|
getDashboard,
|
|
getSalesReport,
|
|
getRewardsReport,
|
|
getSalesItemsReport,
|
|
channelRows,
|
|
rewardLabel,
|
|
REWARD_CODES,
|
|
} from "./adminDashboard";
|
|
|
|
beforeEach(() => mocks.apiGet.mockReset());
|
|
|
|
describe("getDashboard", () => {
|
|
it("calls the BFF with no query when no period is chosen", async () => {
|
|
mocks.apiGet.mockResolvedValue({});
|
|
await getDashboard();
|
|
expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/dashboard");
|
|
});
|
|
|
|
it("sends the chosen period", async () => {
|
|
mocks.apiGet.mockResolvedValue({});
|
|
await getDashboard({ from: "2026-09-01", to: "2026-09-30" });
|
|
expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/dashboard?from=2026-09-01&to=2026-09-30");
|
|
});
|
|
});
|
|
|
|
describe("getSalesReport", () => {
|
|
it("sends paging and the period, omitting empty bounds", async () => {
|
|
mocks.apiGet.mockResolvedValue({});
|
|
await getSalesReport({ page: 1, size: 10, from: "", to: "" });
|
|
expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/reports/sales?page=1&size=10");
|
|
await getSalesReport({ page: 0, size: 20, from: "2026-09-01", to: "2026-09-30" });
|
|
expect(mocks.apiGet).toHaveBeenLastCalledWith("/api/admin/reports/sales?page=0&size=20&from=2026-09-01&to=2026-09-30");
|
|
});
|
|
});
|
|
|
|
describe("getRewardsReport", () => {
|
|
it("sends the reward type filter", async () => {
|
|
mocks.apiGet.mockResolvedValue({});
|
|
await getRewardsReport({ page: 0, size: 10, rewardCode: "SELF_ACORN", from: "2026-09-01", to: "2026-09-30" });
|
|
expect(mocks.apiGet).toHaveBeenCalledWith(
|
|
"/api/admin/reports/rewards?page=0&size=10&from=2026-09-01&to=2026-09-30&rewardCode=SELF_ACORN",
|
|
);
|
|
});
|
|
});
|
|
|
|
// localise-admin-shell: the helpers take t; with an empty catalog they render the English defaults.
|
|
const en = (code: string) => resolveMessage({}, code);
|
|
|
|
describe("labels", () => {
|
|
it("names reward types, and falls back to the raw code", () => {
|
|
expect(rewardLabel("SELF_ACORN", en)).toBe("Self Acorn");
|
|
expect(rewardLabel("SPONSOR_APPLE", en)).toBe("Sponsor Apple");
|
|
expect(rewardLabel("NEW_KIND", en)).toBe("NEW_KIND");
|
|
});
|
|
});
|
|
|
|
describe("repeat top-up Acorn reward labels", () => {
|
|
it("names each tier and lists it in the report filter", () => {
|
|
expect(rewardLabel("REPEAT_ACORN_1000", en)).toBe("Repeat top-up Acorn (RM1,000)");
|
|
expect(rewardLabel("REPEAT_ACORN_2000", en)).toBe("Repeat top-up Acorn (RM2,000)");
|
|
expect(rewardLabel("REPEAT_ACORN_3000", en)).toBe("Repeat top-up Acorn (RM3,000)");
|
|
expect(REWARD_CODES).toEqual(expect.arrayContaining(["REPEAT_ACORN_1000", "REPEAT_ACORN_2000", "REPEAT_ACORN_3000"]));
|
|
});
|
|
});
|
|
|
|
describe("getSalesItemsReport sorting (BABYBOO-54 B3)", () => {
|
|
it("sends sortBy and sortDir when set", async () => {
|
|
mocks.apiGet.mockResolvedValue({ content: [], totalElements: 0 });
|
|
await getSalesItemsReport({ page: 0, size: 10, from: "2026-09-01", to: "2026-09-30", sortBy: "qty", sortDir: "desc" });
|
|
const url = mocks.apiGet.mock.calls[0][0] as string;
|
|
expect(url).toContain("sortBy=qty");
|
|
expect(url).toContain("sortDir=desc");
|
|
});
|
|
|
|
it("sends neither when unsorted", async () => {
|
|
mocks.apiGet.mockResolvedValue({ content: [], totalElements: 0 });
|
|
await getSalesItemsReport({ page: 0, size: 10, from: "2026-09-01", to: "2026-09-30" });
|
|
const url = mocks.apiGet.mock.calls[0][0] as string;
|
|
expect(url).not.toContain("sortBy");
|
|
expect(url).not.toContain("sortDir");
|
|
});
|
|
});
|
|
|
|
import { dashboardLinks } from "./adminDashboard";
|
|
|
|
describe("dashboardLinks", () => {
|
|
const l = dashboardLinks("2026-10-01", "2026-10-31");
|
|
const rewards = "/reports?tab=rewards&rewards.preset=custom&rewards.from=2026-10-01&rewards.to=2026-10-31";
|
|
|
|
it("builds the sales report links", () => {
|
|
const sales = "/reports?sales.preset=custom&sales.from=2026-10-01&sales.to=2026-10-31";
|
|
expect(l.revenue).toBe(sales);
|
|
expect(l.orders).toBe(sales);
|
|
});
|
|
|
|
it("builds the rewards links", () => {
|
|
expect(l.apples).toBe(rewards);
|
|
expect(l.acorns).toBe(rewards);
|
|
expect(l.reward("SELF_ACORN")).toBe(`${rewards}&rewards.reward=SELF_ACORN`);
|
|
});
|
|
|
|
it("encodes values placed in the query string", () => {
|
|
expect(l.reward("A B")).toBe(`${rewards}&rewards.reward=A%20B`);
|
|
expect(l.rank("A&B")).toBe("/members?rank=A%26B");
|
|
expect(l.status("a/b")).toBe("/members?status=a%2Fb");
|
|
});
|
|
|
|
it("builds the member links", () => {
|
|
expect(l.newMembers).toBe("/members?joinDateFrom=2026-10-01&joinDateTo=2026-10-31");
|
|
expect(l.rank("SQUIRREL")).toBe("/members?rank=SQUIRREL");
|
|
expect(l.status("Y")).toBe("/members?status=Y");
|
|
expect(l.status("N")).toBe("/members?status=N");
|
|
});
|
|
|
|
it("builds the channel link to the Sales listing", () => {
|
|
expect(l.channel("OUTLET")).toBe("/orders/web?type=OUTLET&status=Y&from=2026-10-01&to=2026-10-31");
|
|
expect(l.channel("A B")).toBe("/orders/web?type=A%20B&status=Y&from=2026-10-01&to=2026-10-31");
|
|
});
|
|
});
|
|
|
|
import { orderTypeOf, orderTypeTone } from "./adminWebOrders";
|
|
|
|
describe("channelRows", () => {
|
|
const row = (channel: "NORMAL" | "EVENT" | "OUTLET", platform: string | null, revenue: number, orders: number) => ({
|
|
channel,
|
|
platform,
|
|
revenue,
|
|
orders,
|
|
});
|
|
|
|
it("splits NORMAL by platform and maps EVENT/OUTLET, in listing order", () => {
|
|
const rows = channelRows([
|
|
row("OUTLET", "BO", 30, 3),
|
|
row("EVENT", "BO", 40, 4),
|
|
row("NORMAL", "BO", 20, 2),
|
|
row("NORMAL", "CP", 100, 10),
|
|
]);
|
|
expect(rows.map(r => [r.type, r.labelCode, r.revenue, r.orders, r.tone])).toEqual([
|
|
["WEB", "WebOrdTypeWeb", 100, 10, "contrast"],
|
|
["COUNTER", "WebOrdTypeCounter", 20, 2, "neutral"],
|
|
["EVENT", "WebOrdTypeEvent", 40, 4, "violet"],
|
|
["OUTLET", "Outlet", 30, 3, "teal"],
|
|
]);
|
|
});
|
|
|
|
it("sums rows of the same type", () => {
|
|
const rows = channelRows([row("NORMAL", "CP", 10, 1), row("NORMAL", "CP", 5, 2), row("EVENT", "CP", 1, 1), row("EVENT", "BO", 2, 1)]);
|
|
expect(rows[0]).toMatchObject({ type: "WEB", revenue: 15, orders: 3 });
|
|
expect(rows[1]).toMatchObject({ type: "EVENT", revenue: 3, orders: 2 });
|
|
});
|
|
|
|
it("puts NORMAL on another platform under OTHER, last", () => {
|
|
const rows = channelRows([row("NORMAL", "APP", 5, 1), row("OUTLET", null, 0, 0)]);
|
|
expect(rows.map(r => r.type)).toEqual(["WEB", "EVENT", "OUTLET", "OTHER"]);
|
|
expect(rows[3]).toMatchObject({ labelCode: "DashChannelOther", tone: "neutral", revenue: 5 });
|
|
});
|
|
|
|
it("always has WEB, EVENT, OUTLET and omits empty COUNTER/OTHER", () => {
|
|
const rows = channelRows([row("NORMAL", "CP", 0, 0), row("EVENT", null, 0, 0), row("OUTLET", null, 0, 0)]);
|
|
expect(rows.map(r => r.type)).toEqual(["WEB", "EVENT", "OUTLET"]);
|
|
expect(rows.every(r => r.revenue === 0 && r.orders === 0)).toBe(true);
|
|
expect(channelRows([]).map(r => r.type)).toEqual(["WEB", "EVENT", "OUTLET"]);
|
|
});
|
|
|
|
it("counts NORMAL+AD under OTHER, matching the listing's null type", () => {
|
|
const rows = channelRows([row("NORMAL", "AD", 7, 1)]);
|
|
expect(rows.find(r => r.type === "COUNTER")).toBeUndefined();
|
|
expect(rows.find(r => r.type === "OTHER")).toMatchObject({ revenue: 7, orders: 1 });
|
|
expect(orderTypeOf("NORMAL", "AD")).toBeNull();
|
|
});
|
|
|
|
it("counts a missing platform key under OTHER, not WEB", () => {
|
|
const rows = channelRows([{ channel: "NORMAL", revenue: 7, orders: 1 }]);
|
|
expect(rows.find(r => r.type === "WEB")).toMatchObject({ revenue: 0, orders: 0 });
|
|
expect(rows.find(r => r.type === "OTHER")).toMatchObject({ revenue: 7, orders: 1 });
|
|
});
|
|
});
|