From edf307db27a81b2aece406eac1ebf0982091711f Mon Sep 17 00:00:00 2001 From: Evgenij Shangin Date: Mon, 10 Jun 2024 15:45:00 +0300 Subject: [PATCH] Macrotasks decode, encode ids (#126) * Macrotasks decode, encode ids * add deprecated text * install @types/lodash * avoid setImemdiate for first chunk * change @types/lodash version * install lodash --- api/components.ts | 5 +- package-lock.json | 8 +++ package.json | 2 + src/components/middlewares/decode-id.ts | 10 +++- src/components/response-presenter.ts | 29 ++++++++++ src/controllers/collections.ts | 20 +++---- src/controllers/color-palettes.ts | 10 ++-- src/controllers/entries.ts | 30 +++++----- src/controllers/favorites.ts | 10 ++-- src/controllers/locks.ts | 10 ++-- src/controllers/states.ts | 6 +- src/controllers/workbooks.ts | 28 +++++----- .../entry/actions/copy-to-workbook.ts | 2 +- .../new/collection/delete-collection.ts | 4 +- .../new/collection/move-collections-list.ts | 12 ++-- .../new/entry/copy-entries-to-workbook.ts | 2 +- .../new/workbook/get-workbooks-list-by-ids.ts | 6 +- .../new/workbook/move-workbooks-list.ts | 12 ++-- src/utils/utils.ts | 56 +++++++++++++++++++ 19 files changed, 188 insertions(+), 74 deletions(-) diff --git a/api/components.ts b/api/components.ts index 623699cc..b6d068d9 100644 --- a/api/components.ts +++ b/api/components.ts @@ -8,7 +8,10 @@ export { export {OrganizationPermission, ProjectPermission} from '../src/components/iam'; -export {default as prepareResponse} from '../src/components/response-presenter'; +export { + default as prepareResponse, + prepareResponseAsync, +} from '../src/components/response-presenter'; export {default as prepareErrorResponse} from '../src/components/error-response-presenter'; export {isEnabledFeature, Feature} from '../src/components/features'; diff --git a/package-lock.json b/package-lock.json index a5e79afc..17d0c61f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ "axios-retry": "^3.1.9", "crc-32": "1.2.0", "dotenv": "^8.2.0", + "lodash": "^4.17.21", "minimist": "^1.2.5", "moment": "^2.29.4", "pg-error-constants": "^1.0.0", @@ -32,6 +33,7 @@ "@trendyol/jest-testcontainers": "^2.1.1", "@types/express": "^4.17.15", "@types/jest": "^29.5.12", + "@types/lodash": "^4.17.4", "@types/node": "^18.14.4", "@types/pg": "^7.14.11", "@types/supertest": "^2.0.10", @@ -2119,6 +2121,12 @@ "integrity": "sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==", "dev": true }, + "node_modules/@types/lodash": { + "version": "4.17.4", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.4.tgz", + "integrity": "sha512-wYCP26ZLxaT3R39kiN2+HcJ4kTd3U1waI/cY7ivWYqFP6pW3ZNpvi6Wd6PHZx7T/t8z0vlkXMg3QYLa7DZ/IJQ==", + "dev": true + }, "node_modules/@types/mime": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz", diff --git a/package.json b/package.json index ef429888..9ec6ad18 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "axios-retry": "^3.1.9", "crc-32": "1.2.0", "dotenv": "^8.2.0", + "lodash": "^4.17.21", "minimist": "^1.2.5", "moment": "^2.29.4", "pg-error-constants": "^1.0.0", @@ -34,6 +35,7 @@ "@trendyol/jest-testcontainers": "^2.1.1", "@types/express": "^4.17.15", "@types/jest": "^29.5.12", + "@types/lodash": "^4.17.4", "@types/node": "^18.14.4", "@types/pg": "^7.14.11", "@types/supertest": "^2.0.10", diff --git a/src/components/middlewares/decode-id.ts b/src/components/middlewares/decode-id.ts index f448255a..abdcd953 100644 --- a/src/components/middlewares/decode-id.ts +++ b/src/components/middlewares/decode-id.ts @@ -3,7 +3,7 @@ import {Request, Response, NextFunction} from '@gravity-ui/expresskit'; import {AppError} from '@gravity-ui/nodekit'; import US_ERRORS from '../../const/us-error-constants'; -export const decodeId = (req: Request, _res: Response, next: NextFunction) => { +export const decodeId = async (req: Request, _res: Response, next: NextFunction) => { try { for (const idVariable of Utils.idVariables) { if (req.params && req.params[idVariable]) { @@ -15,7 +15,9 @@ export const decodeId = (req: Request, _res: Response, next: NextFunction) => { const entity = req.query[idVariable] as string | string[]; if (Array.isArray(entity)) { - req.query[idVariable] = entity.map((encodedId) => Utils.decodeId(encodedId)); + req.query[idVariable] = await Utils.macrotasksMap(entity, (encodedId) => + Utils.decodeId(encodedId), + ); } else { const encodedId = entity; req.query[idVariable] = Utils.decodeId(encodedId); @@ -26,7 +28,9 @@ export const decodeId = (req: Request, _res: Response, next: NextFunction) => { const entity = req.body[idVariable] as string | string[]; if (Array.isArray(entity)) { - req.body[idVariable] = entity.map((encodedId) => Utils.decodeId(encodedId)); + req.body[idVariable] = await Utils.macrotasksMap(entity, (encodedId) => + Utils.decodeId(encodedId), + ); } else { const encodedId = req.body[idVariable]; req.body[idVariable] = Utils.decodeId(encodedId); diff --git a/src/components/response-presenter.ts b/src/components/response-presenter.ts index 3baf3f5c..4d43cc30 100644 --- a/src/components/response-presenter.ts +++ b/src/components/response-presenter.ts @@ -1,6 +1,7 @@ import * as ST from '../types/services.types'; import Utils from '../utils'; +/** @deprecated use prepareResponseAsync */ export default ({data}: {data: any}): ST.ServiceResponse => { const response = Utils.encodeData(data); @@ -28,3 +29,31 @@ export default ({data}: {data: any}): ST.ServiceResponse => { response, }; }; + +export async function prepareResponseAsync({data}: {data: any}): Promise { + const response = await Utils.macrotasksEncodeData(data); + + if (response.results) { + response.results = await Utils.macrotasksEncodeData(response.results); + } + if (response.entries) { + response.entries = await Utils.macrotasksEncodeData(response.entries); + } + if (response.workbooks) { + response.workbooks = await Utils.macrotasksEncodeData(response.workbooks); + } + if (response.collections) { + response.collections = await Utils.macrotasksEncodeData(response.collections); + } + if (response.embed) { + response.embed = await Utils.macrotasksEncodeData(response.embed); + } + if (response.chart) { + response.chart = await Utils.macrotasksEncodeData(response.chart); + } + + return { + code: 200, + response, + }; +} diff --git a/src/controllers/collections.ts b/src/controllers/collections.ts index 72559436..027aa692 100644 --- a/src/controllers/collections.ts +++ b/src/controllers/collections.ts @@ -1,5 +1,5 @@ import {Request, Response} from '@gravity-ui/expresskit'; -import prepareResponse from '../components/response-presenter'; +import {prepareResponseAsync} from '../components/response-presenter'; import Utils from '../utils'; import { createCollection, @@ -42,7 +42,7 @@ export default { result.operation, ); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -60,7 +60,7 @@ export default { const formattedResponse = formatCollection(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -104,7 +104,7 @@ export default { const formattedResponse = formatCollectionContent(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -112,7 +112,7 @@ export default { getRootPermissions: async (req: Request, res: Response) => { const result = await getRootCollectionPermissions({ctx: req.ctx}); - const {code, response} = prepareResponse({data: result}); + const {code, response} = await prepareResponseAsync({data: result}); res.status(code).send(response); }, @@ -130,7 +130,7 @@ export default { const formattedResponse = formatGetCollectionBreadcrumbs(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -147,7 +147,7 @@ export default { const formattedResponse = formatCollectionModelsList(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -166,7 +166,7 @@ export default { const formattedResponse = formatCollectionModel(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -184,7 +184,7 @@ export default { const formattedResponse = formatCollectionModelsList(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -202,7 +202,7 @@ export default { const formattedResponse = formatCollectionModel(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, diff --git a/src/controllers/color-palettes.ts b/src/controllers/color-palettes.ts index 1c932f5c..186d1540 100644 --- a/src/controllers/color-palettes.ts +++ b/src/controllers/color-palettes.ts @@ -1,5 +1,5 @@ import {Request, Response} from '@gravity-ui/expresskit'; -import prepareResponse from '../components/response-presenter'; +import {prepareResponseAsync} from '../components/response-presenter'; import { deleteColorPalette, getColorPalettesList, @@ -25,7 +25,7 @@ export default { const formattedResponse = formatColorPalette(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -48,7 +48,7 @@ export default { const formattedResponse = formatColorPalette(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -63,7 +63,7 @@ export default { const formattedResponse = formatColorPaletteList(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -78,7 +78,7 @@ export default { const formattedResponse = formatColorPaletteList(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, diff --git a/src/controllers/entries.ts b/src/controllers/entries.ts index c3440311..e8f37c8e 100644 --- a/src/controllers/entries.ts +++ b/src/controllers/entries.ts @@ -1,6 +1,6 @@ import EntryService from '../services/entry.service'; import {Request, Response} from '@gravity-ui/expresskit'; -import prepareResponse from '../components/response-presenter'; +import {prepareResponseAsync} from '../components/response-presenter'; import Utils from '../utils'; import {US_MASTER_TOKEN_HEADER} from '../const'; import NavigationService from '../services/navigation.service'; @@ -48,7 +48,7 @@ export default { ); const formattedResponse = formatGetEntryResponse(req.ctx, result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -65,7 +65,7 @@ export default { ); const formattedResponse = formatGetEntryMetaResponse(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -86,7 +86,7 @@ export default { ); const formattedResponse = formatGetEntryMetaPrivateResponse(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -106,7 +106,7 @@ export default { }, ); - const {code, response} = prepareResponse({data: result}); + const {code, response} = await prepareResponseAsync({data: result}); res.status(code).send(response); }, @@ -134,7 +134,7 @@ export default { ctx: req.ctx, }); - const {code, response} = prepareResponse({data: result}); + const {code, response} = await prepareResponseAsync({data: result}); res.status(code).send(response); }, @@ -161,7 +161,7 @@ export default { ctx: req.ctx, }); - const {code, response} = prepareResponse({data: result}); + const {code, response} = await prepareResponseAsync({data: result}); res.status(code).send(response); }, @@ -184,7 +184,7 @@ export default { skipSyncLinks: body.skipSyncLinks, }); - const {code, response} = prepareResponse({data: result}); + const {code, response} = await prepareResponseAsync({data: result}); res.status(code).send(response); }, @@ -200,7 +200,7 @@ export default { }, ); - const {code, response} = prepareResponse({data: result}); + const {code, response} = await prepareResponseAsync({data: result}); res.status(code).send(response); }, @@ -213,7 +213,7 @@ export default { name: body.name, }); - const {code, response} = prepareResponse({data: result}); + const {code, response} = await prepareResponseAsync({data: result}); res.status(code).send(response); }, @@ -227,7 +227,7 @@ export default { }, ); - const {code, response} = prepareResponse({data: result}); + const {code, response} = await prepareResponseAsync({data: result}); res.status(code).send(response); }, @@ -243,7 +243,7 @@ export default { }, ); - const {code, response} = prepareResponse({data: result}); + const {code, response} = await prepareResponseAsync({data: result}); res.status(code).send(response); }, @@ -262,7 +262,7 @@ export default { const formattedResponse = formatEntryModel(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -277,7 +277,7 @@ export default { }, ); - const {code, response} = prepareResponse({data: result}); + const {code, response} = await prepareResponseAsync({data: result}); res.status(code).send(response); }, @@ -302,7 +302,7 @@ export default { ctx: req.ctx, }); - const {code, response} = prepareResponse({data: result}); + const {code, response} = await prepareResponseAsync({data: result}); res.status(code).send(response); }, diff --git a/src/controllers/favorites.ts b/src/controllers/favorites.ts index d6fb346a..10bba9d3 100644 --- a/src/controllers/favorites.ts +++ b/src/controllers/favorites.ts @@ -1,7 +1,7 @@ import FavoriteService from '../services/favorite.service'; import {Request, Response} from '@gravity-ui/expresskit'; import Utils from '../utils'; -import prepareResponse from '../components/response-presenter'; +import {prepareResponseAsync} from '../components/response-presenter'; import * as ST from '../types/services.types'; import Entry from '../db/models/entry'; @@ -20,7 +20,7 @@ export default { ctx: req.ctx, }); - const {code, response} = prepareResponse({data: result}); + const {code, response} = await prepareResponseAsync({data: result}); res.status(code).send(response); }, @@ -40,7 +40,7 @@ export default { ctx: req.ctx, }); - const {code, response} = prepareResponse({data: result}); + const {code, response} = await prepareResponseAsync({data: result}); res.status(code).send(response); }, @@ -60,7 +60,7 @@ export default { ctx: req.ctx, }); - const {code, response} = prepareResponse({data: result}); + const {code, response} = await prepareResponseAsync({data: result}); res.status(code).send(response); }, @@ -73,7 +73,7 @@ export default { ctx: req.ctx, }); - const {code, response} = prepareResponse({data: result}); + const {code, response} = await prepareResponseAsync({data: result}); res.status(code).send(response); }, diff --git a/src/controllers/locks.ts b/src/controllers/locks.ts index 29e63c8e..53733b27 100644 --- a/src/controllers/locks.ts +++ b/src/controllers/locks.ts @@ -1,6 +1,6 @@ import {Request, Response} from '@gravity-ui/expresskit'; import LockService from '../services/lock.service'; -import prepareResponse from '../components/response-presenter'; +import {prepareResponseAsync} from '../components/response-presenter'; import * as ST from '../types/services.types'; export default { @@ -12,7 +12,7 @@ export default { ctx: req.ctx, }); - const {code, response} = prepareResponse({data: result}); + const {code, response} = await prepareResponseAsync({data: result}); res.status(code).send(response); }, @@ -26,7 +26,7 @@ export default { ctx: req.ctx, }); - const {code, response} = prepareResponse({data: result}); + const {code, response} = await prepareResponseAsync({data: result}); res.status(code).send(response); }, @@ -40,7 +40,7 @@ export default { ctx: req.ctx, }); - const {code, response} = prepareResponse({data: result}); + const {code, response} = await prepareResponseAsync({data: result}); res.status(code).send(response); }, @@ -55,7 +55,7 @@ export default { ctx: req.ctx, }); - const {code, response} = prepareResponse({data: result}); + const {code, response} = await prepareResponseAsync({data: result}); res.status(code).send(response); }, diff --git a/src/controllers/states.ts b/src/controllers/states.ts index 84f51a5d..b523fb5b 100644 --- a/src/controllers/states.ts +++ b/src/controllers/states.ts @@ -1,5 +1,5 @@ import {Request, Response} from '@gravity-ui/expresskit'; -import prepareResponse from '../components/response-presenter'; +import {prepareResponseAsync} from '../components/response-presenter'; import {createState, getState} from '../services/new/state'; import {formatCreateStateResponse, formatGetStateResponse} from '../services/new/state/formatters'; @@ -16,7 +16,7 @@ export default { ); const formattedResponse = formatCreateStateResponse(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -33,7 +33,7 @@ export default { ); const formattedResponse = formatGetStateResponse(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, diff --git a/src/controllers/workbooks.ts b/src/controllers/workbooks.ts index dffeaa65..6e853332 100644 --- a/src/controllers/workbooks.ts +++ b/src/controllers/workbooks.ts @@ -1,5 +1,5 @@ import {Request, Response} from '@gravity-ui/expresskit'; -import prepareResponse from '../components/response-presenter'; +import {prepareResponseAsync} from '../components/response-presenter'; import Utils from '../utils'; import { createWorkbook, @@ -46,7 +46,7 @@ export default { ); const formattedResponse = formatWorkbookWithOperation(result.workbook, result.operation); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -64,7 +64,7 @@ export default { ); const formattedResponse = formatWorkbook(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -86,7 +86,7 @@ export default { ); const formattedResponse = formatGetWorkbookContent(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -108,7 +108,7 @@ export default { ); const formattedResponse = formatWorkbooksList(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -127,7 +127,7 @@ export default { ); const formattedResponse = formatWorkbookModel(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -146,7 +146,7 @@ export default { ); const formattedResponse = formatWorkbookModel(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -163,7 +163,7 @@ export default { const formattedResponse = formatWorkbookModelsList(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -180,7 +180,7 @@ export default { }, ); - const {code, response} = prepareResponse({data: {status: 'ok'}}); + const {code, response} = await prepareResponseAsync({data: {status: 'ok'}}); res.status(code).send(response); }, @@ -204,7 +204,7 @@ export default { result.workbook, result.operation, ); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -222,7 +222,7 @@ export default { ); const formattedResponse = formatSetWorkbookIsTemplate(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -245,7 +245,7 @@ export default { result.workbook, result.operation, ); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -261,7 +261,7 @@ export default { ); const formattedResponse = formatWorkbookModelsList(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, @@ -278,7 +278,7 @@ export default { ); const formattedResponse = formatRestoreWorkbook(result); - const {code, response} = prepareResponse({data: formattedResponse}); + const {code, response} = await prepareResponseAsync({data: formattedResponse}); res.status(code).send(response); }, }; diff --git a/src/services/entry/actions/copy-to-workbook.ts b/src/services/entry/actions/copy-to-workbook.ts index aa6d8a9b..25bb42c7 100644 --- a/src/services/entry/actions/copy-to-workbook.ts +++ b/src/services/entry/actions/copy-to-workbook.ts @@ -70,7 +70,7 @@ export const copyToWorkbook = async (ctx: CTX, params: Params) => { } = params; logInfo(ctx, 'COPY_ENTRY_TO_WORKBOOK_CALL', { - entryIds: entryIds.map((entryId) => Utils.encodeId(entryId)), + entryIds: await Utils.macrotasksMap(entryIds, (entryId) => Utils.encodeId(entryId)), destinationWorkbookId: Utils.encodeId(destinationWorkbookId), tenantIdOverride, }); diff --git a/src/services/new/collection/delete-collection.ts b/src/services/new/collection/delete-collection.ts index b39912c3..b3db609e 100644 --- a/src/services/new/collection/delete-collection.ts +++ b/src/services/new/collection/delete-collection.ts @@ -153,7 +153,9 @@ export const deleteCollection = async ( }); logInfo(ctx, 'DELETE_COLLECTION_FINISH', { - deletedCollections: result.map((item) => Utils.encodeId(item.collectionId)), + deletedCollections: await Utils.macrotasksMap(result, (item) => + Utils.encodeId(item.collectionId), + ), }); // TODO: Return deleted workbooks and entries diff --git a/src/services/new/collection/move-collections-list.ts b/src/services/new/collection/move-collections-list.ts index ab53e59c..42c4fd87 100644 --- a/src/services/new/collection/move-collections-list.ts +++ b/src/services/new/collection/move-collections-list.ts @@ -45,10 +45,12 @@ export const moveCollectionsList = async ( parentId: Utils.encodeId(parentId), }); + const ids = await Utils.macrotasksMap(collectionIds, (id) => Utils.decodeId(id)); + const result = await transaction(targetTrx, async (transactionTrx) => { return await Promise.all( - collectionIds.map( - async (id: string) => + ids.map( + async (collectionId: string) => await moveCollection( { ctx, @@ -57,7 +59,7 @@ export const moveCollectionsList = async ( skipCheckPermissions, }, { - collectionId: Utils.decodeId(id), + collectionId, parentId, }, ), @@ -66,7 +68,9 @@ export const moveCollectionsList = async ( }); ctx.log('MOVE_LIST_COLLECTIONS_END', { - collectionIds: result.map((collection) => Utils.encodeId(collection.collectionId)), + collectionIds: await Utils.macrotasksMap(result, (collection) => + Utils.encodeId(collection.collectionId), + ), parentId: Utils.encodeId(parentId), }); diff --git a/src/services/new/entry/copy-entries-to-workbook.ts b/src/services/new/entry/copy-entries-to-workbook.ts index 8e402ae6..16a5cee7 100644 --- a/src/services/new/entry/copy-entries-to-workbook.ts +++ b/src/services/new/entry/copy-entries-to-workbook.ts @@ -45,7 +45,7 @@ export const copyEntriesToWorkbook = async ( const updatedBy = makeUserId(user.userId); logInfo(ctx, 'COPY_ENTRIES_TO_WORKBOOK_START', { - entryIds: entryIds.map((entryId) => Utils.encodeId(entryId)), + entryIds: await Utils.macrotasksMap(entryIds, (entryId) => Utils.encodeId(entryId)), workbookId: Utils.encodeId(targetWorkbookId), copiedBy: updatedBy, }); diff --git a/src/services/new/workbook/get-workbooks-list-by-ids.ts b/src/services/new/workbook/get-workbooks-list-by-ids.ts index e3bc45d6..3bcd2634 100644 --- a/src/services/new/workbook/get-workbooks-list-by-ids.ts +++ b/src/services/new/workbook/get-workbooks-list-by-ids.ts @@ -57,7 +57,7 @@ export const getWorkbooksListByIds = async ( const {workbookIds, includePermissionsInfo = false} = args; logInfo(ctx, 'GET_WORKBOOKS_LIST_BY_IDS_STARTED', { - workbookIds: workbookIds.map((id) => Utils.encodeId(id)), + workbookIds: await Utils.macrotasksMap(workbookIds, (id) => Utils.encodeId(id)), includePermissionsInfo, }); @@ -168,7 +168,9 @@ export const getWorkbooksListByIds = async ( const result = workbooks.filter((item) => Boolean(item)) as WorkbookInstance[]; logInfo(ctx, 'GET_WORKBOOKS_LIST_BY_IDS_FINISHED', { - workbookIds: workbookList.map((workbook) => Utils.encodeId(workbook.workbookId)), + workbookIds: await Utils.macrotasksMap(workbookList, (workbook) => + Utils.encodeId(workbook.workbookId), + ), }); return result; diff --git a/src/services/new/workbook/move-workbooks-list.ts b/src/services/new/workbook/move-workbooks-list.ts index 1e5c73b0..c4b4f451 100644 --- a/src/services/new/workbook/move-workbooks-list.ts +++ b/src/services/new/workbook/move-workbooks-list.ts @@ -45,10 +45,12 @@ export const moveWorkbooksList = async ( collectionId: Utils.encodeId(collectionId), }); + const ids = await Utils.macrotasksMap(workbookIds, (id) => Utils.decodeId(id)); + const result = await transaction(targetTrx, async (transactionTrx) => { return await Promise.all( - workbookIds.map( - async (id: string) => + ids.map( + async (workbookId: string) => await moveWorkbook( { ctx, @@ -57,7 +59,7 @@ export const moveWorkbooksList = async ( skipCheckPermissions, }, { - workbookId: Utils.decodeId(id), + workbookId, collectionId, }, ), @@ -66,7 +68,9 @@ export const moveWorkbooksList = async ( }); ctx.log('MOVE_LIST_WORKBOOKS_END', { - workbookIds: result.map((workbook) => Utils.encodeId(workbook.workbookId)), + workbookIds: await Utils.macrotasksMap(result, (workbook) => + Utils.encodeId(workbook.workbookId), + ), collectionId: Utils.encodeId(collectionId), }); diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 46640685..ebec9bdc 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -1,6 +1,8 @@ const fs = require('fs'); const PowerRadix = require('power-radix'); +import chunk from 'lodash/chunk'; + import {EntryScope, USAPIResponse} from '../types/models'; import {ID_VARIABLES, CODING_BASE, TRUE_FLAGS, COPY_START, COPY_END} from '../const'; @@ -141,6 +143,33 @@ export class Utils { return rotatedArray; } + static async macrotasksMap unknown>( + arr: T[], + cb: R, + chunkSize = 1000, + ): Promise[]> { + const chunks = chunk(arr, chunkSize); + const results: ReturnType[] = []; + for (const chunkItem of chunks) { + const items = (await new Promise((resolve, reject) => { + function done() { + try { + resolve(chunkItem.map(cb)); + } catch (error) { + reject(error); + } + } + if (chunkItem === chunks[0]) { + done(); + } else { + setImmediate(done); + } + })) as unknown as ReturnType[]; + results.push(...items); + } + return results; + } + static encodeId(bigIntId: any) { let encodedId = ''; @@ -180,6 +209,7 @@ export class Utils { return decodedId; } + /** @deprecated use macrotasksEncodeMapIds */ static encodeMapIds(object: {[key: string]: any}) { return Object.keys(object).reduce(function (result, bigInt) { const encodedId = Utils.encodeId(bigInt); @@ -188,6 +218,17 @@ export class Utils { }, {}); } + static async macrotasksEncodeMapIds(object: Record) { + const ids = await Utils.macrotasksMap(Object.keys(object), (bigInt: string) => ({ + bigInt, + encodedId: Utils.encodeId(bigInt), + })); + return ids.reduce>((result, id) => { + result[id.encodedId] = object[id.bigInt]; + return result; + }, {}); + } + static encodeIds(object: {[key: string]: any}) { for (const idVariable of Utils.idVariables) { if (object && object[idVariable]) { @@ -199,6 +240,7 @@ export class Utils { return object; } + /** @deprecated use macrotasksEncodeData */ static encodeData(data: any) { let dataFormed; @@ -213,6 +255,20 @@ export class Utils { return dataFormed; } + static async macrotasksEncodeData(data: any) { + let dataFormed; + + if (Array.isArray(data)) { + dataFormed = await Utils.macrotasksMap(data, Utils.encodeIds); + } else if (data !== null && typeof data === 'object') { + dataFormed = Utils.encodeIds(data); + } else { + dataFormed = data; + } + + return dataFormed; + } + static generateLockToken() { return ( Math.random().toString(36).substring(2, 10) +