WIP adding create add service

This commit is contained in:
Grégoire Chevalier 2023-05-09 14:10:10 +02:00
parent cac8e90c9b
commit 1db7dc1104
4 changed files with 173 additions and 26 deletions

View File

@ -6,6 +6,7 @@ import {
IsDate,
IsInt,
MinLength,
ValidateIf,
} from 'class-validator';
import { FrequencyType } from '../entities/frequencyEnum';
import { Address } from '../entities/address';
@ -20,12 +21,12 @@ export class CreateAdRequest {
@AutoMap()
userUuid: string;
//@ValidateIf(o => o.passenger? false : true )
@ValidateIf((ad) => (ad.passenger ? false : true))
@IsBoolean()
@AutoMap()
driver: boolean;
driver?: boolean;
//@ValidateIf(o => o.driver? false : true )
@ValidateIf((ad) => (ad.driver ? false : true))
@IsBoolean()
@AutoMap()
passenger?: boolean;
@ -42,92 +43,94 @@ export class CreateAdRequest {
@IsDate()
@IsOptional()
@AutoMap()
monTime: Date;
monTime?: Date;
@IsDate()
@IsOptional()
@AutoMap()
tueTime: Date;
tueTime?: Date;
@IsDate()
@IsOptional()
@AutoMap()
wedTime: Date;
wedTime?: Date;
@IsDate()
@IsOptional()
@AutoMap()
thuTime: Date;
thuTime?: Date;
@IsDate()
@IsOptional()
@AutoMap()
friTime: Date;
friTime?: Date;
@IsDate()
@IsOptional()
@AutoMap()
satTime: Date;
satTime?: Date;
@IsDate()
@IsOptional()
@AutoMap()
sunTime: Date;
sunTime?: Date;
@IsInt()
@IsOptional()
@AutoMap()
Margin: number;
monMargin?: number;
@IsInt()
@IsOptional()
@AutoMap()
tueMargin: number;
tueMargin?: number;
@IsInt()
@IsOptional()
@AutoMap()
wedMargin: number;
wedMargin?: number;
@IsInt()
@IsOptional()
@AutoMap()
thuMargin: number;
thuMargin?: number;
@IsInt()
@IsOptional()
@AutoMap()
friMargin: number;
friMargin?: number;
@IsInt()
@IsOptional()
@AutoMap()
satMargin: number;
satMargin?: number;
@IsInt()
@IsOptional()
@AutoMap()
sunMargin: number;
sunMargin?: number;
//@ValidateIf(o => o.passenger? false : true )
@ValidateIf((ad) => (ad.passenger ? false : true))
@IsInt()
@AutoMap()
seatsDriver?: number;
//@ValidateIf(o => o.driver? false : true )
@ValidateIf((ad) => (ad.driver ? false : true))
@IsInt()
@AutoMap()
seatsPassenger?: number;
@IsDate()
@IsOptional()
@AutoMap()
createdAt: Date;
createdAt?: Date;
@IsDate()
@IsOptional()
@AutoMap()
updatedAt: Date;
updatedAt?: Date;
@MinLength(2)
@AutoMap()
addresses: Array<Address>;
addresses?: Array<Address>;
}

View File

@ -1,6 +1,134 @@
import { AutoMap } from '@automapper/classes';
import {
IsOptional,
IsString,
IsBoolean,
IsDate,
IsInt,
MinLength,
ValidateIf,
} from 'class-validator';
import { FrequencyType } from '../entities/frequencyEnum';
import { Address } from '../entities/address';
export class Ad {
@IsString()
@AutoMap()
uuid: string;
@IsString()
@AutoMap()
userUuid: string;
@ValidateIf((ad) => (ad.passenger ? false : true))
@IsBoolean()
@AutoMap()
driver?: boolean;
@ValidateIf((ad) => (ad.driver ? false : true))
@IsBoolean()
@AutoMap()
passenger?: boolean;
@AutoMap()
frequency: FrequencyType;
@AutoMap()
fromDate: Date;
@AutoMap()
toDate: Date;
@IsDate()
@IsOptional()
@AutoMap()
monTime?: Date;
@IsDate()
@IsOptional()
@AutoMap()
tueTime?: Date;
@IsDate()
@IsOptional()
@AutoMap()
wedTime?: Date;
@IsDate()
@IsOptional()
@AutoMap()
thuTime?: Date;
@IsDate()
@IsOptional()
@AutoMap()
friTime?: Date;
@IsDate()
@IsOptional()
@AutoMap()
satTime?: Date;
@IsDate()
@IsOptional()
@AutoMap()
sunTime?: Date;
@IsInt()
@IsOptional()
@AutoMap()
monMargin?: number;
@IsInt()
@IsOptional()
@AutoMap()
tueMargin?: number;
@IsInt()
@IsOptional()
@AutoMap()
wedMargin?: number;
@IsInt()
@IsOptional()
@AutoMap()
thuMargin?: number;
@IsInt()
@IsOptional()
@AutoMap()
friMargin?: number;
@IsInt()
@IsOptional()
@AutoMap()
satMargin?: number;
@IsInt()
@IsOptional()
@AutoMap()
sunMargin?: number;
@ValidateIf((ad) => (ad.passenger ? false : true))
@IsInt()
@AutoMap()
seatsDriver?: number;
@ValidateIf((ad) => (ad.driver ? false : true))
@IsInt()
@AutoMap()
seatsPassenger?: number;
@IsDate()
@AutoMap()
createdAt: Date;
@IsDate()
@IsOptional()
@AutoMap()
updatedAt?: Date;
@MinLength(2)
@AutoMap()
addresses?: Array<Address>;
}

View File

@ -3,6 +3,7 @@ import { AutomapperProfile, InjectMapper } from '@automapper/nestjs';
import { Injectable } from '@nestjs/common';
import { Ad } from '../domain/entities/ad';
import { AdPresenter } from '../adapters/primaries/ad.presenter';
import { CreateAdRequest } from '../domain/dtos/create-ad.request';
@Injectable()
export class AdProfile extends AutomapperProfile {
@ -13,6 +14,7 @@ export class AdProfile extends AutomapperProfile {
override get profile() {
return (mapper) => {
createMap(mapper, Ad, AdPresenter);
createMap(mapper, CreateAdRequest, Ad);
};
}
}

View File

@ -9,6 +9,8 @@ import { AddressType } from '../../domain/entities/addressEnum';
import { AutomapperModule } from '@automapper/nestjs';
import { classes } from '@automapper/classes';
import { FrequencyType } from '../../domain/entities/frequencyEnum';
import { Ad } from '../../domain/entities/ad';
import { AdProfile } from '../../mappers/ad.profile';
const mockAddress1: Address = {
position: 0,
@ -41,20 +43,23 @@ const newAdRequest: CreateAdRequest = {
tueTime: new Date(''),
wedTime: new Date(''),
seatsPassenger: 2,
adresses: [mockAddress1, mockAddress2],
addresses: [mockAddress1, mockAddress2],
};
const newAdCommand = new CreateAdCommand(newAdRequest);
const mockMessager = {
publish: jest.fn().mockImplementation(),
};
const mockAdRepository = {
findOneByUuid: jest
create: jest
.fn()
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.mockImplementationOnce((command?: CreateAdCommand) => {
return Promise.resolve({
...newAdRequest,
uuid: 'ad000000-0000-4000-a000-000000000000',
createdAt: new Date('01-05-2023'),
});
})
.mockImplementation(() => {
@ -77,6 +82,7 @@ describe('CreateAdUseCase', () => {
useValue: mockMessager,
},
CreateAdUseCase,
AdProfile,
],
}).compile();
@ -87,7 +93,15 @@ describe('CreateAdUseCase', () => {
});
describe('execution', () => {
it('should create an new ad', () => {});
it('should throw an error if the ad already exists', () => {});
it('should create an new ad', async () => {
const newAd: Ad = await createAdUseCase.execute(newAdCommand);
expect(newAd.userUuid).toBe(newAdRequest.userUuid);
expect(newAd.uuid).toBeDefined();
});
it('should throw an error if the ad already exists', async () => {
await expect(
createAdUseCase.execute(newAdCommand),
).rejects.toBeInstanceOf(Error);
});
});
});