UNPKG

@tomei/live-price

Version:

Tomei live-price Package

168 lines 7.52 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.Feed = void 0; const general_1 = require("@tomei/general"); const log_1 = require("@tomei/log"); const feed_repository_1 = require("./feed.repository"); const feed_enum_1 = require("../../enum/feed.enum"); const feed_manual_price_history_1 = require("../feed-manual-price-history/feed-manual-price-history"); const tomei_price_1 = require("../tomei-price/tomei-price"); const company_feed_access_1 = require("../company-feed-access/company-feed-access"); class Feed { constructor(feedAttr) { if (feedAttr) { this.FeedName = feedAttr.FeedName; this.IsSourcePriceYN = feedAttr.IsSourcePriceYN; this.IsManualPriceActivatedYN = feedAttr.IsManualPriceActivatedYN; this.CurrentManualPriceHistoryId = feedAttr.CurrentManualPriceHistoryId; this.Weight = feedAttr.Weight; } } static init(dbTransaction, FeedName) { return __awaiter(this, void 0, void 0, function* () { try { if (FeedName) { const feed = yield Feed._Repo.findByPk(FeedName, dbTransaction); if (feed) { return new Feed(feed); } else { const error = new general_1.ClassError('Feed', 'FeedErrMsg00', 'Feed Not Found'); throw error; } } return new Feed(); } catch (error) { const classError = new general_1.ClassError('Feed', 'FeedErrMsg01', error.message); yield Feed.logger.error({ error, methodName: 'init', transaction: dbTransaction, }); throw classError; } }); } save(dbTransaction) { return __awaiter(this, void 0, void 0, function* () { try { const feed = yield Feed._Repo.findOne({ where: { FeedName: this.FeedName, }, transaction: dbTransaction, }); const payload = { FeedName: this.FeedName, IsSourcePriceYN: this.IsSourcePriceYN, IsManualPriceActivatedYN: this.IsManualPriceActivatedYN, CurrentManualPriceHistoryId: this.CurrentManualPriceHistoryId, Weight: this.Weight, }; if (feed) { yield Feed._Repo.update(payload, { where: { FeedName: this.FeedName, }, transaction: dbTransaction, }); } else { yield Feed._Repo.create(payload, { transaction: dbTransaction }); } } catch (error) { const classError = new general_1.ClassError('Feed', 'FeedErrMsg02', error.message); yield Feed.logger.error({ error, methodName: 'save', transaction: dbTransaction, }); throw classError; } }); } setManualPrice(activatedById, buyPrice, sellPrice, dbTransaction, companyCode) { return __awaiter(this, void 0, void 0, function* () { try { let feedManualPriceHistory = yield feed_manual_price_history_1.FeedManualPriceHistory.init(); feedManualPriceHistory.FeedName = this.FeedName; feedManualPriceHistory.ActivatedById = activatedById; feedManualPriceHistory = yield feedManualPriceHistory.create(this.FeedName, activatedById, buyPrice, sellPrice, companyCode, dbTransaction); this.CurrentManualPriceHistoryId = feedManualPriceHistory.FeedManualPriceHistoryId; this.IsManualPriceActivatedYN = 'Y'; yield this.save(dbTransaction); } catch (error) { yield Feed.logger.error({ error, methodName: 'setManualPrice', transaction: dbTransaction, }); throw error; } }); } deactivateManualPrice(dbTransaction, deactivatedById, companyCode) { return __awaiter(this, void 0, void 0, function* () { try { yield feed_manual_price_history_1.FeedManualPriceHistory.deactivate(this.FeedName, deactivatedById, companyCode, dbTransaction); this.IsManualPriceActivatedYN = 'N'; yield this.save(dbTransaction); } catch (error) { yield Feed.logger.error({ error, methodName: 'deactivateManualPrice', transaction: dbTransaction, }); throw error; } }); } createTomeiPriceHistory(feedHistory_1, dbTransaction_1) { return __awaiter(this, arguments, void 0, function* (feedHistory, dbTransaction, companyCode = 'CTH') { try { const accessibleFeeds = yield company_feed_access_1.CompanyFeedAccess.getAccessibleFeeds(dbTransaction, companyCode); const relevantFeeds = accessibleFeeds.filter((feedName) => { if (this.FeedName === feed_enum_1.SourceFeedName.XAUMYR) { return feedName.includes('GOLD'); } else if (this.FeedName === feed_enum_1.SourceFeedName.XAGMYR) { return feedName.includes('SILVER'); } return false; }); if (relevantFeeds && relevantFeeds.length > 0) { for (const nonSourceFeed of relevantFeeds) { const tomeiPriceHistory = yield tomei_price_1.TomeiPriceHistory.init(); yield tomeiPriceHistory.saveTomeiPrice(nonSourceFeed, feedHistory, undefined, undefined, undefined, companyCode, dbTransaction); } } } catch (error) { yield Feed.logger.error({ error, methodName: 'createTomeiPriceHistory', transaction: dbTransaction, }); throw error; } }); } } exports.Feed = Feed; Feed._Repo = new feed_repository_1.FeedRepository(); Feed.logger = new log_1.ApplicationLog('live-price', 'Feed'); //# sourceMappingURL=feed.js.map