@sourceloop/ctrl-plane-tenant-management-service
Version:
Tenant Management microservice for SaaS control plane
142 lines • 7.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProvisioningWebhookHandler = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@loopback/core");
const repository_1 = require("@loopback/repository");
const rest_1 = require("@loopback/rest");
const core_2 = require("@sourceloop/core");
const decorators_1 = require("../../decorators");
const enums_1 = require("../../enums");
const webhook_types_enum_1 = require("../../enums/webhook-types.enum");
const permissions_1 = require("../../permissions");
const repositories_1 = require("../../repositories");
const types_1 = require("../../types");
const crypto_helper_service_1 = require("../crypto-helper.service");
const notifications_1 = require("../notifications");
const keys_1 = require("../../keys");
const saas_tenant_repository_1 = require("../../repositories/saas-tenant.repository");
/**
* Handler for provisioning webhooks.
* This class implements the IWebhookHandler interface and defines the type of webhook it handles
* as `WebhookType.RESOURCES_PROVISIONED`. It provides functionality to handle a webhook payload
* for resource provisioning, updating the status of a tenant and creating resources associated with the tenant.
*/
let ProvisioningWebhookHandler = class ProvisioningWebhookHandler {
/**
* Constructs a new instance of the ProvisioningWebhookHandler.
* @param {ResourceRepository} resourceRepository - Repository for managing resources.
* @param {TenantRepository} tenantRepository - Repository for managing tenants.
* @param {NotificationService} notificationService - Service for sending notifications.
* @param {CryptoHelperService} cryptoHelperService - Service for cryptographic operations.
* @param {ILogger} logger - Logger service for logging messages.
*/
constructor(resourceRepository, tenantRepository, notificationService, postWebhookHandlerService, cryptoHelperService, logger) {
this.resourceRepository = resourceRepository;
this.tenantRepository = tenantRepository;
this.notificationService = notificationService;
this.postWebhookHandlerService = postWebhookHandlerService;
this.cryptoHelperService = cryptoHelperService;
this.logger = logger;
this.type = webhook_types_enum_1.WebhookType.RESOURCES_PROVISIONED;
}
/**
* This function handles a webhook payload for resource provisioning, updating the status of a tenant
* and creating resources associated with the tenant.
* @param {ResourceProvisionedWebhookPayload} payload - The payload parameter is of type
* ResourceProvisionedWebhookPayload.
*/
async handle(payload) {
var _a, _b;
const transaction = await this.resourceRepository.beginTransaction();
try {
const existing = await this.tenantRepository.findOne({
where: {
id: payload.initiatorId,
status: enums_1.TenantStatus.PROVISIONING,
},
}, {
transaction,
});
if (!existing) {
this.logger.error('Tenant not found or not in provisioning state');
throw new rest_1.HttpErrors.Unauthorized();
}
if (payload.data.status === types_1.WebhookStatus.SUCCESS) {
await this.tenantRepository.updateById(payload.initiatorId, {
status: enums_1.TenantStatus.ACTIVE,
}, { transaction });
if (payload.data.resources.length > 0) {
await this.resourceRepository.create({
tenantId: payload.initiatorId,
externalIdentifier: (_b = (_a = payload.data.resources[0]) === null || _a === void 0 ? void 0 : _a.metadata) === null || _b === void 0 ? void 0 : _b.bucket,
type: types_1.ResourceTypes.BUCKET,
metadata: payload.data.resources[0].metadata,
});
}
await this._sendEmail(transaction, payload);
}
else {
await this.tenantRepository.updateById(payload.initiatorId, {
status: enums_1.TenantStatus.PROVISIONFAILED,
}, { transaction });
}
await this.postWebhookHandlerService.postWebhookHandler(payload);
await transaction.commit();
}
catch (e) {
await transaction.rollback();
this.logger.error(e);
throw e;
}
}
async _sendEmail(transaction, payload) {
var _a, _b, _c, _d;
const tenant = await this.tenantRepository.findById(payload.initiatorId, {
include: [
{
relation: 'contacts',
scope: {
where: {
isPrimary: true,
},
fields: ['email', 'firstName', 'lastName', 'tenantId', 'deleted'],
order: ['createdOn DESC'],
},
},
],
}, {
transaction,
});
const email = (_b = (_a = tenant.contacts) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.email;
const name = (_d = (_c = tenant.contacts) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.firstName;
if (!email) {
this.logger.error(`No email found to notify tenant: ${tenant.id}`);
}
else {
const tempToken = this.cryptoHelperService.generateTempTokenForTenant(tenant, [
permissions_1.PermissionKey.CreateNotification,
permissions_1.PermissionKey.ViewNotificationTemplate,
]);
await this.notificationService.send(email, enums_1.NotificationType.WelcomeTenant, {
name: tenant.name,
user: name,
link: payload.data.appPlaneUrl,
}, tempToken);
}
}
};
exports.ProvisioningWebhookHandler = ProvisioningWebhookHandler;
exports.ProvisioningWebhookHandler = ProvisioningWebhookHandler = tslib_1.__decorate([
(0, decorators_1.webhookHandler)(),
tslib_1.__param(0, (0, repository_1.repository)(repositories_1.ResourceRepository)),
tslib_1.__param(1, (0, repository_1.repository)(saas_tenant_repository_1.SaasTenantRepository)),
tslib_1.__param(2, (0, core_1.inject)('services.NotificationService')),
tslib_1.__param(3, (0, core_1.inject)(keys_1.PostWebhookHandlerServiceKey)),
tslib_1.__param(4, (0, core_1.service)(crypto_helper_service_1.CryptoHelperService)),
tslib_1.__param(5, (0, core_1.inject)(core_2.LOGGER.LOGGER_INJECT)),
tslib_1.__metadata("design:paramtypes", [repositories_1.ResourceRepository,
repositories_1.TenantRepository,
notifications_1.NotificationService, Object, crypto_helper_service_1.CryptoHelperService, Object])
], ProvisioningWebhookHandler);
//# sourceMappingURL=provisioning-webhook.handler.js.map