Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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 @@ -52,6 +52,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
10 changes: 10 additions & 0 deletions src/components/error-response-presenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,16 @@ export default (error: AppError | DBError) => {
};
}

case US_ERRORS.OPERATION_TIMEOUT: {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Operation is used for existing entity, can we use Action or Request here?

return {
code: 504,
response: {
code,
message: 'Operation timed out',
},
};
}

default:
return {
code: 500,
Expand Down
1 change: 1 addition & 0 deletions src/const/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ 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',
OPERATION_TIMEOUT: 'OPERATION_TIMEOUT',
};
66 changes: 66 additions & 0 deletions src/controllers/entries/get-entry/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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: zc.encodedId(),
}),
query: z.object({
branch: z.enum(['saved', 'published']).optional(),
revId: zc.encodedId().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,
},
},
},
},
};

getEntryController.manualDecodeId = true;
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 {GetEntryNextResult} 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,
}: GetEntryNextResult,
): 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,
};
2 changes: 2 additions & 0 deletions src/controllers/entries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {createEntryAltController} from './create-entry-alt';
import {deleteEntryController} from './delete-entry';
import {getEntriesController} from './get-entries';
import {getEntriesDataController} from './get-entries-data';
import {getEntryController as getEntryV2Controller} from './get-entry';
import {renameEntryController} from './rename-entry';
import {updateEntryController} from './update-entry';

Expand All @@ -44,6 +45,7 @@ export default {
createEntryController,
createEntryAltController,
getEntriesController,
getEntryV2Controller,

getEntry: async (req: Request, res: Response) => {
const {query, params} = req;
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
8 changes: 8 additions & 0 deletions src/db/models/new/favorite/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import {Model} from '../../..';
import {Entry} from '../entry';

export const FavoriteColumn = {
EntryId: 'entryId',
TenantId: 'tenantId',
Login: 'login',
Alias: 'alias',
CreatedAt: 'createdAt',
} as const;

export class Favorite extends Model {
static get tableName() {
return 'favorites';
Expand Down
2 changes: 2 additions & 0 deletions src/registry/common/functions-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {CheckEmbedding} from './utils/embedding/types';
import type {
GetEntryAddFormattedFieldsHook,
GetEntryBeforeDbRequestHook,
GetEntryResolveUserLogin,
IsNeedBypassEntryByKey,
} from './utils/entry/types';
import type {LogEvent} from './utils/log-event/types';
Expand All @@ -23,6 +24,7 @@ export const commonFunctionsMap = {
getZitadelUserRole: makeFunctionTemplate<GetZitadelUserRole>(),
getEntryBeforeDbRequestHook: makeFunctionTemplate<GetEntryBeforeDbRequestHook>(),
getEntryAddFormattedFieldsHook: makeFunctionTemplate<GetEntryAddFormattedFieldsHook>(),
getEntryResolveUserLogin: makeFunctionTemplate<GetEntryResolveUserLogin>(),
checkEmbedding: makeFunctionTemplate<CheckEmbedding>(),
logEvent: makeFunctionTemplate<LogEvent>(),
checkTenant: makeFunctionTemplate<CheckTenant>(),
Expand Down
2 changes: 2 additions & 0 deletions src/registry/common/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {checkEmbedding} from './utils/embedding/utils';
import {
getEntryAddFormattedFieldsHook,
getEntryBeforeDbRequestHook,
getEntryResolveUserLogin,
isNeedBypassEntryByKey,
} from './utils/entry/utils';
import {logEvent} from './utils/log-event/utils';
Expand All @@ -33,6 +34,7 @@ export const registerCommonPlugins = () => {
getZitadelUserRole,
getEntryBeforeDbRequestHook,
getEntryAddFormattedFieldsHook,
getEntryResolveUserLogin,
checkEmbedding,
logEvent,
checkTenant,
Expand Down
9 changes: 3 additions & 6 deletions src/registry/common/utils/entry/types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import type {AppContext} from '@gravity-ui/nodekit';

import type {GetEntryResult} from '../../../../services/new/entry/get-entry';

export type IsNeedBypassEntryByKey = (ctx: AppContext, key?: string) => boolean;

export type GetEntryBeforeDbRequestHook = (args: {
ctx: AppContext;
entryId: string;
}) => Promise<void>;

export type GetEntryAddFormattedFieldsHook = (args: {
ctx: AppContext;
result: GetEntryResult;
}) => Promise<Record<string, unknown>>;
export type GetEntryAddFormattedFieldsHook = (args: {ctx: AppContext}) => Record<string, unknown>;

export type GetEntryResolveUserLogin = (args: {ctx: AppContext}) => Promise<string | undefined>;
Loading
Loading