UNPKG

@tomei/product

Version:

NestJS package for product module

164 lines 7.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProductProductCategory = void 0; const product_product_category_repository_1 = require("./product-product-category.repository"); const config_1 = require("@tomei/config"); const settings_category_entity_1 = require("../../entities/settings-category.entity"); const cuid_1 = require("../../helpers/cuid"); const activity_history_1 = require("@tomei/activity-history"); const general_1 = require("@tomei/general"); class ProductProductCategory extends general_1.ObjectBase { constructor() { super(...arguments); this.ObjectType = 'ProductProductCategory'; } static async CheckProductAssignToCategory(Code, dbTransaction) { try { const options = { where: { Code: Code, }, transaction: dbTransaction, }; const result = await ProductProductCategory._Repository.findOne(options); if (result) { return true; } else { return false; } } catch (error) { throw error; } } static async listCategoryAssignedToProduct(ProductId, loginUser, page = 1, rows = 10, dbTransaction) { const limit = rows; const offset = (page - 1) * rows; try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = await loginUser.checkPrivileges(systemCode, 'Product - List Category'); if (!isPrivileged) { throw new Error('You do not have permission to list product category.'); } const options = { where: { ProductId: ProductId, }, include: [ { model: settings_category_entity_1.SettingsCategoryModel, }, ], distinct: true, limit, offset, transaction: dbTransaction, }; const data = await ProductProductCategory._Repository.findAndCountAll(options); const result = { count: data.count, rows: data.rows.map((item) => { return { Code: item.Code, Name: item.Category.Name, Description: item.Category.Description, Status: item.Category.Status, ParentCode: item.Category.ParentCode, CodePath: item.Category.CodePath, CreatedAt: item.Category.CreatedAt, UpdatedAt: item.Category.UpdatedAt, CreatedById: item.Category.CreatedById, UpdatedById: item.Category.UpdatedById, }; }), }; return result; } catch (error) { throw error; } } static async assignProductToCategoryProduct(product, ProductCategory, loginUser, dbTransaction) { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = await loginUser.checkPrivileges(systemCode, 'Product - Assign Category'); if (!isPrivileged) { throw new Error('You do not have permission to assign product category.'); } const ProductCategoryId = (0, cuid_1.default)(); const options = { ProductCategoryId: ProductCategoryId, ProductId: product.ProductId, Code: ProductCategory.Code, }; await ProductProductCategory._Repository.create(options, dbTransaction); const entityValueAfter = { ProductCategoryId: ProductCategoryId, ProductId: product.ProductId, Code: ProductCategory.Code, }; const activity = new activity_history_1.Activity(); activity.ActivityId = (0, cuid_1.default)(); activity.Action = activity_history_1.ActionEnum.CREATE; activity.Description = 'Asssign Product to Category Product'; activity.EntityType = 'ProductProductCategory'; activity.EntityId = ProductCategoryId; activity.EntityValueBefore = JSON.stringify({}); activity.EntityValueAfter = JSON.stringify(entityValueAfter); await activity.create(loginUser.ObjectId, dbTransaction); return { MessageCode: 'ProductCategoryAssigned', Message: 'Product added to Product Category.', }; } catch (error) { throw error; } } static async unassignedProductFromCategory(product, ProductCategory, loginUser, dbTransaction) { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = await loginUser.checkPrivileges(systemCode, 'Product - Unassigned Category'); if (!isPrivileged) { throw new Error('You do not have permission to unassign product category.'); } const options = { where: { ProductId: product.ProductId, Code: ProductCategory.Code, }, transaction: dbTransaction, }; const result = await ProductProductCategory._Repository.findOne(options); if (!result) { throw new Error('Product Category not found.'); } await ProductProductCategory._Repository.delete(result.ProductCategoryId, dbTransaction); const entityValueBefore = { ProductCategoryId: result.ProductCategoryId, ProductId: result.ProductId, Code: result.Code, }; const activity = new activity_history_1.Activity(); activity.ActivityId = (0, cuid_1.default)(); activity.Action = activity_history_1.ActionEnum.DELETE; activity.Description = 'Unassign Product from Category Product'; activity.EntityType = 'ProductProductCategory'; activity.EntityId = result.ProductCategoryId; activity.EntityValueBefore = JSON.stringify(entityValueBefore); activity.EntityValueAfter = JSON.stringify({}); await activity.create(loginUser.ObjectId, dbTransaction); return { MessageCode: 'ProductCategoryUnassigned', Message: 'Product unassigned from Product Category.', }; } catch (error) { throw error; } } } exports.ProductProductCategory = ProductProductCategory; ProductProductCategory._Repository = new product_product_category_repository_1.ProductProductCategoryRepository(); //# sourceMappingURL=product-product-category.js.map