@tomei/product
Version:
NestJS package for product module
348 lines • 14.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProductVariantWithInventory = void 0;
const product_variant_entity_1 = require("../../entities/product-variant.entity");
const variant_product_1 = require("../variant-product/variant-product");
const variant_product_with_inventory_repository_1 = require("./variant-product-with-inventory.repository");
const general_1 = require("@tomei/general");
const activity_history_1 = require("@tomei/activity-history");
const cuid = require("cuid");
class ProductVariantWithInventory extends variant_product_1.ProductVariant {
get TotalUnitsAvailable() {
return this._TotalUnitsAvailable;
}
get TotalUnitsInCurrentOrder() {
return this._TotalUnitsInCurrentOrder;
}
get TotalUnitsSold() {
return this._TotalUnitsSold;
}
get TotalUnitsReserved() {
return this._TotalUnitsReserved;
}
get TotalUnitsInTransit() {
return this._TotalUnitsInTransit;
}
get TotalUnitsOnConsignment() {
return this._TotalUnitsOnConsignment;
}
get TotalUnitsBackOrdered() {
return this._TotalUnitsBackOrdered;
}
get TotalUnitsPreOrdered() {
return this._TotalUnitsPreOrdered;
}
get CreatedAt() {
return super.CreatedAt;
}
get UpdatedAt() {
return super.UpdatedAt;
}
get CreatedById() {
return super.CreatedById;
}
get UpdatedById() {
return super.UpdatedById;
}
get UpdatedSSYN() {
return super.UpdatedSSYN;
}
get SKU() {
return super.SKU;
}
get Size() {
return super.Size;
}
get Colour() {
return super.Colour;
}
get MinWeight() {
return super.MinWeight;
}
get MaxWeight() {
return super.MaxWeight;
}
get MinWidth() {
return super.MinWidth;
}
get MaxWidth() {
return super.MaxWidth;
}
get MinHeight() {
return super.MinHeight;
}
get MaxHeight() {
return super.MaxHeight;
}
get MinLength() {
return super.MinLength;
}
get MaxLength() {
return super.MaxLength;
}
get ParentId() {
return super.ParentId;
}
get Status() {
return super.Status;
}
get VariantId() {
return super.VariantId;
}
set VariantId(variantId) {
super.VariantId = variantId;
}
get ProductId() {
return super.ProductId;
}
set ProductId(productId) {
super.ProductId = productId;
}
get Name() {
return super.Name;
}
set Name(name) {
super.Name = name;
}
get Description() {
return super.Description;
}
set Description(description) {
super.Description = description;
}
get Type() {
return super.Type;
}
set Type(type) {
super.Type = type;
}
get Level() {
return super.Level;
}
set Level(level) {
super.Level = level;
}
setSKU(SKU) {
return super.setSKU(SKU);
}
setSize(size) {
return super.setSize(size);
}
setColour(colour) {
return super.setColour(colour);
}
constructor(variant) {
super(variant);
this.ObjectType = 'ProductVariantWithInventory';
this._TotalUnitsAvailable = 0;
this._TotalUnitsInCurrentOrder = 0;
this._TotalUnitsSold = 0;
this._TotalUnitsReserved = 0;
this._TotalUnitsInTransit = 0;
this._TotalUnitsOnConsignment = 0;
this._TotalUnitsBackOrdered = 0;
this._TotalUnitsPreOrdered = 0;
this.StockLowAlertLevel = 0;
this.StockReorderLevel = 0;
this.BufferStockLevel = 0;
if (variant) {
this._TotalUnitsAvailable = variant.TotalUnitsAvailable;
this._TotalUnitsInCurrentOrder = variant.TotalUnitsInCurrentOrder;
this._TotalUnitsSold = variant.TotalUnitsSold;
this._TotalUnitsReserved = variant.TotalUnitsReserved;
this._TotalUnitsInTransit = variant.TotalUnitsInTransit;
this._TotalUnitsOnConsignment = variant.TotalUnitsOnConsignment;
this._TotalUnitsBackOrdered = variant.TotalUnitsBackOrdered;
this._TotalUnitsPreOrdered = variant.TotalUnitsPreOrdered;
this.StockLowAlertLevel = variant.StockLowAlertLevel;
this.StockReorderLevel = variant.StockReorderLevel;
this.BufferStockLevel = variant.BufferStockLevel;
this.VariantId = variant.VariantId;
this.ProductId = variant.ProductId;
this.Name = variant.Name;
this.Description = variant.Description;
this.Type = variant.Type;
this.Level = variant.Level;
}
}
static async init(dbTransaction, variantId) {
if (variantId) {
try {
const variant = await this._Repository.findOne({
where: { VariantId: variantId },
include: [
{
model: product_variant_entity_1.ProductVariantModel,
},
],
transaction: dbTransaction,
});
const data = {
VariantId: variant.ProductVariant.VariantId,
ProductId: variant.ProductVariant.ProductId,
Name: variant.ProductVariant.Name,
Description: variant.ProductVariant.Description,
SKU: variant.ProductVariant.SKU,
Size: variant.ProductVariant.Size,
Colour: variant.ProductVariant.Colour,
Type: variant.ProductVariant.Type,
Level: variant.ProductVariant.Level,
ParentId: variant.ProductVariant.ParentId,
MinWeight: variant.ProductVariant.MinWeight,
MaxWeight: variant.ProductVariant.MaxWeight,
MinWidth: variant.ProductVariant.MinWidth,
MaxWidth: variant.ProductVariant.MaxWidth,
MinHeight: variant.ProductVariant.MinHeight,
MaxHeight: variant.ProductVariant.MaxHeight,
MinLength: variant.ProductVariant.MinLength,
MaxLength: variant.ProductVariant.MaxLength,
Status: variant.ProductVariant.Status,
CreatedById: variant.ProductVariant.CreatedById,
CreatedAt: variant.ProductVariant.CreatedAt,
UpdatedById: variant.ProductVariant.UpdatedById,
UpdatedAt: variant.ProductVariant.UpdatedAt,
UpdatedSSYN: variant.ProductVariant.UpdatedSSYN,
TotalUnitsAvailable: variant.TotalUnitsAvailable,
TotalUnitsInCurrentOrder: variant.TotalUnitsInCurrentOrder,
TotalUnitsSold: variant.TotalUnitsSold,
TotalUnitsReserved: variant.TotalUnitsReserved,
TotalUnitsInTransit: variant.TotalUnitsInTransit,
TotalUnitsOnConsignment: variant.TotalUnitsOnConsignment,
TotalUnitsBackOrdered: variant.TotalUnitsBackOrdered,
TotalUnitsPreOrdered: variant.TotalUnitsPreOrdered,
StockLowAlertLevel: variant.StockLowAlertLevel,
StockReorderLevel: variant.StockReorderLevel,
BufferStockLevel: variant.BufferStockLevel,
};
return new ProductVariantWithInventory(data);
}
catch (err) {
throw new general_1.ClassError('ProductVariantWithInventory', 'ProductVariantWithInventoryErrMsg01', 'VariantId not found.');
}
}
return new ProductVariantWithInventory();
}
async create(loginUser, dbTransaction) {
try {
await super.create(loginUser, dbTransaction);
const data = {
VariantId: this.VariantId,
TotalUnitsAvailable: this.TotalUnitsAvailable,
TotalUnitsInCurrentOrder: this.TotalUnitsInCurrentOrder,
TotalUnitsSold: this.TotalUnitsSold,
TotalUnitsReserved: this.TotalUnitsReserved,
TotalUnitsInTransit: this.TotalUnitsInTransit,
TotalUnitsOnConsignment: this.TotalUnitsOnConsignment,
TotalUnitsBackOrdered: this.TotalUnitsBackOrdered,
TotalUnitsPreOrdered: this.TotalUnitsPreOrdered,
StockLowAlertLevel: this.StockLowAlertLevel,
StockReorderLevel: this.StockReorderLevel,
BufferStockLevel: this.BufferStockLevel,
};
await ProductVariantWithInventory._Repository.create(data, {
transaction: dbTransaction,
});
const activity = new activity_history_1.Activity();
activity.ActivityId = cuid();
activity.Action = activity_history_1.ActionEnum.CREATE;
activity.Description = 'Add new product variant with inventory';
activity.EntityType = 'ProductVariantWithInventory';
activity.EntityId = data.VariantId;
activity.EntityValueBefore = JSON.stringify({});
activity.EntityValueAfter = JSON.stringify(data);
await activity.create(loginUser.ObjectId, dbTransaction);
return this;
}
catch (error) {
throw error;
}
}
async update(payload, loginUser, dbTransaction) {
await super.update(payload, loginUser, dbTransaction);
const entityValueBefore = {
TotalUnitsAvailable: this.TotalUnitsAvailable,
TotalUnitsInCurrentOrder: this.TotalUnitsInCurrentOrder,
TotalUnitsSold: this.TotalUnitsSold,
TotalUnitsReserved: this.TotalUnitsReserved,
TotalUnitsInTransit: this.TotalUnitsInTransit,
TotalUnitsOnConsignment: this.TotalUnitsOnConsignment,
TotalUnitsBackOrdered: this.TotalUnitsBackOrdered,
TotalUnitsPreOrdered: this.TotalUnitsPreOrdered,
StockLowAlertLevel: this.StockLowAlertLevel,
StockReorderLevel: this.StockReorderLevel,
BufferStockLevel: this.BufferStockLevel,
};
this.StockLowAlertLevel = payload.StockLowAlertLevel;
this.StockReorderLevel = payload.StockReorderLevel;
this.BufferStockLevel = payload.BufferStockLevel;
await ProductVariantWithInventory._Repository.update({
StockLowAlertLevel: this.StockLowAlertLevel,
StockReorderLevel: this.StockReorderLevel,
BufferStockLevel: this.BufferStockLevel,
}, {
where: { VariantId: this.VariantId },
transaction: dbTransaction,
});
const entityValueAfter = {
TotalUnitsAvailable: this.TotalUnitsAvailable,
TotalUnitsInCurrentOrder: this.TotalUnitsInCurrentOrder,
TotalUnitsSold: this.TotalUnitsSold,
TotalUnitsReserved: this.TotalUnitsReserved,
TotalUnitsInTransit: this.TotalUnitsInTransit,
TotalUnitsOnConsignment: this.TotalUnitsOnConsignment,
TotalUnitsBackOrdered: this.TotalUnitsBackOrdered,
TotalUnitsPreOrdered: this.TotalUnitsPreOrdered,
StockLowAlertLevel: this.StockLowAlertLevel,
StockReorderLevel: this.StockReorderLevel,
BufferStockLevel: this.BufferStockLevel,
};
const activity = new activity_history_1.Activity();
activity.ActivityId = cuid();
activity.Action = activity_history_1.ActionEnum.UPDATE;
activity.Description = 'Update product variant with inventory';
activity.EntityType = 'ProductVariantWithInventory';
activity.EntityId = this.VariantId;
activity.EntityValueBefore = JSON.stringify(entityValueBefore);
activity.EntityValueAfter = JSON.stringify(entityValueAfter);
await activity.create(loginUser.ObjectId, dbTransaction);
return this;
}
async delete(loginUser, dbTransaction) {
try {
const entityValueBefore = {
TotalUnitsAvailable: this.TotalUnitsAvailable,
TotalUnitsInCurrentOrder: this.TotalUnitsInCurrentOrder,
TotalUnitsSold: this.TotalUnitsSold,
TotalUnitsReserved: this.TotalUnitsReserved,
TotalUnitsInTransit: this.TotalUnitsInTransit,
TotalUnitsOnConsignment: this.TotalUnitsOnConsignment,
TotalUnitsBackOrdered: this.TotalUnitsBackOrdered,
TotalUnitsPreOrdered: this.TotalUnitsPreOrdered,
StockLowAlertLevel: this.StockLowAlertLevel,
StockReorderLevel: this.StockReorderLevel,
BufferStockLevel: this.BufferStockLevel,
};
await ProductVariantWithInventory._Repository.delete(this.VariantId, dbTransaction);
const activity = new activity_history_1.Activity();
activity.ActivityId = cuid();
activity.Action = activity_history_1.ActionEnum.DELETE;
activity.Description = 'Delete product variant with inventory';
activity.EntityType = 'ProductVariantWithInventory';
activity.EntityId = this.VariantId;
activity.EntityValueBefore = JSON.stringify(entityValueBefore);
activity.EntityValueAfter = JSON.stringify({});
await activity.create(loginUser.ObjectId, dbTransaction);
await super.delete(loginUser, dbTransaction);
return {
MessageCode: 'ProductVariantWithInventoryDeleted',
Message: 'Product Variant With Inventory is deleted.',
};
}
catch (error) {
throw error;
}
}
}
exports.ProductVariantWithInventory = ProductVariantWithInventory;
ProductVariantWithInventory._Repository = new variant_product_with_inventory_repository_1.ProductVariantWithInventoryRepository();
//# sourceMappingURL=variant-product-with-inventory.js.map