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-05-23 12:00:39 +00:00
|
|
|
import { Coordinate } from '../../../geography/domain/entities/coordinate';
|
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.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);
|
2023-05-22 09:25:09 +00:00
|
|
|
this.setTimezone(command.createAdRequest.waypoints);
|
|
|
|
this.setGeography(command.createAdRequest.waypoints);
|
2023-05-12 14:23:42 +00:00
|
|
|
this.setRoles(command.createAdRequest);
|
|
|
|
await this.geography.createRoutes(this.roles, this.georouter, {
|
|
|
|
withDistance: false,
|
|
|
|
withPoints: true,
|
|
|
|
withTime: false,
|
|
|
|
});
|
2023-05-22 14:38:34 +00:00
|
|
|
this.setAdGeography(command);
|
|
|
|
this.setAdSchedule(command);
|
2023-05-12 14:23:42 +00:00
|
|
|
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
|
|
|
|
2023-05-22 09:25:09 +00:00
|
|
|
private setTimezone = (coordinates: Coordinate[]): void => {
|
|
|
|
this.timezone = this.defaultParams.DEFAULT_TIMEZONE;
|
|
|
|
try {
|
|
|
|
const timezones = this.timezoneFinder.timezones(
|
|
|
|
coordinates[0].lon,
|
2023-05-23 12:00:39 +00:00
|
|
|
coordinates[0].lat,
|
2023-05-22 09:25:09 +00:00
|
|
|
);
|
|
|
|
if (timezones.length > 0) this.timezone = timezones[0];
|
|
|
|
} catch (e) {}
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2023-05-22 09:25:09 +00:00
|
|
|
private setGeography = (coordinates: Coordinate[]): void => {
|
|
|
|
this.geography = new Geography(coordinates);
|
2023-05-12 14:23:42 +00:00
|
|
|
};
|
2023-05-22 14:38:34 +00:00
|
|
|
|
|
|
|
private setAdGeography = (command: CreateAdCommand): void => {
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
private setAdSchedule = (command: CreateAdCommand): void => {
|
|
|
|
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,
|
|
|
|
);
|
|
|
|
};
|
2023-04-25 15:49:47 +00:00
|
|
|
}
|