Skip to content

Commit

Permalink
feat: add pending status to the endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tapiarafael committed Nov 6, 2023
1 parent 2cb1c1d commit 9f14c8f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
27 changes: 26 additions & 1 deletion apps/meteor/app/api/server/v1/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
? [
Expand All @@ -605,8 +630,8 @@ API.v1.addRoute(
.aggregate<{ sortedResults: IUser[]; totalCount: { total: number }[] }>([
{
$match: {
active: status === 'active',
roles: role,
...match,
},
},
{
Expand Down
3 changes: 2 additions & 1 deletion packages/rest-typings/src/v1/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -152,7 +153,7 @@ export type UsersEndpoints = {
};

'/v1/users.list/:status': {
GET: (params: PaginatedRequest<{ fields: string }>) => PaginatedResult<{
GET: (params: UsersListStatusParamsGET) => PaginatedResult<{
users: Pick<IUser, '_id' | 'username' | 'name' | 'status' | 'roles' | 'emails' | 'active' | 'avatarETag'>[];
}>;
};
Expand Down
22 changes: 22 additions & 0 deletions packages/rest-typings/src/v1/users/UsersListStatusParamsGET.ts
Original file line number Diff line number Diff line change
@@ -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<UsersListStatusParamsGET>(UsersListStatusParamsGetSchema);

0 comments on commit 9f14c8f

Please sign in to comment.