2023-08-16 10:28:20 +00:00
|
|
|
import { Controller, Inject } from '@nestjs/common';
|
|
|
|
import { CommandBus } from '@nestjs/cqrs';
|
2023-04-25 15:49:47 +00:00
|
|
|
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';
|
2023-05-11 15:47:55 +00:00
|
|
|
import { plainToInstance } from 'class-transformer';
|
2023-05-24 14:21:59 +00:00
|
|
|
import { DatabaseException } from 'src/modules/database/exceptions/database.exception';
|
|
|
|
import { ExceptionCode } from 'src/modules/utils/exception-code.enum';
|
2023-08-16 10:28:20 +00:00
|
|
|
import { IPublishMessage } from 'src/interfaces/message-publisher';
|
|
|
|
import { MESSAGE_PUBLISHER } from 'src/app.constants';
|
|
|
|
import { RabbitSubscribe } from '@mobicoop/message-broker-module';
|
2023-04-24 14:44:52 +00:00
|
|
|
|
|
|
|
@Controller()
|
2023-08-16 10:28:20 +00:00
|
|
|
export class AdMessagerService {
|
2023-04-25 15:49:47 +00:00
|
|
|
constructor(
|
2023-08-16 10:28:20 +00:00
|
|
|
@Inject(MESSAGE_PUBLISHER)
|
|
|
|
private readonly messagePublisher: IPublishMessage,
|
2023-04-26 10:10:22 +00:00
|
|
|
private readonly commandBus: CommandBus,
|
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-05-24 14:21:59 +00:00
|
|
|
let createAdRequest: CreateAdRequest;
|
|
|
|
// parse message to request instance
|
2023-04-25 15:49:47 +00:00
|
|
|
try {
|
2023-05-24 14:21:59 +00:00
|
|
|
createAdRequest = plainToInstance(CreateAdRequest, JSON.parse(message));
|
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)
|
2023-08-16 10:28:20 +00:00
|
|
|
for (const waypoint of createAdRequest.addresses) {
|
2023-04-28 13:53:57 +00:00
|
|
|
try {
|
|
|
|
await validateOrReject(waypoint);
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2023-05-24 14:21:59 +00:00
|
|
|
} catch (e) {
|
2023-08-16 10:28:20 +00:00
|
|
|
this.messagePublisher.publish(
|
2023-05-24 14:21:59 +00:00
|
|
|
'matcher.ad.crit',
|
|
|
|
JSON.stringify({
|
|
|
|
message: `Can't validate message : ${message}`,
|
|
|
|
error: e,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
try {
|
2023-05-23 12:00:39 +00:00
|
|
|
await this.commandBus.execute(new CreateAdCommand(createAdRequest));
|
2023-04-25 15:49:47 +00:00
|
|
|
} catch (e) {
|
2023-05-24 14:21:59 +00:00
|
|
|
if (e instanceof DatabaseException) {
|
|
|
|
if (e.message.includes('already exists')) {
|
2023-08-16 10:28:20 +00:00
|
|
|
this.messagePublisher.publish(
|
2023-05-24 14:21:59 +00:00
|
|
|
'matcher.ad.crit',
|
|
|
|
JSON.stringify({
|
|
|
|
code: ExceptionCode.ALREADY_EXISTS,
|
|
|
|
message: 'Already exists',
|
|
|
|
uuid: createAdRequest.uuid,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (e.message.includes("Can't reach database server")) {
|
2023-08-16 10:28:20 +00:00
|
|
|
this.messagePublisher.publish(
|
2023-05-24 14:21:59 +00:00
|
|
|
'matcher.ad.crit',
|
|
|
|
JSON.stringify({
|
|
|
|
code: ExceptionCode.UNAVAILABLE,
|
|
|
|
message: 'Database server unavailable',
|
|
|
|
uuid: createAdRequest.uuid,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-08-16 10:28:20 +00:00
|
|
|
this.messagePublisher.publish(
|
2023-04-28 13:53:57 +00:00
|
|
|
'logging.matcher.ad.crit',
|
|
|
|
JSON.stringify({
|
2023-05-11 15:47:55 +00:00
|
|
|
message,
|
2023-04-28 13:53:57 +00:00
|
|
|
error: e,
|
|
|
|
}),
|
|
|
|
);
|
2023-04-25 15:49:47 +00:00
|
|
|
}
|
2023-04-24 14:44:52 +00:00
|
|
|
}
|
|
|
|
}
|