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 <[email protected]>
This commit is contained in:
1 parent
986d196a37
commit
68e7e76b31
6 files changed
+98
-2
No files matched your search
+15
@@ -179,6 +179,21 @@ describe("OAuth Client Users Endpoints", () => {
|
|||||||
).toBeTruthy();
|
).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 () => {
|
it(`/GET/:id`, async () => {
|
||||||
const response = await request(app.getHttpServer())
|
const response = await request(app.getHttpServer())
|
||||||
.get(`/api/v2/oauth-clients/${oAuthClient.id}/users/${postResponseData.user.id}`)
|
.get(`/api/v2/oauth-clients/${oAuthClient.id}/users/${postResponseData.user.id}`)
|
||||||
|
|||||||
+23
-1
@@ -20,11 +20,12 @@ import {
|
|||||||
Patch,
|
Patch,
|
||||||
BadRequestException,
|
BadRequestException,
|
||||||
Delete,
|
Delete,
|
||||||
|
Query,
|
||||||
} from "@nestjs/common";
|
} from "@nestjs/common";
|
||||||
import { User } from "@prisma/client";
|
import { User } from "@prisma/client";
|
||||||
|
|
||||||
import { SUCCESS_STATUS } from "@calcom/platform-constants";
|
import { SUCCESS_STATUS } from "@calcom/platform-constants";
|
||||||
import { ApiResponse } from "@calcom/platform-types";
|
import { ApiResponse, Pagination } from "@calcom/platform-types";
|
||||||
|
|
||||||
@Controller({
|
@Controller({
|
||||||
path: "oauth-clients/:clientId/users",
|
path: "oauth-clients/:clientId/users",
|
||||||
@@ -39,6 +40,27 @@ export class OAuthClientUsersController {
|
|||||||
private readonly oauthRepository: OAuthClientRepository
|
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("/")
|
@Post("/")
|
||||||
@UseGuards(OAuthClientCredentialsGuard)
|
@UseGuards(OAuthClientCredentialsGuard)
|
||||||
async createUser(
|
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) {
|
async update(userId: number, updateData: UpdateManagedPlatformUserInput) {
|
||||||
this.formatInput(updateData);
|
this.formatInput(updateData);
|
||||||
|
|
||||||
|
|||||||
@@ -71,6 +71,31 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/api/v2/oauth-clients/{clientId}/users": {
|
"/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": {
|
"post": {
|
||||||
"operationId": "OAuthClientUsersController_createUser",
|
"operationId": "OAuthClientUsersController_createUser",
|
||||||
"parameters": [
|
"parameters": [
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ const env: Partial<Omit<Environment, "NODE_ENV">> = {
|
|||||||
LOG_LEVEL: "trace",
|
LOG_LEVEL: "trace",
|
||||||
REDIS_URL: "redis://localhost:9199",
|
REDIS_URL: "redis://localhost:9199",
|
||||||
};
|
};
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore
|
||||||
process.env = {
|
process.env = {
|
||||||
...env,
|
...env,
|
||||||
...process.env,
|
...process.env,
|
||||||
|
|||||||
@@ -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 { Response as BaseResponse } from "express";
|
||||||
|
|
||||||
import type { ERROR_STATUS, SUCCESS_STATUS, API_ERROR_CODES } from "@calcom/platform-constants";
|
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
|
export type ApiResponse<T = undefined> = T extends undefined
|
||||||
? ApiSuccessResponseWithoutData | ApiErrorResponse
|
? ApiSuccessResponseWithoutData | ApiErrorResponse
|
||||||
: ApiSuccessResponse<T> | 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;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user