From 3cf2667f9224330cab32d23d34979847a5340c25 Mon Sep 17 00:00:00 2001 From: sbriat Date: Mon, 16 Oct 2023 17:25:17 +0200 Subject: [PATCH] pretty, lint --- README.md | 6 +- src/app.module.ts | 8 +- src/modules/ad/ad.module.ts | 7 +- .../commands/create-ad/create-ad.service.ts | 114 +++++++++--------- src/modules/ad/core/domain/ad.entity.ts | 4 +- .../input-datetime-transformer.ts | 8 +- .../ad/infrastructure/prisma.service.ts | 2 +- .../ad/infrastructure/timezone-finder.ts | 5 +- .../dtos/create-ad.request.dto.ts | 6 +- .../dtos/schedule-item.dto.ts | 2 +- .../decorators/has-role.decorator.ts | 2 +- .../decorators/has-seats.decorator.ts | 2 +- .../grpc-controllers/dtos/waypoint.dto.ts | 2 +- .../tests/integration/ad.repository.spec.ts | 13 +- .../ad/tests/unit/core/ad.entity.spec.ts | 5 +- .../tests/unit/core/create-ad.service.spec.ts | 5 +- .../core/find-ad-by-id.query-handler.spec.ts | 14 +-- .../input-datetime-transformer.spec.ts | 5 +- .../output-datetime-transformer.spec.ts | 5 +- .../interface/has-seats.decorator.spec.ts | 4 +- ...s-valid-position-indexes.validator.spec.ts | 4 +- src/modules/messager/messager.module.ts | 10 +- 22 files changed, 101 insertions(+), 132 deletions(-) diff --git a/README.md b/README.md index adf894f..17bd0cb 100644 --- a/README.md +++ b/README.md @@ -155,17 +155,17 @@ The app exposes the following [gRPC](https://grpc.io/) services : { "day": 1, "time": "07:00", - "margin": 600, + "margin": 600 }, { "day": 2, "time": "07:05", - "margin": 600, + "margin": 600 }, { "day": 5, "time": "07:10", - "margin": 600, + "margin": 600 } ], "waypoints": [ diff --git a/src/app.module.ts b/src/app.module.ts index de02158..f39ade0 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -26,10 +26,14 @@ import { HEALTH_CRITICAL_LOGGING_KEY, SERVICE_NAME } from './app.constants'; useFactory: async ( configService: ConfigService, ): Promise => ({ - domain: configService.get('SERVICE_CONFIGURATION_DOMAIN') as string, + domain: configService.get( + 'SERVICE_CONFIGURATION_DOMAIN', + ) as string, messageBroker: { uri: configService.get('MESSAGE_BROKER_URI') as string, - exchange: configService.get('MESSAGE_BROKER_EXCHANGE') as string, + exchange: configService.get( + 'MESSAGE_BROKER_EXCHANGE', + ) as string, }, redis: { host: configService.get('REDIS_HOST') as string, diff --git a/src/modules/ad/ad.module.ts b/src/modules/ad/ad.module.ts index 60f104e..b79cd5f 100644 --- a/src/modules/ad/ad.module.ts +++ b/src/modules/ad/ad.module.ts @@ -81,11 +81,6 @@ const adapters: Provider[] = [ ...orms, ...adapters, ], - exports: [ - PrismaService, - AdMapper, - AD_REPOSITORY, - TIMEZONE_FINDER, - ], + exports: [PrismaService, AdMapper, AD_REPOSITORY, TIMEZONE_FINDER], }) export class AdModule {} 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 b08998a..b7c8d6d 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 @@ -15,26 +15,48 @@ import { DateTimeTransformerPort } from '../../ports/datetime-transformer.port'; @CommandHandler(CreateAdCommand) export class CreateAdService implements ICommandHandler { - constructor( @Inject(AD_REPOSITORY) private readonly repository: AdRepositoryPort, @Inject(INPUT_DATETIME_TRANSFORMER) private readonly datetimeTransformer: DateTimeTransformerPort, - ) { - } + ) {} async execute(command: CreateAdCommand): Promise { - const ad = AdEntity.create( - { - userId: command.userId, - driver: command.driver, - passenger: command.passenger, - frequency: command.frequency, - fromDate: this.datetimeTransformer.fromDate( + const ad = AdEntity.create({ + userId: command.userId, + driver: command.driver, + passenger: command.passenger, + frequency: command.frequency, + fromDate: this.datetimeTransformer.fromDate( + { + date: command.fromDate, + time: command.schedule[0].time, + coordinates: { + lon: command.waypoints[0].lon, + lat: command.waypoints[0].lat, + }, + }, + command.frequency, + ), + toDate: this.datetimeTransformer.toDate( + command.toDate, + { + date: command.fromDate, + time: command.schedule[0].time, + coordinates: { + lon: command.waypoints[0].lon, + lat: command.waypoints[0].lat, + }, + }, + command.frequency, + ), + schedule: command.schedule.map((scheduleItem: ScheduleItem) => ({ + day: this.datetimeTransformer.day( + scheduleItem.day, { date: command.fromDate, - time: command.schedule[0].time, + time: scheduleItem.time, coordinates: { lon: command.waypoints[0].lon, lat: command.waypoints[0].lat, @@ -42,11 +64,10 @@ export class CreateAdService implements ICommandHandler { }, command.frequency, ), - toDate: this.datetimeTransformer.toDate( - command.toDate, + time: this.datetimeTransformer.time( { date: command.fromDate, - time: command.schedule[0].time, + time: scheduleItem.time, coordinates: { lon: command.waypoints[0].lon, lat: command.waypoints[0].lat, @@ -54,52 +75,27 @@ export class CreateAdService implements ICommandHandler { }, command.frequency, ), - schedule: command.schedule.map((scheduleItem: ScheduleItem) => ({ - day: this.datetimeTransformer.day( - scheduleItem.day, - { - date: command.fromDate, - time: scheduleItem.time, - coordinates: { - lon: command.waypoints[0].lon, - lat: command.waypoints[0].lat, - }, - }, - command.frequency, - ), - time: this.datetimeTransformer.time( - { - date: command.fromDate, - time: scheduleItem.time, - coordinates: { - lon: command.waypoints[0].lon, - lat: command.waypoints[0].lat, - }, - }, - command.frequency, - ), - margin: scheduleItem.margin, - })), - seatsProposed: command.seatsProposed ?? 0, - seatsRequested: command.seatsRequested ?? 0, - strict: command.strict, - waypoints: command.waypoints.map((waypoint: Waypoint) => ({ - position: waypoint.position, - address: { - name: waypoint.name, - houseNumber: waypoint.houseNumber, - street: waypoint.street, - postalCode: waypoint.postalCode, - locality: waypoint.locality, - country: waypoint.country, - coordinates: { - lon: waypoint.lon, - lat: waypoint.lat, - }, + margin: scheduleItem.margin, + })), + seatsProposed: command.seatsProposed ?? 0, + seatsRequested: command.seatsRequested ?? 0, + strict: command.strict, + waypoints: command.waypoints.map((waypoint: Waypoint) => ({ + position: waypoint.position, + address: { + name: waypoint.name, + houseNumber: waypoint.houseNumber, + street: waypoint.street, + postalCode: waypoint.postalCode, + locality: waypoint.locality, + country: waypoint.country, + coordinates: { + lon: waypoint.lon, + lat: waypoint.lat, }, - })), - }, - ); + }, + })), + }); try { await this.repository.insert(ad); diff --git a/src/modules/ad/core/domain/ad.entity.ts b/src/modules/ad/core/domain/ad.entity.ts index ea6bd6c..75f9553 100644 --- a/src/modules/ad/core/domain/ad.entity.ts +++ b/src/modules/ad/core/domain/ad.entity.ts @@ -8,9 +8,7 @@ import { WaypointProps } from './value-objects/waypoint.value-object'; export class AdEntity extends AggregateRoot { protected readonly _id: AggregateID; - static create = ( - create: CreateAdProps - ): AdEntity => { + static create = (create: CreateAdProps): AdEntity => { const id = v4(); const props: AdProps = { ...create }; const ad = new AdEntity({ id, props }); diff --git a/src/modules/ad/infrastructure/input-datetime-transformer.ts b/src/modules/ad/infrastructure/input-datetime-transformer.ts index f99e638..6903093 100644 --- a/src/modules/ad/infrastructure/input-datetime-transformer.ts +++ b/src/modules/ad/infrastructure/input-datetime-transformer.ts @@ -5,10 +5,7 @@ import { GeoDateTime, } from '../core/application/ports/datetime-transformer.port'; import { TimeConverterPort } from '../core/application/ports/time-converter.port'; -import { - TIMEZONE_FINDER, - TIME_CONVERTER, -} from '../ad.di-tokens'; +import { TIMEZONE_FINDER, TIME_CONVERTER } from '../ad.di-tokens'; import { TimezoneFinderPort } from '../core/application/ports/timezone-finder.port'; @Injectable() @@ -17,8 +14,7 @@ export class InputDateTimeTransformer implements DateTimeTransformerPort { @Inject(TIMEZONE_FINDER) private readonly timezoneFinder: TimezoneFinderPort, @Inject(TIME_CONVERTER) private readonly timeConverter: TimeConverterPort, - ) { - } + ) {} /** * Compute the fromDate : if an ad is punctual, the departure date diff --git a/src/modules/ad/infrastructure/prisma.service.ts b/src/modules/ad/infrastructure/prisma.service.ts index ed98c67..359f950 100644 --- a/src/modules/ad/infrastructure/prisma.service.ts +++ b/src/modules/ad/infrastructure/prisma.service.ts @@ -1,4 +1,4 @@ -import { INestApplication, Injectable, OnModuleInit } from '@nestjs/common'; +import { Injectable, OnModuleInit } from '@nestjs/common'; import { PrismaClient } from '@prisma/client'; @Injectable() diff --git a/src/modules/ad/infrastructure/timezone-finder.ts b/src/modules/ad/infrastructure/timezone-finder.ts index 89086ea..d2c245a 100644 --- a/src/modules/ad/infrastructure/timezone-finder.ts +++ b/src/modules/ad/infrastructure/timezone-finder.ts @@ -4,8 +4,5 @@ import { TimezoneFinderPort } from '../core/application/ports/timezone-finder.po @Injectable() export class TimezoneFinder implements TimezoneFinderPort { - timezones = ( - lon: number, - lat: number, - ): string[] => find(lat, lon); + timezones = (lon: number, lat: number): string[] => find(lat, lon); } diff --git a/src/modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto.ts index 2e8b463..961cfb6 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto.ts @@ -24,16 +24,16 @@ export class CreateAdRequestDto { @IsBoolean() @HasRole('passenger', { - message: 'At least one of driver or passenger property needs to be truthy' + message: 'At least one of driver or passenger property needs to be truthy', }) @HasSeats('seatsProposed', { - message: 'Number of seats proposed as a driver is required' + message: 'Number of seats proposed as a driver is required', }) driver: boolean; @IsBoolean() @HasSeats('seatsRequested', { - message: 'Number of seats requested as a passenger is required' + message: 'Number of seats requested as a passenger is required', }) passenger: boolean; diff --git a/src/modules/ad/interface/grpc-controllers/dtos/schedule-item.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/schedule-item.dto.ts index a7ceadb..0b8bdd1 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/schedule-item.dto.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/schedule-item.dto.ts @@ -1,4 +1,4 @@ -import { IsOptional, IsMilitaryTime, IsInt, Min, Max } from 'class-validator'; +import { IsMilitaryTime, IsInt, Min, Max } from 'class-validator'; export class ScheduleItemDto { @IsInt() diff --git a/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/has-role.decorator.ts b/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/has-role.decorator.ts index 28226a5..51feff8 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/has-role.decorator.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/has-role.decorator.ts @@ -19,7 +19,7 @@ export function HasRole( validate(value: any, args: ValidationArguments) { const [relatedPropertyName] = args.constraints; const relatedValue = (args.object as any)[relatedPropertyName]; - return value || relatedValue; + return value || relatedValue; }, }, }); diff --git a/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/has-seats.decorator.ts b/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/has-seats.decorator.ts index d788547..984ed3a 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/has-seats.decorator.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/has-seats.decorator.ts @@ -19,7 +19,7 @@ export function HasSeats( validate(value: any, args: ValidationArguments) { const [relatedPropertyName] = args.constraints; const relatedValue = (args.object as any)[relatedPropertyName]; - return (value && relatedValue>0) || !value; + return (value && relatedValue > 0) || !value; }, }, }); diff --git a/src/modules/ad/interface/grpc-controllers/dtos/waypoint.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/waypoint.dto.ts index 8615957..1d6ebd6 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/waypoint.dto.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/waypoint.dto.ts @@ -1,4 +1,4 @@ -import { IsInt, IsOptional } from 'class-validator'; +import { IsInt } from 'class-validator'; import { AddressDto } from './address.dto'; export class WaypointDto extends AddressDto { diff --git a/src/modules/ad/tests/integration/ad.repository.spec.ts b/src/modules/ad/tests/integration/ad.repository.spec.ts index bbe1419..8bcfb9d 100644 --- a/src/modules/ad/tests/integration/ad.repository.spec.ts +++ b/src/modules/ad/tests/integration/ad.repository.spec.ts @@ -7,10 +7,7 @@ import { } from '@modules/ad/ad.di-tokens'; import { AdMapper } from '@modules/ad/ad.mapper'; import { AdEntity } from '@modules/ad/core/domain/ad.entity'; -import { - CreateAdProps, - Frequency, -} from '@modules/ad/core/domain/ad.types'; +import { CreateAdProps, Frequency } from '@modules/ad/core/domain/ad.types'; import { AdRepository } from '@modules/ad/infrastructure/ad.repository'; import { OutputDateTimeTransformer } from '@modules/ad/infrastructure/output-datetime-transformer'; import { PrismaService } from '@modules/ad/infrastructure/prisma.service'; @@ -230,9 +227,7 @@ describe('Ad Repository', () => { ], }; - const adToCreate: AdEntity = AdEntity.create( - createAdProps, - ); + const adToCreate: AdEntity = AdEntity.create(createAdProps); await adRepository.insert(adToCreate); const afterCount = await prismaService.ad.count(); @@ -308,9 +303,7 @@ describe('Ad Repository', () => { ], }; - const adToCreate: AdEntity = AdEntity.create( - createAdProps, - ); + const adToCreate: AdEntity = AdEntity.create(createAdProps); await adRepository.insert(adToCreate); const afterCount = await prismaService.ad.count(); diff --git a/src/modules/ad/tests/unit/core/ad.entity.spec.ts b/src/modules/ad/tests/unit/core/ad.entity.spec.ts index a33f4c4..7876c08 100644 --- a/src/modules/ad/tests/unit/core/ad.entity.spec.ts +++ b/src/modules/ad/tests/unit/core/ad.entity.spec.ts @@ -1,8 +1,5 @@ import { AdEntity } from '@modules/ad/core/domain/ad.entity'; -import { - CreateAdProps, - Frequency, -} from '@modules/ad/core/domain/ad.types'; +import { CreateAdProps, Frequency } from '@modules/ad/core/domain/ad.types'; import { WaypointProps } from '@modules/ad/core/domain/value-objects/waypoint.value-object'; const originWaypointProps: WaypointProps = { diff --git a/src/modules/ad/tests/unit/core/create-ad.service.spec.ts b/src/modules/ad/tests/unit/core/create-ad.service.spec.ts index 6fcb9bc..18ed15d 100644 --- a/src/modules/ad/tests/unit/core/create-ad.service.spec.ts +++ b/src/modules/ad/tests/unit/core/create-ad.service.spec.ts @@ -102,9 +102,8 @@ describe('create-ad.service', () => { AdEntity.create = jest.fn().mockReturnValue({ id: '047a6ecf-23d4-4d3e-877c-3225d560a8da', }); - const result: AggregateID = await createAdService.execute( - createAdCommand, - ); + const result: AggregateID = + await createAdService.execute(createAdCommand); expect(result).toBe('047a6ecf-23d4-4d3e-877c-3225d560a8da'); }); it('should throw an error if something bad happens', async () => { diff --git a/src/modules/ad/tests/unit/core/find-ad-by-id.query-handler.spec.ts b/src/modules/ad/tests/unit/core/find-ad-by-id.query-handler.spec.ts index 2f95525..013bfcb 100644 --- a/src/modules/ad/tests/unit/core/find-ad-by-id.query-handler.spec.ts +++ b/src/modules/ad/tests/unit/core/find-ad-by-id.query-handler.spec.ts @@ -1,9 +1,6 @@ import { AD_REPOSITORY } from '@modules/ad/ad.di-tokens'; import { AdEntity } from '@modules/ad/core/domain/ad.entity'; -import { - CreateAdProps, - Frequency, -} from '@modules/ad/core/domain/ad.types'; +import { CreateAdProps, Frequency } from '@modules/ad/core/domain/ad.types'; import { FindAdByIdQuery } from '@modules/ad/core/application/queries/find-ad-by-id/find-ad-by-id.query'; import { FindAdByIdQueryHandler } from '@modules/ad/core/application/queries/find-ad-by-id/find-ad-by-id.query-handler'; import { WaypointProps } from '@modules/ad/core/domain/value-objects/waypoint.value-object'; @@ -59,9 +56,7 @@ const punctualPassengerCreateAdProps: CreateAdProps = { passenger: true, }; -const ad: AdEntity = AdEntity.create( - punctualPassengerCreateAdProps, -); +const ad: AdEntity = AdEntity.create(punctualPassengerCreateAdProps); const mockAdRepository = { findOneById: jest.fn().mockImplementation(() => ad), @@ -95,9 +90,8 @@ describe('find-ad-by-id.query-handler', () => { const findAdbyIdQuery = new FindAdByIdQuery( 'dd264806-13b4-4226-9b18-87adf0ad5dd1', ); - const ad: AdEntity = await findAdByIdQueryHandler.execute( - findAdbyIdQuery, - ); + const ad: AdEntity = + await findAdByIdQueryHandler.execute(findAdbyIdQuery); expect(ad.getProps().fromDate).toBe('2023-06-22'); }); }); diff --git a/src/modules/ad/tests/unit/infrastructure/input-datetime-transformer.spec.ts b/src/modules/ad/tests/unit/infrastructure/input-datetime-transformer.spec.ts index d7d28ca..f593ceb 100644 --- a/src/modules/ad/tests/unit/infrastructure/input-datetime-transformer.spec.ts +++ b/src/modules/ad/tests/unit/infrastructure/input-datetime-transformer.spec.ts @@ -1,7 +1,4 @@ -import { - TIMEZONE_FINDER, - TIME_CONVERTER, -} from '@modules/ad/ad.di-tokens'; +import { TIMEZONE_FINDER, TIME_CONVERTER } from '@modules/ad/ad.di-tokens'; import { Frequency } from '@modules/ad/core/application/ports/datetime-transformer.port'; import { TimeConverterPort } from '@modules/ad/core/application/ports/time-converter.port'; import { TimezoneFinderPort } from '@modules/ad/core/application/ports/timezone-finder.port'; diff --git a/src/modules/ad/tests/unit/infrastructure/output-datetime-transformer.spec.ts b/src/modules/ad/tests/unit/infrastructure/output-datetime-transformer.spec.ts index 89a6cbb..dd49552 100644 --- a/src/modules/ad/tests/unit/infrastructure/output-datetime-transformer.spec.ts +++ b/src/modules/ad/tests/unit/infrastructure/output-datetime-transformer.spec.ts @@ -1,7 +1,4 @@ -import { - TIMEZONE_FINDER, - TIME_CONVERTER, -} from '@modules/ad/ad.di-tokens'; +import { TIMEZONE_FINDER, TIME_CONVERTER } from '@modules/ad/ad.di-tokens'; import { Frequency } from '@modules/ad/core/application/ports/datetime-transformer.port'; import { TimeConverterPort } from '@modules/ad/core/application/ports/time-converter.port'; import { TimezoneFinderPort } from '@modules/ad/core/application/ports/timezone-finder.port'; diff --git a/src/modules/ad/tests/unit/interface/has-seats.decorator.spec.ts b/src/modules/ad/tests/unit/interface/has-seats.decorator.spec.ts index 774fc04..6d04f88 100644 --- a/src/modules/ad/tests/unit/interface/has-seats.decorator.spec.ts +++ b/src/modules/ad/tests/unit/interface/has-seats.decorator.spec.ts @@ -4,13 +4,13 @@ import { Validator } from 'class-validator'; describe('has seats decorator', () => { class MyClass { @HasSeats('seatsProposed') - driver:boolean; + driver: boolean; @HasSeats('seatsRequested') passenger: boolean; seatsProposed?: number; seatsRequested?: number; -} + } it('should return a property decorator has a function', () => { const hasSeats = HasSeats('property'); expect(typeof hasSeats).toBe('function'); diff --git a/src/modules/ad/tests/unit/interface/has-valid-position-indexes.validator.spec.ts b/src/modules/ad/tests/unit/interface/has-valid-position-indexes.validator.spec.ts index cedf136..585b121 100644 --- a/src/modules/ad/tests/unit/interface/has-valid-position-indexes.validator.spec.ts +++ b/src/modules/ad/tests/unit/interface/has-valid-position-indexes.validator.spec.ts @@ -1,5 +1,5 @@ -import { hasValidPositionIndexes } from "@modules/ad/interface/grpc-controllers/dtos/validators/has-valid-position-indexes.validator"; -import { WaypointDto } from "@modules/ad/interface/grpc-controllers/dtos/waypoint.dto"; +import { hasValidPositionIndexes } from '@modules/ad/interface/grpc-controllers/dtos/validators/has-valid-position-indexes.validator'; +import { WaypointDto } from '@modules/ad/interface/grpc-controllers/dtos/waypoint.dto'; describe('addresses position validator', () => { const waypoint1: WaypointDto = { diff --git a/src/modules/messager/messager.module.ts b/src/modules/messager/messager.module.ts index f97a7fd..d234c61 100644 --- a/src/modules/messager/messager.module.ts +++ b/src/modules/messager/messager.module.ts @@ -3,7 +3,11 @@ import { MESSAGE_PUBLISHER } from './messager.di-tokens'; import { ConfigModule, ConfigService } from '@nestjs/config'; import { SERVICE_NAME } from '@src/app.constants'; -import { MessageBrokerModule, MessageBrokerModuleOptions, MessageBrokerPublisher } from '@mobicoop/message-broker-module'; +import { + MessageBrokerModule, + MessageBrokerModuleOptions, + MessageBrokerPublisher, +} from '@mobicoop/message-broker-module'; const imports = [ MessageBrokerModule.forRootAsync({ @@ -15,7 +19,9 @@ const imports = [ uri: configService.get('MESSAGE_BROKER_URI') as string, exchange: { name: configService.get('MESSAGE_BROKER_EXCHANGE') as string, - durable: configService.get('MESSAGE_BROKER_EXCHANGE_DURABILITY') as boolean + durable: configService.get( + 'MESSAGE_BROKER_EXCHANGE_DURABILITY', + ) as boolean, }, name: SERVICE_NAME, }),