diff --git a/src/modules/ad/tests/integration/ad.repository.spec.ts b/src/modules/ad/tests/integration/ad.repository.spec.ts index 31c9334..82ef736 100644 --- a/src/modules/ad/tests/integration/ad.repository.spec.ts +++ b/src/modules/ad/tests/integration/ad.repository.spec.ts @@ -1,4 +1,10 @@ -import { AD_MESSAGE_PUBLISHER, AD_REPOSITORY } from '@modules/ad/ad.di-tokens'; +import { + AD_MESSAGE_PUBLISHER, + AD_REPOSITORY, + OUTPUT_DATETIME_TRANSFORMER, + TIMEZONE_FINDER, + TIME_CONVERTER, +} from '@modules/ad/ad.di-tokens'; import { AdMapper } from '@modules/ad/ad.mapper'; import { AdEntity } from '@modules/ad/core/domain/ad.entity'; import { @@ -7,7 +13,10 @@ import { 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'; +import { TimeConverter } from '@modules/ad/infrastructure/time-converter'; +import { TimezoneFinder } from '@modules/ad/infrastructure/timezone-finder'; import { ConfigModule } from '@nestjs/config'; import { EventEmitterModule } from '@nestjs/event-emitter'; import { Test } from '@nestjs/testing'; @@ -30,6 +39,9 @@ describe('Ad Repository', () => { const baseUuid = { uuid: 'be459a29-7a41-4c0b-b371-abe90bfb6f00', }; + const baseScheduleUuid = { + uuid: 'bad5e786-3b15-4e51-a8fc-926fa9327ff1', + }; const baseOriginWaypointUuid = { uuid: 'bad5e786-3b15-4e51-a8fc-926fa9327ff1', }; @@ -50,20 +62,11 @@ describe('Ad Repository', () => { frequency: `'PUNCTUAL'`, fromDate: `'2023-01-01'`, toDate: `'2023-01-01'`, - monTime: 'NULL', - tueTime: 'NULL', - wedTime: 'NULL', - thuTime: 'NULL', - friTime: 'NULL', - satTime: 'NULL', - sunTime: `'2023-01-01T07:00:00Z'`, - monMargin: 900, - tueMargin: 900, - wedMargin: 900, - thuMargin: 900, - friMargin: 900, - satMargin: 900, - sunMargin: 900, + }; + const schedulePunctualAd = { + day: 0, + time: `'07:00'`, + margin: 900, }; const originWaypoint = { position: 0, @@ -92,6 +95,11 @@ describe('Ad Repository', () => { for (let i = 0; i < nbToCreate; i++) { adToCreate.uuid = getSeed(i, baseUuid.uuid); await executeInsertCommand('ad', adToCreate); + await executeInsertCommand('schedule_item', { + uuid: getSeed(i, baseScheduleUuid.uuid), + adUuid: adToCreate.uuid, + ...schedulePunctualAd, + }); await executeInsertCommand('waypoint', { uuid: getSeed(i, baseOriginWaypointUuid.uuid), adUuid: adToCreate.uuid, @@ -124,10 +132,26 @@ describe('Ad Repository', () => { providers: [ PrismaService, AdMapper, + { + provide: AD_REPOSITORY, + useClass: AdRepository, + }, { provide: AD_MESSAGE_PUBLISHER, useValue: mockMessagePublisher, }, + { + provide: TIMEZONE_FINDER, + useClass: TimezoneFinder, + }, + { + provide: TIME_CONVERTER, + useClass: TimeConverter, + }, + { + provide: OUTPUT_DATETIME_TRANSFORMER, + useClass: OutputDateTimeTransformer, + }, ], }) // disable logging @@ -151,6 +175,7 @@ describe('Ad Repository', () => { await createPunctualDriverAds(1); const result = await adRepository.findOneById(baseUuid.uuid, { waypoints: true, + schedule: true, }); expect(result.id).toBe(baseUuid.uuid); @@ -158,7 +183,7 @@ describe('Ad Repository', () => { }); describe('create', () => { - it('should create an ad', async () => { + it('should create a punctual ad', async () => { const beforeCount = await prismaService.ad.count(); const createAdProps: CreateAdProps = { @@ -170,7 +195,9 @@ describe('Ad Repository', () => { toDate: '2023-02-01', schedule: [ { + day: 3, time: '12:05', + margin: 900, }, ], seatsProposed: 3, @@ -224,19 +251,92 @@ describe('Ad Repository', () => { expect(afterCount - beforeCount).toBe(1); }); - // it('should throw a UniqueConstraintException if ad already exists', async () => { - // await prismaService.ad.create({ - // data: { - // uuid: uuid, - // password: bcrypt.hashSync(`password`, 10), - // }, - // }); + it('should create a recurrent ad', async () => { + const beforeCount = await prismaService.ad.count(); - // const authenticationToCreate: AuthenticationEntity = - // await AuthenticationEntity.create(createAuthenticationProps); - // await expect( - // authenticationRepository.insert(authenticationToCreate), - // ).rejects.toBeInstanceOf(UniqueConstraintException); - // }); + const createAdProps: CreateAdProps = { + userId: 'b4b56444-f8d3-4110-917c-e37bba77f383', + driver: true, + passenger: false, + frequency: Frequency.RECURRENT, + fromDate: '2023-02-01', + toDate: '2024-01-31', + schedule: [ + { + day: 1, + time: '08:00', + margin: 900, + }, + { + day: 2, + time: '08:00', + margin: 900, + }, + { + day: 3, + time: '09:00', + margin: 900, + }, + { + day: 4, + time: '08:00', + margin: 900, + }, + { + day: 5, + time: '08:00', + margin: 900, + }, + ], + seatsProposed: 3, + seatsRequested: 1, + strict: false, + waypoints: [ + { + position: 0, + address: { + locality: 'Nice', + postalCode: '06000', + country: 'France', + coordinates: { + lon: 43.7102, + lat: 7.262, + }, + }, + }, + { + position: 1, + address: { + locality: 'Marseille', + postalCode: '13000', + country: 'France', + coordinates: { + lon: 43.2965, + lat: 5.3698, + }, + }, + }, + ], + }; + + const defaultAdProps: DefaultAdProps = { + driver: false, + passenger: true, + marginDuration: 900, + seatsProposed: 3, + seatsRequested: 1, + strict: false, + }; + + const adToCreate: AdEntity = AdEntity.create( + createAdProps, + defaultAdProps, + ); + await adRepository.insert(adToCreate); + + const afterCount = await prismaService.ad.count(); + + expect(afterCount - beforeCount).toBe(1); + }); }); });