mirror of
https://gitlab.com/mobicoop/v3/service/matcher.git
synced 2025-12-31 10:02:42 +00:00
refactor to ddh, first commit
This commit is contained in:
82
old/modules/ad/adapters/primaries/ad-messager.service.ts
Normal file
82
old/modules/ad/adapters/primaries/ad-messager.service.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { Controller, Inject } from '@nestjs/common';
|
||||
import { CommandBus } from '@nestjs/cqrs';
|
||||
import { CreateAdCommand } from '../../commands/create-ad.command';
|
||||
import { CreateAdRequest } from '../../domain/dtos/create-ad.request';
|
||||
import { validateOrReject } from 'class-validator';
|
||||
import { plainToInstance } from 'class-transformer';
|
||||
import { DatabaseException } from 'src/modules/database/exceptions/database.exception';
|
||||
import { ExceptionCode } from 'src/modules/utils/exception-code.enum';
|
||||
import { IPublishMessage } from 'src/interfaces/message-publisher';
|
||||
import { MESSAGE_PUBLISHER } from 'src/app.constants';
|
||||
import { RabbitSubscribe } from '@mobicoop/message-broker-module';
|
||||
|
||||
@Controller()
|
||||
export class AdMessagerService {
|
||||
constructor(
|
||||
@Inject(MESSAGE_PUBLISHER)
|
||||
private readonly messagePublisher: IPublishMessage,
|
||||
private readonly commandBus: CommandBus,
|
||||
) {}
|
||||
|
||||
@RabbitSubscribe({
|
||||
name: 'adCreated',
|
||||
})
|
||||
async adCreatedHandler(message: string): Promise<void> {
|
||||
let createAdRequest: CreateAdRequest;
|
||||
// parse message to request instance
|
||||
try {
|
||||
createAdRequest = plainToInstance(CreateAdRequest, JSON.parse(message));
|
||||
// validate instance
|
||||
await validateOrReject(createAdRequest);
|
||||
// validate nested objects (fixes direct nested validation bug)
|
||||
for (const waypoint of createAdRequest.addresses) {
|
||||
try {
|
||||
await validateOrReject(waypoint);
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
this.messagePublisher.publish(
|
||||
'matcher.ad.crit',
|
||||
JSON.stringify({
|
||||
message: `Can't validate message : ${message}`,
|
||||
error: e,
|
||||
}),
|
||||
);
|
||||
}
|
||||
try {
|
||||
await this.commandBus.execute(new CreateAdCommand(createAdRequest));
|
||||
} catch (e) {
|
||||
if (e instanceof DatabaseException) {
|
||||
if (e.message.includes('already exists')) {
|
||||
this.messagePublisher.publish(
|
||||
'matcher.ad.crit',
|
||||
JSON.stringify({
|
||||
code: ExceptionCode.ALREADY_EXISTS,
|
||||
message: 'Already exists',
|
||||
uuid: createAdRequest.uuid,
|
||||
}),
|
||||
);
|
||||
}
|
||||
if (e.message.includes("Can't reach database server")) {
|
||||
this.messagePublisher.publish(
|
||||
'matcher.ad.crit',
|
||||
JSON.stringify({
|
||||
code: ExceptionCode.UNAVAILABLE,
|
||||
message: 'Database server unavailable',
|
||||
uuid: createAdRequest.uuid,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
this.messagePublisher.publish(
|
||||
'logging.matcher.ad.crit',
|
||||
JSON.stringify({
|
||||
message,
|
||||
error: e,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user