From e312a6c712809b8f5c75261991fb5e2ef4f2ee5d Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 2 May 2023 11:56:07 +0200 Subject: [PATCH] fix utc conversion for schedule --- src/modules/ad/domain/entities/ad.ts | 12 +-- .../ad/domain/entities/time-converter.ts | 16 ++++ src/modules/ad/mappers/ad.profile.ts | 84 ++++++++++++++++--- 3 files changed, 93 insertions(+), 19 deletions(-) create mode 100644 src/modules/ad/domain/entities/time-converter.ts diff --git a/src/modules/ad/domain/entities/ad.ts b/src/modules/ad/domain/entities/ad.ts index f19b5e0..1e4d1d6 100644 --- a/src/modules/ad/domain/entities/ad.ts +++ b/src/modules/ad/domain/entities/ad.ts @@ -25,22 +25,22 @@ export class Ad { monTime: Date; @AutoMap() - tueTime: string; + tueTime: Date; @AutoMap() - wedTime: string; + wedTime: Date; @AutoMap() - thuTime: string; + thuTime: Date; @AutoMap() - friTime: string; + friTime: Date; @AutoMap() - satTime: string; + satTime: Date; @AutoMap() - sunTime: string; + sunTime: Date; @AutoMap() monMargin: number; diff --git a/src/modules/ad/domain/entities/time-converter.ts b/src/modules/ad/domain/entities/time-converter.ts new file mode 100644 index 0000000..b5910e6 --- /dev/null +++ b/src/modules/ad/domain/entities/time-converter.ts @@ -0,0 +1,16 @@ +import { DateTime, TimeZone } from 'timezonecomplete'; + +export class TimeConverter { + static toUtcDatetime = ( + date: string, + time: string, + ianaTimezone: string, + ): Date => + date && time + ? new Date( + new DateTime(`${date}T${time}:00`, TimeZone.zone(ianaTimezone, false)) + .convert(TimeZone.zone('UTC')) + .toIsoString(), + ) + : undefined; +} diff --git a/src/modules/ad/mappers/ad.profile.ts b/src/modules/ad/mappers/ad.profile.ts index 3bd2eed..6cf8d8a 100644 --- a/src/modules/ad/mappers/ad.profile.ts +++ b/src/modules/ad/mappers/ad.profile.ts @@ -5,7 +5,7 @@ import { Ad } from '../domain/entities/ad'; import { AdPresenter } from '../adapters/primaries/ad.presenter'; import { CreateAdRequest } from '../domain/dtos/create-ad.request'; import { Coordinates } from '../../geography/domain/entities/coordinates'; -import moment from 'moment-timezone'; +import { TimeConverter } from '../domain/entities/time-converter'; @Injectable() export class AdProfile extends AutomapperProfile { @@ -53,18 +53,76 @@ export class AdProfile extends AutomapperProfile { (dest) => dest.updatedAt, mapFrom((source) => new Date(source.updatedAt)), ), - // forMember( - // (dest) => dest.monTime, - // mapFrom((source) => - // source.monTime - // ? new Date( - // moment - // .tz(`${source.fromDate} ${source.monTime}`, source.timezone) - // .format(), - // ) - // : undefined, - // ), - // ), + forMember( + (dest) => dest.monTime, + mapFrom((source) => + TimeConverter.toUtcDatetime( + source.fromDate, + source.monTime, + source.timezone, + ), + ), + ), + forMember( + (dest) => dest.tueTime, + mapFrom((source) => + TimeConverter.toUtcDatetime( + source.fromDate, + source.tueTime, + source.timezone, + ), + ), + ), + forMember( + (dest) => dest.wedTime, + mapFrom((source) => + TimeConverter.toUtcDatetime( + source.fromDate, + source.wedTime, + source.timezone, + ), + ), + ), + forMember( + (dest) => dest.thuTime, + mapFrom((source) => + TimeConverter.toUtcDatetime( + source.fromDate, + source.thuTime, + source.timezone, + ), + ), + ), + forMember( + (dest) => dest.friTime, + mapFrom((source) => + TimeConverter.toUtcDatetime( + source.fromDate, + source.friTime, + source.timezone, + ), + ), + ), + forMember( + (dest) => dest.satTime, + mapFrom((source) => + TimeConverter.toUtcDatetime( + source.fromDate, + source.satTime, + source.timezone, + ), + ), + ), + forMember( + (dest) => dest.sunTime, + mapFrom((source) => + TimeConverter.toUtcDatetime( + source.fromDate, + source.sunTime, + source.timezone, + ), + ), + ), ); }; }