From c7d47928931fe8717d91e34dddb50c396361761b Mon Sep 17 00:00:00 2001 From: Romain Thouvenin Date: Mon, 29 Apr 2024 16:11:12 +0200 Subject: [PATCH] Consistent and DRY declarations of ScheduleItem types --- src/modules/ad/ad.mapper.ts | 30 +++++++++---------- .../commands/create-ad/create-ad.command.ts | 8 ++--- .../commands/create-ad/create-ad.service.ts | 16 +++++----- .../core/application/types/schedule-item.ts | 5 ---- src/modules/ad/core/domain/ad.entity.ts | 4 +-- .../domain/events/ad-created.domain-event.ts | 9 ++---- .../schedule-item.value-object.ts | 8 ++--- tests/unit/ad/core/ad.fixtures.ts | 2 ++ .../find-ads-by-ids.query-handler.spec.ts | 6 ++-- .../find-ads-by-user-id.query-handler.spec.ts | 6 ++-- .../ad/core/invalidate-ad.service.spec.ts | 10 ++++--- .../unit/ad/core/validate-ad.service.spec.ts | 10 ++++--- 12 files changed, 57 insertions(+), 57 deletions(-) delete mode 100644 src/modules/ad/core/application/types/schedule-item.ts diff --git a/src/modules/ad/ad.mapper.ts b/src/modules/ad/ad.mapper.ts index eb3bf11..255f4fa 100644 --- a/src/modules/ad/ad.mapper.ts +++ b/src/modules/ad/ad.mapper.ts @@ -1,19 +1,19 @@ import { Mapper } from '@mobicoop/ddd-library'; -import { AdResponseDto } from './interface/dtos/ad.response.dto'; import { Inject, Injectable } from '@nestjs/common'; -import { AdEntity } from './core/domain/ad.entity'; -import { - AdWriteModel, - AdReadModel, - WaypointModel, - ScheduleItemModel, -} from './infrastructure/ad.repository'; -import { Frequency, Status } from './core/domain/ad.types'; -import { WaypointProps } from './core/domain/value-objects/waypoint.value-object'; import { v4 } from 'uuid'; -import { ScheduleItemProps } from './core/domain/value-objects/schedule-item.value-object'; import { OUTPUT_DATETIME_TRANSFORMER } from './ad.di-tokens'; import { DateTimeTransformerPort } from './core/application/ports/datetime-transformer.port'; +import { AdEntity } from './core/domain/ad.entity'; +import { Frequency, Status } from './core/domain/ad.types'; +import { ScheduleItemProps } from './core/domain/value-objects/schedule-item.value-object'; +import { WaypointProps } from './core/domain/value-objects/waypoint.value-object'; +import { + AdReadModel, + AdWriteModel, + ScheduleItemModel, + WaypointModel, +} from './infrastructure/ad.repository'; +import { AdResponseDto } from './interface/dtos/ad.response.dto'; /** * Mapper constructs objects that are used in different layers: @@ -47,7 +47,7 @@ export class AdMapper ? { create: copy.schedule.map((scheduleItem: ScheduleItemProps) => ({ uuid: v4(), - day: scheduleItem.day as number, + day: scheduleItem.day, time: new Date( 1970, 0, @@ -55,7 +55,7 @@ export class AdMapper parseInt(scheduleItem.time.split(':')[0]), parseInt(scheduleItem.time.split(':')[1]), ), - margin: scheduleItem.margin as number, + margin: scheduleItem.margin, createdAt: now, updatedAt: now, })), @@ -163,7 +163,7 @@ export class AdMapper response.schedule = props.schedule.map( (scheduleItem: ScheduleItemProps) => ({ day: this.outputDatetimeTransformer.day( - scheduleItem.day as number, + scheduleItem.day, { date: props.fromDate, time: scheduleItem.time, @@ -179,7 +179,7 @@ export class AdMapper }, props.frequency, ), - margin: scheduleItem.margin as number, + margin: scheduleItem.margin, }), ); response.seatsProposed = props.seatsProposed as number; diff --git a/src/modules/ad/core/application/commands/create-ad/create-ad.command.ts b/src/modules/ad/core/application/commands/create-ad/create-ad.command.ts index 7a939c3..fefe004 100644 --- a/src/modules/ad/core/application/commands/create-ad/create-ad.command.ts +++ b/src/modules/ad/core/application/commands/create-ad/create-ad.command.ts @@ -1,7 +1,7 @@ -import { ScheduleItem } from '../../types/schedule-item'; -import { Waypoint } from '../../types/waypoint'; -import { Frequency } from '@modules/ad/core/domain/ad.types'; import { Command, CommandProps } from '@mobicoop/ddd-library'; +import { Frequency } from '@modules/ad/core/domain/ad.types'; +import { ScheduleItemProps } from '@modules/ad/core/domain/value-objects/schedule-item.value-object'; +import { Waypoint } from '../../types/waypoint'; export class CreateAdCommand extends Command { readonly userId: string; @@ -10,7 +10,7 @@ export class CreateAdCommand extends Command { readonly frequency: Frequency; readonly fromDate: string; readonly toDate: string; - readonly schedule: ScheduleItem[]; + readonly schedule: ScheduleItemProps[]; readonly seatsProposed?: number; readonly seatsRequested?: number; readonly strict: boolean; diff --git a/src/modules/ad/core/application/commands/create-ad/create-ad.service.ts b/src/modules/ad/core/application/commands/create-ad/create-ad.service.ts index 5d1d30c..5b3abc3 100644 --- a/src/modules/ad/core/application/commands/create-ad/create-ad.service.ts +++ b/src/modules/ad/core/application/commands/create-ad/create-ad.service.ts @@ -1,17 +1,17 @@ -import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'; -import { CreateAdCommand } from './create-ad.command'; -import { Inject } from '@nestjs/common'; +import { AggregateID, ConflictException } from '@mobicoop/ddd-library'; import { AD_REPOSITORY, INPUT_DATETIME_TRANSFORMER, } from '@modules/ad/ad.di-tokens'; import { AdEntity } from '@modules/ad/core/domain/ad.entity'; -import { Waypoint } from '../../types/waypoint'; -import { AdRepositoryPort } from '../../ports/ad.repository.port'; import { AdAlreadyExistsException } from '@modules/ad/core/domain/ad.errors'; -import { AggregateID, ConflictException } from '@mobicoop/ddd-library'; -import { ScheduleItem } from '../../types/schedule-item'; +import { ScheduleItemProps } from '@modules/ad/core/domain/value-objects/schedule-item.value-object'; +import { Inject } from '@nestjs/common'; +import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'; +import { AdRepositoryPort } from '../../ports/ad.repository.port'; import { DateTimeTransformerPort } from '../../ports/datetime-transformer.port'; +import { Waypoint } from '../../types/waypoint'; +import { CreateAdCommand } from './create-ad.command'; @CommandHandler(CreateAdCommand) export class CreateAdService implements ICommandHandler { @@ -51,7 +51,7 @@ export class CreateAdService implements ICommandHandler { }, command.frequency, ), - schedule: command.schedule.map((scheduleItem: ScheduleItem) => ({ + schedule: command.schedule.map((scheduleItem: ScheduleItemProps) => ({ day: this.datetimeTransformer.day( scheduleItem.day, { diff --git a/src/modules/ad/core/application/types/schedule-item.ts b/src/modules/ad/core/application/types/schedule-item.ts deleted file mode 100644 index 92dab99..0000000 --- a/src/modules/ad/core/application/types/schedule-item.ts +++ /dev/null @@ -1,5 +0,0 @@ -export type ScheduleItem = { - day: number; - time: string; - margin: number; -}; diff --git a/src/modules/ad/core/domain/ad.entity.ts b/src/modules/ad/core/domain/ad.entity.ts index ffbeb0b..b255263 100644 --- a/src/modules/ad/core/domain/ad.entity.ts +++ b/src/modules/ad/core/domain/ad.entity.ts @@ -30,9 +30,9 @@ export class AdEntity extends AggregateRoot { fromDate: props.fromDate, toDate: props.toDate, schedule: props.schedule.map((day: ScheduleItemProps) => ({ - day: day.day as number, + day: day.day, time: day.time, - margin: day.margin as number, + margin: day.margin, })), seatsProposed: props.seatsProposed, seatsRequested: props.seatsRequested, diff --git a/src/modules/ad/core/domain/events/ad-created.domain-event.ts b/src/modules/ad/core/domain/events/ad-created.domain-event.ts index 50cd191..12c45cf 100644 --- a/src/modules/ad/core/domain/events/ad-created.domain-event.ts +++ b/src/modules/ad/core/domain/events/ad-created.domain-event.ts @@ -1,4 +1,5 @@ import { DomainEvent, DomainEventProps } from '@mobicoop/ddd-library'; +import { ScheduleItemProps } from '../value-objects/schedule-item.value-object'; export class AdCreatedDomainEvent extends DomainEvent { readonly userId: string; @@ -7,7 +8,7 @@ export class AdCreatedDomainEvent extends DomainEvent { readonly frequency: string; readonly fromDate: string; readonly toDate: string; - readonly schedule: ScheduleItem[]; + readonly schedule: ScheduleItemProps[]; readonly seatsProposed: number; readonly seatsRequested: number; readonly strict: boolean; @@ -31,12 +32,6 @@ export class AdCreatedDomainEvent extends DomainEvent { } } -export class ScheduleItem { - day: number; - time: string; - margin: number; -} - export class Waypoint { position: number; name?: string; diff --git a/src/modules/ad/core/domain/value-objects/schedule-item.value-object.ts b/src/modules/ad/core/domain/value-objects/schedule-item.value-object.ts index 8303eeb..c5f6cc2 100644 --- a/src/modules/ad/core/domain/value-objects/schedule-item.value-object.ts +++ b/src/modules/ad/core/domain/value-objects/schedule-item.value-object.ts @@ -6,13 +6,13 @@ import { ValueObject } from '@mobicoop/ddd-library'; * */ export interface ScheduleItemProps { - day?: number; + day: number; time: string; - margin?: number; + margin: number; } export class ScheduleItem extends ValueObject { - get day(): number | undefined { + get day(): number { return this.props.day; } @@ -20,7 +20,7 @@ export class ScheduleItem extends ValueObject { return this.props.time; } - get margin(): number | undefined { + get margin(): number { return this.props.margin; } diff --git a/tests/unit/ad/core/ad.fixtures.ts b/tests/unit/ad/core/ad.fixtures.ts index 2ebf55a..3e2663c 100644 --- a/tests/unit/ad/core/ad.fixtures.ts +++ b/tests/unit/ad/core/ad.fixtures.ts @@ -39,7 +39,9 @@ const punctualCreateAdProps = { toDate: '2023-06-22', schedule: [ { + day: 4, time: '08:30', + margin: 900, }, ], frequency: Frequency.PUNCTUAL, diff --git a/tests/unit/ad/core/find-ads-by-ids.query-handler.spec.ts b/tests/unit/ad/core/find-ads-by-ids.query-handler.spec.ts index c9aef33..0f7e05d 100644 --- a/tests/unit/ad/core/find-ads-by-ids.query-handler.spec.ts +++ b/tests/unit/ad/core/find-ads-by-ids.query-handler.spec.ts @@ -1,10 +1,10 @@ import { AD_REPOSITORY } from '@modules/ad/ad.di-tokens'; +import { FindAdsByIdsQuery } from '@modules/ad/core/application/queries/find-ads-by-ids/find-ads-by-ids.query'; +import { FindAdsByIdsQueryHandler } from '@modules/ad/core/application/queries/find-ads-by-ids/find-ads-by-ids.query-handler'; import { AdEntity } from '@modules/ad/core/domain/ad.entity'; import { CreateAdProps, Frequency } from '@modules/ad/core/domain/ad.types'; import { WaypointProps } from '@modules/ad/core/domain/value-objects/waypoint.value-object'; import { Test, TestingModule } from '@nestjs/testing'; -import { FindAdsByIdsQueryHandler } from '@modules/ad/core/application/queries/find-ads-by-ids/find-ads-by-ids.query-handler'; -import { FindAdsByIdsQuery } from '@modules/ad/core/application/queries/find-ads-by-ids/find-ads-by-ids.query'; const originWaypointProps: WaypointProps = { position: 0, @@ -44,7 +44,9 @@ const punctualCreateAdProps = { toDate: '2023-06-22', schedule: [ { + day: 4, time: '08:30', + margin: 900, }, ], frequency: Frequency.PUNCTUAL, diff --git a/tests/unit/ad/core/find-ads-by-user-id.query-handler.spec.ts b/tests/unit/ad/core/find-ads-by-user-id.query-handler.spec.ts index 300c84c..4f7751a 100644 --- a/tests/unit/ad/core/find-ads-by-user-id.query-handler.spec.ts +++ b/tests/unit/ad/core/find-ads-by-user-id.query-handler.spec.ts @@ -1,10 +1,10 @@ import { AD_REPOSITORY } from '@modules/ad/ad.di-tokens'; +import { FindAdsByUserIdQuery } from '@modules/ad/core/application/queries/find-ads-by-user-id/find-ads-by-user-id.query'; +import { FindAdsByUserIdQueryHandler } from '@modules/ad/core/application/queries/find-ads-by-user-id/find-ads-by-user-id.query-handler'; import { AdEntity } from '@modules/ad/core/domain/ad.entity'; import { CreateAdProps, Frequency } from '@modules/ad/core/domain/ad.types'; import { WaypointProps } from '@modules/ad/core/domain/value-objects/waypoint.value-object'; import { Test, TestingModule } from '@nestjs/testing'; -import { FindAdsByUserIdQueryHandler } from '@modules/ad/core/application/queries/find-ads-by-user-id/find-ads-by-user-id.query-handler'; -import { FindAdsByUserIdQuery } from '@modules/ad/core/application/queries/find-ads-by-user-id/find-ads-by-user-id.query'; const originWaypointProps: WaypointProps = { position: 0, @@ -44,7 +44,9 @@ const punctualCreateAdProps = { toDate: '2023-06-22', schedule: [ { + day: 4, time: '08:30', + margin: 900, }, ], frequency: Frequency.PUNCTUAL, diff --git a/tests/unit/ad/core/invalidate-ad.service.spec.ts b/tests/unit/ad/core/invalidate-ad.service.spec.ts index db72a24..8bd1568 100644 --- a/tests/unit/ad/core/invalidate-ad.service.spec.ts +++ b/tests/unit/ad/core/invalidate-ad.service.spec.ts @@ -1,11 +1,11 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { AD_REPOSITORY } from '@modules/ad/ad.di-tokens'; import { AggregateID } from '@mobicoop/ddd-library'; +import { AD_REPOSITORY } from '@modules/ad/ad.di-tokens'; +import { InvalidateAdCommand } from '@modules/ad/core/application/commands/invalidate-ad/invalidate-ad.command'; +import { InvalidateAdService } from '@modules/ad/core/application/commands/invalidate-ad/invalidate-ad.service'; import { AdEntity } from '@modules/ad/core/domain/ad.entity'; import { CreateAdProps, Frequency } from '@modules/ad/core/domain/ad.types'; import { WaypointProps } from '@modules/ad/core/domain/value-objects/waypoint.value-object'; -import { InvalidateAdService } from '@modules/ad/core/application/commands/invalidate-ad/invalidate-ad.service'; -import { InvalidateAdCommand } from '@modules/ad/core/application/commands/invalidate-ad/invalidate-ad.command'; +import { Test, TestingModule } from '@nestjs/testing'; const originWaypointProps: WaypointProps = { position: 0, @@ -45,7 +45,9 @@ const punctualCreateAdProps = { toDate: '2023-06-22', schedule: [ { + day: 4, time: '08:30', + margin: 900, }, ], frequency: Frequency.PUNCTUAL, diff --git a/tests/unit/ad/core/validate-ad.service.spec.ts b/tests/unit/ad/core/validate-ad.service.spec.ts index 0a30c89..1ccd147 100644 --- a/tests/unit/ad/core/validate-ad.service.spec.ts +++ b/tests/unit/ad/core/validate-ad.service.spec.ts @@ -1,11 +1,11 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { AD_REPOSITORY } from '@modules/ad/ad.di-tokens'; import { AggregateID } from '@mobicoop/ddd-library'; +import { AD_REPOSITORY } from '@modules/ad/ad.di-tokens'; +import { ValidateAdCommand } from '@modules/ad/core/application/commands/validate-ad/validate-ad.command'; +import { ValidateAdService } from '@modules/ad/core/application/commands/validate-ad/validate-ad.service'; import { AdEntity } from '@modules/ad/core/domain/ad.entity'; import { CreateAdProps, Frequency } from '@modules/ad/core/domain/ad.types'; -import { ValidateAdService } from '@modules/ad/core/application/commands/validate-ad/validate-ad.service'; -import { ValidateAdCommand } from '@modules/ad/core/application/commands/validate-ad/validate-ad.command'; import { WaypointProps } from '@modules/ad/core/domain/value-objects/waypoint.value-object'; +import { Test, TestingModule } from '@nestjs/testing'; const originWaypointProps: WaypointProps = { position: 0, @@ -45,7 +45,9 @@ const punctualCreateAdProps = { toDate: '2023-06-22', schedule: [ { + day: 4, time: '08:30', + margin: 900, }, ], frequency: Frequency.PUNCTUAL,