import { CommandHandler } from '@nestjs/cqrs'; import { CreateAdCommand } from '../../commands/create-ad.command'; import { Ad } from '../entities/ad'; import { AdRepository } from '../../adapters/secondaries/ad.repository'; import { InjectMapper } from '@automapper/nestjs'; import { Mapper } from '@automapper/core'; import { CreateAdRequest } from '../dtos/create-ad.request'; @CommandHandler(CreateAdCommand) export class CreateAdUseCase { constructor( @InjectMapper() private readonly mapper: Mapper, private readonly adRepository: AdRepository, ) {} async execute(command: CreateAdCommand): Promise { try { const adToCreate: Ad = this.mapper.map( command.createAdRequest, CreateAdRequest, Ad, ); adToCreate.driverDistance = command.geography.driverRoute?.distance; adToCreate.driverDuration = command.geography.driverRoute?.duration; adToCreate.passengerDistance = command.geography.passengerRoute?.distance; adToCreate.passengerDuration = command.geography.passengerRoute?.duration; adToCreate.fwdAzimuth = command.geography.driverRoute ? command.geography.driverRoute.fwdAzimuth : command.geography.passengerRoute.fwdAzimuth; adToCreate.backAzimuth = command.geography.driverRoute ? command.geography.driverRoute.backAzimuth : command.geography.passengerRoute.backAzimuth; return adToCreate; // return await this.adRepository.createAd(adToCreate); } catch (error) { throw error; } } }