2023-04-25 15:49:47 +00:00
|
|
|
import { CommandHandler } from '@nestjs/cqrs';
|
|
|
|
import { CreateAdCommand } from '../../commands/create-ad.command';
|
|
|
|
import { Ad } from '../entities/ad';
|
|
|
|
import { AdRepository } from '../../adapters/secondaries/ad.repository';
|
2023-04-26 10:10:22 +00:00
|
|
|
import { InjectMapper } from '@automapper/nestjs';
|
|
|
|
import { Mapper } from '@automapper/core';
|
|
|
|
import { CreateAdRequest } from '../dtos/create-ad.request';
|
2023-05-12 14:23:42 +00:00
|
|
|
import { Inject } from '@nestjs/common';
|
|
|
|
import { IProvideParams } from '../interfaces/params-provider.interface';
|
|
|
|
import { ICreateGeorouter } from '../../../geography/domain/interfaces/georouter-creator.interface';
|
|
|
|
import { IFindTimezone } from '../../../geography/domain/interfaces/timezone-finder.interface';
|
|
|
|
import { IGeorouter } from '../../../geography/domain/interfaces/georouter.interface';
|
|
|
|
import { DefaultParams } from '../types/default-params.type';
|
|
|
|
import { Role } from '../types/role.enum';
|
|
|
|
import { Geography } from '../entities/geography';
|
|
|
|
import { IEncodeDirection } from '../../../geography/domain/interfaces/direction-encoder.interface';
|
|
|
|
import { TimeConverter } from '../entities/time-converter';
|
2023-04-25 15:49:47 +00:00
|
|
|
|
|
|
|
@CommandHandler(CreateAdCommand)
|
|
|
|
export class CreateAdUseCase {
|
2023-05-12 14:23:42 +00:00
|
|
|
private readonly georouter: IGeorouter;
|
|
|
|
private readonly defaultParams: DefaultParams;
|
|
|
|
private timezone: string;
|
|
|
|
private roles: Role[];
|
|
|
|
private geography: Geography;
|
|
|
|
private ad: Ad;
|
|
|
|
|
2023-04-26 10:10:22 +00:00
|
|
|
constructor(
|
|
|
|
@InjectMapper() private readonly mapper: Mapper,
|
|
|
|
private readonly adRepository: AdRepository,
|
2023-05-12 14:23:42 +00:00
|
|
|
@Inject('ParamsProvider')
|
|
|
|
private readonly defaultParamsProvider: IProvideParams,
|
|
|
|
@Inject('GeorouterCreator')
|
|
|
|
private readonly georouterCreator: ICreateGeorouter,
|
|
|
|
@Inject('TimezoneFinder')
|
|
|
|
private readonly timezoneFinder: IFindTimezone,
|
|
|
|
@Inject('DirectionEncoder')
|
|
|
|
private readonly directionEncoder: IEncodeDirection,
|
|
|
|
) {
|
|
|
|
this.defaultParams = defaultParamsProvider.getParams();
|
|
|
|
this.timezone = this.defaultParams.DEFAULT_TIMEZONE;
|
|
|
|
this.georouter = georouterCreator.create(
|
|
|
|
this.defaultParams.GEOROUTER_TYPE,
|
|
|
|
this.defaultParams.GEOROUTER_URL,
|
|
|
|
);
|
|
|
|
}
|
2023-04-25 15:49:47 +00:00
|
|
|
|
|
|
|
async execute(command: CreateAdCommand): Promise<Ad> {
|
|
|
|
try {
|
2023-05-12 14:23:42 +00:00
|
|
|
this.ad = this.mapper.map(command.createAdRequest, CreateAdRequest, Ad);
|
|
|
|
this.setRoles(command.createAdRequest);
|
|
|
|
this.setGeography(command.createAdRequest);
|
|
|
|
await this.geography.createRoutes(this.roles, this.georouter, {
|
|
|
|
withDistance: false,
|
|
|
|
withPoints: true,
|
|
|
|
withTime: false,
|
|
|
|
});
|
|
|
|
this.ad.driverDistance = this.geography.driverRoute?.distance;
|
|
|
|
this.ad.driverDuration = this.geography.driverRoute?.duration;
|
|
|
|
this.ad.passengerDistance = this.geography.passengerRoute?.distance;
|
|
|
|
this.ad.passengerDuration = this.geography.passengerRoute?.duration;
|
|
|
|
this.ad.fwdAzimuth = this.geography.driverRoute
|
|
|
|
? this.geography.driverRoute.fwdAzimuth
|
|
|
|
: this.geography.passengerRoute.fwdAzimuth;
|
|
|
|
this.ad.backAzimuth = this.geography.driverRoute
|
|
|
|
? this.geography.driverRoute.backAzimuth
|
|
|
|
: this.geography.passengerRoute.backAzimuth;
|
|
|
|
this.ad.waypoints = this.directionEncoder.encode(
|
|
|
|
command.createAdRequest.waypoints,
|
|
|
|
);
|
|
|
|
this.ad.direction = this.geography.driverRoute
|
|
|
|
? this.directionEncoder.encode(this.geography.driverRoute.points)
|
|
|
|
: undefined;
|
|
|
|
this.ad.monTime = TimeConverter.toUtcDatetime(
|
|
|
|
this.ad.fromDate,
|
|
|
|
command.createAdRequest.monTime,
|
|
|
|
this.timezone,
|
|
|
|
);
|
|
|
|
this.ad.tueTime = TimeConverter.toUtcDatetime(
|
|
|
|
this.ad.fromDate,
|
|
|
|
command.createAdRequest.tueTime,
|
|
|
|
this.timezone,
|
|
|
|
);
|
|
|
|
this.ad.wedTime = TimeConverter.toUtcDatetime(
|
|
|
|
this.ad.fromDate,
|
|
|
|
command.createAdRequest.wedTime,
|
|
|
|
this.timezone,
|
|
|
|
);
|
|
|
|
this.ad.thuTime = TimeConverter.toUtcDatetime(
|
|
|
|
this.ad.fromDate,
|
|
|
|
command.createAdRequest.thuTime,
|
|
|
|
this.timezone,
|
|
|
|
);
|
|
|
|
this.ad.friTime = TimeConverter.toUtcDatetime(
|
|
|
|
this.ad.fromDate,
|
|
|
|
command.createAdRequest.friTime,
|
|
|
|
this.timezone,
|
|
|
|
);
|
|
|
|
this.ad.satTime = TimeConverter.toUtcDatetime(
|
|
|
|
this.ad.fromDate,
|
|
|
|
command.createAdRequest.satTime,
|
|
|
|
this.timezone,
|
|
|
|
);
|
|
|
|
this.ad.sunTime = TimeConverter.toUtcDatetime(
|
|
|
|
this.ad.fromDate,
|
|
|
|
command.createAdRequest.sunTime,
|
|
|
|
this.timezone,
|
|
|
|
);
|
|
|
|
return await this.adRepository.createAd(this.ad);
|
2023-04-25 15:49:47 +00:00
|
|
|
} catch (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2023-05-12 14:23:42 +00:00
|
|
|
|
|
|
|
private setRoles = (createAdRequest: CreateAdRequest): void => {
|
|
|
|
this.roles = [];
|
|
|
|
if (createAdRequest.driver) this.roles.push(Role.DRIVER);
|
|
|
|
if (createAdRequest.passenger) this.roles.push(Role.PASSENGER);
|
|
|
|
};
|
|
|
|
|
|
|
|
private setGeography = (createAdRequest: CreateAdRequest): void => {
|
|
|
|
this.geography = new Geography(createAdRequest.waypoints, {
|
|
|
|
timezone: this.defaultParams.DEFAULT_TIMEZONE,
|
|
|
|
finder: this.timezoneFinder,
|
|
|
|
});
|
|
|
|
if (this.geography.timezones.length > 0)
|
|
|
|
this.timezone = this.geography.timezones[0];
|
|
|
|
};
|
2023-04-25 15:49:47 +00:00
|
|
|
}
|