diff --git a/packages/traceability/issuer-irec-api-wrapper/src/Device.ts b/packages/traceability/issuer-irec-api-wrapper/src/Device.ts index 51aa8d67f7..0b66ed292d 100644 --- a/packages/traceability/issuer-irec-api-wrapper/src/Device.ts +++ b/packages/traceability/issuer-irec-api-wrapper/src/Device.ts @@ -10,6 +10,7 @@ import { IsString } from 'class-validator'; import { FileIds } from './File'; +import { timeToTimezoneDate } from './helpers'; export enum DeviceState { Draft = 'Draft', @@ -51,7 +52,7 @@ export class DeviceCreateParams extends FileIds { capacity: string; @Expose({ name: 'commissioning_date', toPlainOnly: true }) - @Transform((value: Date) => value?.toISOString().split('T')[0], { + @Transform((value: Date) => timeToTimezoneDate(value), { toPlainOnly: true }) @Transform((value: string) => new Date(value), { toClassOnly: true }) @@ -59,7 +60,7 @@ export class DeviceCreateParams extends FileIds { commissioningDate: Date; @Expose({ name: 'registration_date', toPlainOnly: true }) - @Transform((value: Date) => value?.toISOString().split('T')[0], { toPlainOnly: true }) + @Transform((value: Date) => timeToTimezoneDate(value), { toPlainOnly: true }) @Transform((value: string) => new Date(value), { toClassOnly: true }) @IsDate() registrationDate: Date; @@ -124,7 +125,7 @@ export class DeviceUpdateParams extends FileIds { @IsOptional() @Expose({ name: 'commissioning_date', toPlainOnly: true }) - @Transform((value: Date) => value?.toISOString().split('T')[0], { + @Transform((value: Date) => timeToTimezoneDate(value), { toPlainOnly: true }) @Transform((value: string) => new Date(value), { toClassOnly: true }) @@ -133,7 +134,7 @@ export class DeviceUpdateParams extends FileIds { @IsOptional() @Expose({ name: 'registration_date', toPlainOnly: true }) - @Transform((value: Date) => value?.toISOString().split('T')[0], { toPlainOnly: true }) + @Transform((value: Date) => timeToTimezoneDate(value), { toPlainOnly: true }) @Transform((value: string) => new Date(value), { toClassOnly: true }) @IsDate() registrationDate?: Date; diff --git a/packages/traceability/issuer-irec-api-wrapper/src/Issue.ts b/packages/traceability/issuer-irec-api-wrapper/src/Issue.ts index fa3a4bcc51..b067d7aa66 100644 --- a/packages/traceability/issuer-irec-api-wrapper/src/Issue.ts +++ b/packages/traceability/issuer-irec-api-wrapper/src/Issue.ts @@ -1,6 +1,7 @@ import { Expose, Transform } from 'class-transformer'; import { IsDate, IsEnum, IsNotEmpty, IsOptional, IsPositive, IsString } from 'class-validator'; import { FileIds } from './File'; +import { timeToTimezoneDate } from './helpers'; export enum IssuanceStatus { Draft = 'Draft', @@ -31,13 +32,13 @@ export class Issue extends FileIds { recipient: string; @Expose({ name: 'start_date', toPlainOnly: true }) - @Transform((value: Date) => value.toISOString().split('T')[0], { toPlainOnly: true }) + @Transform((value: Date) => timeToTimezoneDate(value), { toPlainOnly: true }) @Transform((value: string) => new Date(value), { toClassOnly: true }) @IsDate() start: Date; @Expose({ name: 'end_date', toPlainOnly: true }) - @Transform((value: Date) => value.toISOString().split('T')[0], { toPlainOnly: true }) + @Transform((value: Date) => timeToTimezoneDate(value), { toPlainOnly: true }) @Transform((value: string) => new Date(value), { toClassOnly: true }) @IsDate() end: Date; diff --git a/packages/traceability/issuer-irec-api-wrapper/src/Transfer.ts b/packages/traceability/issuer-irec-api-wrapper/src/Transfer.ts index 645de943d6..41fa367c53 100644 --- a/packages/traceability/issuer-irec-api-wrapper/src/Transfer.ts +++ b/packages/traceability/issuer-irec-api-wrapper/src/Transfer.ts @@ -8,6 +8,7 @@ import { IsString, ValidateNested } from 'class-validator'; +import { timeToTimezoneDate } from './helpers'; export class ReservationItem { @Expose({ name: 'item_code', toPlainOnly: true }) @@ -55,12 +56,12 @@ export class Redemption extends Transfer { beneficiary: number; @Expose({ name: 'period_start', toPlainOnly: true }) - @Transform((value: Date) => value.toISOString().split('T')[0], { toPlainOnly: true }) + @Transform((value: Date) => timeToTimezoneDate(value), { toPlainOnly: true }) @IsDate() start: Date; @Expose({ name: 'period_end', toPlainOnly: true }) - @Transform((value: Date) => value.toISOString().split('T')[0], { toPlainOnly: true }) + @Transform((value: Date) => timeToTimezoneDate(value), { toPlainOnly: true }) @IsDate() end: Date; diff --git a/packages/traceability/issuer-irec-api-wrapper/src/helpers.ts b/packages/traceability/issuer-irec-api-wrapper/src/helpers.ts new file mode 100644 index 0000000000..3de0f7e4f1 --- /dev/null +++ b/packages/traceability/issuer-irec-api-wrapper/src/helpers.ts @@ -0,0 +1,20 @@ +/** + * Formats date object to date (YYYY-MM-DD). + * It respects timezone only if `process.env.TZ` is set. + * If not, it will format date for UTC. + */ +export const timeToTimezoneDate = (time?: Date) => { + if (!time) { + return time; + } + + if (process.env.TZ) { + const year = time.getFullYear(); + const month = time.getMonth() + 1; + const day = time.getDate(); + + return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`; + } else { + return time.toISOString().split('T')[0]; + } +};