import { RepositoryBase, IRepositoryBase } from '@tomei/general';
import { RentalHirerChangeRequestModel } from '../../models/rental-hirer-change-request.entity';

export class RentalHirerChangeRequestRepository
  extends RepositoryBase<RentalHirerChangeRequestModel>
  implements IRepositoryBase<RentalHirerChangeRequestModel>
{
  constructor() {
    super(RentalHirerChangeRequestModel);
  }

  async findByPk(
    id: string,
    transaction?: any,
  ): Promise<RentalHirerChangeRequestModel | null> {
    try {
      const result = await RentalHirerChangeRequestModel.findByPk(id, {
        transaction: transaction,
      });

      return result;
    } catch (error) {
      throw new Error(`An Error occured when fetching : ${error.message}`);
    }
  }

  async delete(requestId: string, dbTransaction?: any) {
    try {
      const options = {
        where: {
          RequestId: requestId,
        },
        transaction: dbTransaction,
      };
      await RentalHirerChangeRequestModel.destroy(options);
    } catch (error) {
      throw new Error(`An Error occured when delete : ${error.message}`);
    }
  }

  async findAndCountAll(options?: any) {
    try {
      let jointHirers: any;
      if (options) {
        jointHirers =
          await RentalHirerChangeRequestModel.findAndCountAll(options);
      } else {
        jointHirers = await RentalHirerChangeRequestModel.findAndCountAll();
      }
      return jointHirers;
    } catch (error) {
      throw new Error(`An Error occured when retriving : ${error.message}`);
    }
  }

  async findAll(options: any): Promise<RentalHirerChangeRequestModel[]> {
    try {
      const result = await RentalHirerChangeRequestModel.findAll(options);
      return result;
    } catch (error) {
      throw new Error(`An Error occured when fetching : ${error.message}`);
    }
  }
}
