@tomei/live-price
Version:
Tomei live-price Package
181 lines • 9.05 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.FeedHistory = void 0;
const general_1 = require("@tomei/general");
const log_1 = require("@tomei/log");
const feed_history_repository_1 = require("./feed-history.repository");
const sequelize_1 = require("sequelize");
class FeedHistory extends general_1.ObjectBase {
get FeedHistoryId() {
return this.ObjectId;
}
set FeedHistoryId(value) {
this.ObjectId = value;
}
constructor(feedHistoryAttr) {
super();
if (feedHistoryAttr) {
this.FeedHistoryId = feedHistoryAttr.FeedHistoryId;
this.FeedName = feedHistoryAttr.FeedName;
this.DateTime = feedHistoryAttr === null || feedHistoryAttr === void 0 ? void 0 : feedHistoryAttr.DateTime;
this.Currency = feedHistoryAttr === null || feedHistoryAttr === void 0 ? void 0 : feedHistoryAttr.Currency;
this.SourceFeedBuyPrice = feedHistoryAttr === null || feedHistoryAttr === void 0 ? void 0 : feedHistoryAttr.SourceFeedBuyPrice;
this.SourceFeedSellPrice = feedHistoryAttr === null || feedHistoryAttr === void 0 ? void 0 : feedHistoryAttr.SourceFeedSellPrice;
this.FeedBuyPrice = feedHistoryAttr === null || feedHistoryAttr === void 0 ? void 0 : feedHistoryAttr.FeedBuyPrice;
this.FeedSellPrice = feedHistoryAttr === null || feedHistoryAttr === void 0 ? void 0 : feedHistoryAttr.FeedSellPrice;
this.IsSourceFeedAvailableYN = feedHistoryAttr === null || feedHistoryAttr === void 0 ? void 0 : feedHistoryAttr.IsSourceFeedAvailableYN;
this.IsManualPriceActivatedYN = feedHistoryAttr === null || feedHistoryAttr === void 0 ? void 0 : feedHistoryAttr.IsManualPriceActivatedYN;
this.IsManualCutOffYN = feedHistoryAttr === null || feedHistoryAttr === void 0 ? void 0 : feedHistoryAttr.IsManualCutOffYN;
this.IsCircuitBreakerActivatedYN =
feedHistoryAttr === null || feedHistoryAttr === void 0 ? void 0 : feedHistoryAttr.IsCircuitBreakerActivatedYN;
}
}
static init(dbTransaction, FeedHistoryId) {
return __awaiter(this, void 0, void 0, function* () {
try {
if (FeedHistoryId) {
const feedHistory = yield FeedHistory._Repo.findByPk(FeedHistoryId, dbTransaction);
if (feedHistory) {
return new FeedHistory(feedHistory);
}
else {
const error = new general_1.ClassError('FeedHistory', 'FeedHistoryErrMsg00', 'FeedHistory Not Found');
yield FeedHistory.logger.error({
error,
methodName: 'init',
transaction: dbTransaction,
});
throw error;
}
}
return new FeedHistory();
}
catch (error) {
throw new general_1.ClassError('FeedHistory', 'FeedHistoryErrMsg00', 'FeedHistory To Initialize FeedHistory');
}
});
}
saveFeedPrice(priceData, dbTransaction) {
return __awaiter(this, void 0, void 0, function* () {
try {
const payload = Object.assign({ FeedHistoryId: this.createId() }, priceData);
this.FeedHistoryId = payload.FeedHistoryId;
this.FeedName = payload.FeedName;
this.DateTime = payload === null || payload === void 0 ? void 0 : payload.DateTime;
this.Currency = payload === null || payload === void 0 ? void 0 : payload.Currency;
this.SourceFeedBuyPrice = payload === null || payload === void 0 ? void 0 : payload.SourceFeedBuyPrice;
this.SourceFeedSellPrice = payload === null || payload === void 0 ? void 0 : payload.SourceFeedSellPrice;
this.FeedBuyPrice = payload === null || payload === void 0 ? void 0 : payload.FeedBuyPrice;
this.FeedSellPrice = payload === null || payload === void 0 ? void 0 : payload.FeedSellPrice;
this.IsSourceFeedAvailableYN = payload === null || payload === void 0 ? void 0 : payload.IsSourceFeedAvailableYN;
this.IsManualCutOffYN = payload === null || payload === void 0 ? void 0 : payload.IsManualCutOffYN;
this.IsCircuitBreakerActivatedYN = payload === null || payload === void 0 ? void 0 : payload.IsCircuitBreakerActivatedYN;
this.IsManualPriceActivatedYN = payload === null || payload === void 0 ? void 0 : payload.IsManualPriceActivatedYN;
const feedHistory = yield FeedHistory._Repo.create(payload, {
transaction: dbTransaction,
});
if (feedHistory) {
return new FeedHistory(feedHistory);
}
else {
const error = new general_1.ClassError('FeedHistory', 'FeedHistoryErrMsg01', 'FeedHistory Not Created');
yield FeedHistory.logger.error({
error,
methodName: 'saveFeedPrice',
transaction: dbTransaction,
});
throw error;
}
}
catch (error) {
yield FeedHistory.logger.error({
error,
methodName: 'saveFeedPrice',
transaction: dbTransaction,
});
throw error;
}
});
}
static getLatestPrice(feedName, currency, timestamp, dbTransaction) {
return __awaiter(this, void 0, void 0, function* () {
try {
const queryFilter = {
FeedName: feedName,
Currency: currency,
};
const options = {
where: queryFilter,
order: [['DateTime', 'DESC']],
limit: 1,
transaction: dbTransaction,
};
if (timestamp) {
options.order = [
[
sequelize_1.Sequelize.literal(`ABS(TIMESTAMPDIFF(SECOND, DateTime, '${timestamp.toISOString()}'))`),
'ASC',
],
];
}
const feedHistory = yield FeedHistory._Repo.findOne(options);
if (feedHistory) {
return feedHistory;
}
else {
return null;
}
}
catch (error) {
yield FeedHistory.logger.error({
error,
methodName: 'getLatestPrice',
transaction: dbTransaction,
});
throw error;
}
});
}
static findAll(page, row, feedName, search, dbTransaction) {
return __awaiter(this, void 0, void 0, function* () {
try {
const queryObj = {
FeedName: feedName,
};
const options = {
transaction: dbTransaction,
limit: row,
offset: row * (page - 1),
order: [['DateTime', 'DESC']],
};
if (search) {
queryObj.DateTime = {
[sequelize_1.Op.between]: [search.DateTime.startDate, search.DateTime.endDate],
};
}
return yield FeedHistory._Repo.findAllWithPagination(Object.assign({ where: queryObj }, options));
}
catch (error) {
yield FeedHistory.logger.error({
error,
methodName: 'getPaginatedHistory',
transaction: dbTransaction,
});
throw error;
}
});
}
}
exports.FeedHistory = FeedHistory;
FeedHistory.logger = new log_1.ApplicationLog('live-price', 'FeedHistory');
FeedHistory._Repo = new feed_history_repository_1.FeedHistoryRepository();
//# sourceMappingURL=feed-history.js.map