UNPKG

@crowdin/app-project-module

Version:

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

169 lines (168 loc) 9.5 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 logger_1 = require("../../../util/logger"); const job_1 = require("../util/job"); const files_1 = require("../util/files"); const logger_2 = require("../../../util/logger"); const types_2 = require("../types"); 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; const projectId = req.crowdinContext.jwtPayload.context.project_id || (req === null || req === void 0 ? void 0 : req.body.projectId); const uploadTranslations = req.query.uploadTranslations === 'true' || ((_a = req.body) === null || _a === void 0 ? void 0 : _a.uploadTranslations); const forcePushSources = req.query.forcePushSources === 'true' || ((_b = req.body) === null || _b === void 0 ? void 0 : _b.forcePushSources); const matchFromCrowdin = req.query.matchFromCrowdin === 'true'; req.logInfo(`Updating crowdin project ${projectId}`); const rootFolder = yield (0, defaults_1.getRootFolder)(config, integration, req.crowdinApiClient, projectId); if (rootFolder) { req.logInfo(`Updating crowdin files under 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 (((_c = config.api) === null || _c === void 0 ? void 0 : _c.default) && req.body.files) { req.body = req.body.files; } let payload = req.body; const matchingWarnings = []; // If matchFromCrowdin is true, we need to match Crowdin files to integration files first if (matchFromCrowdin && integration.matchCrowdinFilesToIntegrationFiles) { const crowdinFiles = req.body; const syncedData = yield (0, storage_1.getStorage)().getSyncedData(req.crowdinContext.clientId, req.crowdinContext.crowdinId, types_2.Provider.INTEGRATION); // Let critical errors bubble up naturally const matchingResult = yield integration.matchCrowdinFilesToIntegrationFiles({ projectId, client: req.crowdinApiClient, credentials: req.integrationCredentials, crowdinFiles, settings: req.integrationSettings, rootFolder, syncedData: (syncedData === null || syncedData === void 0 ? void 0 : syncedData.files) ? JSON.parse(syncedData.files) : [], }); if ((0, files_1.isMatchingResultType)(matchingResult)) { payload = matchingResult.data; if (matchingResult.errors && matchingResult.errors.length > 0) { matchingWarnings.push(...matchingResult.errors); req.logInfo(`File matching completed with ${matchingResult.errors.length} warning(s). ${Array.isArray(payload) ? payload.length : Object.keys(payload).length} file(s) matched successfully and will be synced.`); } } else { payload = matchingResult; } } const excludedTargetLanguages = yield (0, files_1.getExcludedTargetLanguages)({ client: req.crowdinApiClient, projectId, languages: ((_d = req.query.languages) === null || _d === void 0 ? void 0 : _d.split(',')) || [], }); yield (0, job_1.runAsJob)({ integrationId: req.crowdinContext.clientId, crowdinId: req.crowdinContext.crowdinId, type: types_1.JobType.UPDATE_TO_CROWDIN, title: 'Sync files to Crowdin', payload, res, projectId, client: req.crowdinApiClient, 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* () { if (payload && (payload === null || payload === void 0 ? void 0 : payload.length)) { payload = yield (0, files_1.expandFilesTree)(payload, req, integration, job); payload = (0, util_1.uniqBy)(payload, 'id'); } if (integration.forcePushSources === true && !forcePushSources && (payload === null || payload === void 0 ? void 0 : payload.length)) { payload = yield (0, files_1.filterSyncedData)(payload); } const result = yield integration.updateCrowdin({ projectId, client: req.crowdinApiClient, credentials: req.integrationCredentials, request: payload, rootFolder, settings: req.integrationSettings, uploadTranslations, job, excludedTargetLanguages: req.query.languages ? excludedTargetLanguages : undefined, }); const currentJob = yield job.get(); if (currentJob && (currentJob === null || currentJob === void 0 ? void 0 : currentJob.errors)) { const errors = JSON.parse(currentJob.errors); yield (0, logger_1.handleUserError)({ action: 'Sync files to Crowdin', error: errors, crowdinId: req.crowdinContext.crowdinId, clientId: req.crowdinContext.clientId, }); throw errors.toString(); } try { let syncedDataPayload = payload; if (currentJob === null || currentJob === void 0 ? void 0 : currentJob.processedEntities) { syncedDataPayload = JSON.parse(currentJob.processedEntities); } yield (0, files_1.updateSyncedData)(req.crowdinContext.clientId, req.crowdinContext.crowdinId, syncedDataPayload, types_2.Provider.INTEGRATION); } catch (e) { (0, logger_1.logError)(e, req.crowdinContext); } 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, job) => __awaiter(this, void 0, void 0, function* () { const isPartialSuccess = (e === null || e === void 0 ? void 0 : e.isPartialSuccess) === true; if (!isPartialSuccess) { try { const currentJob = yield job.get(); if (currentJob === null || currentJob === void 0 ? void 0 : currentJob.processedEntities) { const processedEntities = JSON.parse(currentJob.processedEntities); yield (0, files_1.updateSyncedData)(req.crowdinContext.clientId, req.crowdinContext.crowdinId, processedEntities, types_2.Provider.INTEGRATION); } } catch (syncError) { (0, logger_1.logError)(syncError, req.crowdinContext); } } 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 Crowdin', error: errorToLog, crowdinId: req.crowdinContext.crowdinId, clientId: req.crowdinContext.clientId, }); throw errorToLog; }), }); })); }