@tomei/product
Version:
NestJS package for product module
343 lines • 14.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProductCategory = void 0;
const general_1 = require("@tomei/general");
const status_enum_1 = require("../../enum/status.enum");
const category_product_repository_1 = require("./category-product.repository");
const config_1 = require("@tomei/config");
const activity_history_1 = require("@tomei/activity-history");
const cuid_1 = require("../../helpers/cuid");
const sequelize_1 = require("sequelize");
const product_product_category_1 = require("../product-product-category/product-product-category");
const platform_category_mapping_product_1 = require("../platform-category-mapping-product/platform-category-mapping-product");
class ProductCategory extends general_1.TreeNodeBase {
get Status() {
return this._Status;
}
get CreatedById() {
return this._CreatedById;
}
get CreatedAt() {
return this._CreatedAt;
}
get UpdatedById() {
return this._UpdatedById;
}
get UpdatedAt() {
return this._UpdatedAt;
}
constructor(productCategory) {
super();
this.ObjectType = 'ProductCategory';
this._Status = status_enum_1.StatusEnum.ACTIVE;
if (productCategory) {
this.Code = productCategory.Code;
this.CodePath = productCategory.CodePath;
this.ParentCode = productCategory.ParentCode;
this.Name = productCategory.Name;
this.Description = productCategory.Description;
this._Status = productCategory.Status;
this._CreatedById = productCategory.CreatedById;
this._CreatedAt = productCategory.CreatedAt;
this._UpdatedById = productCategory.UpdatedById;
this._UpdatedAt = productCategory.UpdatedAt;
}
}
static async init(code) {
try {
if (code) {
const productCategory = await ProductCategory._Repository.findByPk(code);
if (!productCategory) {
throw Error('Product Category not found.');
}
const data = {
Code: productCategory.Code,
CodePath: productCategory.CodePath,
ParentCode: productCategory.ParentCode,
Name: productCategory.Name,
Description: productCategory.Description,
Status: productCategory.Status,
CreatedById: productCategory.CreatedById,
CreatedAt: productCategory.CreatedAt,
UpdatedById: productCategory.UpdatedById,
UpdatedAt: productCategory.UpdatedAt,
};
return new ProductCategory(data);
}
return new ProductCategory();
}
catch (err) {
throw Error('Failed to initiate Product Category.');
}
}
async loadChildren() {
if (!this.Code) {
throw Error('Code is missing.');
}
const children = await ProductCategory._Repository.findAll({
where: { ParentCode: this.Code },
order: [['CreatedAt', 'ASC']],
});
this.children = children.map((child) => {
return new ProductCategory(child);
});
this.isChildrenLoaded = true;
}
async loadParent() {
if (!this.Code) {
throw Error('Code is missing.');
}
if (this.ParentCode) {
if (this.ParentCode !== this.Code) {
const parent = await ProductCategory._Repository.findByPk(this.ParentCode);
this.parent = new ProductCategory(parent);
}
}
this.isParentLoaded = true;
}
async create(loginUser, dbTransaction) {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'ProductCategory - Create');
if (!isPrivileged) {
throw new Error('You do not have permission to create product category.');
}
if (!this.Code || !this.Name || !this.Description) {
throw new Error('Code, Name, and Description are required.');
}
this._CreatedById = loginUser.ObjectId;
this._CreatedAt = new Date();
this._UpdatedById = loginUser.ObjectId;
this._UpdatedAt = new Date();
const createdData = await ProductCategory._Repository.create({
Code: this.Code,
Name: this.Name,
Description: this.Description,
CodePath: this.CodePath,
ParentCode: this.ParentCode,
Status: this.Status,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
}, {
transaction: dbTransaction,
});
const activity = new activity_history_1.Activity();
activity.ActivityId = (0, cuid_1.default)();
activity.Action = activity_history_1.ActionEnum.CREATE;
activity.Description = 'Create Product Category';
activity.EntityType = 'ProductCategory';
activity.EntityId = this.Code;
activity.EntityValueBefore = JSON.stringify({});
activity.EntityValueAfter = JSON.stringify(createdData.get({ plain: true }));
await activity.create(loginUser.ObjectId, dbTransaction);
}
catch (error) {
throw error;
}
}
async update(payload, loginUser, dbTransaction) {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'ProductCategory - Update');
if (!isPrivileged) {
throw new Error('You do not have permission to create product category.');
}
const entityValueBefore = {
Code: this.Code,
Name: this.Name,
Description: this.Description,
CodePath: this.CodePath,
ParentCode: this.ParentCode,
Status: this.Status,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
};
if (payload.Description) {
this.Description = payload.Description;
}
if (payload.ParentCode) {
this.ParentCode = payload.ParentCode;
}
if (payload.Status) {
this._Status = payload.Status;
}
if (payload.Code) {
this.Code = payload.Code;
if (this.ParentCode === this.Code) {
this.ParentCode = payload.Code;
}
}
if (payload.Name) {
this.Name = payload.Name;
this.CodePath = await this.getPath();
await this.checkAndUpdateChildrenCodePath(payload.Name, this.Name, dbTransaction);
}
this._UpdatedById = loginUser.ObjectId;
this._UpdatedAt = new Date();
const entityValueAfter = {
Code: this.Code,
Name: this.Name,
Description: this.Description,
CodePath: this.CodePath,
ParentCode: this.ParentCode,
Status: this.Status,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
};
ProductCategory._Repository.update(Object.assign({}, entityValueAfter), {
where: {
Code: entityValueBefore.Code,
},
transaction: dbTransaction,
});
const activity = new activity_history_1.Activity();
activity.ActivityId = (0, cuid_1.default)();
activity.Action = activity_history_1.ActionEnum.UPDATE;
activity.Description = 'Update Product Category';
activity.EntityType = 'ProductCategory';
activity.EntityId = this.Code;
activity.EntityValueBefore = JSON.stringify(entityValueBefore);
activity.EntityValueAfter = JSON.stringify(entityValueAfter);
await activity.create(loginUser.ObjectId, dbTransaction);
return;
}
async checkAndUpdateChildrenCodePath(oldName, newName, dbTransaction) {
const isLeaf = await this.isLeaf();
if (isLeaf) {
return;
}
const childrens = await ProductCategory._Repository.findAll({
where: {
CodePath: {
[sequelize_1.Op.like]: `${oldName} > %`,
},
},
transaction: dbTransaction,
});
childrens.forEach(async (children) => {
const newCodePath = children.CodePath.replace(oldName, newName);
await children.update({ CodePath: newCodePath }, { transaction: dbTransaction });
});
}
static async findAll(loginUser, dbTransaction, page, rows, search = {}) {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'ProductCategory - View');
if (!isPrivileged) {
throw new Error('You do not have permission to view collection group.');
}
const whereObj = {};
Object.keys(search).forEach((key) => {
if (search[key]) {
whereObj[key] = {
[sequelize_1.Op.substring]: search[key],
};
}
});
let options = {
where: whereObj,
order: [['createdAt', 'DESC']],
transaction: dbTransaction,
};
if (page && rows) {
const offset = rows * (page - 1);
options = Object.assign(Object.assign({}, options), { offset, limit: rows });
}
const result = await ProductCategory._Repository.findAndCountAll(options);
return result;
}
catch (error) {
throw error;
}
}
async delete(loginUser, dbTransaction) {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'ProductCategory - Delete');
if (!isPrivileged) {
throw new Error('You do not have permission to delete product category.');
}
const entityValueBefore = {
Code: this.Code,
Name: this.Name,
Description: this.Description,
CodePath: this.CodePath,
ParentCode: this.ParentCode,
Status: this.Status,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
};
const isProductAssigned = await product_product_category_1.ProductProductCategory.CheckProductAssignToCategory(this.Code, dbTransaction);
await this.loadChildren();
const isGotSubCategory = this.children.length > 0;
if (isProductAssigned || isGotSubCategory) {
this._Status = status_enum_1.StatusEnum.DEACTIVATED;
const childrens = await ProductCategory._Repository.findAll({
where: {
CodePath: {
[sequelize_1.Op.like]: `${this.Name}%`,
},
},
transaction: dbTransaction,
});
childrens.forEach(async (children) => {
const productCategory = await ProductCategory.init(children.Code);
await productCategory.update({
Status: status_enum_1.StatusEnum.DEACTIVATED,
}, loginUser, dbTransaction);
});
await ProductCategory._Repository.update({
Status: status_enum_1.StatusEnum.DEACTIVATED,
}, {
where: {
Code: this.Code,
},
transaction: dbTransaction,
});
return;
}
else {
await ProductCategory._Repository.delete(this.Code, dbTransaction);
const activity = new activity_history_1.Activity();
activity.ActivityId = (0, cuid_1.default)();
activity.Action = activity_history_1.ActionEnum.DELETE;
activity.Description = 'Delete Product Category';
activity.EntityType = 'ProductCategory';
activity.EntityId = this.Code;
activity.EntityValueBefore = JSON.stringify(entityValueBefore);
activity.EntityValueAfter = JSON.stringify({});
await activity.create(loginUser.ObjectId, dbTransaction);
return;
}
}
async assignPlatfromMapping(loginUser, dbTransaction, mappingInfo) {
try {
const pcm = await new platform_category_mapping_product_1.PlatformCategoryMapping();
pcm.Code = this.Code;
pcm.Platform = mappingInfo.platform;
pcm.PlatformCategoryId = mappingInfo.platformCategoryId;
pcm.PlatformCategoryName = mappingInfo.platformCategoryName;
return pcm.create(loginUser, dbTransaction);
}
catch (error) {
throw new Error(`An Error occured when retriving product collection: ${error.message}`);
}
}
async getPlatformMapping(loginUser, dbTransaction, page, row) {
try {
return platform_category_mapping_product_1.PlatformCategoryMapping.findAll(loginUser, dbTransaction, this.Code, page, row);
}
catch (error) {
throw new Error(`An Error occured when retriving product collection: ${error.message}`);
}
}
}
exports.ProductCategory = ProductCategory;
ProductCategory._Repository = new category_product_repository_1.ProductCategoryRepository();
//# sourceMappingURL=category-product.js.map