@tomei/live-price
Version:
Tomei live-price Package
219 lines • 9.96 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FeedManualPriceHistory = void 0;
const general_1 = require("@tomei/general");
const log_1 = require("@tomei/log");
const feed_enum_1 = require("../../enum/feed.enum");
const feed_manual_price_history_repository_1 = require("./feed-manual-price-history.repository");
const sequelize_1 = require("sequelize");
class FeedManualPriceHistory extends general_1.ObjectBase {
get FeedManualPriceHistoryId() {
return this.ObjectId;
}
set FeedManualPriceHistoryId(value) {
this.ObjectId = value;
}
constructor(feedManualPriceHistory) {
super();
this.ObjectType = FeedManualPriceHistory.name;
this.TableName = 'price_FeedManualPriceHistory';
if (feedManualPriceHistory) {
this.ObjectId = feedManualPriceHistory.FeedManualPriceHistoryId;
this.FeedName = feedManualPriceHistory.FeedName;
this.Status = feedManualPriceHistory.Status;
this.ActivationDateTime = feedManualPriceHistory.ActivationDateTime;
this.ActivatedById = feedManualPriceHistory.ActivatedById;
this.DeactivationDateTime = feedManualPriceHistory.DeactivationDateTime;
this.DeactivatedById = feedManualPriceHistory.DeactivatedById;
this.BuyPrice = feedManualPriceHistory.BuyPrice;
this.SellPrice = feedManualPriceHistory.SellPrice;
this.CompanyCode = feedManualPriceHistory.CompanyCode;
}
}
static init(feedManualPriceHistoryId, dbTransaction) {
return __awaiter(this, void 0, void 0, function* () {
try {
if (feedManualPriceHistoryId) {
const data = yield this._Repo.findByPk(feedManualPriceHistoryId, {
transaction: dbTransaction,
});
if (data) {
return new FeedManualPriceHistory(data);
}
else {
const error = new general_1.ClassError('FeedManualPriceHistory', 'FeedManualPriceHistoryErrMsg01', 'FeedManualPriceHistory not found', 'init');
yield FeedManualPriceHistory.logger.error({
error,
methodName: 'init',
transaction: dbTransaction,
});
throw error;
}
}
return new FeedManualPriceHistory();
}
catch (error) {
yield FeedManualPriceHistory.logger.error({
error,
methodName: 'init',
transaction: dbTransaction,
});
throw error;
}
});
}
create(feedName, activatedById, buyPrice, sellPrice, companyCode, dbTransaction) {
return __awaiter(this, void 0, void 0, function* () {
try {
if (!feedName || !activatedById || !buyPrice || !sellPrice) {
const error = new general_1.ClassError('FeedManualPriceHistory', 'FeedManualPriceErrMsg02', 'Missing required fields', 'create');
yield FeedManualPriceHistory.logger.error({
error,
methodName: 'create',
transaction: dbTransaction,
});
throw error;
}
this.ObjectId = this.createId();
this.FeedName = feedName;
this.BuyPrice = buyPrice;
this.SellPrice = sellPrice;
this.ActivatedById = activatedById;
this.ActivationDateTime = new Date();
this.Status = feed_enum_1.ManualPriceStatus.ACTIVE;
this.CompanyCode = companyCode;
yield FeedManualPriceHistory._Repo.create({
FeedManualPriceHistoryId: this.ObjectId,
FeedName: this.FeedName,
BuyPrice: this.BuyPrice,
SellPrice: this.SellPrice,
Status: this.Status,
ActivationDateTime: this.ActivationDateTime,
ActivatedById: this.ActivatedById,
DeactivationDateTime: this.DeactivationDateTime,
DeactivatedById: this.DeactivatedById,
CompanyCode: this.CompanyCode,
}, {
transaction: dbTransaction,
});
return this;
}
catch (error) {
yield FeedManualPriceHistory.logger.error({
error,
methodName: 'create',
transaction: dbTransaction,
});
throw error;
}
});
}
static deactivate(feedName, deactivatedById, companyCode, dbTransaction) {
return __awaiter(this, void 0, void 0, function* () {
try {
const whereConditions = {
FeedName: feedName,
Status: feed_enum_1.ManualPriceStatus.ACTIVE,
};
if (companyCode !== undefined) {
whereConditions.CompanyCode = companyCode;
}
const data = yield FeedManualPriceHistory._Repo.findAll({
order: [['ActivationDateTime', 'DESC']],
transaction: dbTransaction,
where: whereConditions,
});
if (!data) {
const error = new general_1.ClassError('FeedManualPriceHistory', 'FeedManualPriceErrMsg03', 'FeedManualPriceHistory not found', 'deactivate');
yield FeedManualPriceHistory.logger.error({
error,
methodName: 'deactivate',
transaction: dbTransaction,
});
throw error;
}
for (const item of data) {
item.DeactivationDateTime = new Date();
item.Status = feed_enum_1.ManualPriceStatus.DEACTIVATED;
item.DeactivatedById = deactivatedById;
yield item.save({
transaction: dbTransaction,
});
}
return new FeedManualPriceHistory(data[0]);
}
catch (error) {
yield FeedManualPriceHistory.logger.error({
error,
methodName: 'deactivate',
transaction: dbTransaction,
});
throw error;
}
});
}
static findAll(page, row, search, dbTransaction) {
return __awaiter(this, void 0, void 0, function* () {
try {
const queryObj = {};
const options = {
transaction: dbTransaction,
limit: row,
offset: row * (page - 1),
order: [['ActivationDateTime', 'DESC']],
};
if (search) {
if (search.ActivationDateTime) {
queryObj.ActivationDateTime = {
[sequelize_1.Op.between]: [
search.ActivationDateTime.startDate,
search.ActivationDateTime.endDate,
],
};
}
if (search.DeactivationDateTime) {
queryObj.DeactivationDateTime = {
[sequelize_1.Op.between]: [
search.DeactivationDateTime.startDate,
search.DeactivationDateTime.endDate,
],
};
}
if (search.FeedName) {
queryObj.FeedName = {
[sequelize_1.Op.like]: `%${search.FeedName}%`,
};
}
if (search.Status) {
queryObj.Status = search.Status;
}
if (search.CompanyCode) {
queryObj.CompanyCode = search.CompanyCode;
}
}
return yield FeedManualPriceHistory._Repo.findAllWithPagination(Object.assign({ where: queryObj }, options));
}
catch (error) {
yield FeedManualPriceHistory.logger.error({
error,
methodName: 'findAll',
transaction: dbTransaction,
});
throw error;
}
});
}
}
exports.FeedManualPriceHistory = FeedManualPriceHistory;
FeedManualPriceHistory._Repo = new feed_manual_price_history_repository_1.FeedManualPriceHistoryRepository();
FeedManualPriceHistory.logger = new log_1.ApplicationLog('live-price', 'FeedManualPriceHistory');
//# sourceMappingURL=feed-manual-price-history.js.map