UNPKG

@tomei/live-price

Version:

Tomei live-price Package

234 lines 11.9 kB
"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.TomeiPriceHistory = void 0; const general_1 = require("@tomei/general"); const tomei_price_repository_1 = require("./tomei-price.repository"); const sequelize_1 = require("sequelize"); const price_feed_history_entity_1 = require("../../models/price-feed-history.entity"); const feed_manual_price_history_1 = require("../feed-manual-price-history/feed-manual-price-history"); const feed_enum_1 = require("../../enum/feed.enum"); class TomeiPriceHistory extends general_1.ObjectBase { get TomeiPriceHistoryId() { return this.ObjectId; } set TomeiPriceHistoryId(value) { this.ObjectId = value; } constructor(tomeiPriceHistoryAttr) { super(); if (tomeiPriceHistoryAttr) { this.TomeiPriceHistoryId = tomeiPriceHistoryAttr.TomeiPriceHistoryId; this.FeedHistoryId = tomeiPriceHistoryAttr.FeedHistoryId; this.FeedName = tomeiPriceHistoryAttr.FeedName; this.DateTime = tomeiPriceHistoryAttr.DateTime; this.AdjustBuyPercentage = tomeiPriceHistoryAttr.AdjustBuyPercentage; this.AdjustSellPercentage = tomeiPriceHistoryAttr.AdjustSellPercentage; this.TomeiBuyPrice = tomeiPriceHistoryAttr.TomeiBuyPrice; this.TomeiSellPrice = tomeiPriceHistoryAttr.TomeiSellPrice; this.AdjustedById = tomeiPriceHistoryAttr.AdjustedById; this.Currency = tomeiPriceHistoryAttr.Currency; this.AdjustedDateTime = tomeiPriceHistoryAttr.AdjustedDateTime; this.IsManualPriceActivatedYN = tomeiPriceHistoryAttr.IsManualPriceActivatedYN; this.BuyLabourCharges = tomeiPriceHistoryAttr.BuyLabourCharges; this.SellLabourCharges = tomeiPriceHistoryAttr.SellLabourCharges; } } static init(dbTransaction, tomeiPriceHistoryId) { return __awaiter(this, void 0, void 0, function* () { try { if (tomeiPriceHistoryId) { const data = yield TomeiPriceHistory._Repo.findByPk(tomeiPriceHistoryId, dbTransaction); if (data) { return new TomeiPriceHistory(data); } else { throw new general_1.ClassError('TomeiPriceHistory', 'TomeiPriceHistoryErrMsg00', 'TomeiPriceHistory Not Found'); } } return new TomeiPriceHistory(); } catch (error) { throw new general_1.ClassError('TomeiPriceHistory', 'TomeiPriceHistoryErrMsg00', 'TomeiPriceHistory To Initialize FeedHistory'); } }); } saveTomeiPrice(feedName, feedHistory, loginUser, adjustmentPercentage, manualPrice, dbTransaction) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f; try { if (!feedHistory || !feedHistory.FeedHistoryId) { throw new general_1.ClassError('TomeiPriceHistory', 'TomeiPriceHistoryErrMsg01', 'FeedHistoryId is required to create tomei price'); } let adjustmentBy = null; let adjustmentDateTime = new Date(); const latestPrice = yield TomeiPriceHistory.getLatestPrice(feedName, feedHistory.Currency, undefined, dbTransaction); if (!adjustmentPercentage) { if (!latestPrice) { adjustmentPercentage = { adjustBuyPercentage: 0, adjustSellPercentage: 0, buyLabourCharges: 0, sellLabourCharges: 0, }; } else { adjustmentPercentage = { adjustBuyPercentage: latestPrice.AdjustBuyPercentage, adjustSellPercentage: latestPrice.AdjustSellPercentage, buyLabourCharges: latestPrice.BuyLabourCharges, sellLabourCharges: latestPrice.SellLabourCharges, }; adjustmentBy = latestPrice.AdjustedById; adjustmentDateTime = latestPrice.AdjustedDateTime; } } else if (!loginUser && adjustmentPercentage) { throw new general_1.ClassError('TomeiPriceHistory', 'TomeiPriceHistoryErrMsg01', 'Login user is required to create tomei price when updating adjustment buy and sell percentage'); } let tomeiBuyPrice; let tomeiSellPrice; let feedManualPrice = yield feed_manual_price_history_1.FeedManualPriceHistory.findAll(1, 1, { FeedName: feedName, Status: feed_enum_1.ManualPriceStatus.ACTIVE, }, dbTransaction); let IsManualPriceActivatedYN = 'N'; if (manualPrice) { tomeiBuyPrice = manualPrice.buyPrice; tomeiSellPrice = manualPrice.sellPrice; IsManualPriceActivatedYN = 'Y'; } else if (feedManualPrice.count > 0) { const manual = (_a = feedManualPrice.rows) === null || _a === void 0 ? void 0 : _a[0]; tomeiBuyPrice = (_c = (_b = manual === null || manual === void 0 ? void 0 : manual.BuyPrice) !== null && _b !== void 0 ? _b : latestPrice === null || latestPrice === void 0 ? void 0 : latestPrice.TomeiBuyPrice) !== null && _c !== void 0 ? _c : 0; tomeiSellPrice = (_e = (_d = manual === null || manual === void 0 ? void 0 : manual.SellPrice) !== null && _d !== void 0 ? _d : latestPrice === null || latestPrice === void 0 ? void 0 : latestPrice.TomeiSellPrice) !== null && _e !== void 0 ? _e : 0; IsManualPriceActivatedYN = 'Y'; } else { const weightMap = { [feed_enum_1.NonSourceFeedName.GOLD1000G]: 1000, [feed_enum_1.NonSourceFeedName.GOLD500G]: 500, [feed_enum_1.NonSourceFeedName.GOLD100G]: 100, [feed_enum_1.NonSourceFeedName.GOLD50G]: 50, [feed_enum_1.NonSourceFeedName.SILVER1000G]: 1000, [feed_enum_1.NonSourceFeedName.SILVER10OZ]: 311, }; let weight = (_f = weightMap[feedName]) !== null && _f !== void 0 ? _f : 0; tomeiBuyPrice = feedHistory.FeedSellPrice * weight * (1 + adjustmentPercentage.adjustBuyPercentage / 100) + adjustmentPercentage.buyLabourCharges; tomeiSellPrice = feedHistory.FeedSellPrice * weight * (1 + adjustmentPercentage.adjustSellPercentage / 100) + adjustmentPercentage.sellLabourCharges; } const payload = { TomeiPriceHistoryId: this.createId(), FeedHistoryId: feedHistory.FeedHistoryId, FeedName: feedName, DateTime: new Date(), Currency: feedHistory.Currency, AdjustBuyPercentage: adjustmentPercentage.adjustBuyPercentage, AdjustSellPercentage: adjustmentPercentage.adjustSellPercentage, TomeiBuyPrice: tomeiBuyPrice, TomeiSellPrice: tomeiSellPrice, AdjustedById: loginUser ? loginUser.ObjectId : adjustmentBy, AdjustedDateTime: adjustmentDateTime, IsManualPriceActivatedYN: IsManualPriceActivatedYN, BuyLabourCharges: adjustmentPercentage.buyLabourCharges, SellLabourCharges: adjustmentPercentage.sellLabourCharges, }; const tomeiPriceHistory = yield TomeiPriceHistory._Repo.create(payload, { transaction: dbTransaction, }); if (tomeiPriceHistory) { return new TomeiPriceHistory(tomeiPriceHistory); } else { throw new general_1.ClassError('TomeiPriceHistory', 'TomeiPriceHistoryErrMsg01', 'TomeiPriceHistory To Save TomeiPrice'); } } catch (error) { 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 tomeiPrice = yield TomeiPriceHistory._Repo.findOne(options); if (tomeiPrice) { return tomeiPrice; } else { return null; } } catch (error) { 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']], include: [ { model: price_feed_history_entity_1.default, }, ], }; if (search) { queryObj.DateTime = { [sequelize_1.Op.between]: [search.DateTime.startDate, search.DateTime.endDate], }; } return yield TomeiPriceHistory._Repo.findAllWithPagination(Object.assign({ where: queryObj }, options)); } catch (error) { throw error; } }); } } exports.TomeiPriceHistory = TomeiPriceHistory; TomeiPriceHistory._Repo = new tomei_price_repository_1.TomeiPriceHistoryRepository(); //# sourceMappingURL=tomei-price.js.map