Skip to content
Snippets Groups Projects
agent.consumer.service.ts 1.27 KiB
Newer Older
import {
  Injectable,
  Logger,
  OnModuleDestroy,
  OnModuleInit,
} from "@nestjs/common";
import { AgentService } from "../asker/agent.service";
import { IConfAgent } from "../agent.config";
import { ConsumerService } from "@ocm-engine/nats";
import { ConfigService } from "@nestjs/config";

@Injectable()
export class AgentConsumerService implements OnModuleInit, OnModuleDestroy {
  private readonly logger = new Logger(AgentConsumerService.name);
  constructor(
    private readonly agentService: AgentService,
    private readonly consumerService: ConsumerService,
    private readonly configService: ConfigService,
  ) {}

  async onModuleInit(): Promise<void> {
    const config = this.configService.get<IConfAgent>("agent");

    if (config?.agentIsRest) {
      this.logger.log(
        "Agent is configured as rest, there is no need for consumer!",
      );
      return;
    }
    await this.consumerService.subscribe(async (event) => {
      this.logger.debug(JSON.stringify(event, null, 2));
      //TODO: transform the event dto to event<DTO> type
      //call the service methods
      //call the gateway webhook
      return;
    });
  }

  async onModuleDestroy(): Promise<void> {
    this.logger.log("disconnecting from broker");
    await this.consumerService.disconnect();
  }
}