UNPKG

@crowdin/app-project-module

Version:

Module that generates for you all common endpoints for serving standalone Crowdin App

63 lines (62 loc) 3.65 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 }); const storage_1 = require("../storage"); const util_1 = require("../util"); const connection_1 = require("../util/connection"); const logger_1 = require("../util/logger"); const webhooks_1 = require("./integration/util/webhooks"); function handle(config) { return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () { const event = req.body; (0, logger_1.log)(`Received uninstall request ${JSON.stringify(event, null, 2)}`); const projectIntegration = config.projectIntegration; const organization = (event.domain || event.organizationId).toString(); const crowdinCredential = yield (0, storage_1.getStorage)().getCrowdinCredentials(organization); if ((crowdinCredential === null || crowdinCredential === void 0 ? void 0 : crowdinCredential.appSecret) !== event.appSecret) { return res.status(403).send({ error: 'Access denied' }); } if (config.onUninstall) { let allCredentials = []; if (projectIntegration) { const loadedCredentials = yield (0, storage_1.getStorage)().getAllIntegrationCredentials(organization); const allIntegrationConfigs = yield (0, storage_1.getStorage)().getAllIntegrationConfigs(organization); allCredentials = yield Promise.all(loadedCredentials.map((creds) => __awaiter(this, void 0, void 0, function* () { let settings; const integrationConfig = allIntegrationConfigs.find(({ integrationId }) => integrationId === creds.id); if (integrationConfig) { settings = JSON.parse(integrationConfig.config); } const credentials = yield (0, connection_1.prepareIntegrationCredentials)(config, projectIntegration, creds); return { settings, credentials }; }))); } (0, logger_1.log)('Invoking onUninstall hook'); yield config.onUninstall(organization, allCredentials); } if (projectIntegration) { if (projectIntegration.webhooks) { yield (0, webhooks_1.unregisterAllCrowdinWebhooks)({ config, integration: projectIntegration, crowdinId: organization, }); } (0, logger_1.log)(`Deleting all integration credentials for ${organization} organization`); yield (0, storage_1.getStorage)().deleteAllIntegrationCredentials(organization); } (0, logger_1.log)(`Deleting crowdin credentials for ${organization} organization`); yield (0, storage_1.getStorage)().deleteCrowdinCredentials(organization); (0, logger_1.log)('App has been uninstalled'); res.status(204).end(); })); } exports.default = handle;