UNPKG

@tomei/live-price

Version:

Tomei live-price Package

208 lines 9.02 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.CompanyFeedAccess = void 0; const general_1 = require("@tomei/general"); const company_feed_access_repository_1 = require("./company-feed-access.repository"); const cuid2_1 = require("@paralleldrive/cuid2"); class CompanyFeedAccess { constructor(accessAttr) { if (accessAttr) { this.CompanyAccessId = accessAttr.CompanyAccessId; this.CompanyCode = accessAttr.CompanyCode; this.FeedName = accessAttr.FeedName; this.Status = accessAttr.Status; this.PricingLogic = accessAttr.PricingLogic; this.CreatedById = accessAttr.CreatedById; this.CreatedAt = accessAttr.CreatedAt; this.UpdatedById = accessAttr.UpdatedById; this.UpdatedAt = accessAttr.UpdatedAt; } } static init(dbTransaction, CompanyAccessId) { return __awaiter(this, void 0, void 0, function* () { try { if (CompanyAccessId) { const access = yield CompanyFeedAccess._Repo.findByPk(CompanyAccessId, dbTransaction); if (access) { return new CompanyFeedAccess(access); } else { throw new general_1.ClassError('CompanyFeedAccess', 'CompanyFeedAccessErrMsg00', 'Company Feed Access Not Found'); } } return new CompanyFeedAccess(); } catch (error) { throw new general_1.ClassError('CompanyFeedAccess', 'CompanyFeedAccessErrMsg01', error.message); } }); } save(dbTransaction) { return __awaiter(this, void 0, void 0, function* () { try { const existingAccess = yield CompanyFeedAccess._Repo.findOne({ where: { CompanyCode: this.CompanyCode, FeedName: this.FeedName, }, transaction: dbTransaction, }); const payload = { CompanyCode: this.CompanyCode, FeedName: this.FeedName, Status: this.Status, PricingLogic: this.PricingLogic, CreatedById: this.CreatedById, UpdatedById: this.UpdatedById, }; if (existingAccess) { payload.CompanyAccessId = existingAccess.CompanyAccessId; const affectedRows = yield CompanyFeedAccess._Repo.update(payload, { where: { CompanyAccessId: existingAccess.CompanyAccessId }, transaction: dbTransaction, }); if (affectedRows[0] === 0) { throw new Error('Failed to update company feed access'); } this.CompanyAccessId = existingAccess.CompanyAccessId; } else { payload.CompanyAccessId = (0, cuid2_1.createId)(); const newAccess = yield CompanyFeedAccess._Repo.create(payload, { transaction: dbTransaction, }); this.CompanyAccessId = newAccess.CompanyAccessId; this.CreatedAt = newAccess.CreatedAt; this.UpdatedAt = newAccess.UpdatedAt; } } catch (error) { throw new general_1.ClassError('CompanyFeedAccess', 'CompanyFeedAccessErrMsg02', error.message); } }); } static hasAccessToFeed(dbTransaction, feedName, companyCode) { return __awaiter(this, void 0, void 0, function* () { try { const accessRules = yield CompanyFeedAccess._Repo.findAll({ where: { FeedName: feedName, Status: 'Active', }, transaction: dbTransaction, }); if (accessRules.length === 0) { return true; } const hasAccess = accessRules.some((rule) => rule.CompanyCode === companyCode); return hasAccess; } catch (error) { return true; } }); } static getAccessibleFeeds(dbTransaction, companyCode) { return __awaiter(this, void 0, void 0, function* () { try { const accessRules = yield CompanyFeedAccess._Repo.findAll({ where: { CompanyCode: companyCode, Status: 'Active', }, attributes: ['FeedName'], transaction: dbTransaction, }); return accessRules.map((rule) => rule.FeedName); } catch (error) { return []; } }); } static validateFeedAccess(dbTransaction, feedName, companyCode) { return __awaiter(this, void 0, void 0, function* () { const hasAccess = yield CompanyFeedAccess.hasAccessToFeed(dbTransaction, feedName, companyCode); if (!hasAccess) { const allowedCompanies = yield CompanyFeedAccess.getCompaniesWithAccessToFeed(dbTransaction, feedName); throw new Error(`Access denied: Company '${companyCode}' does not have access to feed '${feedName}'. ` + `This feed is restricted to: ${allowedCompanies.join(', ')}`); } }); } static getCompaniesWithAccessToFeed(dbTransaction, feedName) { return __awaiter(this, void 0, void 0, function* () { try { const accessRules = yield CompanyFeedAccess._Repo.findAll({ where: { FeedName: feedName, Status: 'Active', }, attributes: ['CompanyCode'], transaction: dbTransaction, }); return accessRules.map((rule) => rule.CompanyCode); } catch (error) { return []; } }); } static getAllFeedAccessConfiguration(dbTransaction) { return __awaiter(this, void 0, void 0, function* () { try { const accessRules = yield CompanyFeedAccess._Repo.findAll({ where: { Status: 'Active', }, attributes: ['FeedName', 'CompanyCode'], transaction: dbTransaction, }); const feedAccess = {}; for (const rule of accessRules) { if (!feedAccess[rule.FeedName]) { feedAccess[rule.FeedName] = []; } feedAccess[rule.FeedName].push(rule.CompanyCode); } return feedAccess; } catch (error) { return {}; } }); } static getPricingLogic(dbTransaction, feedName, companyCode) { return __awaiter(this, void 0, void 0, function* () { try { const accessRule = yield CompanyFeedAccess._Repo.findOne({ where: { FeedName: feedName, CompanyCode: companyCode, Status: 'Active', }, transaction: dbTransaction, }); if (!accessRule) { throw new Error(`No pricing logic configuration found for company '${companyCode}' and feed '${feedName}'`); } return accessRule.PricingLogic; } catch (error) { throw new Error(`Failed to retrieve pricing logic: ${error.message}`); } }); } } exports.CompanyFeedAccess = CompanyFeedAccess; CompanyFeedAccess._Repo = new company_feed_access_repository_1.CompanyFeedAccessRepository(); //# sourceMappingURL=company-feed-access.js.map