UNPKG

@crowdin/app-project-module

Version:

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

75 lines (74 loc) 3.8 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_uniqby_1 = __importDefault(require("lodash.uniqby")); const util_1 = require("../../../util"); 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"); function checkAutoSyncSettings(integration, appSettings, provider) { var _a; return !!(!integration.webhooks && ((_a = integration.syncNewElements) === null || _a === void 0 ? void 0 : _a[provider]) && appSettings[`new-${provider}-files`]); } function handle(config, integration) { return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () { const { files, provider, expandIntegrationFolders } = req.body; yield (0, job_1.runAsJob)({ integrationId: req.crowdinContext.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, jobCallback: () => __awaiter(this, void 0, void 0, function* () { if (Array.isArray(expandIntegrationFolders) && expandIntegrationFolders.length) { const allFiles = (yield (0, files_1.expandFilesTree)(expandIntegrationFolders, req, integration)).map((node) => ({ id: node.id, name: node.name, // eslint-disable-next-line @typescript-eslint/camelcase node_type: node.nodeType, // eslint-disable-next-line @typescript-eslint/camelcase parent_id: node.parentId, schedule: true, sync: false, type: node.type, })); yield (0, cron_1.createOrUpdateSyncSettings)({ req, files: (0, lodash_uniqby_1.default)([...files, ...allFiles], 'id'), provider, }); } else { yield (0, cron_1.createOrUpdateSyncSettings)({ req, files, provider, }); } const appSettings = req.integrationSettings || {}; if (checkAutoSyncSettings(integration, appSettings, provider)) { yield (0, snapshot_1.createOrUpdateFileSnapshot)(config, integration, req, provider); } }), }); })); } exports.default = handle;