Skip to content

Commit

Permalink
fix(irec): respect timezone when formatting date (device, issue) to y…
Browse files Browse the repository at this point in the history
…ear-month-day
  • Loading branch information
soanvig committed Jun 30, 2022
1 parent 4c074ae commit 5acf662
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
9 changes: 5 additions & 4 deletions packages/traceability/issuer-irec-api-wrapper/src/Device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
IsString
} from 'class-validator';
import { FileIds } from './File';
import { timeToTimezoneDate } from './helpers';

export enum DeviceState {
Draft = 'Draft',
Expand Down Expand Up @@ -51,15 +52,15 @@ 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 })
@IsDate()
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;
Expand Down Expand Up @@ -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 })
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions packages/traceability/issuer-irec-api-wrapper/src/Issue.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions packages/traceability/issuer-irec-api-wrapper/src/Transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
IsString,
ValidateNested
} from 'class-validator';
import { timeToTimezoneDate } from './helpers';

export class ReservationItem {
@Expose({ name: 'item_code', toPlainOnly: true })
Expand Down Expand Up @@ -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;

Expand Down
20 changes: 20 additions & 0 deletions packages/traceability/issuer-irec-api-wrapper/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -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];
}
};

0 comments on commit 5acf662

Please sign in to comment.