diff --git a/src/modules/ad/adapters/secondaries/ads.repository.ts b/src/modules/ad/adapters/secondaries/ads.repository.ts index a46acd3..3b0ea0f 100644 --- a/src/modules/ad/adapters/secondaries/ads.repository.ts +++ b/src/modules/ad/adapters/secondaries/ads.repository.ts @@ -1,6 +1,7 @@ import { Injectable } from '@nestjs/common'; import { AdRepository } from '../../../database/domain/ad-repository'; import { Ad } from '../../domain/entities/ad'; +//TODO : properly implement mutate operation to prisma @Injectable() export class AdsRepository extends AdRepository { protected _model = 'ad'; diff --git a/src/modules/ad/adapters/secondaries/default-params.provider.ts b/src/modules/ad/adapters/secondaries/default-params.provider.ts index 370888f..85cd851 100644 --- a/src/modules/ad/adapters/secondaries/default-params.provider.ts +++ b/src/modules/ad/adapters/secondaries/default-params.provider.ts @@ -8,17 +8,17 @@ export class DefaultParamsProvider implements IProvideParams { constructor(private readonly configService: ConfigService) {} getParams = (): DefaultParams => { return { - MON_MARGIN: this.configService.get('DEPARTURE_MARGIN'), - TUE_MARGIN: this.configService.get('DEPARTURE_MARGIN'), - WED_MARGIN: this.configService.get('DEPARTURE_MARGIN'), - THU_MARGIN: this.configService.get('DEPARTURE_MARGIN'), - FRI_MARGIN: this.configService.get('DEPARTURE_MARGIN'), - SAT_MARGIN: this.configService.get('DEPARTURE_MARGIN'), - SUN_MARGIN: this.configService.get('DEPARTURE_MARGIN'), + MON_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')), + TUE_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')), + WED_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')), + THU_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')), + FRI_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')), + SAT_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')), + SUN_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')), DRIVER: this.configService.get('ROLE') == 'driver' ? true : false, - SEATS_PROVIDED: this.configService.get('SEATS_PROVIDED'), + SEATS_PROVIDED: parseInt(this.configService.get('SEATS_PROVIDED')), PASSENGER: this.configService.get('ROLE') == 'passenger' ? true : false, - SEATS_REQUESTED: this.configService.get('SEATS_REQUESTED'), + SEATS_REQUESTED: parseInt(this.configService.get('SEATS_REQUESTED')), STRICT: false, }; }; diff --git a/src/modules/ad/domain/dtos/ad.creation.ts b/src/modules/ad/domain/dtos/ad.creation.ts index 48356f5..bb34c67 100644 --- a/src/modules/ad/domain/dtos/ad.creation.ts +++ b/src/modules/ad/domain/dtos/ad.creation.ts @@ -10,8 +10,13 @@ import { IsUUID, } from 'class-validator'; import { Frequency } from '../types/frequency.enum'; -import { AddressCreation } from './address.creation'; +import { Address } from '../entities/address'; + export class AdCreation { + @IsUUID(4) + @AutoMap() + uuid: string; + @IsUUID(4) @AutoMap() userUuid: string; @@ -121,5 +126,5 @@ export class AdCreation { @ValidateNested({ each: true }) @AutoMap() - addresses: { create: AddressCreation[] }; + addresses: { create: Address[] }; } diff --git a/src/modules/ad/domain/dtos/address.creation.ts b/src/modules/ad/domain/dtos/address.creation.ts deleted file mode 100644 index 90622db..0000000 --- a/src/modules/ad/domain/dtos/address.creation.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { AutoMap } from '@automapper/classes'; -import { IsInt } from 'class-validator'; - -export class AddressCreation { - @IsInt() - @AutoMap() - position: number; - - @AutoMap() - lon: number; - - @AutoMap() - lat: number; - - @AutoMap() - name?: string; - - @AutoMap() - houseNumber?: string; - - @AutoMap() - street?: string; - - @AutoMap() - locality: string; - - @AutoMap() - postalCode: string; - - @AutoMap() - country: string; -} diff --git a/src/modules/ad/domain/dtos/utils/frequency.mapping.ts b/src/modules/ad/domain/dtos/utils/frequency.mapping.ts index 4d59786..3164960 100644 --- a/src/modules/ad/domain/dtos/utils/frequency.mapping.ts +++ b/src/modules/ad/domain/dtos/utils/frequency.mapping.ts @@ -1,4 +1,6 @@ import { Frequency } from '../../types/frequency.enum'; -export const mappingKeyToFrequency = (index: number): string => { - return Frequency[index - 1]; +export const mappingKeyToFrequency = (index: number): Frequency => { + if (index == 1) return Frequency.PUNCTUAL; + if (index == 2) return Frequency.RECURRENT; + return undefined; }; diff --git a/src/modules/ad/domain/types/frequency.enum.ts b/src/modules/ad/domain/types/frequency.enum.ts index 6c571eb..0126874 100644 --- a/src/modules/ad/domain/types/frequency.enum.ts +++ b/src/modules/ad/domain/types/frequency.enum.ts @@ -1,4 +1,4 @@ export enum Frequency { - PUNCTUAL, - RECURRENT, + PUNCTUAL = 'PUNCTUAL', + RECURRENT = 'RECURRENT', } diff --git a/src/modules/ad/domain/usecases/create-ad.usecase.ts b/src/modules/ad/domain/usecases/create-ad.usecase.ts index b5731da..f9a0229 100644 --- a/src/modules/ad/domain/usecases/create-ad.usecase.ts +++ b/src/modules/ad/domain/usecases/create-ad.usecase.ts @@ -27,6 +27,7 @@ export class CreateAdUseCase { } async execute(command: CreateAdCommand): Promise { + console.log('usecase'); this.ad = this._mapper.map( command.createAdRequest, CreateAdRequest, @@ -38,6 +39,7 @@ export class CreateAdUseCase { this.setDefaultDistanceMargin(); try { + console.log(this.ad); const adCreated: Ad = await this._repository.create(this.ad); this._messager.publish('ad.create', JSON.stringify(adCreated)); this._messager.publish( diff --git a/src/modules/ad/mappers/address.profile.ts b/src/modules/ad/mappers/address.profile.ts deleted file mode 100644 index 6170935..0000000 --- a/src/modules/ad/mappers/address.profile.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { Mapper, createMap } from '@automapper/core'; -import { AutomapperProfile, InjectMapper } from '@automapper/nestjs'; -import { Injectable } from '@nestjs/common'; -import { AddressRequestDTO } from '../domain/dtos/create.address.request'; -import { AddressCreation } from '../domain/dtos/address.creation'; -@Injectable() -export class AdProfile extends AutomapperProfile { - constructor(@InjectMapper() mapper: Mapper) { - super(mapper); - } - - override get profile() { - return (mapper) => { - createMap(mapper, AddressRequestDTO, AddressCreation); - }; - } -} diff --git a/src/modules/ad/tests/integration/ad.repository.spec.ts b/src/modules/ad/tests/integration/ad.repository.spec.ts index a7b6d01..5e64dc2 100644 --- a/src/modules/ad/tests/integration/ad.repository.spec.ts +++ b/src/modules/ad/tests/integration/ad.repository.spec.ts @@ -3,6 +3,10 @@ import { PrismaService } from '../../../database/adapters/secondaries/prisma-ser import { AdsRepository } from '../../adapters/secondaries/ads.repository'; import { DatabaseModule } from '../../../database/database.module'; +import { Frequency } from '../../domain/types/frequency.enum'; +import { AdCreation } from '../../domain/dtos/ad.creation'; +import { Address } from '../../domain/entities/address'; + describe('Ad Repository', () => { let prismaService: PrismaService; let adsRepository: AdsRepository; @@ -383,6 +387,7 @@ describe('Ad Repository', () => { it('should return an ad', async () => { await createPunctualDriverAds(1); const ad = await adsRepository.findOneByUuid(baseUuid.uuid); + expect(ad.uuid).toBe(baseUuid.uuid); }); @@ -396,4 +401,38 @@ describe('Ad Repository', () => { describe('Create Ad', () => { it('should Create an ad ', async () => {}); }); + describe('create', () => { + it('should create an ad', async () => { + const beforeCount = await prismaService.ad.count(); + const adToCreate: AdCreation = new AdCreation(); + + adToCreate.uuid = 'be459a29-7a41-4c0b-b371-abe90bfb6f00'; + adToCreate.userUuid = '4e52b54d-a729-4dbd-9283-f84a11bb2200'; + adToCreate.driver = true; + adToCreate.passenger = false; + adToCreate.frequency = Frequency.PUNCTUAL; + adToCreate.fromDate = new Date('05-22-2023 09:36'); + adToCreate.toDate = new Date('05-22-2023 09:36'); + adToCreate.monTime = '09:36'; + adToCreate.monMargin = 900; + adToCreate.tueMargin = 900; + adToCreate.wedMargin = 900; + adToCreate.thuMargin = 900; + adToCreate.friMargin = 900; + adToCreate.satMargin = 900; + adToCreate.sunMargin = 900; + adToCreate.seatsDriver = 3; + adToCreate.seatsPassenger = 0; + adToCreate.strict = false; + adToCreate.addresses = { + create: [address0 as Address, address1 as Address], + }; + const ad = await adsRepository.create(adToCreate); + + const afterCount = await prismaService.ad.count(); + + expect(afterCount - beforeCount).toBe(1); + expect(ad.uuid).toBe('be459a29-7a41-4c0b-b371-abe90bfb6f00'); + }); + }); }); diff --git a/src/modules/ad/tests/unit/adapters/secondaries/default-param.provider.spec.ts b/src/modules/ad/tests/unit/adapters/secondaries/default-param.provider.spec.ts index 14c1f83..3096b7e 100644 --- a/src/modules/ad/tests/unit/adapters/secondaries/default-param.provider.spec.ts +++ b/src/modules/ad/tests/unit/adapters/secondaries/default-param.provider.spec.ts @@ -6,7 +6,7 @@ import { DefaultParams } from '../../../../domain/types/default-params.type'; const mockConfigService = { get: jest.fn().mockImplementation(() => 'some_default_value'), }; - +//TODO complete coverage describe('DefaultParamsProvider', () => { let defaultParamsProvider: DefaultParamsProvider; @@ -33,7 +33,7 @@ describe('DefaultParamsProvider', () => { it('should provide default params', async () => { const params: DefaultParams = defaultParamsProvider.getParams(); - expect(params.SUN_MARGIN).toBe('some_default_value'); + expect(params.SUN_MARGIN).toBeNaN(); expect(params.PASSENGER).toBe(false); expect(params.DRIVER).toBe(false); }); diff --git a/src/modules/ad/tests/unit/domain/create-ad.usecase.spec.ts b/src/modules/ad/tests/unit/domain/create-ad.usecase.spec.ts index ca82a54..ab7edfa 100644 --- a/src/modules/ad/tests/unit/domain/create-ad.usecase.spec.ts +++ b/src/modules/ad/tests/unit/domain/create-ad.usecase.spec.ts @@ -11,7 +11,7 @@ import { Ad } from '../../../domain/entities/ad'; import { AdProfile } from '../../../mappers/ad.profile'; import { AddressRequestDTO } from '../../../domain/dtos/create.address.request'; import { AdCreation } from '../../../domain/dtos/ad.creation'; -import { AddressCreation } from '../../../domain/dtos/address.creation'; +import { Address } from 'src/modules/ad/domain/entities/address'; const mockAddress1: AddressRequestDTO = { position: 0, @@ -192,7 +192,7 @@ describe('CreateAdUseCase', () => { seatsPassenger: mockDefaultParamsProvider.getParams().SEATS_REQUESTED, strict: mockDefaultParamsProvider.getParams().STRICT, addresses: { - create: minimalReccurentAdREquest.addresses as AddressCreation[], + create: minimalReccurentAdREquest.addresses as Address[], }, createdAt: undefined, } as AdCreation; @@ -246,7 +246,7 @@ describe('CreateAdUseCase', () => { seatsPassenger: newPunctualPassengerAdRequest.seatsPassenger, strict: mockDefaultParamsProvider.getParams().STRICT, addresses: { - create: newPunctualPassengerAdRequest.addresses as AddressCreation[], + create: newPunctualPassengerAdRequest.addresses as Address[], }, createdAt: undefined, } as AdCreation; diff --git a/src/modules/ad/tests/unit/domain/frequency.mapping.spec.ts b/src/modules/ad/tests/unit/domain/frequency.mapping.spec.ts index 063fc7f..d4c309f 100644 --- a/src/modules/ad/tests/unit/domain/frequency.mapping.spec.ts +++ b/src/modules/ad/tests/unit/domain/frequency.mapping.spec.ts @@ -3,10 +3,10 @@ import { Frequency } from '../../../domain/types/frequency.enum'; describe('frequency mapping function ', () => { it('should return punctual', () => { - expect(mappingKeyToFrequency(1)).toBe(Frequency[0]); + expect(mappingKeyToFrequency(1)).toBe(Frequency.PUNCTUAL); }); it('should return recurent', () => { - expect(mappingKeyToFrequency(2)).toBe(Frequency[1]); + expect(mappingKeyToFrequency(2)).toBe(Frequency.RECURRENT); }); it('should return undefined', () => { expect(mappingKeyToFrequency(0)).toBeUndefined(); diff --git a/src/modules/database/adapters/secondaries/prisma-repository.abstract.ts b/src/modules/database/adapters/secondaries/prisma-repository.abstract.ts index c43dfec..98937eb 100644 --- a/src/modules/database/adapters/secondaries/prisma-repository.abstract.ts +++ b/src/modules/database/adapters/secondaries/prisma-repository.abstract.ts @@ -87,6 +87,7 @@ export abstract class PrismaRepository implements IRepository { }); return res; } catch (e) { + console.log(e); if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( Prisma.PrismaClientKnownRequestError.name,