import {ProductInterface} from '../model/interfaces/Interfaces'; import {Attributes, Categorization} from '../model/interfaces/Interfaces'; import {CategorizationCriteriaInstance, VendorCategoriesInterface, AttrCategoriesInterface} from '../model/models'; import {CategorizationCriteriaRepository} from '../repositories/CategorizationCriteriaRepository'; import {CategoriesRepository, CategoryTreeInterface} from '../repositories/CategoriesRepository'; import {VendorCategoriesRepository} from '../repositories/VendorCategoriesRepository'; import {CategorizationCriteriaEnum} from '../model/interfaces/CategorizationCriteriaEnum'; import {VendorRepository} from '../repositories/VendorRepository'; import {AttrCategoriesRepository} from '../repositories/AttrCategoriesRepository'; import {StoreRepository} from '../repositories/StoreRepository'; export class CategorizationController { private _categorizationCriteriaRepository: CategorizationCriteriaRepository = new CategorizationCriteriaRepository; private _vendorCategoryRepository: VendorCategoriesRepository = new VendorCategoriesRepository; private _categoryRepository: CategoriesRepository = new CategoriesRepository; private _vendorRepository: VendorRepository = new VendorRepository; private _attrCategoriesRepository: AttrCategoriesRepository = new AttrCategoriesRepository; private _storeRepository: StoreRepository = new StoreRepository; /** * Function to do the classification of a product * @param productInformation * @param vendorSlug * @param storeCode * @return Promise */ public async doCategorization(productInformation: ProductInterface, vendorSlug: string, storeCode: string): Promise { let response: Categorization = { category: ['Other'], attributes: {}, tax_category: 'Other' }; const categorizationCriteria: CategorizationCriteriaInstance[] = await this._categorizationCriteriaRepository.findAll(); const storeObject = await this._storeRepository.findOneByCode(storeCode); if(storeObject === null){ throw new Error('Store can`t be null'); } if (productInformation.sku === null || productInformation.category === null || productInformation.name === null) { throw new Error('Product information must be complete'); } if (vendorSlug == null) { throw new Error('Vendor slug can`t be null'); } const vendorObj = await this._vendorRepository.findAllBySlug(vendorSlug); if (vendorObj === null) { throw new Error('Vendor does not exist in our database'); } let categoryTree: CategoryTreeInterface | null = null; // extract the categorizations criteria for (const item of categorizationCriteria) { const vendorCategories = await this._vendorCategoryRepository.findAllByCategorization(item.id, vendorObj?.id) ?? []; const productAttrCriteria: Record = CategorizationController._getProductDataByCriteria(productInformation, item); // for every categorization criteria do something for (const vendorCategory of vendorCategories) { categoryTree = await this._bind(productAttrCriteria, vendorCategory); if (categoryTree !== null) { response.category = categoryTree.tree; break; } } // if we have a product category we stop to iterate over the rest of categorization criteria // because the categorizations criteria are ordered by priority if (categoryTree !== null) { break; } } // find the attr by category if (categoryTree !== null) { response.attributes = await this._getAttrCategory(categoryTree, storeObject?.id); } //get the tax tree by the last value of tmp.ids return response; } /** * Function to get the product attr by the classification criteria to get classify * @param productInformation * @param categorizationCriteria * @return Record * @private */ private static _getProductDataByCriteria(productInformation: ProductInterface, categorizationCriteria: CategorizationCriteriaInstance): Record { let result: Record = {}; switch (categorizationCriteria.criteria) { case CategorizationCriteriaEnum.sku.toString(): result = {'attr': productInformation.sku, 'criteria': categorizationCriteria.criteria}; break; case CategorizationCriteriaEnum.keyword.toString(): // eslint-disable-next-line no-case-declarations const tmp: string = productInformation.name + productInformation.shortDescription + productInformation.largeDescription; result = {'attr': tmp, 'criteria': categorizationCriteria.criteria}; break; case CategorizationCriteriaEnum.breadcrumb.toString(): result = {'attr': productInformation.category, 'criteria': categorizationCriteria.criteria}; break; } return result; } /** * Function to bind the product data with the store data for classify * @param productAttrCriteria * @param vendorCategory * @return Promise * @private */ private async _bind(productAttrCriteria: Record, vendorCategory: VendorCategoriesInterface): Promise { let bound = false; switch (productAttrCriteria.criteria) { case CategorizationCriteriaEnum.sku:// bind by sku case CategorizationCriteriaEnum.breadcrumb: //bind by breadcrumb bound = productAttrCriteria.attr == vendorCategory.name; break; case CategorizationCriteriaEnum.keyword: //bind by keyword bound = productAttrCriteria.attr.indexOf(vendorCategory.name) !== -1; break; } return bound ? await this._categoryRepository.findParentTree(vendorCategory.category_id) : null; } /** * Function to get the category attribute, this function review all de category tree attributes and makes a single * category attributes set to send to Magento, checking for all the parent's attributes. * @param categoryTree * @param storeId * @private */ private async _getAttrCategory(categoryTree: CategoryTreeInterface, storeId: number): Promise { let allAttributes: Attributes[] = []; for (let i = 0; i < categoryTree.ids.length; i++) { const categoryAttrTmp: AttrCategoriesInterface[] = await this._attrCategoriesRepository.findByCategory(categoryTree.ids[i], storeId); // console.log('categoryAttrTmp', categoryAttrTmp); if (categoryAttrTmp.length >= 1) { // if not exist in the allAttributes add in the final array if (!this._alreadyExistAttr(categoryAttrTmp, allAttributes)) { categoryAttrTmp.forEach((item: AttrCategoriesInterface) => { allAttributes.push({ name: item.name, value: item.value }); }); } else { // if the attibute already exist override it with the new one because the new one represents an // attribute of a children allAttributes.forEach((item: Attributes, index: number) => { if (item.name === categoryAttrTmp[0].name) { allAttributes[index]['value'] = categoryAttrTmp[0].value; } }); } } } return allAttributes; } /** * Function to check if the attribute already exist on the category attributes list * @param categoryAttrTmp * @param allAttributes * @return boolean * @private */ private _alreadyExistAttr(categoryAttrTmp: AttrCategoriesInterface[], allAttributes: Attributes[]): boolean { const tmp = categoryAttrTmp[0]; const exist = allAttributes.filter((attr: Attributes) => { return tmp?.name === attr.name; }); return exist.length >= 1; } }