extract timezone and timeconverter to infrastructure

This commit is contained in:
sbriat 2023-06-22 11:40:31 +02:00
parent 22565eb253
commit 4ad00b96c0
9 changed files with 175 additions and 63 deletions

View File

@ -1,3 +1,4 @@
export const PARAMS_PROVIDER = Symbol('PARAMS_PROVIDER'); export const PARAMS_PROVIDER = Symbol('PARAMS_PROVIDER');
export const TIMEZONE_FINDER = Symbol('TIMEZONE_FINDER'); export const TIMEZONE_FINDER = Symbol('TIMEZONE_FINDER');
export const TIME_CONVERTER = Symbol('TIME_CONVERTER');
export const AD_REPOSITORY = Symbol('AD_REPOSITORY'); export const AD_REPOSITORY = Symbol('AD_REPOSITORY');

View File

@ -10,12 +10,15 @@ import {
import { Frequency } from './core/ad.types'; import { Frequency } from './core/ad.types';
import { WaypointProps } from './core/value-objects/waypoint.value-object'; import { WaypointProps } from './core/value-objects/waypoint.value-object';
import { v4 } from 'uuid'; import { v4 } from 'uuid';
import { PARAMS_PROVIDER, TIMEZONE_FINDER } from './ad.di-tokens'; import {
PARAMS_PROVIDER,
TIMEZONE_FINDER,
TIME_CONVERTER,
} from './ad.di-tokens';
import { TimezoneFinderPort } from './core/ports/timezone-finder.port'; import { TimezoneFinderPort } from './core/ports/timezone-finder.port';
import { Coordinates } from './core/types/coordinates';
import { DefaultParamsProviderPort } from './core/ports/default-params-provider.port'; import { DefaultParamsProviderPort } from './core/ports/default-params-provider.port';
import { DefaultParams } from './core/ports/default-params.type'; import { DefaultParams } from './core/ports/default-params.type';
import { DateTime, TimeZone } from 'timezonecomplete'; import { TimeConverterPort } from './core/ports/time-converter.port';
/** /**
* Mapper constructs objects that are used in different layers: * Mapper constructs objects that are used in different layers:
@ -35,13 +38,20 @@ export class AdMapper
private readonly defaultParamsProvider: DefaultParamsProviderPort, private readonly defaultParamsProvider: DefaultParamsProviderPort,
@Inject(TIMEZONE_FINDER) @Inject(TIMEZONE_FINDER)
private readonly timezoneFinder: TimezoneFinderPort, private readonly timezoneFinder: TimezoneFinderPort,
@Inject(TIME_CONVERTER)
private readonly timeConverter: TimeConverterPort,
) { ) {
this.defaultParams = defaultParamsProvider.getParams(); this.defaultParams = defaultParamsProvider.getParams();
} }
toPersistence = (entity: AdEntity): AdWriteModel => { toPersistence = (entity: AdEntity): AdWriteModel => {
const copy = entity.getProps(); const copy = entity.getProps();
const timezone = this.getTimezone(copy.waypoints[0].address.coordinates); const { lon, lat } = copy.waypoints[0].address.coordinates;
const timezone = this.timezoneFinder.timezones(
lon,
lat,
this.defaultParams.DEFAULT_TIMEZONE,
)[0];
const now = new Date(); const now = new Date();
const record: AdWriteModel = { const record: AdWriteModel = {
uuid: copy.id, uuid: copy.id,
@ -52,50 +62,50 @@ export class AdMapper
fromDate: new Date(copy.fromDate), fromDate: new Date(copy.fromDate),
toDate: new Date(copy.toDate), toDate: new Date(copy.toDate),
monTime: copy.schedule.mon monTime: copy.schedule.mon
? AdMapper.toUtcDatetime( ? this.timeConverter.dateTimeToUtc(
new Date(copy.fromDate), copy.fromDate,
copy.schedule.mon, copy.schedule.mon,
timezone, timezone,
) )
: undefined, : undefined,
tueTime: copy.schedule.tue tueTime: copy.schedule.tue
? AdMapper.toUtcDatetime( ? this.timeConverter.dateTimeToUtc(
new Date(copy.fromDate), copy.fromDate,
copy.schedule.tue, copy.schedule.tue,
timezone, timezone,
) )
: undefined, : undefined,
wedTime: copy.schedule.wed wedTime: copy.schedule.wed
? AdMapper.toUtcDatetime( ? this.timeConverter.dateTimeToUtc(
new Date(copy.fromDate), copy.fromDate,
copy.schedule.wed, copy.schedule.wed,
timezone, timezone,
) )
: undefined, : undefined,
thuTime: copy.schedule.thu thuTime: copy.schedule.thu
? AdMapper.toUtcDatetime( ? this.timeConverter.dateTimeToUtc(
new Date(copy.fromDate), copy.fromDate,
copy.schedule.thu, copy.schedule.thu,
timezone, timezone,
) )
: undefined, : undefined,
friTime: copy.schedule.fri friTime: copy.schedule.fri
? AdMapper.toUtcDatetime( ? this.timeConverter.dateTimeToUtc(
new Date(copy.fromDate), copy.fromDate,
copy.schedule.fri, copy.schedule.fri,
timezone, timezone,
) )
: undefined, : undefined,
satTime: copy.schedule.sat satTime: copy.schedule.sat
? AdMapper.toUtcDatetime( ? this.timeConverter.dateTimeToUtc(
new Date(copy.fromDate), copy.fromDate,
copy.schedule.sat, copy.schedule.sat,
timezone, timezone,
) )
: undefined, : undefined,
sunTime: copy.schedule.sun sunTime: copy.schedule.sun
? AdMapper.toUtcDatetime( ? this.timeConverter.dateTimeToUtc(
new Date(copy.fromDate), copy.fromDate,
copy.schedule.sun, copy.schedule.sun,
timezone, timezone,
) )
@ -199,35 +209,4 @@ export class AdMapper
(avoid blacklisting, which will return everything (avoid blacklisting, which will return everything
but blacklisted items, which can lead to a data leak). but blacklisted items, which can lead to a data leak).
*/ */
private getTimezone = (coordinates: Coordinates): string => {
try {
const timezones = this.timezoneFinder.timezones(
coordinates.lon,
coordinates.lat,
);
if (timezones.length > 0) return timezones[0];
} catch (e) {}
return this.defaultParams.DEFAULT_TIMEZONE;
};
private static toUtcDatetime = (
date: Date,
time: string,
timezone: string,
): Date => {
try {
if (!date || !time || !timezone) throw new Error();
return new Date(
new DateTime(
`${date.toISOString().split('T')[0]}T${time}:00`,
TimeZone.zone(timezone, false),
)
.convert(TimeZone.zone('UTC'))
.toIsoString(),
);
} catch (e) {
return undefined;
}
};
} }

View File

@ -5,6 +5,7 @@ import {
AD_REPOSITORY, AD_REPOSITORY,
PARAMS_PROVIDER, PARAMS_PROVIDER,
TIMEZONE_FINDER, TIMEZONE_FINDER,
TIME_CONVERTER,
} from './ad.di-tokens'; } from './ad.di-tokens';
import { import {
MESSAGE_BROKER_PUBLISHER, MESSAGE_BROKER_PUBLISHER,
@ -18,6 +19,7 @@ import { AdMapper } from './ad.mapper';
import { CreateAdService } from './core/commands/create-ad/create-ad.service'; import { CreateAdService } from './core/commands/create-ad/create-ad.service';
import { TimezoneFinder } from './infrastructure/timezone-finder'; import { TimezoneFinder } from './infrastructure/timezone-finder';
import { PrismaService } from '@libs/db/prisma.service'; import { PrismaService } from '@libs/db/prisma.service';
import { TimeConverter } from './infrastructure/time-converter';
@Module({ @Module({
imports: [CqrsModule], imports: [CqrsModule],
@ -46,6 +48,10 @@ import { PrismaService } from '@libs/db/prisma.service';
provide: TIMEZONE_FINDER, provide: TIMEZONE_FINDER,
useClass: TimezoneFinder, useClass: TimezoneFinder,
}, },
{
provide: TIME_CONVERTER,
useClass: TimeConverter,
},
], ],
exports: [ exports: [
PrismaService, PrismaService,

View File

@ -0,0 +1,8 @@
export interface TimeConverterPort {
dateTimeToUtc(
date: string,
time: string,
timezone: string,
dst?: boolean,
): Date;
}

View File

@ -1,3 +1,3 @@
export interface TimezoneFinderPort { export interface TimezoneFinderPort {
timezones(lon: number, lat: number): string[]; timezones(lon: number, lat: number, defaultTimezone?: string): string[];
} }

View File

@ -0,0 +1,24 @@
import { Injectable } from '@nestjs/common';
import { TimeConverterPort } from '../core/ports/time-converter.port';
import { DateTime, TimeZone } from 'timezonecomplete';
@Injectable()
export class TimeConverter implements TimeConverterPort {
dateTimeToUtc = (
date: string,
time: string,
timezone: string,
dst?: boolean,
): Date => {
try {
if (!date || !time || !timezone) throw new Error();
return new Date(
new DateTime(`${date}T${time}`, TimeZone.zone(timezone, dst))
.convert(TimeZone.zone('UTC'))
.toIsoString(),
);
} catch (e) {
return undefined;
}
};
}

View File

@ -4,5 +4,13 @@ import { find } from 'geo-tz';
@Injectable() @Injectable()
export class TimezoneFinder implements TimezoneFinderPort { export class TimezoneFinder implements TimezoneFinderPort {
timezones = (lon: number, lat: number): string[] => find(lat, lon); timezones = (
lon: number,
lat: number,
defaultTimezone?: string,
): string[] => {
const foundTimezones = find(lat, lon);
if (defaultTimezone && foundTimezones.length == 0) return [defaultTimezone];
return foundTimezones;
};
} }

View File

@ -1,13 +1,19 @@
import { PARAMS_PROVIDER, TIMEZONE_FINDER } from '@modules/ad/ad.di-tokens'; import {
PARAMS_PROVIDER,
TIMEZONE_FINDER,
TIME_CONVERTER,
} from '@modules/ad/ad.di-tokens';
import { AdMapper } from '@modules/ad/ad.mapper'; import { AdMapper } from '@modules/ad/ad.mapper';
import { AdEntity } from '@modules/ad/core/ad.entity'; import { AdEntity } from '@modules/ad/core/ad.entity';
import { Frequency } from '@modules/ad/core/ad.types'; import { Frequency } from '@modules/ad/core/ad.types';
import { DefaultParamsProviderPort } from '@modules/ad/core/ports/default-params-provider.port'; import { DefaultParamsProviderPort } from '@modules/ad/core/ports/default-params-provider.port';
import { TimeConverterPort } from '@modules/ad/core/ports/time-converter.port';
import { TimezoneFinderPort } from '@modules/ad/core/ports/timezone-finder.port'; import { TimezoneFinderPort } from '@modules/ad/core/ports/timezone-finder.port';
import { import {
AdReadModel, AdReadModel,
AdWriteModel, AdWriteModel,
} from '@modules/ad/infrastructure/ad.repository'; } from '@modules/ad/infrastructure/ad.repository';
import { AdResponseDto } from '@modules/ad/interface/dtos/ad.response.dto';
import { Test } from '@nestjs/testing'; import { Test } from '@nestjs/testing';
const now = new Date('2023-06-21 06:00:00'); const now = new Date('2023-06-21 06:00:00');
@ -21,7 +27,13 @@ const adEntity: AdEntity = new AdEntity({
fromDate: '2023-06-21', fromDate: '2023-06-21',
toDate: '2023-06-21', toDate: '2023-06-21',
schedule: { schedule: {
mon: '07:15',
tue: '07:15',
wed: '07:15', wed: '07:15',
thu: '07:15',
fri: '07:15',
sat: '07:15',
sun: '07:15',
}, },
waypoints: [ waypoints: [
{ {
@ -33,8 +45,8 @@ const adEntity: AdEntity = new AdEntity({
postalCode: '54000', postalCode: '54000',
country: 'France', country: 'France',
coordinates: { coordinates: {
lon: 48.68944505415954, lat: 48.68944505415954,
lat: 6.176510296462267, lon: 6.176510296462267,
}, },
}, },
}, },
@ -45,8 +57,8 @@ const adEntity: AdEntity = new AdEntity({
postalCode: '75000', postalCode: '75000',
country: 'France', country: 'France',
coordinates: { coordinates: {
lon: 48.8566, lat: 48.8566,
lat: 2.3522, lon: 2.3522,
}, },
}, },
}, },
@ -91,8 +103,8 @@ const adReadModel: AdReadModel = {
locality: 'Nancy', locality: 'Nancy',
postalCode: '54000', postalCode: '54000',
country: 'France', country: 'France',
lon: 48.68944505415954, lat: 48.68944505415954,
lat: 6.176510296462267, lon: 6.176510296462267,
createdAt: now, createdAt: now,
updatedAt: now, updatedAt: now,
}, },
@ -102,8 +114,8 @@ const adReadModel: AdReadModel = {
locality: 'Paris', locality: 'Paris',
postalCode: '75000', postalCode: '75000',
country: 'France', country: 'France',
lon: 48.8566, lat: 48.8566,
lat: 2.3522, lon: 2.3522,
createdAt: now, createdAt: now,
updatedAt: now, updatedAt: now,
}, },
@ -143,7 +155,20 @@ const mockDefaultParamsProvider: DefaultParamsProviderPort = {
}; };
const mockTimezoneFinder: TimezoneFinderPort = { const mockTimezoneFinder: TimezoneFinderPort = {
timezones: jest.fn(), // eslint-disable-next-line @typescript-eslint/no-unused-vars
timezones: jest.fn().mockImplementation((lon: number, lat: number) => {
if (lon < 60) return 'Europe/Paris';
return 'America/New_York';
}),
};
const mockTimeConverter: TimeConverterPort = {
dateTimeToUtc: jest
.fn()
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.mockImplementation((datetime: Date, timezone: string, dst?: boolean) => {
return datetime;
}),
}; };
describe('Ad Mapper', () => { describe('Ad Mapper', () => {
@ -161,6 +186,10 @@ describe('Ad Mapper', () => {
provide: TIMEZONE_FINDER, provide: TIMEZONE_FINDER,
useValue: mockTimezoneFinder, useValue: mockTimezoneFinder,
}, },
{
provide: TIME_CONVERTER,
useValue: mockTimeConverter,
},
], ],
}).compile(); }).compile();
adMapper = module.get<AdMapper>(AdMapper); adMapper = module.get<AdMapper>(AdMapper);
@ -178,9 +207,14 @@ describe('Ad Mapper', () => {
it('should map persisted data to domain entity', async () => { it('should map persisted data to domain entity', async () => {
const mapped: AdEntity = adMapper.toDomain(adReadModel); const mapped: AdEntity = adMapper.toDomain(adReadModel);
expect(mapped.getProps().waypoints[0].address.coordinates.lon).toBe( expect(mapped.getProps().waypoints[0].address.coordinates.lat).toBe(
48.68944505415954, 48.68944505415954,
); );
expect(mapped.getProps().waypoints[1].address.coordinates.lat).toBe(2.3522); expect(mapped.getProps().waypoints[1].address.coordinates.lon).toBe(2.3522);
});
it('should map domain entity to response', async () => {
const mapped: AdResponseDto = adMapper.toResponse(adEntity);
expect(mapped.uuid).toBe('c160cf8c-f057-4962-841f-3ad68346df44');
}); });
}); });

View File

@ -0,0 +1,52 @@
import { TimeConverter } from '@modules/ad/infrastructure/time-converter';
describe('Time Converter', () => {
it('should be defined', () => {
const timeConverter: TimeConverter = new TimeConverter();
expect(timeConverter).toBeDefined();
});
it('should convert a paris datetime to utc', () => {
const timeConverter: TimeConverter = new TimeConverter();
const parisDate = '2023-06-22';
const parisTime = '08:00';
const utcDatetime = timeConverter.dateTimeToUtc(
parisDate,
parisTime,
'Europe/Paris',
);
expect(utcDatetime.toISOString()).toEqual('2023-06-22T06:00:00.000Z');
});
it('should return undefined if date is invalid', () => {
const timeConverter: TimeConverter = new TimeConverter();
const parisDate = '2023-16-22';
const parisTime = '08:00';
const utcDatetime = timeConverter.dateTimeToUtc(
parisDate,
parisTime,
'Europe/Paris',
);
expect(utcDatetime).toBeUndefined();
});
it('should return undefined if time is invalid', () => {
const timeConverter: TimeConverter = new TimeConverter();
const parisDate = '2023-06-22';
const parisTime = '28:00';
const utcDatetime = timeConverter.dateTimeToUtc(
parisDate,
parisTime,
'Europe/Paris',
);
expect(utcDatetime).toBeUndefined();
});
it('should return undefined if timezone is invalid', () => {
const timeConverter: TimeConverter = new TimeConverter();
const parisDate = '2023-06-22';
const parisTime = '08:00';
const utcDatetime = timeConverter.dateTimeToUtc(
parisDate,
parisTime,
'Foo/Bar',
);
expect(utcDatetime).toBeUndefined();
});
});