fix ad validation : remove schedule and waypoints duplication

This commit is contained in:
Sylvain Briat 2024-01-18 15:53:48 +01:00
parent 2009355b18
commit b039dbb3bd
3 changed files with 53 additions and 47 deletions

View File

@ -43,43 +43,45 @@ export class AdMapper
frequency: copy.frequency,
fromDate: new Date(copy.fromDate),
toDate: new Date(copy.toDate),
schedule: {
create: copy.schedule.map((scheduleItem: ScheduleItemProps) => ({
uuid: v4(),
day: scheduleItem.day as number,
time: new Date(
1970,
0,
1,
parseInt(scheduleItem.time.split(':')[0]),
parseInt(scheduleItem.time.split(':')[1]),
),
margin: scheduleItem.margin as number,
createdAt: now,
updatedAt: now,
})),
},
schedule: copy.schedule
? {
create: copy.schedule.map((scheduleItem: ScheduleItemProps) => ({
uuid: v4(),
day: scheduleItem.day as number,
time: new Date(
1970,
0,
1,
parseInt(scheduleItem.time.split(':')[0]),
parseInt(scheduleItem.time.split(':')[1]),
),
margin: scheduleItem.margin as number,
createdAt: now,
updatedAt: now,
})),
}
: undefined,
seatsProposed: copy.seatsProposed as number,
seatsRequested: copy.seatsRequested as number,
strict: copy.strict as boolean,
waypoints: {
create: copy.waypoints.map((waypoint: WaypointProps) => ({
uuid: v4(),
position: waypoint.position,
name: waypoint.address.name,
houseNumber: waypoint.address.houseNumber,
street: waypoint.address.street,
locality: waypoint.address.locality,
postalCode: waypoint.address.postalCode,
country: waypoint.address.country,
lon: waypoint.address.coordinates.lon,
lat: waypoint.address.coordinates.lat,
createdAt: now,
updatedAt: now,
})),
},
createdAt: copy.createdAt,
updatedAt: copy.updatedAt,
waypoints: copy.waypoints
? {
create: copy.waypoints.map((waypoint: WaypointProps) => ({
uuid: v4(),
position: waypoint.position,
name: waypoint.address.name,
houseNumber: waypoint.address.houseNumber,
street: waypoint.address.street,
locality: waypoint.address.locality,
postalCode: waypoint.address.postalCode,
country: waypoint.address.country,
lon: waypoint.address.coordinates.lon,
lat: waypoint.address.coordinates.lat,
createdAt: now,
updatedAt: now,
})),
}
: undefined,
};
return record;
};
@ -97,7 +99,7 @@ export class AdMapper
frequency: record.frequency as Frequency,
fromDate: record.fromDate.toISOString().split('T')[0],
toDate: record.toDate.toISOString().split('T')[0],
schedule: record.schedule.map((scheduleItem: ScheduleItemModel) => ({
schedule: record.schedule?.map((scheduleItem: ScheduleItemModel) => ({
day: scheduleItem.day,
time: `${scheduleItem.time
.getUTCHours()
@ -111,7 +113,7 @@ export class AdMapper
seatsProposed: record.seatsProposed,
seatsRequested: record.seatsRequested,
strict: record.strict,
waypoints: record.waypoints.map((waypoint: WaypointModel) => ({
waypoints: record.waypoints?.map((waypoint: WaypointModel) => ({
position: waypoint.position,
address: {
name: waypoint.name,

View File

@ -24,22 +24,26 @@ export type AdBaseModel = {
seatsProposed: number;
seatsRequested: number;
strict: boolean;
createdAt: Date;
updatedAt: Date;
};
export type AdReadModel = AdBaseModel & {
waypoints: WaypointModel[];
schedule: ScheduleItemModel[];
createdAt: Date;
updatedAt: Date;
};
export type AdWriteModel = AdBaseModel & {
waypoints: {
create: WaypointModel[];
};
schedule: {
create: ScheduleItemModel[];
};
schedule: ScheduleWriteModel | undefined;
waypoints: WaypointWriteModel | undefined;
};
export type ScheduleWriteModel = {
create: ScheduleItemModel[];
};
export type WaypointWriteModel = {
create: WaypointModel[];
};
export type ScheduleItemModel = {

View File

@ -144,9 +144,9 @@ describe('Ad Mapper', () => {
it('should map domain entity to persistence data', async () => {
const mapped: AdWriteModel = adMapper.toPersistence(adEntity);
expect(mapped.waypoints.create[0].uuid.length).toBe(36);
expect(mapped.waypoints.create[1].uuid.length).toBe(36);
expect(mapped.schedule.create.length).toBe(1);
expect(mapped.waypoints?.create[0].uuid.length).toBe(36);
expect(mapped.waypoints?.create[1].uuid.length).toBe(36);
expect(mapped.schedule?.create.length).toBe(1);
});
it('should map persisted data to domain entity', async () => {