@tomei/product
Version:
NestJS package for product module
1,098 lines (967 loc) • 31.2 kB
text/typescript
import { ProductModel } from '../../entities/product.entity';
import { throwException } from '../../helpers';
import { ProductRepository } from './product.repository';
import { IJewelleryProductAttr, IProductAttrBase } from '../../interfaces';
import { ApplicationConfig } from '@tomei/config';
import { Activity, ActionEnum } from '@tomei/activity-history';
import * as cuid from 'cuid';
import { LoginUser } from '@tomei/sso';
import { ProductTag } from '../../components/tag-product/tag-product';
import { ProductBrand } from '../../components/brand-product/brand-product';
import { ProductBrandModel } from '../../entities/product-brand.entity';
import { ProductVariant } from '../../components/variant-product/variant-product';
import { ObjectBase } from '@tomei/general';
export abstract class ProductBase extends ObjectBase {
ObjectId: string;
ObjectName: string;
TableName: string;
ObjectType = 'Product';
private _ProductId = 'New';
private _Name: string;
private _SKU: string;
private _Description: string;
private _Type: string;
private _Remark: string;
private _IsTaxableYN = 'N';
private _TaxCode: string;
private _IsPriceInclusiveTaxYN: string;
private _Status = 'Active';
private _VerifiedYN = 'N';
private _VerifiedById: string;
private _VerifiedAt: Date;
private _VariantLevels = 0;
private _VariantTypeLevel1: string;
private _VariantTypeLevel2: string;
private _VariantTypeLevel3: string;
private _CreatedById: string;
private _CreatedAt = new Date();
private _UpdatedById: string;
private _UpdatedAt = new Date();
private _UpdatedSSYN = 'N';
protected static _Repository = new ProductRepository();
constructor(product?: IProductAttrBase) {
super();
if (product) {
this._ProductId = product.ProductId;
this._Name = product.Name;
this._Description = product.Description;
this._SKU = product.SKU;
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;
}
}
// create getter and setter for all attributes
get ProductId(): string {
return this._ProductId;
}
set ProductId(value: string) {
this._ProductId = value;
}
get Name(): string {
return this._Name;
}
set Name(value: string) {
this._Name = value;
}
get Description(): string {
return this._Description;
}
set Description(value: string) {
this._Description = value;
}
get SKU(): string {
return this._SKU;
}
set SKU(value: string) {
this._SKU = value;
}
get Type(): string {
return this._Type;
}
set Type(value: string) {
this._Type = value;
}
get Remark(): string {
return this._Remark;
}
set Remark(value: string) {
this._Remark = value;
}
get IsTaxableYN(): string {
return this._IsTaxableYN;
}
set IsTaxableYN(value: string) {
this._IsTaxableYN = value;
}
get TaxCode(): string {
return this._TaxCode;
}
set TaxCode(value: string) {
this._TaxCode = value;
}
get IsPriceInclusiveTaxYN(): string {
return this._IsPriceInclusiveTaxYN;
}
set IsPriceInclusiveTaxYN(value: string) {
this._IsPriceInclusiveTaxYN = value;
}
get Status(): string {
return this._Status;
}
set Status(value: string) {
this._Status = value;
}
get VerifiedYN(): string {
return this._VerifiedYN;
}
set VerifiedYN(value: string) {
this._VerifiedYN = value;
}
get VerifiedById(): string {
return this._VerifiedById;
}
set VerifiedById(value: string) {
this._VerifiedById = value;
}
get VerifiedAt(): Date {
return this._VerifiedAt;
}
set VerifiedAt(value: Date) {
this._VerifiedAt = value;
}
get VariantLevels(): number {
return this._VariantLevels;
}
set VariantLevels(value: number) {
this._VariantLevels = value;
}
get VariantTypeLevel1(): string {
return this._VariantTypeLevel1;
}
set VariantTypeLevel1(value: string) {
this._VariantTypeLevel1 = value;
}
get VariantTypeLevel2(): string {
return this._VariantTypeLevel2;
}
set VariantTypeLevel2(value: string) {
this._VariantTypeLevel2 = value;
}
get VariantTypeLevel3(): string {
return this._VariantTypeLevel3;
}
set VariantTypeLevel3(value: string) {
this._VariantTypeLevel3 = value;
}
get CreatedById(): string {
return this._CreatedById;
}
set CreatedById(value: string) {
this._CreatedById = value;
}
get CreatedAt(): Date {
return this._CreatedAt;
}
set CreatedAt(value: Date) {
this._CreatedAt = value;
}
get UpdatedById(): string {
return this._UpdatedById;
}
set UpdatedById(value: string) {
this._UpdatedById = value;
}
get UpdatedAt(): Date {
return this._UpdatedAt;
}
set UpdatedAt(value: Date) {
this._UpdatedAt = value;
}
get UpdatedSSYN(): string {
return this._UpdatedSSYN;
}
set UpdatedSSYN(value: string) {
this._UpdatedSSYN = value;
}
async create(
loginUser: LoginUser,
dbTransaction?: any,
): Promise<ProductModel | any> {
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 =
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,
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();
activity.ActivityId = cuid();
activity.Action = 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: any, dbTransaction?: any): Promise<any> {
try {
const data = await ProductBase._Repository.findOne({
...searchOptions,
transaction: dbTransaction,
});
return data;
} catch (err) {
throwException(err);
}
}
static async findAll(loginUser: LoginUser): Promise<any> {
try {
// Privilege checking
const systemCode =
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: any,
dbTransaction?: any,
): Promise<{ rows: ProductModel[]; count: number }> {
try {
return ProductBase._Repository.findAllWithPagination({
searchOptions,
transaction: dbTransaction,
});
} catch (err) {
throw err;
}
}
async update(
loginUser: LoginUser,
productInfo: IJewelleryProductAttr,
dbTransaction?: any,
): Promise<ProductModel | any> {
try {
const systemCode =
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,
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',
'Type',
'Remark',
'IsTaxableYN',
'TaxCode',
'IsPriceInclusiveTaxYN',
'Status',
'VerifiedYN',
'VerifiedById',
'VerifiedAt',
'VariantLevels',
'VariantTypeLevel1',
'VariantTypeLevel2',
'VariantTypeLevel3',
'CreatedById',
'CreatedAt',
'UpdatedById',
'UpdatedAt',
'UpdatedSSYN',
];
//Object assigning 'this' with 'productInfo' parameter
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.');
}
// Object assigning product with 'this' property
for (const property of propertiesToCopy) {
product[property] = this[property];
}
const updatedInfo = await product.save({
transaction: dbTransaction,
});
const entityValueAfter = productInfo;
const activity = new Activity();
activity.ActivityId = cuid();
activity.Action = 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: LoginUser, dbTransaction?: any): Promise<void> {
try {
const systemCode =
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,
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,
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();
activity.ActivityId = cuid();
activity.Action = 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: LoginUser, dbTransaction?: any): Promise<void> {
try {
// Privilege checking
const systemCode =
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.');
}
// Verifying product
const entityValueBefore = {
ProductId: this.ProductId,
Name: this.Name,
Description: this.Description,
SKU: this.SKU,
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,
});
// Record Activity History
const entityValueAfter = {
ProductId: this.ProductId,
Name: this.Name,
Description: this.Description,
SKU: this.SKU,
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();
activity.ActivityId = cuid();
activity.Action = 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: LoginUser, dbTransaction?: any): Promise<void> {
try {
// Privilege checking
const systemCode =
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.');
}
// Activating product
const entityValueBefore = {
ProductId: this.ProductId,
Name: this.Name,
Description: this.Description,
SKU: this.SKU,
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,
});
// Record Activity History
const entityValueAfter = {
ProductId: this.ProductId,
Name: this.Name,
Description: this.Description,
SKU: this.SKU,
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();
activity.ActivityId = cuid();
activity.Action = 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: LoginUser, dbTransaction?: any): Promise<void> {
try {
const systemCode =
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,
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,
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();
activity.ActivityId = cuid();
activity.Action = 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: LoginUser, dbTransaction?: any): Promise<void> {
try {
// Privilege checking
const systemCode =
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.');
}
// Delete product
const entityValueBefore = {
ProductId: this.ProductId,
Name: this.Name,
Description: this.Description,
SKU: this.SKU,
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,
});
// Record Activity History
const entityValueAfter = {
ProductId: this.ProductId,
Name: this.Name,
Description: this.Description,
SKU: this.SKU,
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();
activity.ActivityId = cuid();
activity.Action = 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: string,
loginUser: LoginUser,
dbTransaction?: any,
): Promise<void> {
try {
const productTag = await ProductTag.init(dbTransaction);
productTag.Name = name;
productTag.ProductId = this.ProductId;
productTag.create(loginUser, dbTransaction);
} catch (error) {
throw error;
}
}
async getBrand(
loginUser: LoginUser,
dbTransaction?: any,
): Promise<{
rows: ProductBrandModel[];
count: number;
}> {
try {
const productBrand = await ProductBrand.findAll(
this.ProductId,
loginUser,
dbTransaction,
);
return productBrand;
} catch (error) {
throw error;
}
}
async assignBrand(
brand: string,
loginUser: LoginUser,
dbTransaction?: any,
): Promise<any> {
try {
const productBrand = await 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: string,
loginUser: LoginUser,
dbTransaction?: any,
): Promise<any> {
try {
const productBrand = await 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: LoginUser,
dbTransaction?: any,
page?: number,
row?: number,
) {
try {
const result = await ProductVariant.findAll(
this.ProductId,
loginUser,
page,
row,
dbTransaction,
);
return result;
} catch (error) {
throw error;
}
}
}