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 entry create/update availability check #224

Merged
merged 2 commits into from
Dec 24, 2024
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
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 {GatewayApi} from '../../src/registry';
export type {
CheckCreateEntryAvailability,
CheckUpdateEntryAvailability,
} from '../../src/registry/common/utils/entry/types';
4 changes: 4 additions & 0 deletions src/registry/common/functions-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type {BulkFetchWorkbooksAllPermissions} from './entities/workbook/types';
import type {ColorPalettesAdminValidator} from './utils/color-palettes/types';
import type {CheckEmbedding} from './utils/embedding/types';
import type {
CheckCreateEntryAvailability,
CheckUpdateEntryAvailability,
GetEntryAddFormattedFieldsHook,
GetEntryBeforeDbRequestHook,
IsNeedBypassEntryByKey,
Expand All @@ -23,4 +25,6 @@ export const commonFunctionsMap = {
getEntryBeforeDbRequestHook: makeFunctionTemplate<GetEntryBeforeDbRequestHook>(),
getEntryAddFormattedFieldsHook: makeFunctionTemplate<GetEntryAddFormattedFieldsHook>(),
checkEmbedding: makeFunctionTemplate<CheckEmbedding>(),
checkCreateEntryAvailability: makeFunctionTemplate<CheckCreateEntryAvailability>(),
checkUpdateEntryAvailability: makeFunctionTemplate<CheckUpdateEntryAvailability>(),
} as const;
13 changes: 13 additions & 0 deletions src/registry/common/utils/entry/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {AppContext} from '@gravity-ui/nodekit';

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

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

Expand All @@ -13,3 +14,15 @@ export type GetEntryAddFormattedFieldsHook = (args: {
ctx: AppContext;
result: GetEntryResult;
}) => Promise<Record<string, unknown>>;

export type CheckCreateEntryAvailability = (args: {
ctx: AppContext;
scope: EntryScope;
type: string | undefined;
}) => Promise<void>;

export type CheckUpdateEntryAvailability = (args: {
ctx: AppContext;
scope: EntryScope;
type: string | undefined;
}) => Promise<void>;
10 changes: 10 additions & 0 deletions src/services/entry.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export default class EntryService {
initialParentId,
ctx,
}: ST.CreateEntry) {
const registry = ctx.get('registry');
const {checkCreateEntryAvailability} = registry.common.functions.get();

await checkCreateEntryAvailability({ctx, scope, type});

if (workbookId) {
return await createEntryInWorkbook(ctx, {
workbookId,
Expand Down Expand Up @@ -106,6 +111,11 @@ export default class EntryService {
initialParentId,
ctx,
}: ST.CreateEntry) {
const registry = ctx.get('registry');
const {checkCreateEntryAvailability} = registry.common.functions.get();

await checkCreateEntryAvailability({ctx, scope, type});

if (workbookId) {
return await createEntryInWorkbook(ctx, {
workbookId,
Expand Down
12 changes: 10 additions & 2 deletions src/services/entry/actions/update-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,19 @@ export async function updateEntry(ctx: CTX, updateData: UpdateEntryData) {
.timeout(DEFAULT_QUERY_TIMEOUT);

if (entry) {
let checkEntryPromise;

if (!isPrivateRoute && !entry.workbookId) {
await checkEntry(ctx, Entry.replica, {verifiableEntry: entry});
checkEntryPromise = checkEntry(ctx, Entry.replica, {verifiableEntry: entry});
}

await Lock.checkLock({entryId, lockToken}, ctx);
const {checkUpdateEntryAvailability} = registry.common.functions.get();

await Promise.all([
checkEntryPromise,
Lock.checkLock({entryId, lockToken}, ctx),
Comment on lines +159 to +160
Copy link
Contributor Author

Choose a reason for hiding this comment

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

as I see checkEntry and checkLock are independent, so i guess we could run them in parallel

checkUpdateEntryAvailability({ctx, scope: entry.scope, type: entry.type}),
]);
} else {
throw new AppError(US_ERRORS.NOT_EXIST_ENTRY, {
code: US_ERRORS.NOT_EXIST_ENTRY,
Expand Down
Loading