UNPKG

@tomei/product

Version:

NestJS package for product module

128 lines 5.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProductBrand = void 0; const cuid = require("cuid"); const brand_product_repository_1 = require("./brand-product.repository"); const config_1 = require("@tomei/config"); const general_1 = require("@tomei/general"); const activity_history_1 = require("@tomei/activity-history"); class ProductBrand extends general_1.ObjectBase { get Brand() { return this.ObjectId; } set Brand(value) { this.ObjectId = value; } constructor(brandAttr) { super(); this.ObjectType = 'ProductBrand'; if (brandAttr) { this.Brand = brandAttr.Brand; this.ProductId = brandAttr.ProductId; } } static async init(dbTransaction, brand) { if (brand) { const data = await ProductBrand._Repo.findByPk(brand, dbTransaction); if (data) { return new ProductBrand({ ProductId: data.ProductId, Brand: data.Brand, }); } else { throw new general_1.ClassError('ProductBrand', 'ProductBrandErrMsg01', 'ProductBrand not found.'); } } return new ProductBrand(dbTransaction); } static async findAll(ProductId, loginUser, dbTransaction) { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = await loginUser.checkPrivileges(systemCode, 'ProductBrand - List'); if (!isPrivileged) { throw new Error('You do not have permission to list product brand.'); } const options = { where: { ProductId: ProductId, }, transaction: dbTransaction, }; const productBrand = await this._Repo.findAndCountAll(options); return productBrand; } catch (error) { throw error; } } async create(loginUser, dbTransaction) { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = await loginUser.checkPrivileges(systemCode, 'ProductBrand - Create'); if (!isPrivileged) { throw new Error('You do not have permission to Create product tag.'); } if (!this.ProductId) { throw new general_1.ClassError('ProductBrand', 'ProductBrandErrMsg02', 'ProductId is missing.'); } if (!this.Brand) { throw new general_1.ClassError('ProductTag', 'ProductTagErrMsg03', 'Brand is missing.'); } const brand = await ProductBrand._Repo.create({ Brand: this.Brand, ProductId: this.ProductId, }); const entityValueAfter = { Brand: this.Brand, ProductId: this.ProductId, }; const activity = new activity_history_1.Activity(); activity.ActivityId = cuid(); activity.Action = activity_history_1.ActionEnum.CREATE; activity.Description = 'Add Product Brand'; activity.EntityType = 'ProductBrand'; activity.EntityId = this.Brand; activity.EntityValueBefore = JSON.stringify({}); activity.EntityValueAfter = JSON.stringify(entityValueAfter); await activity.create(loginUser.ObjectId, dbTransaction); return brand; } catch (error) { throw error; } } async delete(loginUser, dbTransaction) { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = await loginUser.checkPrivileges(systemCode, 'ProductBrand - Delete'); if (!isPrivileged) { throw new Error('You do not have permission to Delete product brand.'); } const entityValueBefore = { Brand: this.Brand, ProductId: this.ProductId, }; await ProductBrand._Repo.delete(this.Brand, dbTransaction); const activity = new activity_history_1.Activity(); activity.ActivityId = cuid(); activity.Action = activity_history_1.ActionEnum.DELETE; activity.Description = 'Delete Product Brand'; activity.EntityType = 'ProductBrand'; activity.EntityId = this.Brand; activity.EntityValueBefore = JSON.stringify(entityValueBefore); activity.EntityValueAfter = JSON.stringify({}); await activity.create(loginUser.ObjectId, dbTransaction); return { MessageCode: 'ProductBrandDeleted', Message: 'Product brand is deleted.', }; } catch (error) { throw error; } } } exports.ProductBrand = ProductBrand; ProductBrand._Repo = new brand_product_repository_1.ProductBrandRepository(); //# sourceMappingURL=brand-product.js.map