From a23e6998597256a3b1daac8df53387faed36590a Mon Sep 17 00:00:00 2001 From: Ricardo Garim Date: Wed, 16 Oct 2024 18:41:00 -0300 Subject: [PATCH] chore!: make get directory stop to use query param (#33563) --- .../app/api/server/helpers/parseJsonQuery.ts | 7 +++- apps/meteor/app/api/server/v1/misc.ts | 12 ++++-- .../directory/hooks/useDirectoryQuery.ts | 10 ++--- .../tests/end-to-end/api/miscellaneous.ts | 42 +++++++------------ packages/rest-typings/src/v1/directory.ts | 23 +++++++--- 5 files changed, 50 insertions(+), 44 deletions(-) diff --git a/apps/meteor/app/api/server/helpers/parseJsonQuery.ts b/apps/meteor/app/api/server/helpers/parseJsonQuery.ts index 7f3b606fd2d2..e1574ce04559 100644 --- a/apps/meteor/app/api/server/helpers/parseJsonQuery.ts +++ b/apps/meteor/app/api/server/helpers/parseJsonQuery.ts @@ -54,7 +54,12 @@ export async function parseJsonQuery(api: PartialThis): Promise<{ } // TODO: Remove this once we have all routes migrated to the new API params - const hasSupportedRoutes = ['/api/v1/channels.files', '/api/v1/integrations.list', '/api/v1/custom-user-status.list'].includes(route); + const hasSupportedRoutes = [ + '/api/v1/directory', + '/api/v1/channels.files', + '/api/v1/integrations.list', + '/api/v1/custom-user-status.list', + ].includes(route); const isUnsafeQueryParamsAllowed = process.env.ALLOW_UNSAFE_QUERY_AND_FIELDS_API_PARAMS?.toUpperCase() === 'TRUE'; const messageGenerator = ({ endpoint, version, parameter }: { endpoint: string; version: string; parameter: string }): string => diff --git a/apps/meteor/app/api/server/v1/misc.ts b/apps/meteor/app/api/server/v1/misc.ts index 5d7505074a01..349dbe824bb2 100644 --- a/apps/meteor/app/api/server/v1/misc.ts +++ b/apps/meteor/app/api/server/v1/misc.ts @@ -363,8 +363,14 @@ API.v1.addRoute( async get() { const { offset, count } = await getPaginationItems(this.queryParams); const { sort, query } = await this.parseJsonQuery(); + const { text, type, workspace = 'local' } = this.queryParams; - const { text, type, workspace = 'local' } = query; + const filter = { + ...(query ? { ...query } : {}), + ...(text ? { text } : {}), + ...(type ? { type } : {}), + ...(workspace ? { workspace } : {}), + }; if (sort && Object.keys(sort).length > 1) { return API.v1.failure('This method support only one "sort" parameter'); @@ -373,9 +379,7 @@ API.v1.addRoute( const sortDirection = sort && Object.values(sort)[0] === 1 ? 'asc' : 'desc'; const result = await Meteor.callAsync('browseChannels', { - text, - type, - workspace, + ...filter, sortBy, sortDirection, offset: Math.max(0, offset), diff --git a/apps/meteor/client/views/directory/hooks/useDirectoryQuery.ts b/apps/meteor/client/views/directory/hooks/useDirectoryQuery.ts index bbc16568a645..da0c3dfac7a2 100644 --- a/apps/meteor/client/views/directory/hooks/useDirectoryQuery.ts +++ b/apps/meteor/client/views/directory/hooks/useDirectoryQuery.ts @@ -5,14 +5,12 @@ export function useDirectoryQuery( [column, direction]: [string, 'asc' | 'desc'], type: string, workspace = 'local', -): { offset?: number | undefined; count?: number; query: string; sort: string } { +): { sort: string; offset?: number | undefined; count?: number; query?: string; text?: string; type?: string; workspace?: string } { return useMemo( () => ({ - query: JSON.stringify({ - type, - text, - workspace, - }), + text, + type, + workspace, sort: JSON.stringify({ [column]: direction === 'asc' ? 1 : -1 }), ...(itemsPerPage && { count: itemsPerPage }), ...(current && { offset: current }), diff --git a/apps/meteor/tests/end-to-end/api/miscellaneous.ts b/apps/meteor/tests/end-to-end/api/miscellaneous.ts index f664882cb385..b6abac4f2014 100644 --- a/apps/meteor/tests/end-to-end/api/miscellaneous.ts +++ b/apps/meteor/tests/end-to-end/api/miscellaneous.ts @@ -233,10 +233,8 @@ describe('miscellaneous', () => { .get(api('directory')) .set(credentials) .query({ - query: JSON.stringify({ - text: user.username, - type: 'users', - }), + text: user.username, + type: 'users', }) .expect('Content-Type', 'application/json') .expect(200) @@ -260,10 +258,8 @@ describe('miscellaneous', () => { .get(api('directory')) .set(normalUserCredentials) .query({ - query: JSON.stringify({ - text: user.username, - type: 'users', - }), + text: user.username, + type: 'users', }) .expect('Content-Type', 'application/json') .expect(200) @@ -286,10 +282,8 @@ describe('miscellaneous', () => { .get(api('directory')) .set(credentials) .query({ - query: JSON.stringify({ - text: testChannel.name, - type: 'channels', - }), + text: testChannel.name, + type: 'channels', }) .expect('Content-Type', 'application/json') .expect(200) @@ -311,10 +305,8 @@ describe('miscellaneous', () => { .get(api('directory')) .set(credentials) .query({ - query: JSON.stringify({ - text: testChannel.name, - type: 'channels', - }), + text: testChannel.name, + type: 'channels', sort: JSON.stringify({ name: 1, }), @@ -339,10 +331,8 @@ describe('miscellaneous', () => { .get(api('directory')) .set(credentials) .query({ - query: JSON.stringify({ - text: 'invalid channel', - type: 'invalid', - }), + text: 'invalid channel', + type: 'invalid', }) .expect('Content-Type', 'application/json') .expect(400) @@ -356,10 +346,8 @@ describe('miscellaneous', () => { .get(api('directory')) .set(credentials) .query({ - query: JSON.stringify({ - text: testChannel.name, - type: 'channels', - }), + text: testChannel.name, + type: 'channels', sort: JSON.stringify({ name: 1, test: 1, @@ -378,10 +366,8 @@ describe('miscellaneous', () => { .get(api('directory')) .set(normalUserCredentials) .query({ - query: JSON.stringify({ - text: '', - type: 'teams', - }), + text: '', + type: 'teams', sort: JSON.stringify({ name: 1, }), diff --git a/packages/rest-typings/src/v1/directory.ts b/packages/rest-typings/src/v1/directory.ts index 930c8fc9abe5..391d0f7b4a37 100644 --- a/packages/rest-typings/src/v1/directory.ts +++ b/packages/rest-typings/src/v1/directory.ts @@ -8,15 +8,11 @@ const ajv = new Ajv({ coerceTypes: true, }); -type DirectoryProps = PaginatedRequest; +type DirectoryProps = PaginatedRequest<{ text?: string; type?: string; workspace?: string; query?: string }>; const DirectorySchema = { type: 'object', properties: { - query: { - type: 'string', - nullable: true, - }, count: { type: 'number', nullable: true, @@ -29,7 +25,24 @@ const DirectorySchema = { type: 'string', nullable: true, }, + text: { + type: 'string', + nullable: true, + }, + type: { + type: 'string', + nullable: true, + }, + workspace: { + type: 'string', + nullable: true, + }, + query: { + type: 'string', + nullable: true, + }, }, + required: [], additionalProperties: false, };