UNPKG

@crowdin/app-project-module

Version:

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

149 lines (148 loc) 7.96 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 util_1 = require("../../../util"); const storage_1 = require("../../../storage"); const cron_1 = require("../util/cron"); const snapshot_1 = require("../util/snapshot"); const files_1 = require("../util/files"); const job_1 = require("../util/job"); const types_1 = require("../util/types"); const types_2 = require("../types"); const SUPPORTED_SCHEDULE_VALUES = new Set(['0', '1', '3', '6', '12', '24']); function isScheduleDisabled(schedule) { return schedule === undefined || schedule === null || String(schedule) === '0'; } function getAutoEnabledSchedule(integration) { var _a; const configuredValue = String((_a = integration.autoEnableScheduleOnAutoSync) !== null && _a !== void 0 ? _a : '12'); return SUPPORTED_SCHEDULE_VALUES.has(configuredValue) ? configuredValue : '12'; } function getScheduleLabel(schedule) { return schedule === '1' ? '1 hour' : `${schedule} hours`; } function handle(config, integration) { return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () { var _a; const { files, provider, expandIntegrationFolders, hasLoadMoreElements, folderIds } = req.body; const clientId = req.crowdinContext.clientId; const autoEnabledSchedule = getAutoEnabledSchedule(integration); let scheduleAutoEnabled = false; const scheduleMessage = `Auto Sync requires a sync schedule. It has been automatically set to every ${getScheduleLabel(autoEnabledSchedule)}. You can adjust this in the app settings.`; if (req.isApiCall) { if (!files || !provider) { return res.status(400).json({ error: { message: `Missing required parameter: ${!files ? 'files' : 'provider'}`, }, }); } if (!Object.values(types_2.Provider).includes(provider)) { return res.status(400).json({ error: { message: `Invalid provider. Must be one of: ${Object.values(types_2.Provider).join(', ')}`, }, }); } } if (autoEnabledSchedule !== '0' && isScheduleDisabled((_a = req.integrationSettings) === null || _a === void 0 ? void 0 : _a.schedule)) { const integrationConfig = yield (0, storage_1.getStorage)().getIntegrationConfig(clientId); const currentSettings = (integrationConfig === null || integrationConfig === void 0 ? void 0 : integrationConfig.config) ? JSON.parse(integrationConfig.config) : req.integrationSettings || {}; const nextSettings = Object.assign(Object.assign({}, currentSettings), { schedule: autoEnabledSchedule }); if (!integrationConfig) { yield (0, storage_1.getStorage)().saveIntegrationConfig(clientId, req.crowdinContext.crowdinId, JSON.stringify(nextSettings)); } else { yield (0, storage_1.getStorage)().updateIntegrationConfig(clientId, JSON.stringify(nextSettings)); } req.integrationSettings = nextSettings; scheduleAutoEnabled = true; } yield (0, job_1.runAsJob)({ integrationId: clientId, crowdinId: req.crowdinContext.crowdinId, type: `${provider}SyncSettingsSave`, title: 'Save sync settings', payload: req.body, res, projectId: req.crowdinContext.jwtPayload.context.project_id, 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* () { var _a, _b, _c, _d, _e; if (scheduleAutoEnabled) { const { isCanceled } = yield job.update({ info: scheduleMessage }); if (isCanceled) { return; } } if (types_1.JobStatus.CANCELED === ((_a = (yield job.get())) === null || _a === void 0 ? void 0 : _a.status)) { return; } if (Array.isArray(expandIntegrationFolders) && expandIntegrationFolders.length) { const expandedFiles = hasLoadMoreElements ? yield (0, files_1.expandFilesTreeWithPagination)(expandIntegrationFolders, req, integration, job) : yield (0, files_1.expandFilesTree)(expandIntegrationFolders, req, integration, job); const allFiles = expandedFiles.map((node) => ({ id: node.id, name: node.name, node_type: node.nodeType, parent_id: node.parentId, schedule: true, sync: false, type: node.type, })); req.logInfo(`Expanded ${allFiles.length} files for auto-sync`); if (types_1.JobStatus.CANCELED === ((_b = (yield job.get())) === null || _b === void 0 ? void 0 : _b.status)) { return; } yield (0, cron_1.createOrUpdateSyncSettings)({ req, files: (0, util_1.uniqBy)([...files, ...allFiles], 'id'), provider, }); } else { if (types_1.JobStatus.CANCELED === ((_c = (yield job.get())) === null || _c === void 0 ? void 0 : _c.status)) { return; } yield (0, cron_1.createOrUpdateSyncSettings)({ req, files: (0, files_1.prepareSyncFiles)(files), provider, }); } const settings = req.integrationSettings || {}; const syncFiles = (0, files_1.prepareSyncFiles)(files); const hasSyncedFolders = provider === types_2.Provider.INTEGRATION ? (0, files_1.hasFolders)(syncFiles) : Array.isArray(folderIds) && folderIds.length > 0; const needsSnapshot = !integration.webhooks && ((_d = integration.syncNewElements) === null || _d === void 0 ? void 0 : _d[provider]) && (settings[`new-${provider}-files`] || hasSyncedFolders); if (needsSnapshot) { if (types_1.JobStatus.CANCELED === ((_e = (yield job.get())) === null || _e === void 0 ? void 0 : _e.status)) { return; } yield (0, snapshot_1.createOrUpdateFileSnapshot)(config, integration, req, provider, job); } if (scheduleAutoEnabled) { yield job.update({ info: scheduleMessage }); } }), }); })); }