UNPKG

@tomei/product

Version:

NestJS package for product module

247 lines 9.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProductCollection = void 0; const status_enum_1 = require("../../enum/status.enum"); const collection_product_repository_1 = require("./collection-product.repository"); const config_1 = require("@tomei/config"); const activity_history_1 = require("@tomei/activity-history"); const cuid = require("cuid"); const sequelize_1 = require("sequelize"); const general_1 = require("@tomei/general"); class ProductCollection extends general_1.ObjectBase { get Status() { return this._Status; } set Status(value) { this._Status = value; } get Code() { return this.ObjectId; } set Code(value) { this.ObjectId = 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(productCollection) { super(); this.ObjectType = 'ProductCollection'; this._Status = status_enum_1.StatusEnum.ACTIVE; if (productCollection) { this.Code = productCollection.Code; this.Name = productCollection.Name; this.Description = productCollection.Description; this._Status = productCollection.Status; this._CreatedById = productCollection.CreatedById; this._CreatedAt = productCollection.CreatedAt; this._UpdatedById = productCollection.UpdatedById; this._UpdatedAt = productCollection.UpdatedAt; } } static async init(options) { try { if (options) { const { code, collectionInfo } = options; if (code) { const productCollection = await ProductCollection._Repo.findByPk(code); if (!productCollection) { throw Error('Product Collection not found.'); } return new ProductCollection(productCollection); } else if (!collectionInfo) { throw Error('Group Collection or code is required.'); } else { return new ProductCollection(collectionInfo); } } else { return new ProductCollection(); } } catch (err) { throw Error(err); } } static async findAll(loginUser, dbTransaction, page, rows, search = {}) { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = await loginUser.checkPrivileges(systemCode, 'ProductCollection - View'); if (!isPrivileged) { throw new Error('You do not have permission to view collection 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 ProductCollection._Repo.findAndCountAll(options); } else { result = await ProductCollection._Repo.findAndCountAll(); } return result; } catch (error) { throw error; } } async create(loginUser, dbTransaction) { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = await loginUser.checkPrivileges(systemCode, 'ProductCollection - Create'); if (!isPrivileged) { throw new Error('You do not have permission to create product collection.'); } 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 collectionProduct = await ProductCollection._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 = cuid(); activity.Action = activity_history_1.ActionEnum.CREATE; activity.Description = 'Create Product Collection'; activity.EntityType = 'ProductCollection'; activity.EntityId = this.Code; activity.EntityValueBefore = JSON.stringify({}); activity.EntityValueAfter = JSON.stringify(entityValueAfter); await activity.create(loginUser.ObjectId, dbTransaction); return collectionProduct; } catch (error) { throw error; } } async update(loginUser, collectionInfo, dbTransaction) { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = await loginUser.checkPrivileges(systemCode, 'ProductCollection - Update'); if (!isPrivileged) { throw new Error('You do not have permission to update product collection.'); } 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 = collectionInfo.Code; this.Name = collectionInfo.Name; this.Description = collectionInfo.Description; this.Status = collectionInfo.Status; this.CreatedById = collectionInfo.CreatedById; this.CreatedAt = collectionInfo.CreatedAt; this.UpdatedById = loginUser.ObjectName; this.UpdatedAt = new Date(); const collection = await ProductCollection._Repo.findOne({ where: { Code: this.Code }, transaction: dbTransaction, }); if (!collection) { throw new Error('Product Collection not found.'); } collection.Code = this.Code; collection.Name = this.Name; collection.Description = this.Description; collection.Status = this.Status; collection.CreatedById = this.CreatedById; collection.CreatedAt = this.CreatedAt; collection.UpdatedById = this.UpdatedById; collection.UpdatedAt = this.UpdatedAt; const updatedInfo = await collection.save({ transaction: dbTransaction, }); const entityValueAfter = collectionInfo; const activity = new activity_history_1.Activity(); activity.ActivityId = cuid(); activity.Action = activity_history_1.ActionEnum.UPDATE; activity.Description = 'Update Product Collection'; activity.EntityType = 'ProductCollection'; 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; } } } exports.ProductCollection = ProductCollection; ProductCollection._Repo = new collection_product_repository_1.ProductCollectionRepository(); //# sourceMappingURL=collection-product.js.map