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

* feat: list managed users with apiv2 api

* refactor: isPending -> isLoading

* Revert "refactor: isPending -> isLoading"

This reverts commit dc2108e05508070185d57633346f3c532458e2a8.

---------

Co-authored-by: supalarry <laurisskraucis@gmail.com>
This commit is contained in:
Morgan
2024-04-04 09:41:47 +00:00
committed by GitHub
co-authored by supalarry
parent 986d196a37
commit 68e7e76b31
6 changed files with 98 additions and 2 deletions
@@ -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}`)
@@ -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(
@@ -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);
+25
View File
@@ -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": [
+2 -1
View File
@@ -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,
+18
View File
@@ -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;
}