WIP adding create add service
This commit is contained in:
		
							parent
							
								
									cac8e90c9b
								
							
						
					
					
						commit
						1db7dc1104
					
				| 
						 | 
					@ -6,6 +6,7 @@ import {
 | 
				
			||||||
  IsDate,
 | 
					  IsDate,
 | 
				
			||||||
  IsInt,
 | 
					  IsInt,
 | 
				
			||||||
  MinLength,
 | 
					  MinLength,
 | 
				
			||||||
 | 
					  ValidateIf,
 | 
				
			||||||
} from 'class-validator';
 | 
					} from 'class-validator';
 | 
				
			||||||
import { FrequencyType } from '../entities/frequencyEnum';
 | 
					import { FrequencyType } from '../entities/frequencyEnum';
 | 
				
			||||||
import { Address } from '../entities/address';
 | 
					import { Address } from '../entities/address';
 | 
				
			||||||
| 
						 | 
					@ -20,12 +21,12 @@ export class CreateAdRequest {
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  userUuid: string;
 | 
					  userUuid: string;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  //@ValidateIf(o => o.passenger? false : true )
 | 
					  @ValidateIf((ad) => (ad.passenger ? false : true))
 | 
				
			||||||
  @IsBoolean()
 | 
					  @IsBoolean()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  driver: boolean;
 | 
					  driver?: boolean;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  //@ValidateIf(o => o.driver? false : true )
 | 
					  @ValidateIf((ad) => (ad.driver ? false : true))
 | 
				
			||||||
  @IsBoolean()
 | 
					  @IsBoolean()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  passenger?: boolean;
 | 
					  passenger?: boolean;
 | 
				
			||||||
| 
						 | 
					@ -42,92 +43,94 @@ export class CreateAdRequest {
 | 
				
			||||||
  @IsDate()
 | 
					  @IsDate()
 | 
				
			||||||
  @IsOptional()
 | 
					  @IsOptional()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  monTime: Date;
 | 
					  monTime?: Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @IsDate()
 | 
					  @IsDate()
 | 
				
			||||||
  @IsOptional()
 | 
					  @IsOptional()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  tueTime: Date;
 | 
					  tueTime?: Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @IsDate()
 | 
					  @IsDate()
 | 
				
			||||||
  @IsOptional()
 | 
					  @IsOptional()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  wedTime: Date;
 | 
					  wedTime?: Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @IsDate()
 | 
					  @IsDate()
 | 
				
			||||||
  @IsOptional()
 | 
					  @IsOptional()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  thuTime: Date;
 | 
					  thuTime?: Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @IsDate()
 | 
					  @IsDate()
 | 
				
			||||||
  @IsOptional()
 | 
					  @IsOptional()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  friTime: Date;
 | 
					  friTime?: Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @IsDate()
 | 
					  @IsDate()
 | 
				
			||||||
  @IsOptional()
 | 
					  @IsOptional()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  satTime: Date;
 | 
					  satTime?: Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @IsDate()
 | 
					  @IsDate()
 | 
				
			||||||
  @IsOptional()
 | 
					  @IsOptional()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  sunTime: Date;
 | 
					  sunTime?: Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @IsInt()
 | 
					  @IsInt()
 | 
				
			||||||
  @IsOptional()
 | 
					  @IsOptional()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  Margin: number;
 | 
					  monMargin?: number;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @IsInt()
 | 
					  @IsInt()
 | 
				
			||||||
  @IsOptional()
 | 
					  @IsOptional()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  tueMargin: number;
 | 
					  tueMargin?: number;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @IsInt()
 | 
					  @IsInt()
 | 
				
			||||||
  @IsOptional()
 | 
					  @IsOptional()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  wedMargin: number;
 | 
					  wedMargin?: number;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @IsInt()
 | 
					  @IsInt()
 | 
				
			||||||
  @IsOptional()
 | 
					  @IsOptional()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  thuMargin: number;
 | 
					  thuMargin?: number;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @IsInt()
 | 
					  @IsInt()
 | 
				
			||||||
  @IsOptional()
 | 
					  @IsOptional()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  friMargin: number;
 | 
					  friMargin?: number;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @IsInt()
 | 
					  @IsInt()
 | 
				
			||||||
  @IsOptional()
 | 
					  @IsOptional()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  satMargin: number;
 | 
					  satMargin?: number;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @IsInt()
 | 
					  @IsInt()
 | 
				
			||||||
  @IsOptional()
 | 
					  @IsOptional()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  sunMargin: number;
 | 
					  sunMargin?: number;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  //@ValidateIf(o => o.passenger? false : true )
 | 
					  @ValidateIf((ad) => (ad.passenger ? false : true))
 | 
				
			||||||
  @IsInt()
 | 
					  @IsInt()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  seatsDriver?: number;
 | 
					  seatsDriver?: number;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  //@ValidateIf(o => o.driver? false : true )
 | 
					  @ValidateIf((ad) => (ad.driver ? false : true))
 | 
				
			||||||
  @IsInt()
 | 
					  @IsInt()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  seatsPassenger?: number;
 | 
					  seatsPassenger?: number;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @IsDate()
 | 
					  @IsDate()
 | 
				
			||||||
 | 
					  @IsOptional()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  createdAt: Date;
 | 
					  createdAt?: Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @IsDate()
 | 
					  @IsDate()
 | 
				
			||||||
 | 
					  @IsOptional()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  updatedAt: Date;
 | 
					  updatedAt?: Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @MinLength(2)
 | 
					  @MinLength(2)
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  addresses: Array<Address>;
 | 
					  addresses?: Array<Address>;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,134 @@
 | 
				
			||||||
import { AutoMap } from '@automapper/classes';
 | 
					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 {
 | 
					export class Ad {
 | 
				
			||||||
 | 
					  @IsString()
 | 
				
			||||||
  @AutoMap()
 | 
					  @AutoMap()
 | 
				
			||||||
  uuid: string;
 | 
					  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>;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3,6 +3,7 @@ import { AutomapperProfile, InjectMapper } from '@automapper/nestjs';
 | 
				
			||||||
import { Injectable } from '@nestjs/common';
 | 
					import { Injectable } from '@nestjs/common';
 | 
				
			||||||
import { Ad } from '../domain/entities/ad';
 | 
					import { Ad } from '../domain/entities/ad';
 | 
				
			||||||
import { AdPresenter } from '../adapters/primaries/ad.presenter';
 | 
					import { AdPresenter } from '../adapters/primaries/ad.presenter';
 | 
				
			||||||
 | 
					import { CreateAdRequest } from '../domain/dtos/create-ad.request';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@Injectable()
 | 
					@Injectable()
 | 
				
			||||||
export class AdProfile extends AutomapperProfile {
 | 
					export class AdProfile extends AutomapperProfile {
 | 
				
			||||||
| 
						 | 
					@ -13,6 +14,7 @@ export class AdProfile extends AutomapperProfile {
 | 
				
			||||||
  override get profile() {
 | 
					  override get profile() {
 | 
				
			||||||
    return (mapper) => {
 | 
					    return (mapper) => {
 | 
				
			||||||
      createMap(mapper, Ad, AdPresenter);
 | 
					      createMap(mapper, Ad, AdPresenter);
 | 
				
			||||||
 | 
					      createMap(mapper, CreateAdRequest, Ad);
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -9,6 +9,8 @@ import { AddressType } from '../../domain/entities/addressEnum';
 | 
				
			||||||
import { AutomapperModule } from '@automapper/nestjs';
 | 
					import { AutomapperModule } from '@automapper/nestjs';
 | 
				
			||||||
import { classes } from '@automapper/classes';
 | 
					import { classes } from '@automapper/classes';
 | 
				
			||||||
import { FrequencyType } from '../../domain/entities/frequencyEnum';
 | 
					import { FrequencyType } from '../../domain/entities/frequencyEnum';
 | 
				
			||||||
 | 
					import { Ad } from '../../domain/entities/ad';
 | 
				
			||||||
 | 
					import { AdProfile } from '../../mappers/ad.profile';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const mockAddress1: Address = {
 | 
					const mockAddress1: Address = {
 | 
				
			||||||
  position: 0,
 | 
					  position: 0,
 | 
				
			||||||
| 
						 | 
					@ -41,20 +43,23 @@ const newAdRequest: CreateAdRequest = {
 | 
				
			||||||
  tueTime: new Date(''),
 | 
					  tueTime: new Date(''),
 | 
				
			||||||
  wedTime: new Date(''),
 | 
					  wedTime: new Date(''),
 | 
				
			||||||
  seatsPassenger: 2,
 | 
					  seatsPassenger: 2,
 | 
				
			||||||
  adresses: [mockAddress1, mockAddress2],
 | 
					  addresses: [mockAddress1, mockAddress2],
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const newAdCommand = new CreateAdCommand(newAdRequest);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const mockMessager = {
 | 
					const mockMessager = {
 | 
				
			||||||
  publish: jest.fn().mockImplementation(),
 | 
					  publish: jest.fn().mockImplementation(),
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
const mockAdRepository = {
 | 
					const mockAdRepository = {
 | 
				
			||||||
  findOneByUuid: jest
 | 
					  create: jest
 | 
				
			||||||
    .fn()
 | 
					    .fn()
 | 
				
			||||||
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
 | 
					    // eslint-disable-next-line @typescript-eslint/no-unused-vars
 | 
				
			||||||
    .mockImplementationOnce((command?: CreateAdCommand) => {
 | 
					    .mockImplementationOnce((command?: CreateAdCommand) => {
 | 
				
			||||||
      return Promise.resolve({
 | 
					      return Promise.resolve({
 | 
				
			||||||
        ...newAdRequest,
 | 
					        ...newAdRequest,
 | 
				
			||||||
        uuid: 'ad000000-0000-4000-a000-000000000000',
 | 
					        uuid: 'ad000000-0000-4000-a000-000000000000',
 | 
				
			||||||
 | 
					        createdAt: new Date('01-05-2023'),
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
    .mockImplementation(() => {
 | 
					    .mockImplementation(() => {
 | 
				
			||||||
| 
						 | 
					@ -77,6 +82,7 @@ describe('CreateAdUseCase', () => {
 | 
				
			||||||
          useValue: mockMessager,
 | 
					          useValue: mockMessager,
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        CreateAdUseCase,
 | 
					        CreateAdUseCase,
 | 
				
			||||||
 | 
					        AdProfile,
 | 
				
			||||||
      ],
 | 
					      ],
 | 
				
			||||||
    }).compile();
 | 
					    }).compile();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -87,7 +93,15 @@ describe('CreateAdUseCase', () => {
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  describe('execution', () => {
 | 
					  describe('execution', () => {
 | 
				
			||||||
    it('should create an new ad', () => {});
 | 
					    it('should create an new ad', async () => {
 | 
				
			||||||
    it('should throw an error if the ad already exists', () => {});
 | 
					      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);
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue