UNPKG

@crowdin/app-project-module

Version:

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

132 lines (131 loc) 6.79 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.default = handle; const types_1 = require("../util/types"); const util_1 = require("../../../util"); const defaults_1 = require("../util/defaults"); const job_1 = require("../util/job"); const logger_1 = require("../../../util/logger"); const types_2 = require("../types"); const files_1 = require("../util/files"); const logger_2 = require("../../../util/logger"); const storage_1 = require("../../../storage"); function handle(config, integration) { return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; req.logInfo('Updating integration data'); const client = req.crowdinApiClient; const projectId = req.crowdinContext.jwtPayload.context.project_id; const forcePushTranslations = ((_a = req.query) === null || _a === void 0 ? void 0 : _a.forcePushTranslations) === 'true' || !!((_b = req.body) === null || _b === void 0 ? void 0 : _b.forcePushTranslations); const matchFromIntegration = ((_c = req.query) === null || _c === void 0 ? void 0 : _c.matchFromIntegration) === 'true'; const rootFolder = yield (0, defaults_1.getRootFolder)(config, integration, req.crowdinApiClient, projectId); if (rootFolder) { req.logInfo(`Updating integration data for crowding root folder ${rootFolder.id}`); } if (req.isApiCall && !req.body.files) { return res.status(400).json({ error: { message: 'Missing required parameter: files', }, }); } // A request via API has a different structure if (((_d = config.api) === null || _d === void 0 ? void 0 : _d.default) && req.body.files) { req.body = req.body.files; } let payload = req.body; const matchingWarnings = []; // If matchFromIntegration is true, we need to match integration files to Crowdin files first if (matchFromIntegration && integration.matchCrowdinFilesToIntegrationFiles) { const syncedData = yield (0, storage_1.getStorage)().getSyncedData(req.crowdinContext.clientId, req.crowdinContext.crowdinId, types_2.Provider.INTEGRATION); const result = yield integration.matchCrowdinFilesToIntegrationFiles({ projectId, client, credentials: req.integrationCredentials, integrationFiles: req.body, settings: req.integrationSettings, rootFolder, syncedData, }); if ((0, files_1.isMatchingResultType)(result)) { payload = result.data; if (result.errors && result.errors.length > 0) { matchingWarnings.push(...result.errors); req.logInfo(`File matching completed with ${result.errors.length} warning(s). ${Array.isArray(payload) ? payload.length : Object.keys(payload).length} file(s) matched successfully and will be synced.`); } } else { payload = result; } } if (integration.excludedTargetLanguages) { let options = {}; if (rootFolder) { options = { directoryId: rootFolder.id, recursion: 'true', }; } const files = (yield client.sourceFilesApi.withFetchAll().listProjectFiles(projectId, options)).data.map((d) => d.data); payload = (0, files_1.filterLanguages)(payload, files); } yield (0, job_1.runAsJob)({ integrationId: req.crowdinContext.clientId, crowdinId: req.crowdinContext.crowdinId, type: types_1.JobType.UPDATE_TO_INTEGRATION, title: 'Sync files to ' + config.name, payload, res, projectId, client, jobType: types_1.JobClientType.MANUAL, jobStoreType: integration.jobStoreType, initiatedBy: String(req.crowdinContext.jwtPayload.context.user_id), jobCallback: (job) => __awaiter(this, void 0, void 0, function* () { const result = yield integration.updateIntegration({ projectId, client, credentials: req.integrationCredentials, request: payload, rootFolder, settings: req.integrationSettings, job, }); if (matchingWarnings.length > 0) { req.logInfo(`Sync completed with warnings: ${matchingWarnings.length} file(s) could not be matched.`); const error = new logger_2.AppModuleAggregateError(matchingWarnings, 'Finished with errors. See error logs.'); error.isPartialSuccess = true; throw error; } let message; if ((0, files_1.isExtendedResultType)(result)) { message = result.message; } return { message }; }), onError: (e) => __awaiter(this, void 0, void 0, function* () { const isPartialSuccess = (e === null || e === void 0 ? void 0 : e.isPartialSuccess) === true; const errorToLog = !isPartialSuccess && matchingWarnings.length > 0 ? new logger_2.AppModuleAggregateError([e, ...matchingWarnings], 'Sync failed with multiple errors') : e; yield (0, logger_1.handleUserError)({ action: isPartialSuccess ? 'File matching warnings' : 'Sync files to External Service', error: errorToLog, crowdinId: req.crowdinContext.crowdinId, clientId: req.crowdinContext.clientId, }); throw errorToLog; }), forcePushTranslations, }); })); }