2023-04-24 14:44:52 +00:00
|
|
|
import { RabbitSubscribe } from '@golevelup/nestjs-rabbitmq';
|
|
|
|
import { Controller } from '@nestjs/common';
|
2023-04-25 15:49:47 +00:00
|
|
|
import { Ad } from '../../domain/entities/ad';
|
|
|
|
import { InjectMapper } from '@automapper/nestjs';
|
|
|
|
import { Mapper } from '@automapper/core';
|
|
|
|
import { CommandBus } from '@nestjs/cqrs';
|
|
|
|
import { CreateAdCommand } from '../../commands/create-ad.command';
|
|
|
|
import { CreateAdRequest } from '../../domain/dtos/create-ad.request';
|
2023-04-28 13:53:57 +00:00
|
|
|
import { validateOrReject } from 'class-validator';
|
|
|
|
import { Messager } from '../secondaries/messager';
|
|
|
|
import { GeoTimezoneFinder } from 'src/modules/geography/adapters/secondaries/geo-timezone-finder';
|
2023-04-24 14:44:52 +00:00
|
|
|
|
|
|
|
@Controller()
|
|
|
|
export class AdMessagerController {
|
2023-04-25 15:49:47 +00:00
|
|
|
constructor(
|
2023-04-28 13:53:57 +00:00
|
|
|
private readonly messager: Messager,
|
2023-04-26 10:10:22 +00:00
|
|
|
private readonly commandBus: CommandBus,
|
|
|
|
@InjectMapper() private readonly mapper: Mapper,
|
2023-04-28 13:53:57 +00:00
|
|
|
private readonly timezoneFinder: GeoTimezoneFinder,
|
2023-04-25 15:49:47 +00:00
|
|
|
) {}
|
|
|
|
|
2023-04-24 14:44:52 +00:00
|
|
|
@RabbitSubscribe({
|
|
|
|
name: 'adCreated',
|
|
|
|
})
|
2023-04-26 10:10:22 +00:00
|
|
|
async adCreatedHandler(message: string): Promise<void> {
|
2023-04-28 13:53:57 +00:00
|
|
|
let createAdRequest: CreateAdRequest;
|
2023-04-25 15:49:47 +00:00
|
|
|
try {
|
2023-04-26 10:10:22 +00:00
|
|
|
// parse message to conform to CreateAdRequest (not a real instance yet)
|
|
|
|
const parsedMessage: CreateAdRequest = JSON.parse(message);
|
|
|
|
// create a real instance of CreateAdRequest from parsed message
|
2023-04-28 13:53:57 +00:00
|
|
|
createAdRequest = this.mapper.map(
|
|
|
|
parsedMessage,
|
|
|
|
CreateAdRequest,
|
|
|
|
CreateAdRequest,
|
|
|
|
);
|
2023-04-26 10:10:22 +00:00
|
|
|
// validate instance
|
2023-04-28 13:53:57 +00:00
|
|
|
await validateOrReject(createAdRequest);
|
|
|
|
// validate nested objects (fixes direct nested validation bug)
|
|
|
|
for (const waypoint of createAdRequest.waypoints) {
|
|
|
|
try {
|
|
|
|
await validateOrReject(waypoint);
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
createAdRequest.timezone = this.timezoneFinder.timezones(
|
|
|
|
createAdRequest.waypoints[0].lon,
|
|
|
|
createAdRequest.waypoints[0].lat,
|
|
|
|
)[0];
|
2023-04-26 10:10:22 +00:00
|
|
|
const ad: Ad = await this.commandBus.execute(
|
2023-04-25 15:49:47 +00:00
|
|
|
new CreateAdCommand(createAdRequest),
|
|
|
|
);
|
2023-04-26 10:10:22 +00:00
|
|
|
console.log(ad);
|
2023-04-25 15:49:47 +00:00
|
|
|
} catch (e) {
|
2023-04-28 13:53:57 +00:00
|
|
|
this.messager.publish(
|
|
|
|
'logging.matcher.ad.crit',
|
|
|
|
JSON.stringify({
|
|
|
|
createAdRequest,
|
|
|
|
error: e,
|
|
|
|
}),
|
|
|
|
);
|
2023-04-25 15:49:47 +00:00
|
|
|
}
|
2023-04-24 14:44:52 +00:00
|
|
|
}
|
|
|
|
}
|