@tomei/product
Version:
NestJS package for product module
153 lines • 6.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProductProductGroup = void 0;
const product_group_product_repository_1 = require("./product-group-product.repository");
const config_1 = require("@tomei/config");
const settings_group_entity_1 = require("../../entities/settings-group.entity");
const cuid = require("cuid");
const activity_history_1 = require("@tomei/activity-history");
const general_1 = require("@tomei/general");
class ProductProductGroup extends general_1.ObjectBase {
constructor() {
super(...arguments);
this.ObjectType = 'ProductProductGroup';
}
get ProductGroupId() {
return this.ObjectId;
}
set ProductGroupId(value) {
this.ObjectId = value;
}
static async CheckProductAssignToGroup(code, dbTransaction) {
try {
const options = {
where: {
Code: code,
},
};
const productGroup = await this._Repo.findOne(Object.assign(Object.assign({}, options), { transaction: dbTransaction }));
if (productGroup) {
return true;
}
else {
return false;
}
}
catch (error) {
throw error;
}
}
static async listGroupAssignedToProduct(ProductId, loginUser, dbTransaction) {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'Product - List Group');
if (!isPrivileged) {
throw new Error('You do not have permission to list product group.');
}
const options = {
where: {
ProductId: ProductId,
},
include: [
{
model: settings_group_entity_1.SettingsGroupModel,
},
],
transaction: dbTransaction,
};
const productGroup = await this._Repo
.findAndCountAll(options)
.then((groups) => {
const productGroups = [];
groups.rows.forEach((pg) => {
productGroups.push(pg.ProductGroup);
});
return {
count: groups.count,
rows: productGroups,
};
});
return productGroup;
}
catch (error) {
throw error;
}
}
static async assignProductToGroup(Product, ProductGroup, loginUser, dbTransaction) {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'Product - Assign Group');
if (!isPrivileged) {
throw new Error('You do not have permission to assign product group.');
}
const ProductGroupId = cuid();
ProductProductGroup._Repo.create({
ProductGroupId: ProductGroupId,
Code: ProductGroup.Code,
ProductId: Product.ProductId,
}, {
transaction: dbTransaction,
});
const entityValueAfter = {
ProductGroupId: ProductGroupId,
Code: ProductGroup.Code,
ProductId: Product.ProductId,
};
const activity = new activity_history_1.Activity();
activity.ActivityId = cuid();
activity.Action = activity_history_1.ActionEnum.CREATE;
activity.Description = 'Assign Product to Product Group';
activity.EntityType = 'ProductProductGroup';
activity.EntityId = ProductGroupId;
activity.EntityValueBefore = JSON.stringify({});
activity.EntityValueAfter = JSON.stringify(entityValueAfter);
await activity.create(loginUser.ObjectId, dbTransaction);
return {
MessageCode: 'ProductGroupAssigned',
Message: 'Product added to Product Group.',
};
}
catch (error) {
throw error;
}
}
static async unassignedProductFromGroup(Product, ProductGroup, loginUser, dbTransaction) {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'Product - Unassigned Group');
if (!isPrivileged) {
throw new Error('You do not have permission to unassigned product group.');
}
const productGroup = await ProductProductGroup._Repo.findOne({
where: { ProductId: Product.ProductId, Code: ProductGroup.Code },
transaction: dbTransaction,
});
const ProductGroupId = productGroup.ProductGroupId;
ProductProductGroup._Repo.delete(ProductGroupId, dbTransaction);
const EntityValueBefore = {
ProductGroupId: productGroup.ProductGroupId,
Code: productGroup.Code,
ProductId: productGroup.ProductId,
};
const activity = new activity_history_1.Activity();
activity.ActivityId = cuid();
activity.Action = activity_history_1.ActionEnum.DELETE;
activity.Description = 'Unassigned Product from Product Group';
activity.EntityType = 'ProductProductGroup';
activity.EntityId = ProductGroupId;
activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
activity.EntityValueAfter = JSON.stringify({});
await activity.create(loginUser.ObjectId, dbTransaction);
return {
MessageCode: 'ProductGroupUnassigned',
Message: 'Product removed to Product Group.',
};
}
catch (error) {
throw error;
}
}
}
exports.ProductProductGroup = ProductProductGroup;
ProductProductGroup._Repo = new product_group_product_repository_1.ProductProductGroupRepository();
//# sourceMappingURL=product-group-product.js.map