feat(sync): synchronize customer orders from Hiboutik sales

For each customer sale not yet stored as completed, fetch the full sale
(GET /sales/:id) and upsert it as a STORE order keyed on hiboutikSaleId.
Lines are replaced on each upsert and linked to the matching product
version when it still exists. Quotations are skipped, orders are never
deleted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
MastersGame
2026-11-07 10:48:54 +01:00
parent ad8e39f63c
commit 1a04bafa0b
10 changed files with 371 additions and 4 deletions

View File

@@ -7,6 +7,9 @@ describe('CustomerHiboutikService', () => {
const customerConverter = { fromHiboutik: jest.fn() };
const customerAddressDatabaseRepository = { upsert: jest.fn(), deleteMissing: jest.fn() };
const customerAddressConverter = { fromHiboutik: jest.fn((dto: unknown) => dto) };
const saleHiboutikRepository = { getSaleById: jest.fn() };
const orderDatabaseRepository = { upsert: jest.fn(), getCompletedSaleIds: jest.fn() };
const orderConverter = { fromHiboutik: jest.fn((dto: unknown) => dto) };
let service: CustomerHiboutikService;
beforeAll(() => {
@@ -22,12 +25,17 @@ describe('CustomerHiboutikService', () => {
customerDatabaseRepository.upsert.mockResolvedValue({ id: 'customer-uuid' });
customerDatabaseRepository.deleteMissing.mockResolvedValue(0);
customerAddressDatabaseRepository.deleteMissing.mockResolvedValue(0);
orderDatabaseRepository.getCompletedSaleIds.mockResolvedValue(new Set());
saleHiboutikRepository.getSaleById.mockImplementation((id: number) => Promise.resolve({ sale_id: id, quotation: 0 }));
service = new CustomerHiboutikService(
customerHiboutikRepository as never,
customerDatabaseRepository as never,
customerConverter as never,
customerAddressDatabaseRepository as never,
customerAddressConverter as never,
saleHiboutikRepository as never,
orderDatabaseRepository as never,
orderConverter as never,
);
});
@@ -148,4 +156,51 @@ describe('CustomerHiboutikService', () => {
expect(customerAddressDatabaseRepository.upsert).toHaveBeenCalledTimes(2);
});
});
describe('orders', () => {
beforeEach(() => {
customerHiboutikRepository.getCustomers.mockResolvedValue([{ customers_id: 1 }]);
customerHiboutikRepository.getCustomerById.mockResolvedValue({
customers_id: 1,
addresses: [],
sales: [{ sale_id: 5 }, { sale_id: '6' }, { sale_id: 7 }],
});
});
it('fetches and upserts the sales not yet stored as completed, linked to the customer', async () => {
orderDatabaseRepository.getCompletedSaleIds.mockResolvedValue(new Set([6]));
await service.syncCustomers();
expect(orderDatabaseRepository.getCompletedSaleIds).toHaveBeenCalledWith([5, 6, 7]);
expect(saleHiboutikRepository.getSaleById.mock.calls.map((call: unknown[]) => call[0])).toEqual([5, 7]);
expect(orderDatabaseRepository.upsert).toHaveBeenCalledTimes(2);
expect(orderConverter.fromHiboutik).toHaveBeenCalledWith({ sale_id: 5, quotation: 0, customer_id: 1 });
});
it('ignores quotations', async () => {
saleHiboutikRepository.getSaleById.mockResolvedValueOnce({ sale_id: 5, quotation: 1 });
await service.syncCustomers();
expect(orderDatabaseRepository.upsert).toHaveBeenCalledTimes(2);
});
it('keeps syncing the other sales when one fails', async () => {
saleHiboutikRepository.getSaleById.mockRejectedValueOnce(new Error('boom'));
await service.syncCustomers();
expect(orderDatabaseRepository.upsert).toHaveBeenCalledTimes(2);
});
it('skips orders when the payload has no sales array', async () => {
customerHiboutikRepository.getCustomerById.mockResolvedValue({ customers_id: 1, addresses: [] });
await service.syncCustomers();
expect(saleHiboutikRepository.getSaleById).not.toHaveBeenCalled();
expect(orderDatabaseRepository.upsert).not.toHaveBeenCalled();
});
});
});