Skip to content

Commit

Permalink
Add controllers callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
stepanenkoxx committed Dec 11, 2024
1 parent ad57c9d commit 074024f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
4 changes: 4 additions & 0 deletions api/registry/types.ts
Original file line number Diff line number Diff line change
@@ -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';
43 changes: 34 additions & 9 deletions src/controllers/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,43 @@ import {
} from '../services/new/collection/formatters';
import Utils from '../utils';

export type CreateCollectionReqBody = {
title: string;
description?: string;
parentId: Nullable<string>;
};

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<string>,
parentId: body.parentId as Nullable<string>,
},
);
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,
Expand Down
9 changes: 9 additions & 0 deletions src/registry/common/functions-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -23,4 +27,9 @@ export const commonFunctionsMap = {
getEntryBeforeDbRequestHook: makeFunctionTemplate<GetEntryBeforeDbRequestHook>(),
getEntryAddFormattedFieldsHook: makeFunctionTemplate<GetEntryAddFormattedFieldsHook>(),
checkEmbedding: makeFunctionTemplate<CheckEmbedding>(),

controllersCallbacks: {
onCreateCollectionSuccess: makeFunctionTemplate<OnCreateCollectionSuccess>(),
onCreateCollectionError: makeFunctionTemplate<OnCreateCollectionError>(),
},
} as const;
16 changes: 16 additions & 0 deletions src/registry/common/utils/controllers-callbacks/collections.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 074024f

Please sign in to comment.