173 lines
6.8 KiB
JavaScript
173 lines
6.8 KiB
JavaScript
import { describe, expect, it, vi, beforeEach } from "vitest";
|
||
|
||
const { fromMock, updateMock, eqMock, selectMock, singleMock } = vi.hoisted(() => ({
|
||
fromMock: vi.fn(),
|
||
updateMock: vi.fn(),
|
||
eqMock: vi.fn(),
|
||
selectMock: vi.fn(),
|
||
singleMock: vi.fn(),
|
||
}));
|
||
|
||
vi.mock("../../supabaseClient", () => ({
|
||
hasSupabaseConfig: true,
|
||
supabase: {
|
||
from: fromMock,
|
||
},
|
||
}));
|
||
|
||
import {
|
||
mapOrderGroupRowToDeliveryGroup,
|
||
updateOrderGroupDeliveryChoice,
|
||
} from "./orderGroupRepository";
|
||
|
||
describe("mapOrderGroupRowToDeliveryGroup", () => {
|
||
it("normalizes the order_groups row into a delivery-group view model", () => {
|
||
const group = mapOrderGroupRowToDeliveryGroup({
|
||
id: "953c5bda-7e77-47af-9b7f-9d2c2cf3e7c5",
|
||
group_key: "3939375462|14.04.26",
|
||
customer_name: "Калинина Дарья Егоровна",
|
||
customer_phone: "3939375462",
|
||
customer_date: "14.04.26",
|
||
delivery_address: "Симферополь, ул. Ленина, 10",
|
||
orders_count: 1,
|
||
ready_count: 1,
|
||
not_ready_count: 0,
|
||
order_numbers: ["СФ Т\\ЕА-23094"],
|
||
status: "ready_for_notification",
|
||
delivery_status: "agreed",
|
||
delivery_date: "2026-04-16",
|
||
sms_sent_at: null,
|
||
delivery_time: "До обеда",
|
||
created_from_exchange_at: null,
|
||
source_key: null,
|
||
legacy_customer_name: null,
|
||
legacy_customer_phone: null,
|
||
legacy_customer_phone_normalized: null,
|
||
legacy_customer_date: null,
|
||
legacy_orders_total: null,
|
||
legacy_orders_ready: null,
|
||
legacy_orders_not_ready: null,
|
||
source_orders: null,
|
||
created_at: "2026-05-05 09:43:53.750061+00",
|
||
updated_at: "2026-05-05 09:43:53.750061+00",
|
||
});
|
||
|
||
expect(group.id).toBe("953c5bda-7e77-47af-9b7f-9d2c2cf3e7c5");
|
||
expect(group.groupKey).toBe("3939375462|14.04.26");
|
||
expect(group.customer.name).toBe("Калинина Дарья Егоровна");
|
||
expect(group.customer.phone).toBe("3939375462");
|
||
expect(group.customer.date).toBe("14.04.26");
|
||
expect(group.deliveryAddress).toBe("Симферополь, ул. Ленина, 10");
|
||
expect(group.ordersCount).toBe(1);
|
||
expect(group.readyCount).toBe(1);
|
||
expect(group.notReadyCount).toBe(0);
|
||
expect(group.orderNumbers).toEqual(["СФ Т\\ЕА-23094"]);
|
||
expect(group.displayTitle).toBe("Калинина Дарья Егоровна");
|
||
expect(group.displaySubtitle).toBe("3939375462 · 14.04.26");
|
||
expect(group.deliveryDate).toBe("2026-04-16");
|
||
expect(group.deliveryHalfDay).toBe("Первая половина дня");
|
||
expect(group.deliveryStatus).toBe("agreed");
|
||
expect(group.searchText).toContain("симферополь, ул. ленина, 10");
|
||
});
|
||
|
||
it("infers readiness counters from order numbers for ready groups when counters are empty", () => {
|
||
const group = mapOrderGroupRowToDeliveryGroup({
|
||
id: "group-without-counters",
|
||
group_key: "9781632663|28.04.26",
|
||
order_numbers: ["СФ Т\\ЕА-26979"],
|
||
status: "ready_for_notification",
|
||
delivery_status: "pending_confirmation",
|
||
created_at: "2026-05-05 09:43:53.750061+00",
|
||
updated_at: "2026-05-05 09:43:53.750061+00",
|
||
});
|
||
|
||
expect(group.ordersCount).toBe(1);
|
||
expect(group.readyCount).toBe(1);
|
||
expect(group.notReadyCount).toBe(0);
|
||
expect(group.deliveryDate).toBe("");
|
||
});
|
||
|
||
it("drops invalid delivery time values instead of showing them", () => {
|
||
const group = mapOrderGroupRowToDeliveryGroup({
|
||
id: "group-with-bad-time",
|
||
group_key: "9781632663|08.05.26",
|
||
customer_name: "Зиновьев Алексей Гаврилович",
|
||
customer_phone: "9781632663",
|
||
customer_date: "08.05.26",
|
||
delivery_time: "Зиновьев Алексей Гаврилович",
|
||
delivery_half_day: null,
|
||
order_numbers: ["СФ Т\\ЕА-26979"],
|
||
status: "ready_for_notification",
|
||
delivery_status: "pending_confirmation",
|
||
created_at: "2026-05-05 09:43:53.750061+00",
|
||
updated_at: "2026-05-05 09:43:53.750061+00",
|
||
});
|
||
|
||
expect(group.deliveryTime).toBe("");
|
||
expect(group.searchText).not.toContain("зиновьев алексей гаврилович зиновьев алексей гаврилович");
|
||
});
|
||
});
|
||
|
||
describe("updateOrderGroupDeliveryChoice", () => {
|
||
beforeEach(() => {
|
||
fromMock.mockReset();
|
||
updateMock.mockReset();
|
||
eqMock.mockReset();
|
||
selectMock.mockReset();
|
||
singleMock.mockReset();
|
||
|
||
fromMock
|
||
.mockReturnValueOnce({ update: updateMock })
|
||
.mockReturnValueOnce({ select: selectMock });
|
||
updateMock.mockReturnValue({ eq: eqMock });
|
||
eqMock.mockReturnValueOnce({ error: null, status: 200, statusText: "OK" })
|
||
.mockReturnValueOnce({ single: singleMock });
|
||
selectMock.mockReturnValue({ eq: eqMock });
|
||
});
|
||
|
||
it("updates the group directly in order_groups", async () => {
|
||
singleMock.mockResolvedValueOnce({
|
||
data: {
|
||
id: "group-id",
|
||
group_key: "9788151605|18.02.26",
|
||
status: "ready_for_notification",
|
||
delivery_status: "agreed",
|
||
delivery_date: "2026-05-13",
|
||
delivery_time: "Первая половина дня",
|
||
notification_status: "confirmed",
|
||
created_at: "2026-05-12T09:00:00Z",
|
||
updated_at: "2026-05-12T09:05:00Z",
|
||
},
|
||
error: null,
|
||
});
|
||
|
||
await expect(
|
||
updateOrderGroupDeliveryChoice({
|
||
orderGroupId: "group-id",
|
||
deliveryDate: "2026-05-13",
|
||
deliveryTime: "Первая половина дня",
|
||
}),
|
||
).resolves.toMatchObject({
|
||
data: expect.objectContaining({
|
||
id: "group-id",
|
||
deliveryStatus: "agreed",
|
||
deliveryDate: "2026-05-13",
|
||
deliveryTime: "Первая половина дня",
|
||
}),
|
||
error: null,
|
||
});
|
||
|
||
expect(fromMock).toHaveBeenCalledWith("order_groups");
|
||
expect(updateMock).toHaveBeenCalledWith({
|
||
delivery_status: "agreed",
|
||
delivery_date: "2026-05-13",
|
||
delivery_time: "Первая половина дня",
|
||
notification_status: "confirmed",
|
||
updated_at: expect.any(String),
|
||
});
|
||
expect(eqMock).toHaveBeenCalledWith("id", "group-id");
|
||
expect(selectMock).toHaveBeenCalledWith("id, group_key, order_numbers, status, delivery_status, sms_sent_at, created_at, updated_at, created_from_exchange_at, source_key, customer_name, customer_phone, customer_phone_normalized, customer_date, orders_total, orders_ready, orders_not_ready, source_orders, order_list, order_list_structured, delivery_invitation_id, delivery_link, notification_status, sms_attempts, first_sms_sent_at, second_sms_sent_at, last_sms_error, next_notification_check_at, delivery_date, delivery_time, delivery_address, manual_confirmation_at, paid_storage_at, assigned_driver_id, assigned_driver:users!order_groups_assigned_driver_id_fkey(id, name)");
|
||
expect(singleMock).toHaveBeenCalledTimes(1);
|
||
});
|
||
});
|