import { RepositoryBase, IRepositoryBase } from '@tomei/general';
import { SettingsCollectionModel } from '../../entities/settings-collection.entity';

export class ProductCollectionRepository
  extends RepositoryBase<SettingsCollectionModel>
  implements IRepositoryBase<SettingsCollectionModel>
{
  constructor() {
    super(SettingsCollectionModel);
  }

  async findByPk(code: string): Promise<SettingsCollectionModel> {
    try {
      const options = {
        where: {
          Code: code,
        },
      };
      const result = await SettingsCollectionModel.findOne(options);
      return result;
    } catch (error) {
      throw new Error(
        `An Error occured when fetching product collection: ${error.message}`,
      );
    }
  }

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

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