Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getEntriesData endpoint #227

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/components/response-presenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ export default ({data}: {data: any}): ST.ServiceResponse => {
};
};

export async function prepareResponseAsync({data}: {data: any}): Promise<ST.ServiceResponse> {
export async function prepareResponseAsync<T extends any = any>({
data,
}: {
data: T;
}): Promise<ST.ServiceResponse<T>> {
const response = await Utils.macrotasksEncodeData(data);

if (response.results) {
Expand Down
12 changes: 1 addition & 11 deletions src/const/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,7 @@ export const RETURN_WORKBOOK_COLUMNS = [
'createdAt',
];

export const ALLOWED_SCOPE_VALUES = [
'dataset',
'pdf',
'folder',
'dash',
'connection',
'widget',
'config',
'presentation',
'report',
];
export const ALLOWED_SCOPE_VALUES = Object.values(EntryScope);

export const ID_VARIABLES = [
'ids',
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/entries/getEntriesData/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const NOT_FOUND_ERROR_CODE = 'NOT_FOUND';
export const ACCESS_DENIED_ERROR_CODE = 'ACCESS_DENIED';
61 changes: 61 additions & 0 deletions src/controllers/entries/getEntriesData/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {Request, Response} from '@gravity-ui/expresskit';

import {prepareResponseAsync} from '../../../components/response-presenter';
import {makeSchemaValidator} from '../../../components/validation-schema-compiler';
import {ALLOWED_SCOPE_VALUES} from '../../../const';
import {EntryScope} from '../../../db/models/new/entry/types';
import {getJoinedEntriesRevisionsByIds} from '../../../services/new/entry';

import type {GetEntriesDataResponseBody} from './types';
import {formatGetEntriesDataResponse} from './utils';

type GetEntriesDataRequestBody = {
entryIds: string[];
scope?: EntryScope;
type?: string;
fields: string[];
};

const validateBody = makeSchemaValidator({
type: 'object',
required: ['entryIds', 'fields'],
properties: {
entryIds: {
type: 'array',
items: {type: 'string'},
},
scope: {
type: 'string',
enum: ALLOWED_SCOPE_VALUES,
},
type: {
type: 'string',
},
fields: {
type: 'array',
items: {type: 'string'},
},
},
});

export const getEntriesDataController = async (
req: Request,
res: Response<GetEntriesDataResponseBody>,
) => {
const body = validateBody<GetEntriesDataRequestBody>(req.body);

const result = await getJoinedEntriesRevisionsByIds(
{ctx: req.ctx},
{
entryIds: body.entryIds,
scope: body.scope,
type: body.type,
},
);

const formattedResponse = formatGetEntriesDataResponse({result, fields: body.fields});

const {code, response} = await prepareResponseAsync({data: formattedResponse});

res.status(code).send(response);
};
24 changes: 24 additions & 0 deletions src/controllers/entries/getEntriesData/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {EntryScope} from '../../../db/models/new/entry/types';

import {ACCESS_DENIED_ERROR_CODE, NOT_FOUND_ERROR_CODE} from './constants';

export type GetEntriesDataResponseItemResult = {
scope: Nullable<EntryScope>;
Copy link
Contributor Author

@stepanenkoxx stepanenkoxx Dec 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have nullable type in our Entry model
Can it really be null?

type: string;
data: Record<string, unknown>;
};

export type GetEntriesDataResponseItem = {
entryId: string;
} & (
| {
result: GetEntriesDataResponseItemResult;
}
| {
error: {
code: typeof NOT_FOUND_ERROR_CODE | typeof ACCESS_DENIED_ERROR_CODE;
};
}
);

export type GetEntriesDataResponseBody = GetEntriesDataResponseItem[];
54 changes: 54 additions & 0 deletions src/controllers/entries/getEntriesData/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import _ from 'lodash';

import type {GetJoinedEntriesRevisionsByIdsResult} from '../../../services/new/entry';

import {ACCESS_DENIED_ERROR_CODE, NOT_FOUND_ERROR_CODE} from './constants';
import type {GetEntriesDataResponseBody, GetEntriesDataResponseItemResult} from './types';

export const formatGetEntriesDataResponse = ({
result,
fields,
}: {
result: GetJoinedEntriesRevisionsByIdsResult;
fields: string[];
}): GetEntriesDataResponseBody => {
const {entries, notFoundEntryIds, accessDeniedEntryIds} = result;

const formattedResult: GetEntriesDataResponseBody = [];

entries.forEach(({entryId, scope, type, data}) => {
let responseData: GetEntriesDataResponseItemResult['data'];

if (data) {
responseData = fields.reduce<GetEntriesDataResponseItemResult['data']>(
(acc, fieldPath) => {
acc[fieldPath] = _.get(data, fieldPath);

return acc;
},
{},
);
} else {
responseData = {};
}

formattedResult.push({
entryId,
result: {
scope,
type,
data: responseData,
},
});
});

notFoundEntryIds.forEach((entryId) => {
formattedResult.push({entryId, error: {code: NOT_FOUND_ERROR_CODE}});
});

accessDeniedEntryIds.forEach((entryId) => {
formattedResult.push({entryId, error: {code: ACCESS_DENIED_ERROR_CODE}});
});

return formattedResult;
};
24 changes: 14 additions & 10 deletions src/controllers/entries.ts → src/controllers/entries/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Request, Response} from '@gravity-ui/expresskit';

import {prepareResponseAsync} from '../components/response-presenter';
import {US_MASTER_TOKEN_HEADER} from '../const';
import {EntryScope} from '../db/models/new/entry/types';
import {prepareResponseAsync} from '../../components/response-presenter';
import {US_MASTER_TOKEN_HEADER} from '../../const';
import {EntryScope} from '../../db/models/new/entry/types';
import {
DeleteEntryData,
GetEntryRevisionsData,
Expand All @@ -13,9 +13,9 @@ import {
renameEntry,
switchRevisionEntry,
updateEntry,
} from '../services/entry';
import EntryService from '../services/entry.service';
import NavigationService from '../services/navigation.service';
} from '../../services/entry';
import EntryService from '../../services/entry.service';
import NavigationService from '../../services/navigation.service';
import {
GetEntryArgs,
GetEntryMetaPrivateArgs,
Expand All @@ -24,15 +24,17 @@ import {
getEntry,
getEntryMeta,
getEntryMetaPrivate,
} from '../services/new/entry';
} from '../../services/new/entry';
import {
formatEntryModel,
formatGetEntryMetaPrivateResponse,
formatGetEntryMetaResponse,
formatGetEntryResponse,
} from '../services/new/entry/formatters';
import * as ST from '../types/services.types';
import Utils from '../utils';
} from '../../services/new/entry/formatters';
import * as ST from '../../types/services.types';
import Utils from '../../utils';

import {getEntriesDataController} from './getEntriesData';

export default {
getEntry: async (req: Request, res: Response) => {
Expand Down Expand Up @@ -320,4 +322,6 @@ export default {

res.status(code).send(response);
},

getEntriesData: getEntriesDataController,
};
6 changes: 3 additions & 3 deletions src/registry/common/components/dls/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export interface DLSConstructor {
checkPermissionsArgs: MT.CheckPermissionDlsConfig,
) => Promise<any>;

checkBulkPermission(
checkBulkPermission<E extends object>(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should probably be {entryId: string} not object
But we have some usages where entity type is {entryId?: string}

{ctx, trx}: {ctx: AppContext; trx?: TransactionOrKnex},
checkBulkPermissionArgs: MT.CheckBulkPermissionsDlsConfig,
): Promise<any[]>;
checkBulkPermissionArgs: MT.CheckBulkPermissionsDlsConfig<E>,
): Promise<Array<E & MT.DlsEntity>>;

addEntity(
{ctx, trx}: {ctx: AppContext; trx?: TransactionOrKnex},
Expand Down
5 changes: 5 additions & 0 deletions src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ export function getRoutes(_nodekit: NodeKit, options: GetRoutesOptions) {
private: true,
}),

getEntriesData: makeRoute({
route: 'POST /v1/get-entries-data',
handler: entriesController.getEntriesData,
}),

verifyLockExistence: makeRoute({
route: 'GET /v1/locks/:entryId',
handler: locksController.verifyExistence,
Expand Down
2 changes: 1 addition & 1 deletion src/services/entry/actions/create-in-workbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const validateCreateEntryInWorkbook = makeSchemaValidator({
},
scope: {
type: 'string',
enum: ['connection', 'dataset', 'dash', 'widget', 'presentation', 'report'],
enum: ['connection', 'dataset', 'dash', 'widget', 'report'],
},
type: {
type: 'string',
Expand Down
Loading
Loading