@tomei/live-price
Version:
Tomei live-price Package
136 lines • 6.02 kB
JavaScript
;
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 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");
class Feed {
constructor(feedAttr) {
if (feedAttr) {
this.FeedName = feedAttr.FeedName;
this.IsSourcePriceYN = feedAttr.IsSourcePriceYN;
this.IsManualPriceActivatedYN = feedAttr.IsManualPriceActivatedYN;
this.CurrentManualPriceHistoryId = feedAttr.CurrentManualPriceHistoryId;
}
}
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 {
throw new general_1.ClassError('Feed', 'FeedErrMsg00', 'Feed Not Found');
}
}
return new Feed();
}
catch (error) {
throw new general_1.ClassError('Feed', 'FeedErrMsg01', error.message);
}
});
}
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,
};
if (feed) {
yield Feed._Repo.update(payload, {
where: {
FeedName: this.FeedName,
},
transaction: dbTransaction,
});
}
else {
yield Feed._Repo.create(payload, { transaction: dbTransaction });
}
}
catch (error) {
throw new general_1.ClassError('Feed', 'FeedErrMsg02', error.message);
}
});
}
setManualPrice(activatedById, buyPrice, sellPrice, dbTransaction) {
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, dbTransaction);
this.CurrentManualPriceHistoryId =
feedManualPriceHistory.FeedManualPriceHistoryId;
this.IsManualPriceActivatedYN = 'Y';
yield this.save(dbTransaction);
}
catch (error) {
throw error;
}
});
}
deactivateManualPrice(dbTransaction, deactivatedById) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield feed_manual_price_history_1.FeedManualPriceHistory.deactivate(this.FeedName, deactivatedById, dbTransaction);
this.IsManualPriceActivatedYN = 'N';
yield this.save(dbTransaction);
}
catch (error) {
throw error;
}
});
}
createTomeiPriceHistory(feedHistory, dbTransaction) {
return __awaiter(this, void 0, void 0, function* () {
try {
const feedMap = {
[feed_enum_1.SourceFeedName.XAUMYR]: [
feed_enum_1.NonSourceFeedName.GOLD1000G,
feed_enum_1.NonSourceFeedName.GOLD500G,
],
[feed_enum_1.SourceFeedName.XAGMYR]: [
feed_enum_1.NonSourceFeedName.SILVER1000G,
feed_enum_1.NonSourceFeedName.SILVER10OZ,
],
};
const nonSourceFeeds = feedMap[this.FeedName];
if (nonSourceFeeds && nonSourceFeeds.length > 0) {
for (const nonSourceFeed of nonSourceFeeds) {
const tomeiPriceHistory = yield tomei_price_1.TomeiPriceHistory.init();
yield tomeiPriceHistory.saveTomeiPrice(nonSourceFeed, feedHistory, undefined, undefined, undefined, dbTransaction);
}
}
}
catch (error) {
throw error;
}
});
}
}
exports.Feed = Feed;
Feed._Repo = new feed_repository_1.FeedRepository();
//# sourceMappingURL=feed.js.map