import { IGeorouter } from '../../geography/domain/interfaces/georouter.interface'; import { ICreateGeorouter } from '../../geography/domain/interfaces/georouter-creator.interface'; import { CreateAdRequest } from '../domain/dtos/create-ad.request'; import { Geography } from '../domain/entities/geography'; import { IDefaultParams } from '../domain/types/default-params.type'; import { Role } from '../domain/types/role.enum'; import { IFindTimezone } from '../../geography/domain/interfaces/timezone-finder.interface'; export class CreateAdCommand { readonly createAdRequest: CreateAdRequest; private readonly defaultParams: IDefaultParams; private readonly georouter: IGeorouter; private readonly timezoneFinder: IFindTimezone; roles: Role[]; geography: Geography; timezone: string; constructor( request: CreateAdRequest, defaultParams: IDefaultParams, georouterCreator: ICreateGeorouter, timezoneFinder: IFindTimezone, ) { this.createAdRequest = request; this.defaultParams = defaultParams; this.georouter = georouterCreator.create( defaultParams.GEOROUTER_TYPE, defaultParams.GEOROUTER_URL, ); this.timezoneFinder = timezoneFinder; this.setRoles(); this.setGeography(); } private setRoles = (): void => { this.roles = []; if (this.createAdRequest.driver) this.roles.push(Role.DRIVER); if (this.createAdRequest.passenger) this.roles.push(Role.PASSENGER); }; private setGeography = async (): Promise => { this.geography = new Geography(this.createAdRequest.waypoints, { timezone: this.defaultParams.DEFAULT_TIMEZONE, finder: this.timezoneFinder, }); if (this.geography.timezones.length > 0) this.createAdRequest.timezone = this.geography.timezones[0]; try { await this.geography.createRoutes(this.roles, this.georouter); } catch (e) { console.log(e); } }; }