import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { CustomerLog } from './customerlog.entity';
import { Repository } from 'typeorm';

@Injectable()
export class CustomerlogService {
  constructor(
    @InjectRepository(CustomerLog)
    private customerLogRepo: Repository<CustomerLog>,
  ) {}

  async getAll(): Promise<any> {
    const data = await this.customerLogRepo.query(
      'SELECT c.*,cus.First_Name FROM customer_log c INNER JOIN customer cus ON c.Customer_idCustomer = cus.idCustomer',
    );
    return data;
  }

  async addLog(body: any): Promise<any> {
    const userLog = await this.customerLogRepo.create(body);
    await this.customerLogRepo.save(userLog);
    return { success: true, message: 'Customer Log adding completed!' };
  }

  async getCustomerLogByCustomerId(idCustomer: number): Promise<any> {
    const data = await this.customerLogRepo.findBy({
      Customer_idCustomer: idCustomer,
    });
    return data;
  }
}
