fix utc conversion for schedule

This commit is contained in:
sbriat 2023-05-02 11:56:07 +02:00
parent 1f9a9896e9
commit e312a6c712
3 changed files with 93 additions and 19 deletions

View File

@ -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;

View File

@ -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;
}

View File

@ -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,
),
),
),
);
};
}