diff --git a/apps/meteor/app/api/server/v1/roles.ts b/apps/meteor/app/api/server/v1/roles.ts index 8985e48c0aff5..17743fd9507e7 100644 --- a/apps/meteor/app/api/server/v1/roles.ts +++ b/apps/meteor/app/api/server/v1/roles.ts @@ -2,7 +2,6 @@ import { api, Authorization } from '@rocket.chat/core-services'; import type { IRole } from '@rocket.chat/core-typings'; import { Roles, Users } from '@rocket.chat/models'; import { ajv, isRoleAddUserToRoleProps, isRoleDeleteProps, isRoleRemoveUserFromRoleProps } from '@rocket.chat/rest-typings'; -import { check, Match } from 'meteor/check'; import { Meteor } from 'meteor/meteor'; import { removeUserFromRolesAsync } from '../../../../server/lib/roles/removeUserFromRoles'; @@ -29,29 +28,6 @@ API.v1.addRoute( }, ); -API.v1.addRoute( - 'roles.sync', - { authRequired: true }, - { - async get() { - check( - this.queryParams, - Match.ObjectIncluding({ - updatedSince: Match.Where((value: unknown): value is string => typeof value === 'string' && !Number.isNaN(Date.parse(value))), - }), - ); - - const { updatedSince } = this.queryParams; - - return API.v1.success({ - roles: { - update: await Roles.findByUpdatedDate(new Date(updatedSince)).toArray(), - remove: await Roles.trashFindDeletedAfter(new Date(updatedSince)).toArray(), - }, - }); - }, - }, -); API.v1.addRoute( 'roles.addUserToRole', @@ -225,38 +201,91 @@ API.v1.addRoute( }, ); -const rolesRoutes = API.v1.get( - 'roles.getUsersInPublicRoles', - { - authRequired: true, - response: { - 200: ajv.compile<{ - users: { - _id: string; - username: string; - roles: string[]; - }[]; - }>({ - type: 'object', - properties: { +const rolesRoutes = API.v1 + .get( + 'roles.getUsersInPublicRoles', + { + authRequired: true, + response: { + 200: ajv.compile<{ users: { - type: 'array', - items: { - type: 'object', - properties: { _id: { type: 'string' }, username: { type: 'string' }, roles: { type: 'array', items: { type: 'string' } } }, + _id: string; + username: string; + roles: string[]; + }[]; + }>({ + type: 'object', + properties: { + users: { + type: 'array', + items: { + type: 'object', + properties: { _id: { type: 'string' }, username: { type: 'string' }, roles: { type: 'array', items: { type: 'string' } } }, + }, }, }, + }), + }, + }, + async () => { + return API.v1.success({ + users: await Authorization.getUsersFromPublicRoles(), + }); + }, + ) + .get( + 'roles.sync', + { + authRequired: true, + query: ajv.compile<{ updatedSince: string }>({ + type: 'object', + properties: { + updatedSince: { + type: 'string', + }, }, + required: ['updatedSince'], + additionalProperties: false, }), + response: { + 200: ajv.compile<{ + roles: { + update: IRole[]; + remove: object[]; + }; + }>({ + type: 'object', + properties: { + roles: { + type: 'object', + properties: { + update: { type: 'array', items: { type: 'object' } }, + remove: { type: 'array', items: { type: 'object' } }, + }, + required: ['update', 'remove'], + }, + }, + required: ['roles'], + }), + }, }, - }, + async function () { + const { updatedSince } = this.queryParams; - async () => { - return API.v1.success({ - users: await Authorization.getUsersFromPublicRoles(), - }); - }, -); + if (Number.isNaN(Date.parse(updatedSince))) { + throw new Meteor.Error('error-updatedSince-param-invalid', 'The "updatedSince" query parameter must be a valid date.'); + } + + const updatedSinceDate = new Date(updatedSince); + + return API.v1.success({ + roles: { + update: await Roles.findByUpdatedDate(updatedSinceDate).toArray(), + remove: await Roles.trashFindDeletedAfter(updatedSinceDate).toArray(), + }, + }); + }, + ); type RolesEndpoints = ExtractRoutesFromAPI; diff --git a/packages/rest-typings/src/v1/roles.ts b/packages/rest-typings/src/v1/roles.ts index 93845a150cecd..a9db34e62224f 100644 --- a/packages/rest-typings/src/v1/roles.ts +++ b/packages/rest-typings/src/v1/roles.ts @@ -1,4 +1,4 @@ -import type { RocketChatRecordDeleted, IRole, IUserInRole } from '@rocket.chat/core-typings'; +import type { IRole, IUserInRole } from '@rocket.chat/core-typings'; import { ajv } from './Ajv'; import type { PaginatedRequest } from '../helpers/PaginatedRequest'; @@ -113,24 +113,12 @@ const RolesGetUsersInRolePropsSchema = { export const isRolesGetUsersInRoleProps = ajv.compile(RolesGetUsersInRolePropsSchema); -type RoleSyncProps = { - updatedSince?: string; -}; - export type RolesEndpoints = { '/v1/roles.list': { GET: () => { roles: IRole[]; }; }; - '/v1/roles.sync': { - GET: (params: RoleSyncProps) => { - roles: { - update: IRole[]; - remove: RocketChatRecordDeleted[]; - }; - }; - }; '/v1/roles.addUserToRole': { POST: (params: RoleAddUserToRoleProps) => {