UNPKG

@sourceloop/ctrl-plane-subscription-service

Version:

Subscription management microservice for SaaS control plane.

91 lines 3.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PlanFeaturesService = void 0; const tslib_1 = require("tslib"); const core_1 = require("@loopback/core"); const repository_1 = require("@loopback/repository"); const feature_toggle_service_1 = require("@sourceloop/feature-toggle-service"); const repositories_1 = require("../repositories"); let PlanFeaturesService = class PlanFeaturesService { constructor(planRepository, featureValuesRepository, featureRepository) { this.planRepository = planRepository; this.featureValuesRepository = featureValuesRepository; this.featureRepository = featureRepository; } async planFeatures(id) { const plan = await this.planRepository.findById(id, { fields: ['id', 'name', 'tier', 'size'], }); const allFeatures = await this.featureRepository.find({ fields: [ 'id', 'key', 'name', 'description', 'type', 'defaultValue', 'metadata', ], }); const features = []; const featureValuesThePlanHas = await this.featureValuesRepository.find({ where: { strategyEntityId: id, }, fields: [ 'id', 'featureKey', 'value', 'status', 'strategyEntityId', 'strategyKey', ], }); for (const featDefinition of allFeatures) { features.push({ ...featDefinition, value: featureValuesThePlanHas.find(e => e.featureKey === featDefinition.id), }); } return { ...plan, features: features, }; } async updatePlanFeatures(id, featureValues) { const updatedFeatures = []; for (const featureValue of featureValues) { if (!featureValue.id) { throw new Error('Feature value ID is required for update'); } // Ensure the feature value belongs to the specified plan const existingFeatureValue = await this.featureValuesRepository.findOne({ where: { id: featureValue.id, strategyEntityId: id, strategyKey: 'Plan', }, }); if (!existingFeatureValue) { throw new Error(`Feature value with ID ${featureValue.id} not found for this plan`); } // Update the feature value await this.featureValuesRepository.updateById(featureValue.id, featureValue); // Fetch the updated feature value const updatedFeatureValue = await this.featureValuesRepository.findById(featureValue.id); updatedFeatures.push(updatedFeatureValue); } return updatedFeatures; } }; exports.PlanFeaturesService = PlanFeaturesService; exports.PlanFeaturesService = PlanFeaturesService = tslib_1.__decorate([ (0, core_1.injectable)({ scope: core_1.BindingScope.TRANSIENT }), tslib_1.__param(0, (0, repository_1.repository)(repositories_1.PlanRepository)), tslib_1.__param(1, (0, repository_1.repository)(feature_toggle_service_1.FeatureValuesRepository)), tslib_1.__param(2, (0, repository_1.repository)(feature_toggle_service_1.FeatureRepository)), tslib_1.__metadata("design:paramtypes", [repositories_1.PlanRepository, feature_toggle_service_1.FeatureValuesRepository, feature_toggle_service_1.FeatureRepository]) ], PlanFeaturesService); //# sourceMappingURL=plan-features-helper.service.js.map