Skip to content

Commit 9a4cf14

Browse files
committed
Add filters to deleteEntry
1 parent 522ba9c commit 9a4cf14

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

src/components/zod/custom-types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export * from './json-string';
77
export * from './entity-name';
88
export * from './tenant-settings';
99
export * from './primitive';
10+
export * from './query-string-array';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {z} from 'zod';
2+
3+
export const queryStringArray = () => {
4+
return z
5+
.union([z.string(), z.array(z.string())])
6+
.transform((val) => (typeof val === 'string' ? [val] : val))
7+
.optional();
8+
};

src/controllers/entries/delete-entry.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {AppRouteHandler} from '@gravity-ui/expresskit';
22

33
import {prepareResponseAsync} from '../../components/response-presenter';
44
import {makeReqParser, z, zc} from '../../components/zod';
5+
import {EntryScope} from '../../db/models/new/entry/types';
56
import {LogEventType} from '../../registry/common/utils/log-event/types';
67
import {deleteEntry} from '../../services/entry';
78

@@ -11,6 +12,8 @@ const requestSchema = {
1112
}),
1213
query: z.object({
1314
lockToken: z.string().optional(),
15+
scope: z.nativeEnum(EntryScope).optional(),
16+
types: zc.queryStringArray().optional(),
1417
}),
1518
};
1619

@@ -30,6 +33,8 @@ export const deleteEntryController: AppRouteHandler = async (req, res) => {
3033
{
3134
entryId: params.entryId,
3235
lockToken: query.lockToken,
36+
scope: query.scope,
37+
types: query.types,
3338
},
3439
);
3540

src/services/entry/actions/delete-entry.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ import {AppError} from '@gravity-ui/nodekit';
22
import {transaction} from 'objection';
33

44
import {makeSchemaValidator} from '../../../components/validation-schema-compiler';
5-
import {BiTrackingLogs, DEFAULT_QUERY_TIMEOUT, RETURN_COLUMNS, US_ERRORS} from '../../../const';
5+
import {
6+
ALLOWED_SCOPE_VALUES,
7+
BiTrackingLogs,
8+
DEFAULT_QUERY_TIMEOUT,
9+
RETURN_COLUMNS,
10+
US_ERRORS,
11+
} from '../../../const';
612
import Entry from '../../../db/models/entry';
713
import Lock from '../../../db/models/lock';
14+
import {EntryColumn} from '../../../db/models/new/entry';
815
import {WorkbookPermission} from '../../../entities/workbook';
9-
import {DlsActions, EntryColumns, UsPermissions} from '../../../types/models';
16+
import {DlsActions, EntryColumns, EntryScope, UsPermissions} from '../../../types/models';
1017
import Utils, {makeUserId} from '../../../utils';
1118
import {ServiceArgs} from '../../new/types';
1219
import {getWorkbook} from '../../new/workbook/get-workbook';
@@ -28,20 +35,32 @@ const validateArgs = makeSchemaValidator({
2835
useLegacyLogin: {
2936
type: 'boolean',
3037
},
38+
scope: {
39+
type: 'string',
40+
enum: ALLOWED_SCOPE_VALUES,
41+
},
42+
types: {
43+
type: 'array',
44+
items: {
45+
type: 'string',
46+
},
47+
},
3148
},
3249
});
3350

3451
export type DeleteEntryData = {
3552
entryId: string;
3653
lockToken?: string;
3754
useLegacyLogin?: boolean;
55+
scope?: EntryScope;
56+
types?: string[];
3857
};
3958

4059
export async function deleteEntry(
4160
{ctx, skipValidation = false}: ServiceArgs,
4261
args: DeleteEntryData,
4362
) {
44-
const {entryId, lockToken, useLegacyLogin = false} = args;
63+
const {entryId, lockToken, useLegacyLogin = false, scope, types} = args;
4564

4665
ctx.log('DELETE_ENTRY_REQUEST', {
4766
entryId: Utils.encodeId(entryId),
@@ -66,6 +85,15 @@ export async function deleteEntry(
6685
entryId,
6786
isDeleted: false,
6887
})
88+
.where((builder) => {
89+
if (scope) {
90+
builder.andWhere({[`${Entry.tableName}.${EntryColumn.Scope}`]: scope});
91+
}
92+
93+
if (types) {
94+
builder.whereIn([`${Entry.tableName}.${EntryColumn.Type}`], types);
95+
}
96+
})
6997
.first()
7098
.timeout(DEFAULT_QUERY_TIMEOUT);
7199

0 commit comments

Comments
 (0)