import { Controller } from '@nestjs/common';
import { MessagePattern, RpcException } from '@nestjs/microservices';
import { CustomersavinglogService } from './customersavinglog.service';

@Controller()
export class CustomersavinglogController {
  constructor(private readonly logService: CustomersavinglogService) {}

  @MessagePattern({ cmd: 'customer_saving_log_get_all' })
  async getAll() {
    try {
      return await this.logService.getAll();
    } catch (err) {
      console.error(err);
      throw new RpcException('Failed to retrieve logs');
    }
  }

  @MessagePattern({ cmd: 'customer_saving_log_get_by_id' })
  async getById(data: { id: number }) {
    try {
      return await this.logService.getById(data.id);
    } catch (err) {
      console.error(err);
      throw new RpcException('Failed to get log by ID');
    }
  }

  @MessagePattern({ cmd: 'customer_saving_log_get_by_saving_id' })
  async getBySavingId(data: { id: number }) {
    try {
      return await this.logService.getBySavingId(data.id);
    } catch (err) {
      console.error(err);
      throw new RpcException('Failed to get log by saving ID');
    }
  }

  @MessagePattern({ cmd: 'customer_saving_log_add' })
  async add(data: any) {
    try {
      return await this.logService.add(data);
    } catch (err) {
      console.error(err);
      throw new RpcException('Failed to add log');
    }
  }
}
