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

export class ProductCategoryRepository
  extends RepositoryBase<SettingsCategoryModel>
  implements IRepositoryBase<SettingsCategoryModel>
{
  constructor() {
    super(SettingsCategoryModel);
  }

  async findByPk(code: string): Promise<SettingsCategoryModel> {
    try {
      const options = {
        where: {
          Code: code,
        },
      };
      const result = await SettingsCategoryModel.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 SettingsCategoryModel.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 SettingsCategoryModel.findAndCountAll(options);
      } else {
        collection = await SettingsCategoryModel.findAndCountAll();
      }
      return collection;
    } catch (error) {
      throw new Error(
        `An Error occured when retriving product collection: ${error.message}`,
      );
    }
  }
}
