import { RepositoryBase, IRepositoryBase } from '@tomei/general';
import { AgreementSignatureModel } from '../../models/agreement-signature.entity';

export class AgreementSignatureRepository
  extends RepositoryBase<AgreementSignatureModel>
  implements IRepositoryBase<AgreementSignatureModel>
{
  constructor() {
    super(AgreementSignatureModel);
  }

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

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

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

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