import { RepositoryBase, IRepositoryBase } from '@tomei/general';
import { JointHirerModel } from '../../models/joint-hirer.entity';

export class JointHirerRepository
  extends RepositoryBase<JointHirerModel>
  implements IRepositoryBase<JointHirerModel>
{
  constructor() {
    super(JointHirerModel);
  }

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

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

  async delete(hirerId: string, dbTransaction?: any) {
    try {
      const options = {
        where: {
          HirerId: hirerId,
        },
        transaction: dbTransaction,
      };
      await JointHirerModel.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 JointHirerModel.findAndCountAll(options);
      } else {
        jointHirers = await JointHirerModel.findAndCountAll();
      }
      return jointHirers;
    } catch (error) {
      throw new Error(`An Error occured when retriving : ${error.message}`);
    }
  }
}
