feat(sync): add converters from Hiboutik DTOs to Prisma inputs
Includes helpers for Hiboutik dates ("0000-00-00" is ignored) and optional prices ("0.00" means no value).
This commit is contained in:
15
src/utils/date.spec.ts
Normal file
15
src/utils/date.spec.ts
Normal 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
10
src/utils/date.ts
Normal 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
11
src/utils/price.spec.ts
Normal 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
10
src/utils/price.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user