From 9f14c8f74f8adad4ac7c8b0c0c33a027422d6707 Mon Sep 17 00:00:00 2001 From: Rafael Tapia Date: Mon, 6 Nov 2023 17:46:28 -0300 Subject: [PATCH] feat: add pending status to the endpoint --- apps/meteor/app/api/server/v1/users.ts | 27 ++++++++++++++++++- packages/rest-typings/src/v1/users.ts | 3 ++- .../src/v1/users/UsersListStatusParamsGET.ts | 22 +++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 packages/rest-typings/src/v1/users/UsersListStatusParamsGET.ts diff --git a/apps/meteor/app/api/server/v1/users.ts b/apps/meteor/app/api/server/v1/users.ts index 256ced775bc3..1d581528f790 100644 --- a/apps/meteor/app/api/server/v1/users.ts +++ b/apps/meteor/app/api/server/v1/users.ts @@ -592,6 +592,31 @@ API.v1.addRoute( actualSort.nameInsensitive = sort.name; } + let match; + + switch (status) { + case 'active': + match = { + active: true, + lastLogin: { $exists: true }, + }; + break; + case 'inactive': + match = { + active: false, + lastLogin: { $exists: true }, + }; + break; + case 'pending': + match = { + lastLogin: { $exists: false }, + type: { $ne: 'bot' }, + }; + break; + default: + throw new Meteor.Error('error-invalid-status', 'Invalid status parameter'); + } + const limit = count !== 0 ? [ @@ -605,8 +630,8 @@ API.v1.addRoute( .aggregate<{ sortedResults: IUser[]; totalCount: { total: number }[] }>([ { $match: { - active: status === 'active', roles: role, + ...match, }, }, { diff --git a/packages/rest-typings/src/v1/users.ts b/packages/rest-typings/src/v1/users.ts index a58720f04db8..6b2affd10d72 100644 --- a/packages/rest-typings/src/v1/users.ts +++ b/packages/rest-typings/src/v1/users.ts @@ -18,6 +18,7 @@ import type { UserRegisterParamsPOST } from './users/UserRegisterParamsPOST'; import type { UserSetActiveStatusParamsPOST } from './users/UserSetActiveStatusParamsPOST'; import type { UsersAutocompleteParamsGET } from './users/UsersAutocompleteParamsGET'; import type { UsersInfoParamsGet } from './users/UsersInfoParamsGet'; +import type { UsersListStatusParamsGET } from './users/UsersListStatusParamsGET'; import type { UsersListTeamsParamsGET } from './users/UsersListTeamsParamsGET'; import type { UsersSendConfirmationEmailParamsPOST } from './users/UsersSendConfirmationEmailParamsPOST'; import type { UsersSetPreferencesParamsPOST } from './users/UsersSetPreferenceParamsPOST'; @@ -152,7 +153,7 @@ export type UsersEndpoints = { }; '/v1/users.list/:status': { - GET: (params: PaginatedRequest<{ fields: string }>) => PaginatedResult<{ + GET: (params: UsersListStatusParamsGET) => PaginatedResult<{ users: Pick[]; }>; }; diff --git a/packages/rest-typings/src/v1/users/UsersListStatusParamsGET.ts b/packages/rest-typings/src/v1/users/UsersListStatusParamsGET.ts new file mode 100644 index 000000000000..e4226afbf377 --- /dev/null +++ b/packages/rest-typings/src/v1/users/UsersListStatusParamsGET.ts @@ -0,0 +1,22 @@ +import Ajv from 'ajv'; + +import type { PaginatedRequest } from '../../helpers/PaginatedRequest'; + +const ajv = new Ajv({ + coerceTypes: true, +}); + +export type UsersListStatusParamsGET = { role: string } & PaginatedRequest<{ fields: string }>; + +const UsersListStatusParamsGetSchema = { + type: 'object', + properties: { + role: { + type: 'string', + }, + }, + required: ['role'], + additionalProperties: false, +}; + +export const isUsersListStatusProps = ajv.compile(UsersListStatusParamsGetSchema);