Skip to content

Commit

Permalink
Add entry create/update availability check
Browse files Browse the repository at this point in the history
  • Loading branch information
stepanenkoxx committed Dec 24, 2024
1 parent ad57c9d commit 37f71c8
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 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 {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;
11 changes: 11 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,13 @@ export type GetEntryAddFormattedFieldsHook = (args: {
ctx: AppContext;
result: GetEntryResult;
}) => Promise<Record<string, unknown>>;

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

export type CheckUpdateEntryAvailability = (args: {
ctx: AppContext;
scope: EntryScope;
}) => 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});

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});

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),
checkUpdateEntryAvailability({ctx, scope: entry.scope}),
]);
} else {
throw new AppError(US_ERRORS.NOT_EXIST_ENTRY, {
code: US_ERRORS.NOT_EXIST_ENTRY,
Expand Down

0 comments on commit 37f71c8

Please sign in to comment.