From 4afbe2650680c3ede74a5156f31abb525b40215a Mon Sep 17 00:00:00 2001 From: Lauris Skraucis Date: Tue, 22 Oct 2024 15:17:12 +0200 Subject: [PATCH] refactor: v2 response in case of duplicate managed user (#17214) --- .../oauth-client-users.controller.e2e-spec.ts | 24 +++++++++++++++++++ .../outputs/create-managed-user.output.ts | 2 ++ .../services/oauth-clients-users.service.ts | 15 ++++++------ 3 files changed, 34 insertions(+), 7 deletions(-) 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 390690526b..880782c3bf 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 @@ -248,6 +248,30 @@ describe("OAuth Client Users Endpoints", () => { expect(schedule?.userId).toEqual(userId); } + it(`should fail /POST using already used managed user email`, async () => { + const requestBody: CreateManagedUserInput = { + email: userEmail, + timeZone: userTimeZone, + weekStart: "Monday", + timeFormat: 24, + locale: Locales.FR, + name: "Alice Smith", + }; + + const response = await request(app.getHttpServer()) + .post(`/api/v2/oauth-clients/${oAuthClient.id}/users`) + .set("x-cal-secret-key", oAuthClient.secret) + .send(requestBody) + .expect(409); + + const responseBody: CreateManagedUserOutput = response.body; + const error = responseBody.error; + expect(error).toBeDefined(); + expect(error?.message).toEqual( + `User with the provided e-mail already exists. Existing user ID=${postResponseData.user.id}` + ); + }); + 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`) diff --git a/apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/outputs/create-managed-user.output.ts b/apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/outputs/create-managed-user.output.ts index a34bab773c..54c2e40e0f 100644 --- a/apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/outputs/create-managed-user.output.ts +++ b/apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/outputs/create-managed-user.output.ts @@ -34,4 +34,6 @@ export class CreateManagedUserOutput { @ValidateNested() @Type(() => CreateManagedUserData) data!: CreateManagedUserData; + + error?: Error; } diff --git a/apps/api/v2/src/modules/oauth-clients/services/oauth-clients-users.service.ts b/apps/api/v2/src/modules/oauth-clients/services/oauth-clients-users.service.ts index c8ca87236c..83cc989817 100644 --- a/apps/api/v2/src/modules/oauth-clients/services/oauth-clients-users.service.ts +++ b/apps/api/v2/src/modules/oauth-clients/services/oauth-clients-users.service.ts @@ -5,7 +5,7 @@ import { TokensRepository } from "@/modules/tokens/tokens.repository"; import { CreateManagedUserInput } from "@/modules/users/inputs/create-managed-user.input"; import { UpdateManagedUserInput } from "@/modules/users/inputs/update-managed-user.input"; import { UsersRepository } from "@/modules/users/users.repository"; -import { BadRequestException, Injectable } from "@nestjs/common"; +import { BadRequestException, ConflictException, Injectable } from "@nestjs/common"; import { User } from "@prisma/client"; import { createNewUsersConnectToOrgIfExists, slugify } from "@calcom/platform-libraries"; @@ -26,9 +26,11 @@ export class OAuthClientUsersService { isPlatformManaged: boolean, organizationId?: number ) { - const existsWithEmail = await this.managedUserExistsWithEmail(oAuthClientId, body.email); - if (existsWithEmail) { - throw new BadRequestException("User with the provided e-mail already exists."); + const existingUser = await this.getExistingUserByEmail(oAuthClientId, body.email); + if (existingUser) { + throw new ConflictException( + `User with the provided e-mail already exists. Existing user ID=${existingUser.id}` + ); } let user: User; @@ -93,10 +95,9 @@ export class OAuthClientUsersService { }; } - async managedUserExistsWithEmail(oAuthClientId: string, email: string) { + async getExistingUserByEmail(oAuthClientId: string, email: string) { const oAuthEmail = this.getOAuthUserEmail(oAuthClientId, email); - const user = await this.userRepository.findByEmail(oAuthEmail); - return !!user; + return await this.userRepository.findByEmail(oAuthEmail); } async updateOAuthClientUser(oAuthClientId: string, userId: number, body: UpdateManagedUserInput) {