diff --git a/src/common/dayjs.ts b/src/common/dayjs.ts index 7e0f70d..3a8a6cd 100644 --- a/src/common/dayjs.ts +++ b/src/common/dayjs.ts @@ -1,5 +1,6 @@ import 'dayjs/locale/nl' +import { TransformationType, TransformFnParams } from 'class-transformer' import * as dayjs from 'dayjs' import * as customParseFormat from 'dayjs/plugin/customParseFormat' import * as isSameOrBefore from 'dayjs/plugin/isSameOrBefore' @@ -41,4 +42,20 @@ const dayjsTz = ( return dayjs(date, format).tz('Europe/Amsterdam') } +export const DayjsTransformer = ({ value, type }: TransformFnParams) => { + switch (type) { + case TransformationType.PLAIN_TO_CLASS: + return dayjs(value) + case TransformationType.CLASS_TO_PLAIN: + return value.format() + default: + return value + } +} + +export const DayjsColumnTransformer = { + to: (value: dayjs.Dayjs) => value.format(), + from: (value: Date) => dayjs(value), +} + export default dayjsTz diff --git a/src/reservations/entity.ts b/src/reservations/entity.ts index f585dfa..1948fe4 100644 --- a/src/reservations/entity.ts +++ b/src/reservations/entity.ts @@ -1,9 +1,11 @@ import { Exclude, Transform, Type } from 'class-transformer' -import { TransformationType } from 'class-transformer' import { Dayjs } from 'dayjs' import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm' -import dayjs from '../common/dayjs' +import dayjs, { + DayjsColumnTransformer, + DayjsTransformer, +} from '../common/dayjs' export interface Opponent { id: string @@ -20,42 +22,18 @@ export class Reservation { @Column('datetime', { nullable: false, - transformer: { - to: (value: Dayjs) => value.format(), - from: (value: Date) => dayjs(value), - }, + transformer: DayjsColumnTransformer, }) @Type(() => Dayjs) - @Transform(({ value, type }) => { - switch (type) { - case TransformationType.PLAIN_TO_CLASS: - return dayjs(value) - case TransformationType.CLASS_TO_PLAIN: - return value.format() - default: - return value - } - }) + @Transform(DayjsTransformer) dateRangeStart: Dayjs @Column('datetime', { nullable: false, - transformer: { - to: (value: Dayjs) => value.format(), - from: (value: Date) => dayjs(value), - }, + transformer: DayjsColumnTransformer, }) @Type(() => Dayjs) - @Transform(({ value, type }) => { - switch (type) { - case TransformationType.PLAIN_TO_CLASS: - return dayjs(value) - case TransformationType.CLASS_TO_PLAIN: - return value.format() - default: - return value - } - }) + @Transform(DayjsTransformer) dateRangeEnd: Dayjs @Column('json', { nullable: false })