UNPKG

@crowdin/app-project-module

Version:

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

104 lines (103 loc) 7.22 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 util_1 = require("../../../util"); const logger_1 = require("../../../util/logger"); const files_1 = require("../util/files"); const types_1 = require("../types"); const storage_1 = require("../../../storage"); function handle(integration) { return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; const { parent_id: parentId, search, page, paginationData } = req.query; req.logInfo('Received request to get integration data'); let message; let stopPagination; let files = []; const { crowdinId, clientId } = req.crowdinContext; try { const appSettings = req.integrationSettings; const parsedPaginationData = paginationData ? JSON.parse(paginationData) : undefined; const result = yield integration.getIntegrationFiles(req.integrationCredentials, appSettings, parentId, search, page, parsedPaginationData); if ((0, files_1.isExtendedResultType)(result)) { files = result.data || []; message = result.message; stopPagination = result.stopPagination; } else { files = result || []; } if ((appSettings === null || appSettings === void 0 ? void 0 : appSettings.skipIntegrationNodesToggle) === true || ((appSettings === null || appSettings === void 0 ? void 0 : appSettings.skipIntegrationNodesToggle) === undefined && ((_a = integration.skipIntegrationNodesToggle) === null || _a === void 0 ? void 0 : _a.value))) { files = (0, files_1.skipFilesByRegex)(files, integration.skipIntegrationNodes); } if (integration.filterByPathIntegrationFiles) { const includePatterns = (_b = appSettings === null || appSettings === void 0 ? void 0 : appSettings.includeByFilePath) === null || _b === void 0 ? void 0 : _b.split('\n').filter(Boolean); const excludePatterns = (_c = appSettings === null || appSettings === void 0 ? void 0 : appSettings.excludeByFilePath) === null || _c === void 0 ? void 0 : _c.split('\n').filter(Boolean); files = (0, files_1.filterFilesByPath)(files, includePatterns, excludePatterns); } const needsFileStatus = ((_e = (_d = integration.filtering) === null || _d === void 0 ? void 0 : _d.integrationFileStatus) === null || _e === void 0 ? void 0 : _e.isNew) || ((_g = (_f = integration.filtering) === null || _f === void 0 ? void 0 : _f.integrationFileStatus) === null || _g === void 0 ? void 0 : _g.isUpdated) || ((_j = (_h = integration.filtering) === null || _h === void 0 ? void 0 : _h.integrationFileStatus) === null || _j === void 0 ? void 0 : _j.notSynced) || ((_l = (_k = integration.filtering) === null || _k === void 0 ? void 0 : _k.integrationFileStatus) === null || _l === void 0 ? void 0 : _l.synced) || integration.forcePushSources === true; if (needsFileStatus) { const syncedData = yield (0, storage_1.getStorage)().getSyncedData(clientId, crowdinId, types_1.Provider.INTEGRATION); const syncedFiles = (syncedData === null || syncedData === void 0 ? void 0 : syncedData.files) ? JSON.parse(syncedData.files) : []; const lastSyncTimestamp = (syncedData === null || syncedData === void 0 ? void 0 : syncedData.updatedAt) ? Number(syncedData.updatedAt) : null; files = files.map((file) => { var _a, _b, _c, _d, _e, _f, _g, _h; if (!file.type) { return file; } const syncedFile = syncedFiles.find((syncedItem) => syncedItem.id === file.id); const notSynced = ((_b = (_a = integration.filtering) === null || _a === void 0 ? void 0 : _a.integrationFileStatus) === null || _b === void 0 ? void 0 : _b.notSynced) === true ? !syncedFile : false; const synced = ((_d = (_c = integration.filtering) === null || _c === void 0 ? void 0 : _c.integrationFileStatus) === null || _d === void 0 ? void 0 : _d.synced) === true ? !!syncedFile : false; const fileSyncTimestamp = (syncedFile === null || syncedFile === void 0 ? void 0 : syncedFile.syncedAt) ? Number(syncedFile.syncedAt) : lastSyncTimestamp; const isNew = ((_f = (_e = integration.filtering) === null || _e === void 0 ? void 0 : _e.integrationFileStatus) === null || _f === void 0 ? void 0 : _f.isNew) === true ? !syncedFile && fileSyncTimestamp && file.createdAt && (typeof file.createdAt === 'string' ? new Date(file.createdAt).getTime() : file.createdAt) > fileSyncTimestamp : false; const shouldCalculateIsUpdated = ((_h = (_g = integration.filtering) === null || _g === void 0 ? void 0 : _g.integrationFileStatus) === null || _h === void 0 ? void 0 : _h.isUpdated) === true || integration.forcePushSources === true; const isUpdated = shouldCalculateIsUpdated ? syncedFile && fileSyncTimestamp && file.updatedAt && (typeof file.updatedAt === 'string' ? new Date(file.updatedAt).getTime() : file.updatedAt) > fileSyncTimestamp : false; return Object.assign(Object.assign({}, file), { isNew, notSynced, isUpdated, synced }); }); } } catch (e) { yield (0, logger_1.handleUserError)({ action: 'Get External Service data', error: e, crowdinId, clientId, }); res.send({ data: [], message: (e === null || e === void 0 ? void 0 : e.message) || e }); throw e; } req.logInfo(`Integration data response ${JSON.stringify(files, null, 2)}`); res.send({ data: files, message, stopPagination }); })); } exports.default = handle;