From 074024fbc34b43b30bb730d94a165c3f4cb2348a Mon Sep 17 00:00:00 2001 From: Vladimir Stepanenko Date: Wed, 11 Dec 2024 15:24:43 +0300 Subject: [PATCH] Add controllers callbacks --- api/registry/types.ts | 4 ++ src/controllers/collections.ts | 43 +++++++++++++++---- src/registry/common/functions-map.ts | 9 ++++ .../controllers-callbacks/collections.ts | 16 +++++++ 4 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 src/registry/common/utils/controllers-callbacks/collections.ts diff --git a/api/registry/types.ts b/api/registry/types.ts index 39bb967e..51f5eabe 100644 --- a/api/registry/types.ts +++ b/api/registry/types.ts @@ -1,2 +1,6 @@ export type {DLSConstructor} from '../../src/registry/common/components/dls/types'; +export type { + OnCreateCollectionError, + OnCreateCollectionSuccess, +} from '../../src/registry/common/utils/controllers-callbacks/collections'; export type {GatewayApi} from '../../src/registry'; diff --git a/src/controllers/collections.ts b/src/controllers/collections.ts index 58d71090..a8a5b19b 100644 --- a/src/controllers/collections.ts +++ b/src/controllers/collections.ts @@ -26,18 +26,43 @@ import { } from '../services/new/collection/formatters'; import Utils from '../utils'; +export type CreateCollectionReqBody = { + title: string; + description?: string; + parentId: Nullable; +}; + export default { - create: async (req: Request, res: Response) => { + create: async (req: Request<{}, {}, CreateCollectionReqBody>, res: Response) => { const {body} = req; - const result = await createCollection( - {ctx: req.ctx}, - { - title: body.title, - description: body.description as Optional, - parentId: body.parentId as Nullable, - }, - ); + const registry = req.ctx.get('registry'); + const { + controllersCallbacks: {onCreateCollectionError, onCreateCollectionSuccess}, + } = registry.common.functions.get(); + + let result; + + try { + result = await createCollection( + {ctx: req.ctx}, + { + title: body.title, + description: body.description, + parentId: body.parentId, + }, + ); + + onCreateCollectionSuccess({ + ctx: req.ctx, + reqBody: body, + collection: result.collection.model, + }); + } catch (error) { + onCreateCollectionError({ctx: req.ctx, reqBody: body, error}); + + throw error; + } const formattedResponse = formatCollectionWithOperation( result.collection, diff --git a/src/registry/common/functions-map.ts b/src/registry/common/functions-map.ts index e5970ea6..8784bf43 100644 --- a/src/registry/common/functions-map.ts +++ b/src/registry/common/functions-map.ts @@ -5,6 +5,10 @@ import type {CheckOrganizationPermission, CheckProjectPermission} from './compon import type {BulkFetchCollectionsAllPermissions} from './entities/collection/types'; import type {BulkFetchWorkbooksAllPermissions} from './entities/workbook/types'; import type {ColorPalettesAdminValidator} from './utils/color-palettes/types'; +import type { + OnCreateCollectionError, + OnCreateCollectionSuccess, +} from './utils/controllers-callbacks/collections'; import type {CheckEmbedding} from './utils/embedding/types'; import type { GetEntryAddFormattedFieldsHook, @@ -23,4 +27,9 @@ export const commonFunctionsMap = { getEntryBeforeDbRequestHook: makeFunctionTemplate(), getEntryAddFormattedFieldsHook: makeFunctionTemplate(), checkEmbedding: makeFunctionTemplate(), + + controllersCallbacks: { + onCreateCollectionSuccess: makeFunctionTemplate(), + onCreateCollectionError: makeFunctionTemplate(), + }, } as const; diff --git a/src/registry/common/utils/controllers-callbacks/collections.ts b/src/registry/common/utils/controllers-callbacks/collections.ts new file mode 100644 index 00000000..779c7a60 --- /dev/null +++ b/src/registry/common/utils/controllers-callbacks/collections.ts @@ -0,0 +1,16 @@ +import type {AppContext} from '@gravity-ui/nodekit'; + +import type {CreateCollectionReqBody} from '../../../../controllers/collections'; +import type {CollectionModel} from '../../../../db/models/new/collection'; + +export type OnCreateCollectionSuccess = (args: { + ctx: AppContext; + reqBody: CreateCollectionReqBody; + collection: CollectionModel; +}) => void; + +export type OnCreateCollectionError = (args: { + ctx: AppContext; + reqBody: CreateCollectionReqBody; + error: unknown; +}) => void;