feat(sync): add converters from Hiboutik DTOs to Prisma inputs

This commit is contained in:
MastersGame
2026-11-05 23:00:48 +01:00
parent 9cfcd12351
commit aa4262c45a
12 changed files with 294 additions and 0 deletions

15
src/utils/date.spec.ts Normal file
View File

@@ -0,0 +1,15 @@
import { parseHiboutikDate } from './date';
describe('parseHiboutikDate', () => {
it('parses a Hiboutik datetime', () => {
expect(parseHiboutikDate('2026-06-28 17:02:55')).toEqual(new Date('2026-06-28T17:02:55'));
});
it('parses a date without time', () => {
expect(parseHiboutikDate('1990-05-12')).toEqual(new Date('1990-05-12'));
});
it.each(['', '0000-00-00', '0000-00-00 00:00:00', 'not a date', null, undefined])('returns undefined for %p', (value) => {
expect(parseHiboutikDate(value)).toBeUndefined();
});
});

10
src/utils/date.ts Normal file
View File

@@ -0,0 +1,10 @@
/**
* Parses a Hiboutik date ("YYYY-MM-DD HH:mm:ss" or "YYYY-MM-DD").
* Returns undefined for empty or invalid dates (e.g. "0000-00-00").
*/
export function parseHiboutikDate(value: string | null | undefined): Date | undefined {
if (!value) return undefined;
const date = new Date(value.trim().replace(' ', 'T'));
return isNaN(date.getTime()) ? undefined : date;
}

11
src/utils/price.spec.ts Normal file
View File

@@ -0,0 +1,11 @@
import { parseOptionalHiboutikPrice } from './price';
describe('parseOptionalHiboutikPrice', () => {
it('keeps a positive price', () => {
expect(parseOptionalHiboutikPrice('12.50')).toBe('12.50');
});
it.each(['0.00', '0', '', 'abc', '-1.00', null, undefined])('returns null for %p', (value) => {
expect(parseOptionalHiboutikPrice(value)).toBeNull();
});
});

10
src/utils/price.ts Normal file
View File

@@ -0,0 +1,10 @@
/**
* Parses an optional Hiboutik price (discount price, size price override).
* Hiboutik sends "0.00" when the value is not set: returns null for empty, zero or invalid values.
*/
export function parseOptionalHiboutikPrice(value: string | null | undefined): string | null {
if (!value) return null;
const price = Number(value);
return Number.isFinite(price) && price > 0 ? value : null;
}