Skip to content
Merged
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
1 change: 1 addition & 0 deletions api/registry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type {
export {LogEventType} from '../../src/registry/common/utils/log-event/types';
export type {GatewayApi} from '../../src/registry';
export type {CheckTenant, GetServicePlan} from '../../src/registry/common/utils/tenant/types';
export type {GetEntryResolveUserLogin} from '../../src/registry/common/utils/entry/types';
export type {
CollectionConstructor,
CollectionInstance,
Expand Down
26 changes: 14 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"@asteasolutions/zod-to-openapi": "^7.3.0",
"@gravity-ui/expresskit": "^2.4.0",
"@gravity-ui/gateway": "^2.6.2",
"@gravity-ui/gateway": "^4.7.2",
"@gravity-ui/nodekit": "^2.4.1",
"@gravity-ui/postgreskit": "^2.0.0",
"ajv": "^6.12.4",
Expand Down
11 changes: 11 additions & 0 deletions src/components/error-response-presenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ export default (error: AppError | DBError) => {
};
}

case US_ERRORS.ENTRY_AND_WORKBOOK_TENANT_MISMATCH:
case US_ERRORS.TOO_MANY_COLOR_PALETTES: {
return {
code: 500,
Expand Down Expand Up @@ -400,6 +401,16 @@ export default (error: AppError | DBError) => {
};
}

case US_ERRORS.ACTION_TIMEOUT: {
return {
code: 504,
response: {
code,
message: 'Action timed out',
},
};
}

default:
return {
code: 500,
Expand Down
1 change: 1 addition & 0 deletions src/components/features/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export enum Feature {
WorkbookIsolationEnabled = 'WorkbookIsolationEnabled',
DefaultColorPaletteEnabled = 'DefaultColorPaletteEnabled',
TenantsEnabled = 'TenantsEnabled',
GetEntryV2Enabled = 'GetEntryV2Enabled',
}

export type FeaturesConfig = {
Expand Down
2 changes: 2 additions & 0 deletions src/const/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ export const US_ERRORS = {
COLLECTION_WITH_WORKBOOK_TEMPLATE_CANT_BE_DELETED:
'COLLECTION_WITH_WORKBOOK_TEMPLATE_CANT_BE_DELETED',
TENANT_ID_MISSING_IN_CONTEXT: 'TENANT_ID_MISSING_IN_CONTEXT',
ACTION_TIMEOUT: 'ACTION_TIMEOUT',
ENTRY_AND_WORKBOOK_TENANT_MISMATCH: 'ENTRY_AND_WORKBOOK_TENANT_MISMATCH',
};
64 changes: 64 additions & 0 deletions src/controllers/entries/get-entry/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {Request, Response} from '@gravity-ui/expresskit';

import {ApiTag} from '../../../components/api-docs';
import {makeReqParser, z, zc} from '../../../components/zod';
import {CONTENT_TYPE_JSON} from '../../../const';
import {getEntryV2} from '../../../services/new/entry';

import {getEntryResult} from './response-model';

const requestSchema = {
params: z.object({
entryId: z.string(),
}),
query: z.object({
branch: z.enum(['saved', 'published']).optional(),
revId: z.string().optional(),
includePermissionsInfo: zc.stringBoolean().optional(),
includeLinks: zc.stringBoolean().optional(),
includeServicePlan: zc.stringBoolean().optional(),
includeTenantFeatures: zc.stringBoolean().optional(),
includeFavorite: zc.stringBoolean().optional(),
}),
};

const parseReq = makeReqParser(requestSchema);

export const getEntryController = async (req: Request, res: Response) => {
const {query, params} = await parseReq(req);

const result = await getEntryV2(
{ctx: req.ctx},
{
entryId: params.entryId,
branch: query.branch,
revId: query.revId,
includePermissionsInfo: query.includePermissionsInfo,
includeLinks: query.includeLinks,
includeServicePlan: query.includeServicePlan,
includeTenantFeatures: query.includeTenantFeatures,
includeFavorite: query.includeFavorite,
},
);

res.status(200).send(getEntryResult.format(req.ctx, result));
};

getEntryController.api = {
summary: 'Get entry',
tags: [ApiTag.Entries],
request: {
params: requestSchema.params,
query: requestSchema.query,
},
responses: {
200: {
description: getEntryResult.schema.description ?? '',
content: {
[CONTENT_TYPE_JSON]: {
schema: getEntryResult.schema,
},
},
},
},
};
106 changes: 106 additions & 0 deletions src/controllers/entries/get-entry/response-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import {AppContext} from '@gravity-ui/nodekit';

import {z} from '../../../components/zod';
import {EntryScope} from '../../../db/models/new/entry/types';
import {GetEntryV2Result} from '../../../services/new/entry';
import Utils from '../../../utils';

const schema = z
.object({
entryId: z.string(),
scope: z.nativeEnum(EntryScope),
type: z.string(),
key: z.string().nullable(),
unversionedData: z.record(z.string(), z.unknown()).optional(),
createdBy: z.string(),
createdAt: z.string(),
updatedBy: z.string(),
updatedAt: z.string(),
savedId: z.string().nullable(),
publishedId: z.string().nullable(),
revId: z.string(),
tenantId: z.string().nullable(),
data: z.record(z.string(), z.unknown()).nullable(),
meta: z.record(z.string(), z.unknown()).nullable(),
hidden: z.boolean(),
public: z.boolean(),
workbookId: z.string().nullable(),
links: z.record(z.string(), z.unknown()).optional().nullable(),
isFavorite: z.boolean().optional(),
permissions: z
.object({
execute: z.boolean().optional(),
read: z.boolean().optional(),
edit: z.boolean().optional(),
admin: z.boolean().optional(),
})
.optional(),
servicePlan: z.string().optional(),
tenantFeatures: z.record(z.string(), z.unknown()).optional(),
})
.describe('Get entry result');

const format = (
ctx: AppContext,
{
entry,
revision,
includePermissionsInfo,
permissions,
includeLinks,
includeServicePlan,
servicePlan,
includeTenantFeatures,
tenantFeatures,
includeFavorite,
}: GetEntryV2Result,
): z.infer<typeof schema> => {
const {privatePermissions, onlyPublic} = ctx.get('info');

let isHiddenUnversionedData = false;
if (!privatePermissions.ownedScopes.includes(entry.scope)) {
isHiddenUnversionedData = true;
}

let isFavorite: boolean | undefined;

if (includeFavorite && !onlyPublic) {
isFavorite = Boolean(entry.favorite);
}

const registry = ctx.get('registry');
const {getEntryAddFormattedFieldsHook} = registry.common.functions.get();
const additionalFields = getEntryAddFormattedFieldsHook({ctx});

return {
entryId: Utils.encodeId(entry.entryId),
scope: entry.scope,
type: entry.type,
key: entry.displayKey,
unversionedData: isHiddenUnversionedData ? undefined : entry.unversionedData,
createdBy: entry.createdBy,
createdAt: entry.createdAt,
updatedBy: revision.updatedBy,
updatedAt: revision.updatedAt,
savedId: entry.savedId ? Utils.encodeId(entry.savedId) : null,
publishedId: entry.publishedId ? Utils.encodeId(entry.publishedId) : null,
revId: Utils.encodeId(revision.revId),
tenantId: entry.tenantId,
data: revision.data,
meta: revision.meta,
hidden: entry.hidden,
public: entry.public,
workbookId: entry.workbookId ? Utils.encodeId(entry.workbookId) : null,
links: includeLinks ? revision.links : undefined,
isFavorite,
permissions: includePermissionsInfo ? permissions : undefined,
servicePlan: includeServicePlan ? servicePlan : undefined,
tenantFeatures: includeTenantFeatures ? tenantFeatures : undefined,
...additionalFields,
};
};

export const getEntryResult = {
schema,
format,
};
44 changes: 25 additions & 19 deletions src/controllers/entries/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Request, Response} from '@gravity-ui/expresskit';

import {Feature, isEnabledFeature} from '../../components/features';
import {prepareResponseAsync} from '../../components/response-presenter';
import {US_MASTER_TOKEN_HEADER} from '../../const';
import {EntryScope} from '../../db/models/new/entry/types';
Expand Down Expand Up @@ -32,6 +33,7 @@ import {deleteEntryController} from './delete-entry';
import {getEntriesController} from './get-entries';
import {getEntriesDataController} from './get-entries-data';
import {getEntriesMetaController} from './get-entries-meta';
import {getEntryController as getEntryV2Controller} from './get-entry';
import {renameEntryController} from './rename-entry';
import {updateEntryController} from './update-entry';

Expand All @@ -48,25 +50,29 @@ export default {
getEntriesController,

getEntry: async (req: Request, res: Response) => {
const {query, params} = req;

const result = await getEntry(
{ctx: req.ctx},
{
entryId: params.entryId,
branch: query.branch as GetEntryArgs['branch'],
revId: query.revId as GetEntryArgs['revId'],
includePermissionsInfo: isTrueArg(query.includePermissionsInfo),
includeLinks: isTrueArg(query.includeLinks),
includeServicePlan: isTrueArg(query.includeServicePlan),
includeTenantFeatures: isTrueArg(query.includeTenantFeatures),
},
);
const formattedResponse = await formatGetEntryResponse(req.ctx, result);

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

res.status(code).send(response);
if (isEnabledFeature(req.ctx, Feature.GetEntryV2Enabled)) {
await getEntryV2Controller(req, res);
} else {
const {query, params} = req;

const result = await getEntry(
{ctx: req.ctx},
{
entryId: params.entryId,
branch: query.branch as GetEntryArgs['branch'],
revId: query.revId as GetEntryArgs['revId'],
includePermissionsInfo: isTrueArg(query.includePermissionsInfo),
includeLinks: isTrueArg(query.includeLinks),
includeServicePlan: isTrueArg(query.includeServicePlan),
includeTenantFeatures: isTrueArg(query.includeTenantFeatures),
},
);
const formattedResponse = await formatGetEntryResponse(req.ctx, result);

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

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

getEntryMeta: async (req: Request, res: Response) => {
Expand Down
12 changes: 11 additions & 1 deletion src/db/models/new/entry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Model} from '../../..';
import {EntryPermissions} from '../../../../services/new/entry/types';
import {Favorite} from '../favorite';
import {RevisionModel} from '../revision';
import {Tenant, TenantColumn} from '../tenant';
import {WorkbookModel} from '../workbook';

import {EntryScope} from './types';
Expand Down Expand Up @@ -101,12 +102,20 @@ export class Entry extends Model {
},
favorite: {
relation: Model.HasOneRelation,
modelClass: RevisionModel,
modelClass: Favorite,
join: {
from: `${Entry.tableName}.${EntryColumn.EntryId}`,
to: `${Favorite.tableName}.entryId`,
},
},
tenant: {
relation: Model.BelongsToOneRelation,
modelClass: Tenant,
join: {
from: `${Entry.tableName}.${EntryColumn.TenantId}`,
to: `${Tenant.tableName}.${TenantColumn.TenantId}`,
},
},
};
}

Expand Down Expand Up @@ -138,6 +147,7 @@ export class Entry extends Model {
publishedRevision?: RevisionModel;
workbook?: WorkbookModel;
favorite?: Favorite;
tenant?: Tenant;

permissions?: EntryPermissions;
isLocked?: boolean;
Expand Down
Loading
Loading