@tomei/product
Version:
NestJS package for product module
888 lines • 34.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProductBase = void 0;
const helpers_1 = require("../../helpers");
const product_repository_1 = require("./product.repository");
const config_1 = require("@tomei/config");
const activity_history_1 = require("@tomei/activity-history");
const cuid = require("cuid");
const tag_product_1 = require("../../components/tag-product/tag-product");
const brand_product_1 = require("../../components/brand-product/brand-product");
const variant_product_1 = require("../../components/variant-product/variant-product");
const general_1 = require("@tomei/general");
class ProductBase extends general_1.ObjectBase {
constructor(product) {
super();
this.ObjectType = 'Product';
this._ProductId = 'New';
this._IsTaxableYN = 'N';
this._Status = 'Active';
this._VerifiedYN = 'N';
this._VariantLevels = 0;
this._CreatedAt = new Date();
this._UpdatedAt = new Date();
this._UpdatedSSYN = 'N';
if (product) {
this._ProductId = product.ProductId;
this._Name = product.Name;
this._Description = product.Description;
this._SKU = product.SKU;
this._PreviousSKU = product.PreviousSKU;
this._Type = product.Type;
this._Remark = product.Remark;
this._IsTaxableYN = product.IsTaxableYN;
this._TaxCode = product.TaxCode;
this._IsPriceInclusiveTaxYN = product.IsPriceInclusiveTaxYN;
this._Status = product.Status;
this._VerifiedYN = product.VerifiedYN;
this._VerifiedById = product.VerifiedById;
this._VerifiedAt = product.VerifiedAt;
this._VariantLevels = product.VariantLevels;
this._VariantTypeLevel1 = product.VariantTypeLevel1;
this._VariantTypeLevel2 = product.VariantTypeLevel2;
this._VariantTypeLevel3 = product.VariantTypeLevel3;
this._CreatedById = product.CreatedById;
this._CreatedAt = product.CreatedAt;
this._UpdatedById = product.UpdatedById;
this._UpdatedAt = product.UpdatedAt;
this._UpdatedSSYN = product.UpdatedSSYN;
}
}
get ProductId() {
return this._ProductId;
}
set ProductId(value) {
this._ProductId = value;
}
get Name() {
return this._Name;
}
set Name(value) {
this._Name = value;
}
get Description() {
return this._Description;
}
set Description(value) {
this._Description = value;
}
get SKU() {
return this._SKU;
}
set SKU(value) {
this._SKU = value;
}
get PreviousSKU() {
return this._PreviousSKU;
}
set PreviousSKU(value) {
this._PreviousSKU = value;
}
get Type() {
return this._Type;
}
set Type(value) {
this._Type = value;
}
get Remark() {
return this._Remark;
}
set Remark(value) {
this._Remark = value;
}
get IsTaxableYN() {
return this._IsTaxableYN;
}
set IsTaxableYN(value) {
this._IsTaxableYN = value;
}
get TaxCode() {
return this._TaxCode;
}
set TaxCode(value) {
this._TaxCode = value;
}
get IsPriceInclusiveTaxYN() {
return this._IsPriceInclusiveTaxYN;
}
set IsPriceInclusiveTaxYN(value) {
this._IsPriceInclusiveTaxYN = value;
}
get Status() {
return this._Status;
}
set Status(value) {
this._Status = value;
}
get VerifiedYN() {
return this._VerifiedYN;
}
set VerifiedYN(value) {
this._VerifiedYN = value;
}
get VerifiedById() {
return this._VerifiedById;
}
set VerifiedById(value) {
this._VerifiedById = value;
}
get VerifiedAt() {
return this._VerifiedAt;
}
set VerifiedAt(value) {
this._VerifiedAt = value;
}
get VariantLevels() {
return this._VariantLevels;
}
set VariantLevels(value) {
this._VariantLevels = value;
}
get VariantTypeLevel1() {
return this._VariantTypeLevel1;
}
set VariantTypeLevel1(value) {
this._VariantTypeLevel1 = value;
}
get VariantTypeLevel2() {
return this._VariantTypeLevel2;
}
set VariantTypeLevel2(value) {
this._VariantTypeLevel2 = value;
}
get VariantTypeLevel3() {
return this._VariantTypeLevel3;
}
set VariantTypeLevel3(value) {
this._VariantTypeLevel3 = 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;
}
get UpdatedSSYN() {
return this._UpdatedSSYN;
}
set UpdatedSSYN(value) {
this._UpdatedSSYN = value;
}
async create(loginUser, dbTransaction) {
try {
const sameSKUFound = await ProductBase._Repository.findOne({
where: {
SKU: this.SKU,
},
});
if (sameSKUFound) {
throw new Error('SKU is already existed.');
}
if (this.IsTaxableYN === 'Y') {
if (!this.TaxCode) {
throw new Error('TaxCode is required if IsTaxableYN equal Y.');
}
if (this.IsPriceInclusiveTaxYN === 'Y') {
throw new Error('IsPriceInclusiveTaxYN is required if IsTaxableYN equal Y.');
}
}
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'Product - Add');
if (!isPrivileged) {
throw new Error('You do not have permission to add product.');
}
if (!this.Status) {
this.Status = 'Active';
}
this.ProductId = cuid();
this._CreatedById = loginUser.ObjectId;
this._CreatedAt = new Date();
this._UpdatedById = loginUser.ObjectId;
this._UpdatedAt = new Date();
const data = await ProductBase._Repository.create({
ProductId: this.ProductId,
Name: this.Name,
Description: this.Description,
SKU: this.SKU,
PreviousSKU: this.PreviousSKU,
Type: this.Type,
Remark: this.Remark,
IsTaxableYN: this.IsTaxableYN,
TaxCode: this.TaxCode,
Status: this.Status,
IsPriceInclusiveTaxYN: this.IsPriceInclusiveTaxYN,
VariantLevels: this.VariantLevels,
VariantTypeLevel1: this.VariantTypeLevel1,
VariantTypeLevel2: this.VariantTypeLevel2,
VariantTypeLevel3: this.VariantTypeLevel3,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
UpdatedSSYN: this.UpdatedSSYN,
}, {
transaction: dbTransaction,
});
const activity = new activity_history_1.Activity();
activity.ActivityId = cuid();
activity.Action = activity_history_1.ActionEnum.CREATE;
activity.Description = 'Add new product';
activity.EntityType = 'Product';
activity.EntityId = data.ProductId;
activity.EntityValueBefore = JSON.stringify({});
activity.EntityValueAfter = JSON.stringify(data);
await activity.create(loginUser.ObjectId, dbTransaction);
return data;
}
catch (err) {
throw err;
}
}
static async findOne(searchOptions, dbTransaction) {
try {
const data = await ProductBase._Repository.findOne(Object.assign(Object.assign({}, searchOptions), { transaction: dbTransaction }));
return data;
}
catch (err) {
(0, helpers_1.throwException)(err);
}
}
static async findAll(loginUser) {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'Product - View');
if (!isPrivileged) {
throw new Error('You do not have permission to view product.');
}
return systemCode;
}
catch (err) {
throw err;
}
}
async findAllWithPagination(searchOptions, dbTransaction) {
try {
return ProductBase._Repository.findAllWithPagination({
searchOptions,
transaction: dbTransaction,
});
}
catch (err) {
throw err;
}
}
async update(loginUser, productInfo, dbTransaction) {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'Product - Update');
if (!isPrivileged) {
throw new Error('You do not have permission to edit product.');
}
if (this.IsTaxableYN === 'Y') {
if (!this.TaxCode) {
throw new Error('TaxCode is required if IsTaxableYN equal Y.');
}
if (this.IsPriceInclusiveTaxYN === 'Y') {
throw new Error('IsPriceInclusiveTaxYN is required if IsTaxableYN equal Y.');
}
}
const entityValueBefore = {
ProductId: this.ProductId,
Name: this.Name,
Description: this.Description,
SKU: this.SKU,
PreviousSKU: this.PreviousSKU,
Type: this.Type,
Remark: this.Remark,
IsTaxableYN: this.IsTaxableYN,
TaxCode: this.TaxCode,
IsPriceInclusiveTaxYN: this.IsPriceInclusiveTaxYN,
Status: this.Status,
VerifiedYN: this.VerifiedYN,
VerifiedById: this.VerifiedById,
VerifiedAt: this.VerifiedAt,
VariantLevels: this.VariantLevels,
VariantTypeLevel1: this.VariantTypeLevel1,
VariantTypeLevel2: this.VariantTypeLevel2,
VariantTypeLevel3: this.VariantTypeLevel3,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
UpdatedSSYN: this.UpdatedSSYN,
};
const propertiesToCopy = [
'Name',
'Description',
'SKU',
'PreviousSKU',
'Type',
'Remark',
'IsTaxableYN',
'TaxCode',
'IsPriceInclusiveTaxYN',
'Status',
'VerifiedYN',
'VerifiedById',
'VerifiedAt',
'VariantLevels',
'VariantTypeLevel1',
'VariantTypeLevel2',
'VariantTypeLevel3',
'CreatedById',
'CreatedAt',
'UpdatedById',
'UpdatedAt',
'UpdatedSSYN',
];
for (const property of propertiesToCopy) {
this[property] = productInfo[property];
}
const product = await ProductBase._Repository.findOne({
where: {
SKU: this.SKU,
},
transaction: dbTransaction,
});
if (!product) {
throw new Error('Product not found.');
}
for (const property of propertiesToCopy) {
product[property] = this[property];
}
const updatedInfo = await product.save({
transaction: dbTransaction,
});
const entityValueAfter = productInfo;
const activity = new activity_history_1.Activity();
activity.ActivityId = cuid();
activity.Action = activity_history_1.ActionEnum.UPDATE;
activity.Description = 'Update product';
activity.EntityType = 'Product';
activity.EntityId = this.ProductId;
activity.EntityValueBefore = JSON.stringify(entityValueBefore);
activity.EntityValueAfter = JSON.stringify(entityValueAfter);
await activity.create(loginUser.ObjectId, dbTransaction);
return updatedInfo;
}
catch (err) {
throw err;
}
}
async deactivate(loginUser, dbTransaction) {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'Product - Deactivate');
if (!isPrivileged) {
throw new Error('You do not have permission to deactivate product.');
}
const entityValueBefore = {
ProductId: this.ProductId,
Name: this.Name,
Description: this.Description,
SKU: this.SKU,
PreviousSKU: this.PreviousSKU,
Type: this.Type,
Remark: this.Remark,
IsTaxableYN: this.IsTaxableYN,
TaxCode: this.TaxCode,
IsPriceInclusiveTaxYN: this.IsPriceInclusiveTaxYN,
Status: this.Status,
VerifiedYN: this.VerifiedYN,
VerifiedById: this.VerifiedById,
VerifiedAt: this.VerifiedAt,
VariantLevels: this.VariantLevels,
VariantTypeLevel1: this.VariantTypeLevel1,
VariantTypeLevel2: this.VariantTypeLevel2,
VariantTypeLevel3: this.VariantTypeLevel3,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
UpdatedSSYN: this.UpdatedSSYN,
};
this.Status = 'Inactive';
this.UpdatedById = loginUser.ObjectId;
this.UpdatedAt = new Date();
this.UpdatedSSYN = 'N';
const product = await ProductBase._Repository.findOne({
where: {
SKU: this.SKU,
},
transaction: dbTransaction,
});
product.Status = this.Status;
product.UpdatedById = this.UpdatedById;
product.UpdatedAt = this.UpdatedAt;
product.UpdatedSSYN = this.UpdatedSSYN;
await product.save({
transaction: dbTransaction,
});
const entityValueAfter = {
ProductId: this.ProductId,
Name: this.Name,
Description: this.Description,
SKU: this.SKU,
PreviousSKU: this.PreviousSKU,
Type: this.Type,
Remark: this.Remark,
IsTaxableYN: this.IsTaxableYN,
TaxCode: this.TaxCode,
IsPriceInclusiveTaxYN: this.IsPriceInclusiveTaxYN,
Status: this.Status,
VerifiedYN: this.VerifiedYN,
VerifiedById: this.VerifiedById,
VerifiedAt: this.VerifiedAt,
VariantLevels: this.VariantLevels,
VariantTypeLevel1: this.VariantTypeLevel1,
VariantTypeLevel2: this.VariantTypeLevel2,
VariantTypeLevel3: this.VariantTypeLevel3,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
UpdatedSSYN: this.UpdatedSSYN,
};
const activity = new activity_history_1.Activity();
activity.ActivityId = cuid();
activity.Action = activity_history_1.ActionEnum.UPDATE;
activity.Description = 'Deactivate Product';
activity.EntityType = 'Product';
activity.EntityId = this.ProductId;
activity.EntityValueBefore = JSON.stringify(entityValueBefore);
activity.EntityValueAfter = JSON.stringify(entityValueAfter);
await activity.create(loginUser.ObjectId, dbTransaction);
}
catch (error) {
throw error;
}
}
async verify(loginUser, dbTransaction) {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'Product - Verify');
if (!isPrivileged) {
throw new Error('You do not have permission to verify product.');
}
const entityValueBefore = {
ProductId: this.ProductId,
Name: this.Name,
Description: this.Description,
SKU: this.SKU,
PreviousSKU: this.PreviousSKU,
Type: this.Type,
Remark: this.Remark,
IsTaxableYN: this.IsTaxableYN,
TaxCode: this.TaxCode,
IsPriceInclusiveTaxYN: this.IsPriceInclusiveTaxYN,
Status: this.Status,
VerifiedYN: this.VerifiedYN,
VerifiedById: this.VerifiedById,
VerifiedAt: this.VerifiedAt,
VariantLevels: this.VariantLevels,
VariantTypeLevel1: this.VariantTypeLevel1,
VariantTypeLevel2: this.VariantTypeLevel2,
VariantTypeLevel3: this.VariantTypeLevel3,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
UpdatedSSYN: this.UpdatedSSYN,
};
this.VerifiedYN = 'Y';
this.VerifiedById = loginUser.ObjectId;
this.VerifiedAt = new Date();
const product = await ProductBase._Repository.findOne({
where: {
SKU: this.SKU,
},
transaction: dbTransaction,
});
product.VerifiedYN = this.VerifiedYN;
product.VerifiedById = this.VerifiedById;
product.VerifiedAt = this.VerifiedAt;
await product.save({
transaction: dbTransaction,
});
const entityValueAfter = {
ProductId: this.ProductId,
Name: this.Name,
Description: this.Description,
SKU: this.SKU,
PreviousSKU: this.PreviousSKU,
Type: this.Type,
Remark: this.Remark,
IsTaxableYN: this.IsTaxableYN,
TaxCode: this.TaxCode,
IsPriceInclusiveTaxYN: this.IsPriceInclusiveTaxYN,
Status: this.Status,
VerifiedYN: this.VerifiedYN,
VerifiedById: this.VerifiedById,
VerifiedAt: this.VerifiedAt,
VariantLevels: this.VariantLevels,
VariantTypeLevel1: this.VariantTypeLevel1,
VariantTypeLevel2: this.VariantTypeLevel2,
VariantTypeLevel3: this.VariantTypeLevel3,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
UpdatedSSYN: this.UpdatedSSYN,
};
const activity = new activity_history_1.Activity();
activity.ActivityId = cuid();
activity.Action = activity_history_1.ActionEnum.UPDATE;
activity.Description = 'Verify Product';
activity.EntityType = 'Product';
activity.EntityId = this.ProductId;
activity.EntityValueBefore = JSON.stringify(entityValueBefore);
activity.EntityValueAfter = JSON.stringify(entityValueAfter);
await activity.create(loginUser.ObjectId, dbTransaction);
}
catch (error) {
throw error;
}
}
async activate(loginUser, dbTransaction) {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'Product - Activate');
if (!isPrivileged) {
throw new Error('You do not have permission to activate product.');
}
const entityValueBefore = {
ProductId: this.ProductId,
Name: this.Name,
Description: this.Description,
SKU: this.SKU,
PreviousSKU: this.PreviousSKU,
Type: this.Type,
Remark: this.Remark,
IsTaxableYN: this.IsTaxableYN,
TaxCode: this.TaxCode,
IsPriceInclusiveTaxYN: this.IsPriceInclusiveTaxYN,
Status: this.Status,
VerifiedYN: this.VerifiedYN,
VerifiedById: this.VerifiedById,
VerifiedAt: this.VerifiedAt,
VariantLevels: this.VariantLevels,
VariantTypeLevel1: this.VariantTypeLevel1,
VariantTypeLevel2: this.VariantTypeLevel2,
VariantTypeLevel3: this.VariantTypeLevel3,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
UpdatedSSYN: this.UpdatedSSYN,
};
this.Status = 'Active';
this.UpdatedById = loginUser.ObjectId;
this.UpdatedAt = new Date();
this.UpdatedSSYN = 'N';
const product = await ProductBase._Repository.findOne({
where: {
SKU: this.SKU,
},
transaction: dbTransaction,
});
product.Status = this.Status;
product.UpdatedById = this.UpdatedById;
product.UpdatedAt = this.UpdatedAt;
product.UpdatedSSYN = this.UpdatedSSYN;
await product.save({
transaction: dbTransaction,
});
const entityValueAfter = {
ProductId: this.ProductId,
Name: this.Name,
Description: this.Description,
SKU: this.SKU,
PreviousSKU: this.PreviousSKU,
Type: this.Type,
Remark: this.Remark,
IsTaxableYN: this.IsTaxableYN,
TaxCode: this.TaxCode,
IsPriceInclusiveTaxYN: this.IsPriceInclusiveTaxYN,
Status: this.Status,
VerifiedYN: this.VerifiedYN,
VerifiedById: this.VerifiedById,
VerifiedAt: this.VerifiedAt,
VariantLevels: this.VariantLevels,
VariantTypeLevel1: this.VariantTypeLevel1,
VariantTypeLevel2: this.VariantTypeLevel2,
VariantTypeLevel3: this.VariantTypeLevel3,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
UpdatedSSYN: this.UpdatedSSYN,
};
const activity = new activity_history_1.Activity();
activity.ActivityId = cuid();
activity.Action = activity_history_1.ActionEnum.UPDATE;
activity.Description = 'Activate Product';
activity.EntityType = 'Product';
activity.EntityId = this.ProductId;
activity.EntityValueBefore = JSON.stringify(entityValueBefore);
activity.EntityValueAfter = JSON.stringify(entityValueAfter);
await activity.create(loginUser.ObjectId, dbTransaction);
}
catch (error) {
throw error;
}
}
async discontinue(loginUser, dbTransaction) {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'Product - Discontinue');
if (!isPrivileged) {
throw new Error('You do not have permission to discontinue product.');
}
const entityValueBefore = {
ProductId: this.ProductId,
Name: this.Name,
Description: this.Description,
SKU: this.SKU,
PreviousSKU: this.PreviousSKU,
Type: this.Type,
Remark: this.Remark,
IsTaxableYN: this.IsTaxableYN,
TaxCode: this.TaxCode,
IsPriceInclusiveTaxYN: this.IsPriceInclusiveTaxYN,
Status: this.Status,
VerifiedYN: this.VerifiedYN,
VerifiedById: this.VerifiedById,
VerifiedAt: this.VerifiedAt,
VariantLevels: this.VariantLevels,
VariantTypeLevel1: this.VariantTypeLevel1,
VariantTypeLevel2: this.VariantTypeLevel2,
VariantTypeLevel3: this.VariantTypeLevel3,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
UpdatedSSYN: this.UpdatedSSYN,
};
this.Status = 'Discontinue';
this.UpdatedById = loginUser.ObjectId;
this.UpdatedAt = new Date();
this.UpdatedSSYN = 'N';
const product = await ProductBase._Repository.findOne({
where: {
SKU: this.SKU,
},
transaction: dbTransaction,
});
product.Status = this.Status;
product.UpdatedById = this.UpdatedById;
product.UpdatedAt = this.UpdatedAt;
product.UpdatedSSYN = this.UpdatedSSYN;
await product.save({
transaction: dbTransaction,
});
const entityValueAfter = {
ProductId: this.ProductId,
Name: this.Name,
Description: this.Description,
SKU: this.SKU,
PreviousSKU: this.PreviousSKU,
Type: this.Type,
Remark: this.Remark,
IsTaxableYN: this.IsTaxableYN,
TaxCode: this.TaxCode,
IsPriceInclusiveTaxYN: this.IsPriceInclusiveTaxYN,
Status: this.Status,
VerifiedYN: this.VerifiedYN,
VerifiedById: this.VerifiedById,
VerifiedAt: this.VerifiedAt,
VariantLevels: this.VariantLevels,
VariantTypeLevel1: this.VariantTypeLevel1,
VariantTypeLevel2: this.VariantTypeLevel2,
VariantTypeLevel3: this.VariantTypeLevel3,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
UpdatedSSYN: this.UpdatedSSYN,
};
const activity = new activity_history_1.Activity();
activity.ActivityId = cuid();
activity.Action = activity_history_1.ActionEnum.UPDATE;
activity.Description = 'Discontinue Product';
activity.EntityType = 'Product';
activity.EntityId = this.ProductId;
activity.EntityValueBefore = JSON.stringify(entityValueBefore);
activity.EntityValueAfter = JSON.stringify(entityValueAfter);
await activity.create(loginUser.ObjectId, dbTransaction);
}
catch (error) {
throw error;
}
}
async delete(loginUser, dbTransaction) {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'Product - Delete');
if (!isPrivileged) {
throw new Error('You do not have permission to delete product.');
}
const entityValueBefore = {
ProductId: this.ProductId,
Name: this.Name,
Description: this.Description,
SKU: this.SKU,
PreviousSKU: this.PreviousSKU,
Type: this.Type,
Remark: this.Remark,
IsTaxableYN: this.IsTaxableYN,
TaxCode: this.TaxCode,
IsPriceInclusiveTaxYN: this.IsPriceInclusiveTaxYN,
Status: this.Status,
VerifiedYN: this.VerifiedYN,
VerifiedById: this.VerifiedById,
VerifiedAt: this.VerifiedAt,
VariantLevels: this.VariantLevels,
VariantTypeLevel1: this.VariantTypeLevel1,
VariantTypeLevel2: this.VariantTypeLevel2,
VariantTypeLevel3: this.VariantTypeLevel3,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
UpdatedSSYN: this.UpdatedSSYN,
};
this.Status = 'Deleted';
this.UpdatedById = loginUser.ObjectId;
this.UpdatedAt = new Date();
this.UpdatedSSYN = 'N';
const product = await ProductBase._Repository.findOne({
where: {
SKU: this.SKU,
},
transaction: dbTransaction,
});
product.Status = this.Status;
product.UpdatedById = this.UpdatedById;
product.UpdatedAt = this.UpdatedAt;
product.UpdatedSSYN = this.UpdatedSSYN;
await product.save({
transaction: dbTransaction,
});
const entityValueAfter = {
ProductId: this.ProductId,
Name: this.Name,
Description: this.Description,
SKU: this.SKU,
PreviousSKU: this.PreviousSKU,
Type: this.Type,
Remark: this.Remark,
IsTaxableYN: this.IsTaxableYN,
TaxCode: this.TaxCode,
IsPriceInclusiveTaxYN: this.IsPriceInclusiveTaxYN,
Status: this.Status,
VerifiedYN: this.VerifiedYN,
VerifiedById: this.VerifiedById,
VerifiedAt: this.VerifiedAt,
VariantLevels: this.VariantLevels,
VariantTypeLevel1: this.VariantTypeLevel1,
VariantTypeLevel2: this.VariantTypeLevel2,
VariantTypeLevel3: this.VariantTypeLevel3,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
UpdatedSSYN: this.UpdatedSSYN,
};
const activity = new activity_history_1.Activity();
activity.ActivityId = cuid();
activity.Action = activity_history_1.ActionEnum.DELETE;
activity.Description = 'Delete Product';
activity.EntityType = 'Product';
activity.EntityId = this.ProductId;
activity.EntityValueBefore = JSON.stringify(entityValueBefore);
activity.EntityValueAfter = JSON.stringify(entityValueAfter);
await activity.create(loginUser.ObjectId, dbTransaction);
}
catch (error) {
throw error;
}
}
async addTag(name, loginUser, dbTransaction) {
try {
const productTag = await tag_product_1.ProductTag.init(dbTransaction);
productTag.Name = name;
productTag.ProductId = this.ProductId;
productTag.create(loginUser, dbTransaction);
}
catch (error) {
throw error;
}
}
async getBrand(loginUser, dbTransaction) {
try {
const productBrand = await brand_product_1.ProductBrand.findAll(this.ProductId, loginUser, dbTransaction);
return productBrand;
}
catch (error) {
throw error;
}
}
async assignBrand(brand, loginUser, dbTransaction) {
try {
const productBrand = await brand_product_1.ProductBrand.init(dbTransaction);
productBrand.ProductId = this.ProductId;
productBrand.Brand = brand;
await productBrand.create(loginUser, dbTransaction);
return {
MessageCode: 'ProductBrandAsssigned',
Message: 'Brand assigned to a product',
};
}
catch (error) {
throw error;
}
}
async unassignBrand(brand, loginUser, dbTransaction) {
try {
const productBrand = await brand_product_1.ProductBrand.init(dbTransaction, brand);
await productBrand.delete(loginUser, dbTransaction);
return {
MessageCode: 'ProductBrandUnasssigned',
Message: 'Brand unassigned to a product',
};
}
catch (error) {
throw error;
}
}
async getVariants(loginUser, dbTransaction, page, row) {
try {
const result = await variant_product_1.ProductVariant.findAll(this.ProductId, loginUser, page, row, dbTransaction);
return result;
}
catch (error) {
throw error;
}
}
}
exports.ProductBase = ProductBase;
ProductBase._Repository = new product_repository_1.ProductRepository();
//# sourceMappingURL=product.base.js.map