import { Controller } from '@nestjs/common';
import { MerchantlogService } from './merchantlog.service';
import { MessagePattern, RpcException } from '@nestjs/microservices';

@Controller()
export class MerchantlogController {
  constructor(private readonly merchantLogDbService: MerchantlogService) {}

  @MessagePattern({ cmd: 'merchant_log_get_all' })
  async getAll(data: { protocol: string; host: string }): Promise<any> {
    try {
      const { protocol, host } = data;
      const getallLogs = await this.merchantLogDbService.getAll();
      return getallLogs;
    } catch (err) {
      console.log(err);
      throw new RpcException('Failed to retrieve logs');
    }
  }

  @MessagePattern({ cmd: 'merchant_log_add' })
  async addLog(data: { body: any }): Promise<any> {
    try {
      const { body } = data;
      const add = await this.merchantLogDbService.addLog(body);
      return add;
    } catch (err) {
      console.error(err);
      throw new RpcException('failed to add merchant log!');
    }
  }

  @MessagePattern({ cmd: 'get_merchant_log_by_merchant_id' })
  async getMerchantLogByMerchantId(data: { idMerchant: number }): Promise<any> {
    try {
      const { idMerchant } = data;
      const fetchData =
        await this.merchantLogDbService.getMerchantLogByMerchantId(idMerchant);

      return fetchData;
    } catch (err) {
      console.error(err);
      throw new RpcException('failed to fetch merchant log by merchant id!');
    }
  }
}
