Skip to content

Scheduling infrastructure for absolutely everyone.

refactor: v2 response in case of duplicate managed user (#17214)

Lauris Skraucis4afbe2650680
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 390690526b4..880782c3bf6 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 a34bab773cf..54c2e40e0f1 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 c8ca87236c5..83cc9898174 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) {