Skip to content

Scheduling infrastructure for absolutely everyone.

feat: list managed users of an oauth client (#14330)

Morgan68e7e76b315d
diff --git a/apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/oauth-client-users.controller.e2e-spec.ts b/apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/oauth-client-users.controller.e2e-spec.ts
index fd9fce0f157..c09aa43808c 100644
--- a/apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/oauth-client-users.controller.e2e-spec.ts
+++ b/apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/oauth-client-users.controller.e2e-spec.ts
@@ -179,6 +179,21 @@ describe("OAuth Client Users Endpoints", () => {
).toBeTruthy();
}
+ it(`/GET: return list of managed users`, async () => {
+ const response = await request(app.getHttpServer())
+ .get(`/api/v2/oauth-clients/${oAuthClient.id}/users?limit=10&offset=0`)
+ .set("x-cal-secret-key", oAuthClient.secret)
+ .set("Origin", `${CLIENT_REDIRECT_URI}`)
+ .expect(200);
+
+ const responseBody: ApiSuccessResponse<UserReturned[]> = response.body;
+
+ expect(responseBody.status).toEqual(SUCCESS_STATUS);
+ expect(responseBody.data).toBeDefined();
+ expect(responseBody.data?.length).toBeGreaterThan(0);
+ expect(responseBody.data[0].email).toEqual(postResponseData.user.email);
+ });
+
it(`/GET/:id`, async () => {
const response = await request(app.getHttpServer())
.get(`/api/v2/oauth-clients/${oAuthClient.id}/users/${postResponseData.user.id}`)
diff --git a/apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/oauth-client-users.controller.ts b/apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/oauth-client-users.controller.ts
index 26a92ab3cdb..1cc99141353 100644
--- a/apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/oauth-client-users.controller.ts
+++ b/apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/oauth-client-users.controller.ts
@@ -20,11 +20,12 @@ import {
Patch,
BadRequestException,
Delete,
+ Query,
} from "@nestjs/common";
import { User } from "@prisma/client";
import { SUCCESS_STATUS } from "@calcom/platform-constants";
-import { ApiResponse } from "@calcom/platform-types";
+import { ApiResponse, Pagination } from "@calcom/platform-types";
@Controller({
path: "oauth-clients/:clientId/users",
@@ -39,6 +40,27 @@ export class OAuthClientUsersController {
private readonly oauthRepository: OAuthClientRepository
) {}
+ @Get("/")
+ @UseGuards(OAuthClientCredentialsGuard)
+ async getManagedUsers(
+ @Param("clientId") oAuthClientId: string,
+ @Query() queryParams: Pagination
+ ): Promise<ApiResponse<User[]>> {
+ this.logger.log(`getting managed users with data for OAuth Client with ID ${oAuthClientId}`);
+ const { offset, limit } = queryParams;
+
+ const existingUsers = await this.userRepository.findManagedUsersByOAuthClientId(
+ oAuthClientId,
+ offset ?? 0,
+ limit ?? 50
+ );
+
+ return {
+ status: SUCCESS_STATUS,
+ data: existingUsers,
+ };
+ }
+
@Post("/")
@UseGuards(OAuthClientCredentialsGuard)
async createUser(
diff --git a/apps/api/v2/src/modules/users/users.repository.ts b/apps/api/v2/src/modules/users/users.repository.ts
index 57555ce1c3c..ed68ecea267 100644
--- a/apps/api/v2/src/modules/users/users.repository.ts
+++ b/apps/api/v2/src/modules/users/users.repository.ts
@@ -102,6 +102,21 @@ export class UsersRepository {
});
}
+ async findManagedUsersByOAuthClientId(oauthClientId: string, cursor: number, limit: number) {
+ return this.dbRead.prisma.user.findMany({
+ where: {
+ platformOAuthClients: {
+ some: {
+ id: oauthClientId,
+ },
+ },
+ isPlatformManaged: true,
+ },
+ take: limit,
+ skip: cursor,
+ });
+ }
+
async update(userId: number, updateData: UpdateManagedPlatformUserInput) {
this.formatInput(updateData);
diff --git a/apps/api/v2/swagger/documentation.json b/apps/api/v2/swagger/documentation.json
index ec595b86be3..d573e7ab536 100644
--- a/apps/api/v2/swagger/documentation.json
+++ b/apps/api/v2/swagger/documentation.json
@@ -71,6 +71,31 @@
}
},
"/api/v2/oauth-clients/{clientId}/users": {
+ "get": {
+ "operationId": "OAuthClientUsersController_getManagedUsers",
+ "parameters": [
+ {
+ "name": "clientId",
+ "required": true,
+ "in": "path",
+ "schema": {
+ "type": "string"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "",
+ "content": {
+ "application/json": {
+ "schema": {
+ "type": "object"
+ }
+ }
+ }
+ }
+ }
+ },
"post": {
"operationId": "OAuthClientUsersController_createUser",
"parameters": [
diff --git a/apps/api/v2/test/setEnvVars.ts b/apps/api/v2/test/setEnvVars.ts
index d353423dfa1..a0e9bce9055 100644
--- a/apps/api/v2/test/setEnvVars.ts
+++ b/apps/api/v2/test/setEnvVars.ts
@@ -9,7 +9,8 @@ const env: Partial<Omit<Environment, "NODE_ENV">> = {
LOG_LEVEL: "trace",
REDIS_URL: "redis://localhost:9199",
};
-
+// eslint-disable-next-line @typescript-eslint/ban-ts-comment
+// @ts-ignore
process.env = {
...env,
...process.env,
diff --git a/packages/platform/types/api.ts b/packages/platform/types/api.ts
index 6b067f28697..b2039ed22f8 100644
--- a/packages/platform/types/api.ts
+++ b/packages/platform/types/api.ts
@@ -1,3 +1,5 @@
+import { Transform } from "class-transformer";
+import { IsNumber, Min, Max, IsOptional } from "class-validator";
import type { Response as BaseResponse } from "express";
import type { ERROR_STATUS, SUCCESS_STATUS, API_ERROR_CODES } from "@calcom/platform-constants";
@@ -29,3 +31,19 @@ export type Response<T = unknown> = BaseResponse<ApiResponse<T>>;
export type ApiResponse<T = undefined> = T extends undefined
? ApiSuccessResponseWithoutData | ApiErrorResponse
: ApiSuccessResponse<T> | ApiErrorResponse;
+
+export class Pagination {
+ @Transform(({ value }: { value: string }) => value && parseInt(value))
+ @IsNumber()
+ @Min(1)
+ @Max(100)
+ @IsOptional()
+ limit?: number;
+
+ @Transform(({ value }: { value: string }) => value && parseInt(value))
+ @IsNumber()
+ @Min(0)
+ @Max(100)
+ @IsOptional()
+ offset?: number | null;
+}