@tomei/product
Version:
NestJS package for product module
298 lines • 11.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProductGroup = void 0;
const status_enum_1 = require("./enum/status.enum");
const group_product_repository_1 = require("./group-product.repository");
const config_1 = require("@tomei/config");
const activity_history_1 = require("@tomei/activity-history");
const cuid_1 = require("../../helpers/cuid");
const product_group_product_1 = require("../product-group-product/product-group-product");
const sequelize_1 = require("sequelize");
const general_1 = require("@tomei/general");
class ProductGroup extends general_1.ObjectBase {
get Status() {
return this._Status;
}
set Status(value) {
this._Status = value;
}
get CreatedById() {
return this._CreatedById;
}
set CreatedById(value) {
this._CreatedById = value;
}
get CreatedAt() {
return this._CreatedAt;
}
set CreatedAt(value) {
this._CreatedAt = value;
}
get UpdatedById() {
return this._UpdatedById;
}
set UpdatedById(value) {
this._UpdatedById = value;
}
get UpdatedAt() {
return this._UpdatedAt;
}
set UpdatedAt(value) {
this._UpdatedAt = value;
}
constructor(productGroup) {
super();
this.ObjectType = 'ProductGroup';
this._Status = status_enum_1.StatusEnum.ACTIVE;
if (productGroup) {
this.Code = productGroup.Code;
this.Name = productGroup.Name;
this.Description = productGroup.Description;
this._Status = productGroup.Status;
this._CreatedById = productGroup.CreatedById;
this._CreatedAt = productGroup.CreatedAt;
this._UpdatedById = productGroup.UpdatedById;
this._UpdatedAt = productGroup.UpdatedAt;
}
}
static async init(options) {
try {
if (options) {
const { code, groupInfo } = options;
if (code) {
const productGroup = await ProductGroup._Repo.findByPk(code);
if (!productGroup) {
throw Error('Product Group not found.');
}
return new ProductGroup(productGroup);
}
else if (!groupInfo) {
throw Error('Group info or code is required.');
}
else {
return new ProductGroup(groupInfo);
}
}
else {
return new ProductGroup();
}
}
catch (err) {
throw Error(err);
}
}
async create(loginUser, dbTransaction) {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'ProductGroup - Create');
if (!isPrivileged) {
throw new Error('You do not have permission to create product group.');
}
this.CreatedById = loginUser.ObjectId;
this.CreatedAt = new Date();
this.UpdatedById = loginUser.ObjectId;
this.UpdatedAt = new Date();
if (!this.Code) {
throw Error('Code is required.');
}
if (!this.Name) {
throw Error('Name is required.');
}
if (!this.Description) {
throw Error('Description is required.');
}
if (!this.Status) {
this.Status = status_enum_1.StatusEnum.ACTIVE;
}
const groupProduct = await ProductGroup._Repo.create({
Code: this.Code,
Name: this.Name,
Description: this.Description,
Status: this.Status,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
});
const entityValueAfter = {
Code: this.Code,
Name: this.Name,
Description: this.Description,
Status: this.Status,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
};
const activity = new activity_history_1.Activity();
activity.ActivityId = (0, cuid_1.default)();
activity.Action = activity_history_1.ActionEnum.CREATE;
activity.Description = 'Create Product Group';
activity.EntityType = 'ProductGroup';
activity.EntityId = this.Code;
activity.EntityValueBefore = JSON.stringify({});
activity.EntityValueAfter = JSON.stringify(entityValueAfter);
await activity.create(loginUser.ObjectId, dbTransaction);
return groupProduct;
}
catch (error) {
throw error;
}
}
async update(loginUser, groupInfo, dbTransaction) {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'ProductGroup - Update');
if (!isPrivileged) {
throw new Error('You do not have permission to update product group.');
}
const EntityValueBefore = {
Code: this.Code,
Name: this.Name,
Description: this.Description,
Status: this.Status,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
};
this.Code = groupInfo.Code;
this.Name = groupInfo.Name;
this.Description = groupInfo.Description;
this.Status = groupInfo.Status;
this.CreatedById = groupInfo.CreatedById;
this.CreatedAt = groupInfo.CreatedAt;
this.UpdatedById = loginUser.ObjectName;
this.UpdatedAt = new Date();
const group = await ProductGroup._Repo.findOne({
where: { Code: this.Code },
transaction: dbTransaction,
});
if (!group) {
throw new Error('Product Group not found.');
}
group.Code = this.Code;
group.Name = this.Name;
group.Description = this.Description;
group.Status = this.Status;
group.CreatedById = this.CreatedById;
group.CreatedAt = this.CreatedAt;
group.UpdatedById = this.UpdatedById;
group.UpdatedAt = this.UpdatedAt;
const updatedInfo = await group.save({
transaction: dbTransaction,
});
const entityValueAfter = groupInfo;
const activity = new activity_history_1.Activity();
activity.ActivityId = (0, cuid_1.default)();
activity.Action = activity_history_1.ActionEnum.UPDATE;
activity.Description = 'Update Product Group';
activity.EntityType = 'ProductGroup';
activity.EntityId = this.Code;
activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
activity.EntityValueAfter = JSON.stringify(entityValueAfter);
await activity.create(loginUser.ObjectId, dbTransaction);
return updatedInfo;
}
catch (error) {
throw error;
}
}
async delete(loginUser, dbTransaction) {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'ProductGroup - Delete');
if (!isPrivileged) {
throw new Error('You do not have permission to delete product group.');
}
const EntityValueBefore = {
Code: this.Code,
Name: this.Name,
Description: this.Description,
Status: this.Status,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
};
const checkProduct = await product_group_product_1.ProductProductGroup.CheckProductAssignToGroup(this.Code, dbTransaction);
if (checkProduct) {
this._Status = status_enum_1.StatusEnum.DEACTIVATED;
this.update(loginUser, {
Code: this.Code,
Name: this.Name,
Description: this.Description,
Status: this.Status,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
});
return;
}
const group = await ProductGroup._Repo.findOne({
where: { Code: this.Code },
transaction: dbTransaction,
});
if (!group) {
throw new Error('Product Group not found.');
}
await ProductGroup._Repo.delete(this.Code, dbTransaction);
const entityValueAfter = {};
const activity = new activity_history_1.Activity();
activity.ActivityId = (0, cuid_1.default)();
activity.Action = activity_history_1.ActionEnum.DELETE;
activity.Description = 'Delete Product Group';
activity.EntityType = 'ProductGroup';
activity.EntityId = this.Code;
activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
activity.EntityValueAfter = JSON.stringify(entityValueAfter);
await activity.create(loginUser.ObjectId, dbTransaction);
return;
}
catch (error) {
throw error;
}
}
static async findAll(loginUser, dbTransaction, page, rows, search = {}) {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'ProductGroup - View');
if (!isPrivileged) {
throw new Error('You do not have permission to view product group.');
}
let result;
if (page && rows) {
const offset = rows * (page - 1);
const options = {
offset,
limit: rows,
order: [['createdAt', 'DESC']],
transaction: dbTransaction,
};
const whereObj = {};
Object.keys(search).forEach((key) => {
if (search[key]) {
whereObj[key] = {
[sequelize_1.Op.substring]: search[key],
};
}
});
if (whereObj) {
options.where = whereObj;
}
result = await ProductGroup._Repo.findAndCountAll(options);
}
else {
result = await ProductGroup._Repo.findAndCountAll();
}
return result;
}
catch (error) {
throw error;
}
}
}
exports.ProductGroup = ProductGroup;
ProductGroup._Repo = new group_product_repository_1.ProductGroupRepository();
//# sourceMappingURL=group-product.js.map