UNPKG

@tomei/product

Version:

NestJS package for product module

225 lines (216 loc) 7.45 kB
import { ProductJewelleryModel } from '../../entities/product-jewellery.entity'; import { RepositoryBase, IRepositoryBase } from '@tomei/general'; import { ProductBrandModel } from '../../entities/product-brand.entity'; import { SettingsCategoryModel } from '../../entities/settings-category.entity'; import { StoreModel } from '../../entities/store.entity'; import { SettingsGroupModel } from '../../entities/settings-group.entity'; import { SettingsCollectionModel } from '../../entities/settings-collection.entity'; import { ProductCertificateModel } from '../../entities/product-certificate.entity'; import { ProductTagModel } from '../../entities/product-tag.entity'; import { ProductCustomizeOptionModel } from '../../entities/product-customize-option.entity'; import { ProductMaterialModel } from '../../entities/product-material.entity'; import { ProductWithInventoryModel } from '../../entities/product-with-inventory.entity'; import { ProductModel } from '../../entities/product.entity'; export class ProductJewelleryRepository extends RepositoryBase<ProductJewelleryModel> implements IRepositoryBase<ProductJewelleryModel> { constructor() { super(ProductJewelleryModel); } async findAllWithPagination( options: any, ): Promise<{ rows: ProductJewelleryModel[]; count: number }> { const { productQuery, rows = 10, offset = 0, transaction } = options; if (!productQuery) { throw new Error('productQuery is required'); } if (!rows) { throw new Error('rows is required'); } if (offset === undefined || offset === null) { throw new Error('offset is required'); } const queryOptions = { include: [ { model: ProductWithInventoryModel, include: [ { model: ProductModel, where: productQuery.whereProduct && Object.keys(productQuery.whereProduct).length !== 0 ? productQuery.whereProduct : null, include: [ { model: StoreModel, where: productQuery.StoreId ? { StoreId: productQuery.StoreId, } : null, }, { model: SettingsCategoryModel, attributes: ['Code', 'Name', 'Description', 'Status'], where: productQuery.CategoryCode ? { Code: productQuery.CategoryCode, } : null, }, { model: SettingsCategoryModel, attributes: ['Code', 'Name', 'Description', 'Status'], where: productQuery.CategoryCode ? { Code: productQuery.CategoryCode, } : null, }, { model: SettingsCollectionModel, attributes: ['Code', 'Name', 'Description', 'Status'], where: productQuery.CollectionCode ? { Code: productQuery.CollectionCode, } : null, }, { model: SettingsGroupModel, attributes: ['Code', 'Name', 'Description', 'Status'], where: productQuery.GroupCode ? { Code: productQuery.GroupCode, } : null, }, { model: ProductCertificateModel, // separate: true, where: productQuery.CertificateId ? { CertificateId: productQuery.CertificateId, } : null, }, { model: ProductTagModel, // separate: true, where: productQuery.TagId ? { TagId: productQuery.TagId, } : null, }, { model: ProductBrandModel, // separate: true, where: productQuery.Brand ? { Brand: productQuery.Brand, } : null, }, { model: ProductCustomizeOptionModel, // separate: true, where: productQuery.CustomizeOptionId ? { CustomizeOptionId: productQuery.CustomizeOptionId, } : null, }, { model: ProductMaterialModel, // separate: true, where: productQuery.MaterialId ? { MaterialId: productQuery.MaterialId, } : null, }, ], }, ], }, ], transaction, }; try { const result = await ProductJewelleryModel.findAndCountAll(queryOptions); return result; } catch (error) { throw new Error( `An Error occured when fetching product jewelleries: ${error.message}`, ); } } async findAll(transaction: any): Promise<ProductJewelleryModel[]> { const queryOptions = { include: [ { model: ProductWithInventoryModel, include: [ { model: ProductModel, include: [ { model: StoreModel, }, { model: SettingsCategoryModel, attributes: ['Code', 'Name', 'Description', 'Status'], }, { model: SettingsCategoryModel, attributes: ['Code', 'Name', 'Description', 'Status'], }, { model: SettingsCollectionModel, attributes: ['Code', 'Name', 'Description', 'Status'], }, { model: SettingsGroupModel, attributes: ['Code', 'Name', 'Description', 'Status'], }, { model: ProductCertificateModel, // separate: true, }, { model: ProductTagModel, // separate: true, }, { model: ProductBrandModel, // separate: true, }, { model: ProductCustomizeOptionModel, // separate: true, }, { model: ProductMaterialModel, // separate: true, }, ], }, ], }, ], transaction, }; try { const result = await ProductJewelleryModel.findAll(queryOptions); return result; } catch (error) { throw new Error( `An Error occured when fetching product jewellery: ${error.message}`, ); } } }