@sourceloop/ctrl-plane-tenant-management-service
Version:
Tenant Management microservice for SaaS control plane
82 lines • 4.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProvisioningService = void 0;
const tslib_1 = require("tslib");
const repository_1 = require("@loopback/repository");
const crypto_helper_service_1 = require("./crypto-helper.service");
const core_1 = require("@loopback/core");
const repositories_1 = require("../repositories");
const rest_1 = require("@loopback/rest");
const core_2 = require("@sourceloop/core");
const enums_1 = require("../enums");
const keys_1 = require("../keys");
const event_connector_1 = require("./event-connector");
const crypto_1 = require("crypto");
/**
* Service for provisioning tenants.
*/
let ProvisioningService = class ProvisioningService {
/**
* Constructs a new instance of the ProvisioningService.
* @param cryptoHelperService - Service for cryptographic operations.
* @param eventConnector - Event Connector provided by consumer to invoke publish on.
* @param webhookSecretRepo - Repository for webhook secrets.
* @param logger - Logger service for logging messages.
*/
constructor(cryptoHelperService, eventConnector, webhookSecretRepo, tenantRepository, logger) {
this.cryptoHelperService = cryptoHelperService;
this.eventConnector = eventConnector;
this.webhookSecretRepo = webhookSecretRepo;
this.tenantRepository = tenantRepository;
this.logger = logger;
}
/**
* The `provisionTenant` function provisions a new tenant by generating a temporary token, retrieving
* the subscription details, generating a random HMAC secret, starting a build process, and storing the
* webhook secret and build context to later verify the webhook callback.
* @param {Tenant} tenant - The `tenant` parameter is an object that represents a tenant. It likely
* contains information such as the tenant's ID, name, and other relevant details.
* @param {string} subscription - The `subscription` parameter is an object that contains
* the details of the subscription opted by the tenant.
*/
async provisionTenant(tenant, subscription) {
var _a;
await this.tenantRepository.updateById(tenant.id, {
status: enums_1.TenantStatus.PROVISIONING,
});
if (!subscription.id) {
throw rest_1.HttpErrors.BadRequest('Subscription ID is required');
}
const hmacSecret = this.cryptoHelperService.generateRandomString(32);
if (((_a = subscription.plan) === null || _a === void 0 ? void 0 : _a.tier) === undefined) {
this.logger.error(`Tier not found in plan for subscription: ${subscription.id}`);
throw new rest_1.HttpErrors.InternalServerError();
}
const context = (0, crypto_1.randomUUID)();
await this.eventConnector.publish({
type: event_connector_1.EventTypes.TENANT_PROVISIONING,
tenant: tenant,
subscription: subscription,
secret: hmacSecret,
context: context,
});
await this.webhookSecretRepo.set(tenant.id, {
secret: hmacSecret,
context: context,
});
await this.webhookSecretRepo.expire(tenant.id, +process.env.WEBHOOK_SECRET_EXPIRY);
}
};
exports.ProvisioningService = ProvisioningService;
exports.ProvisioningService = ProvisioningService = tslib_1.__decorate([
tslib_1.__param(0, (0, core_1.service)(crypto_helper_service_1.CryptoHelperService)),
tslib_1.__param(1, (0, core_1.inject)(keys_1.EventConnectorBinding)),
tslib_1.__param(2, (0, repository_1.repository)(repositories_1.WebhookSecretRepository)),
tslib_1.__param(3, (0, repository_1.repository)(repositories_1.TenantRepository)),
tslib_1.__param(4, (0, core_1.inject)(core_2.LOGGER.LOGGER_INJECT)),
tslib_1.__metadata("design:paramtypes", [crypto_helper_service_1.CryptoHelperService,
event_connector_1.EventConnector,
repositories_1.WebhookSecretRepository,
repositories_1.TenantRepository, Object])
], ProvisioningService);
//# sourceMappingURL=provisioning.service.js.map