refactor: v2 docs (#14446)
* refactor: group endpoints by tags * refactor: remove swagger header * feat: managed users docs * feat: gcal docs * feat: provider docs * feat: managed users docs * fix: event-types locations docs * refactor: schedules remove forAtom logic - return just for atoms * feat: schedules docs * feat: schedules docs * fix: me tests * fix: schedules tests * feat: me docs * feat: calendar busy times docs * feat: calendar connected and destination calendars docs * feat: some docs for bookings endpoints * fix: add OAuth id when updating user email + disallow updating username * fix: creating user with same e-mail results in 500 * fix: me e2e tests * fix: event-types e2e tests * fix: oauth-client-users e2e tests * fix: some bookings e2e * revert: re-add name for create update user * fix: schedule create isDefault required * lock file from main * lock file from main * comment broken bookings tests * fix: connected calendars output type * fix: connected calendars output type
This commit is contained in:
1 parent
880ba8f1dd
commit
8382ebc057
68 files changed
+3608
-757
No files matched your search
@@ -1,10 +1,12 @@
|
||||
import { BadRequestException } from "@nestjs/common";
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { Transform, TransformFnParams } from "class-transformer";
|
||||
import { IsArray, IsDate, IsNumber } from "class-validator";
|
||||
|
||||
export class CreateAvailabilityInput {
|
||||
@IsArray()
|
||||
@IsNumber({}, { each: true })
|
||||
@ApiProperty({ example: [1, 2] })
|
||||
days!: number[];
|
||||
|
||||
@IsDate()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { PrismaReadService } from "@/modules/prisma/prisma-read.service";
|
||||
import { Controller, Get, NotFoundException, InternalServerErrorException, Query } from "@nestjs/common";
|
||||
import { ApiTags as DocsTags } from "@nestjs/swagger";
|
||||
|
||||
import { SUCCESS_STATUS } from "@calcom/platform-constants";
|
||||
import { getPublicEvent } from "@calcom/platform-libraries";
|
||||
@@ -11,6 +12,7 @@ import { PrismaClient } from "@calcom/prisma";
|
||||
path: "events",
|
||||
version: "2",
|
||||
})
|
||||
@DocsTags("Event types")
|
||||
export class EventsController {
|
||||
constructor(private readonly prismaReadService: PrismaReadService) {}
|
||||
|
||||
|
||||
+19
-10
@@ -7,8 +7,8 @@ import {
|
||||
CreateUserResponse,
|
||||
UserReturned,
|
||||
} from "@/modules/oauth-clients/controllers/oauth-client-users/oauth-client-users.controller";
|
||||
import { CreateManagedPlatformUserInput } from "@/modules/users/inputs/create-managed-platform-user.input";
|
||||
import { UpdateManagedPlatformUserInput } from "@/modules/users/inputs/update-managed-platform-user.input";
|
||||
import { CreateManagedUserInput } from "@/modules/users/inputs/create-managed-user.input";
|
||||
import { UpdateManagedUserInput } from "@/modules/users/inputs/update-managed-user.input";
|
||||
import { UsersModule } from "@/modules/users/users.module";
|
||||
import { INestApplication } from "@nestjs/common";
|
||||
import { NestExpressApplication } from "@nestjs/platform-express";
|
||||
@@ -78,6 +78,8 @@ describe("OAuth Client Users Endpoints", () => {
|
||||
|
||||
let postResponseData: CreateUserResponse;
|
||||
|
||||
const userEmail = "[email protected]";
|
||||
|
||||
beforeAll(async () => {
|
||||
const moduleRef = await Test.createTestingModule({
|
||||
providers: [PrismaExceptionFilter, HttpExceptionFilter],
|
||||
@@ -117,8 +119,8 @@ describe("OAuth Client Users Endpoints", () => {
|
||||
});
|
||||
|
||||
it(`should fail /POST with incorrect timeZone`, async () => {
|
||||
const requestBody: CreateManagedPlatformUserInput = {
|
||||
email: "[email protected]",
|
||||
const requestBody: CreateManagedUserInput = {
|
||||
email: userEmail,
|
||||
timeZone: "incorrect-time-zone",
|
||||
};
|
||||
|
||||
@@ -130,8 +132,8 @@ describe("OAuth Client Users Endpoints", () => {
|
||||
});
|
||||
|
||||
it(`/POST`, async () => {
|
||||
const requestBody: CreateManagedPlatformUserInput = {
|
||||
email: "[email protected]",
|
||||
const requestBody: CreateManagedUserInput = {
|
||||
email: userEmail,
|
||||
};
|
||||
|
||||
const response = await request(app.getHttpServer())
|
||||
@@ -150,7 +152,7 @@ describe("OAuth Client Users Endpoints", () => {
|
||||
|
||||
expect(responseBody.status).toEqual(SUCCESS_STATUS);
|
||||
expect(responseBody.data).toBeDefined();
|
||||
expect(responseBody.data.user.email).toEqual(requestBody.email);
|
||||
expect(responseBody.data.user.email).toEqual(getOAuthUserEmail(oAuthClient.id, requestBody.email));
|
||||
expect(responseBody.data.accessToken).toBeDefined();
|
||||
expect(responseBody.data.refreshToken).toBeDefined();
|
||||
|
||||
@@ -205,12 +207,12 @@ describe("OAuth Client Users Endpoints", () => {
|
||||
|
||||
expect(responseBody.status).toEqual(SUCCESS_STATUS);
|
||||
expect(responseBody.data).toBeDefined();
|
||||
expect(responseBody.data.email).toEqual(postResponseData.user.email);
|
||||
expect(responseBody.data.email).toEqual(getOAuthUserEmail(oAuthClient.id, userEmail));
|
||||
});
|
||||
|
||||
it(`/PUT/:id`, async () => {
|
||||
const userUpdatedEmail = "[email protected]";
|
||||
const body: UpdateManagedPlatformUserInput = { email: userUpdatedEmail };
|
||||
const body: UpdateManagedUserInput = { email: userUpdatedEmail };
|
||||
|
||||
const response = await request(app.getHttpServer())
|
||||
.patch(`/api/v2/oauth-clients/${oAuthClient.id}/users/${postResponseData.user.id}`)
|
||||
@@ -223,7 +225,7 @@ describe("OAuth Client Users Endpoints", () => {
|
||||
|
||||
expect(responseBody.status).toEqual(SUCCESS_STATUS);
|
||||
expect(responseBody.data).toBeDefined();
|
||||
expect(responseBody.data.email).toEqual(userUpdatedEmail);
|
||||
expect(responseBody.data.email).toEqual(getOAuthUserEmail(oAuthClient.id, userUpdatedEmail));
|
||||
});
|
||||
|
||||
it(`/DELETE/:id`, () => {
|
||||
@@ -234,6 +236,13 @@ describe("OAuth Client Users Endpoints", () => {
|
||||
.expect(200);
|
||||
});
|
||||
|
||||
function getOAuthUserEmail(oAuthClientId: string, userEmail: string) {
|
||||
const [username, emailDomain] = userEmail.split("@");
|
||||
const email = `${username}+${oAuthClientId}@${emailDomain}`;
|
||||
|
||||
return email;
|
||||
}
|
||||
|
||||
afterAll(async () => {
|
||||
await oauthClientRepositoryFixture.delete(oAuthClient.id);
|
||||
await teamRepositoryFixture.delete(organization.id);
|
||||
|
||||
+38
-33
@@ -1,10 +1,16 @@
|
||||
import { GetUser } from "@/modules/auth/decorators/get-user/get-user.decorator";
|
||||
import { AccessTokenGuard } from "@/modules/auth/guards/access-token/access-token.guard";
|
||||
import { CreateManagedUserOutput } from "@/modules/oauth-clients/controllers/oauth-client-users/outputs/create-managed-user.output";
|
||||
import { DeleteManagedUserOutput } from "@/modules/oauth-clients/controllers/oauth-client-users/outputs/delete-managed-user.output";
|
||||
import { GetManagedUserOutput } from "@/modules/oauth-clients/controllers/oauth-client-users/outputs/get-managed-user.output";
|
||||
import { GetManagedUsersOutput } from "@/modules/oauth-clients/controllers/oauth-client-users/outputs/get-managed-users.output";
|
||||
import { ManagedUserOutput } from "@/modules/oauth-clients/controllers/oauth-client-users/outputs/managed-user.output";
|
||||
import { UpdateManagedUserOutput } from "@/modules/oauth-clients/controllers/oauth-client-users/outputs/update-managed-user.output";
|
||||
import { OAuthClientCredentialsGuard } from "@/modules/oauth-clients/guards/oauth-client-credentials/oauth-client-credentials.guard";
|
||||
import { OAuthClientRepository } from "@/modules/oauth-clients/oauth-client.repository";
|
||||
import { OAuthClientUsersService } from "@/modules/oauth-clients/services/oauth-clients-users.service";
|
||||
import { CreateManagedPlatformUserInput } from "@/modules/users/inputs/create-managed-platform-user.input";
|
||||
import { UpdateManagedPlatformUserInput } from "@/modules/users/inputs/update-managed-platform-user.input";
|
||||
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 {
|
||||
Body,
|
||||
@@ -22,15 +28,17 @@ import {
|
||||
Delete,
|
||||
Query,
|
||||
} from "@nestjs/common";
|
||||
import { ApiTags as DocsTags } from "@nestjs/swagger";
|
||||
import { User } from "@prisma/client";
|
||||
|
||||
import { SUCCESS_STATUS } from "@calcom/platform-constants";
|
||||
import { ApiResponse, Pagination } from "@calcom/platform-types";
|
||||
import { Pagination } from "@calcom/platform-types";
|
||||
|
||||
@Controller({
|
||||
path: "oauth-clients/:clientId/users",
|
||||
version: "2",
|
||||
})
|
||||
@DocsTags("Managed users")
|
||||
export class OAuthClientUsersController {
|
||||
private readonly logger = new Logger("UserController");
|
||||
|
||||
@@ -45,7 +53,7 @@ export class OAuthClientUsersController {
|
||||
async getManagedUsers(
|
||||
@Param("clientId") oAuthClientId: string,
|
||||
@Query() queryParams: Pagination
|
||||
): Promise<ApiResponse<User[]>> {
|
||||
): Promise<GetManagedUsersOutput> {
|
||||
this.logger.log(`getting managed users with data for OAuth Client with ID ${oAuthClientId}`);
|
||||
const { offset, limit } = queryParams;
|
||||
|
||||
@@ -57,7 +65,7 @@ export class OAuthClientUsersController {
|
||||
|
||||
return {
|
||||
status: SUCCESS_STATUS,
|
||||
data: existingUsers,
|
||||
data: existingUsers.map((user) => getResponseUser(user)),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -65,8 +73,8 @@ export class OAuthClientUsersController {
|
||||
@UseGuards(OAuthClientCredentialsGuard)
|
||||
async createUser(
|
||||
@Param("clientId") oAuthClientId: string,
|
||||
@Body() body: CreateManagedPlatformUserInput
|
||||
): Promise<ApiResponse<CreateUserResponse>> {
|
||||
@Body() body: CreateManagedUserInput
|
||||
): Promise<CreateManagedUserOutput> {
|
||||
this.logger.log(
|
||||
`Creating user with data: ${JSON.stringify(body, null, 2)} for OAuth Client with ID ${oAuthClientId}`
|
||||
);
|
||||
@@ -88,11 +96,7 @@ export class OAuthClientUsersController {
|
||||
return {
|
||||
status: SUCCESS_STATUS,
|
||||
data: {
|
||||
user: {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
username: user.username,
|
||||
},
|
||||
user: getResponseUser(user),
|
||||
accessToken: tokens.accessToken,
|
||||
refreshToken: tokens.refreshToken,
|
||||
},
|
||||
@@ -108,7 +112,7 @@ export class OAuthClientUsersController {
|
||||
@Param("clientId") _: string,
|
||||
@GetUser("id") accessTokenUserId: number,
|
||||
@Param("userId") userId: number
|
||||
): Promise<ApiResponse<UserReturned>> {
|
||||
): Promise<GetManagedUserOutput> {
|
||||
if (accessTokenUserId !== userId) {
|
||||
throw new BadRequestException("userId parameter does not match access token");
|
||||
}
|
||||
@@ -120,11 +124,7 @@ export class OAuthClientUsersController {
|
||||
|
||||
return {
|
||||
status: SUCCESS_STATUS,
|
||||
data: {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
username: user.username,
|
||||
},
|
||||
data: getResponseUser(user),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -134,26 +134,22 @@ export class OAuthClientUsersController {
|
||||
async updateUser(
|
||||
// @Param("clientId") is added to generate OpenAPI schema correctly: clientId is in @Controller path, and unless
|
||||
// also added here as @Param, then it does not appear in OpenAPI schema.
|
||||
@Param("clientId") _: string,
|
||||
@Param("clientId") oAuthClientId: string,
|
||||
@GetUser("id") accessTokenUserId: number,
|
||||
@Param("userId") userId: number,
|
||||
@Body() body: UpdateManagedPlatformUserInput
|
||||
): Promise<ApiResponse<UserReturned>> {
|
||||
@Body() body: UpdateManagedUserInput
|
||||
): Promise<UpdateManagedUserOutput> {
|
||||
if (accessTokenUserId !== userId) {
|
||||
throw new BadRequestException("userId parameter does not match access token");
|
||||
}
|
||||
|
||||
this.logger.log(`Updating user with ID ${userId}: ${JSON.stringify(body, null, 2)}`);
|
||||
|
||||
const user = await this.userRepository.update(userId, body);
|
||||
const user = await this.oAuthClientUsersService.updateOAuthClientUser(oAuthClientId, userId, body);
|
||||
|
||||
return {
|
||||
status: SUCCESS_STATUS,
|
||||
data: {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
username: user.username,
|
||||
},
|
||||
data: getResponseUser(user),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -166,7 +162,7 @@ export class OAuthClientUsersController {
|
||||
@Param("clientId") _: string,
|
||||
@GetUser("id") accessTokenUserId: number,
|
||||
@Param("userId") userId: number
|
||||
): Promise<ApiResponse<UserReturned>> {
|
||||
): Promise<DeleteManagedUserOutput> {
|
||||
if (accessTokenUserId !== userId) {
|
||||
throw new BadRequestException("userId parameter does not match access token");
|
||||
}
|
||||
@@ -187,15 +183,24 @@ export class OAuthClientUsersController {
|
||||
|
||||
return {
|
||||
status: SUCCESS_STATUS,
|
||||
data: {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
username: user.username,
|
||||
},
|
||||
data: getResponseUser(user),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function getResponseUser(user: User): ManagedUserOutput {
|
||||
return {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
username: user.username,
|
||||
timeZone: user.timeZone,
|
||||
weekStart: user.weekStart,
|
||||
createdDate: user.createdDate,
|
||||
timeFormat: user.timeFormat,
|
||||
defaultScheduleId: user.defaultScheduleId,
|
||||
};
|
||||
}
|
||||
|
||||
export type UserReturned = Pick<User, "id" | "email" | "username">;
|
||||
|
||||
export type CreateUserResponse = { user: UserReturned; accessToken: string; refreshToken: string };
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
import { ManagedUserOutput } from "@/modules/oauth-clients/controllers/oauth-client-users/outputs/managed-user.output";
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { Type } from "class-transformer";
|
||||
import { IsEnum, IsString, ValidateNested } from "class-validator";
|
||||
|
||||
import { SUCCESS_STATUS, ERROR_STATUS } from "@calcom/platform-constants";
|
||||
|
||||
class CreateManagedUserData {
|
||||
@ApiProperty({
|
||||
type: ManagedUserOutput,
|
||||
})
|
||||
@ValidateNested()
|
||||
@Type(() => ManagedUserOutput)
|
||||
user!: ManagedUserOutput;
|
||||
|
||||
@IsString()
|
||||
accessToken!: string;
|
||||
|
||||
@IsString()
|
||||
refreshToken!: string;
|
||||
}
|
||||
|
||||
export class CreateManagedUserOutput {
|
||||
@ApiProperty({ example: SUCCESS_STATUS, enum: [SUCCESS_STATUS, ERROR_STATUS] })
|
||||
@IsEnum([SUCCESS_STATUS, ERROR_STATUS])
|
||||
status!: typeof SUCCESS_STATUS | typeof ERROR_STATUS;
|
||||
|
||||
@ApiProperty({
|
||||
type: CreateManagedUserData,
|
||||
})
|
||||
@ValidateNested()
|
||||
@Type(() => CreateManagedUserData)
|
||||
data!: CreateManagedUserData;
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
import { ManagedUserOutput } from "@/modules/oauth-clients/controllers/oauth-client-users/outputs/managed-user.output";
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { Type } from "class-transformer";
|
||||
import { IsEnum, ValidateNested } from "class-validator";
|
||||
|
||||
import { SUCCESS_STATUS, ERROR_STATUS } from "@calcom/platform-constants";
|
||||
|
||||
export class DeleteManagedUserOutput {
|
||||
@ApiProperty({ example: SUCCESS_STATUS, enum: [SUCCESS_STATUS, ERROR_STATUS] })
|
||||
@IsEnum([SUCCESS_STATUS, ERROR_STATUS])
|
||||
status!: typeof SUCCESS_STATUS | typeof ERROR_STATUS;
|
||||
|
||||
@Type(() => ManagedUserOutput)
|
||||
@ValidateNested()
|
||||
data!: ManagedUserOutput;
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
import { ManagedUserOutput } from "@/modules/oauth-clients/controllers/oauth-client-users/outputs/managed-user.output";
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { Type } from "class-transformer";
|
||||
import { IsEnum, ValidateNested } from "class-validator";
|
||||
|
||||
import { SUCCESS_STATUS, ERROR_STATUS } from "@calcom/platform-constants";
|
||||
|
||||
export class GetManagedUserOutput {
|
||||
@ApiProperty({ example: SUCCESS_STATUS, enum: [SUCCESS_STATUS, ERROR_STATUS] })
|
||||
@IsEnum([SUCCESS_STATUS, ERROR_STATUS])
|
||||
status!: typeof SUCCESS_STATUS | typeof ERROR_STATUS;
|
||||
|
||||
@Type(() => ManagedUserOutput)
|
||||
@ValidateNested()
|
||||
data!: ManagedUserOutput;
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
import { ManagedUserOutput } from "@/modules/oauth-clients/controllers/oauth-client-users/outputs/managed-user.output";
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { Type } from "class-transformer";
|
||||
import { IsArray, IsEnum, ValidateNested } from "class-validator";
|
||||
|
||||
import { SUCCESS_STATUS, ERROR_STATUS } from "@calcom/platform-constants";
|
||||
|
||||
export class GetManagedUsersOutput {
|
||||
@ApiProperty({ example: SUCCESS_STATUS, enum: [SUCCESS_STATUS, ERROR_STATUS] })
|
||||
@IsEnum([SUCCESS_STATUS, ERROR_STATUS])
|
||||
status!: typeof SUCCESS_STATUS | typeof ERROR_STATUS;
|
||||
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => ManagedUserOutput)
|
||||
@IsArray()
|
||||
data!: ManagedUserOutput[];
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { Transform } from "class-transformer";
|
||||
|
||||
export class ManagedUserOutput {
|
||||
@ApiProperty({ example: 1 })
|
||||
id!: number;
|
||||
|
||||
@ApiProperty({ example: "[email protected]" })
|
||||
email!: string;
|
||||
|
||||
@ApiProperty({ example: "alice" })
|
||||
username!: string | null;
|
||||
|
||||
@ApiProperty({ example: "America/New_York" })
|
||||
timeZone!: string;
|
||||
|
||||
@ApiProperty({ example: "Sunday" })
|
||||
weekStart!: string;
|
||||
|
||||
@ApiProperty({ example: "2024-04-01T00:00:00.000Z", type: "string" })
|
||||
@Transform(({ value }) => value.toISOString())
|
||||
createdDate!: Date;
|
||||
|
||||
@ApiProperty({ example: 12, nullable: true })
|
||||
timeFormat!: number | null;
|
||||
|
||||
@ApiProperty({ example: null })
|
||||
defaultScheduleId!: number | null;
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
import { ManagedUserOutput } from "@/modules/oauth-clients/controllers/oauth-client-users/outputs/managed-user.output";
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { Type } from "class-transformer";
|
||||
import { IsEnum, ValidateNested } from "class-validator";
|
||||
|
||||
import { SUCCESS_STATUS, ERROR_STATUS } from "@calcom/platform-constants";
|
||||
|
||||
export class UpdateManagedUserOutput {
|
||||
@ApiProperty({ example: SUCCESS_STATUS, enum: [SUCCESS_STATUS, ERROR_STATUS] })
|
||||
@IsEnum([SUCCESS_STATUS, ERROR_STATUS])
|
||||
status!: typeof SUCCESS_STATUS | typeof ERROR_STATUS;
|
||||
|
||||
@Type(() => ManagedUserOutput)
|
||||
@ValidateNested()
|
||||
data!: ManagedUserOutput;
|
||||
}
|
||||
+1
-1
@@ -43,7 +43,7 @@ Second, make sure that the logged in user has organizationId set to pass the Org
|
||||
})
|
||||
@UseGuards(NextAuthGuard, OrganizationRolesGuard)
|
||||
@DocsExcludeController(getEnv("NODE_ENV") === "production")
|
||||
@DocsTags("Development only")
|
||||
@DocsTags("OAuth - development only")
|
||||
export class OAuthClientsController {
|
||||
private readonly logger = new Logger("OAuthClientController");
|
||||
|
||||
|
||||
+1
-1
@@ -37,7 +37,7 @@ import { SUCCESS_STATUS, X_CAL_SECRET_KEY } from "@calcom/platform-constants";
|
||||
version: "2",
|
||||
})
|
||||
@DocsExcludeController(getEnv("NODE_ENV") === "production")
|
||||
@DocsTags("Development only")
|
||||
@DocsTags("OAuth - development only")
|
||||
export class OAuthFlowController {
|
||||
constructor(
|
||||
private readonly oauthClientRepository: OAuthClientRepository,
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { EventTypesService } from "@/ee/event-types/services/event-types.service";
|
||||
import { TokensRepository } from "@/modules/tokens/tokens.repository";
|
||||
import { CreateManagedPlatformUserInput } from "@/modules/users/inputs/create-managed-platform-user.input";
|
||||
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 { User } from "@prisma/client";
|
||||
import * as crypto from "crypto";
|
||||
|
||||
import { createNewUsersConnectToOrgIfExists } from "@calcom/platform-libraries";
|
||||
import { slugify } from "@calcom/platform-libraries";
|
||||
|
||||
@Injectable()
|
||||
export class OAuthClientUsersService {
|
||||
@@ -18,16 +19,20 @@ export class OAuthClientUsersService {
|
||||
|
||||
async createOauthClientUser(
|
||||
oAuthClientId: string,
|
||||
body: CreateManagedPlatformUserInput,
|
||||
body: CreateManagedUserInput,
|
||||
isPlatformManaged: boolean,
|
||||
organizationId?: number
|
||||
) {
|
||||
const existsWithEmail = await this.userExistsWithEmail(oAuthClientId, body.email);
|
||||
if (existsWithEmail) {
|
||||
throw new BadRequestException("User with the provided e-mail already exists.");
|
||||
}
|
||||
|
||||
let user: User;
|
||||
if (!organizationId) {
|
||||
throw new BadRequestException("You cannot create a managed user outside of an organization");
|
||||
} else {
|
||||
const [username, emailDomain] = body.email.split("@");
|
||||
const email = `${username}+${oAuthClientId}@${emailDomain}`;
|
||||
const email = this.getOAuthUserEmail(oAuthClientId, body.email);
|
||||
user = (
|
||||
await createNewUsersConnectToOrgIfExists({
|
||||
usernamesOrEmails: [email],
|
||||
@@ -67,23 +72,28 @@ export class OAuthClientUsersService {
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function generateShortHash(email: string, clientId: string): string {
|
||||
// Get the current timestamp
|
||||
const timestamp = Date.now().toString();
|
||||
async userExistsWithEmail(oAuthClientId: string, email: string) {
|
||||
const oAuthEmail = this.getOAuthUserEmail(oAuthClientId, email);
|
||||
const user = await this.userRepository.findByEmail(oAuthEmail);
|
||||
return !!user;
|
||||
}
|
||||
|
||||
// Concatenate the timestamp and email
|
||||
const data = timestamp + email + clientId;
|
||||
async updateOAuthClientUser(oAuthClientId: string, userId: number, body: UpdateManagedUserInput) {
|
||||
if (body.email) {
|
||||
const emailWithOAuthId = this.getOAuthUserEmail(oAuthClientId, body.email);
|
||||
body.email = emailWithOAuthId;
|
||||
const newUsername = slugify(emailWithOAuthId);
|
||||
await this.userRepository.updateUsername(userId, newUsername);
|
||||
}
|
||||
|
||||
// Create a SHA256 hash
|
||||
const hash = crypto
|
||||
.createHash("sha256")
|
||||
.update(data)
|
||||
.digest("base64")
|
||||
.replace("=", "")
|
||||
.replace("/", "")
|
||||
.replace("+", "");
|
||||
return this.userRepository.update(userId, body);
|
||||
}
|
||||
|
||||
return hash.toLowerCase();
|
||||
getOAuthUserEmail(oAuthClientId: string, userEmail: string) {
|
||||
const [username, emailDomain] = userEmail.split("@");
|
||||
const email = `${username}+${oAuthClientId}@${emailDomain}`;
|
||||
|
||||
return email;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { AccessTokenGuard } from "@/modules/auth/guards/access-token/access-token.guard";
|
||||
import { SlotsService } from "@/modules/slots/services/slots.service";
|
||||
import { Query, Body, Controller, Get, Delete, Post, Req, Res, UseGuards } from "@nestjs/common";
|
||||
import { ApiTags as DocsTags } from "@nestjs/swagger";
|
||||
import { Response as ExpressResponse, Request as ExpressRequest } from "express";
|
||||
|
||||
import { SUCCESS_STATUS } from "@calcom/platform-constants";
|
||||
@@ -14,6 +15,7 @@ import { ApiResponse, GetAvailableSlotsInput } from "@calcom/platform-types";
|
||||
version: "2",
|
||||
})
|
||||
@UseGuards(AccessTokenGuard)
|
||||
@DocsTags("Slots")
|
||||
export class SlotsController {
|
||||
constructor(private readonly slotsService: SlotsService) {}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { TimezonesService } from "@/modules/timezones/services/timezones.service";
|
||||
import { Controller, Get } from "@nestjs/common";
|
||||
import { ApiTags as DocsTags } from "@nestjs/swagger";
|
||||
|
||||
import { SUCCESS_STATUS } from "@calcom/platform-constants";
|
||||
import type { CityTimezones } from "@calcom/platform-libraries";
|
||||
@@ -9,6 +10,7 @@ import { ApiResponse } from "@calcom/platform-types";
|
||||
path: "timezones",
|
||||
version: "2",
|
||||
})
|
||||
@DocsTags("Timezones")
|
||||
export class TimezonesController {
|
||||
constructor(private readonly timezonesService: TimezonesService) {}
|
||||
|
||||
|
||||
+6
-1
@@ -1,9 +1,11 @@
|
||||
import { IsTimeFormat } from "@/modules/users/inputs/validators/is-time-format";
|
||||
import { IsWeekStart } from "@/modules/users/inputs/validators/is-week-start";
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { IsNumber, IsOptional, IsTimeZone, IsString, Validate } from "class-validator";
|
||||
|
||||
export class CreateManagedPlatformUserInput {
|
||||
export class CreateManagedUserInput {
|
||||
@IsString()
|
||||
@ApiProperty({ example: "[email protected]" })
|
||||
email!: string;
|
||||
|
||||
@IsString()
|
||||
@@ -13,14 +15,17 @@ export class CreateManagedPlatformUserInput {
|
||||
@IsNumber()
|
||||
@IsOptional()
|
||||
@Validate(IsTimeFormat)
|
||||
@ApiProperty({ example: 12 })
|
||||
timeFormat?: number;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@Validate(IsWeekStart)
|
||||
@ApiProperty({ example: "Sunday" })
|
||||
weekStart?: string;
|
||||
|
||||
@IsTimeZone()
|
||||
@IsOptional()
|
||||
@ApiProperty({ example: "America/New_York" })
|
||||
timeZone?: string;
|
||||
}
|
||||
+1
-1
@@ -2,7 +2,7 @@ import { IsTimeFormat } from "@/modules/users/inputs/validators/is-time-format";
|
||||
import { IsWeekStart } from "@/modules/users/inputs/validators/is-week-start";
|
||||
import { IsNumber, IsOptional, IsString, IsTimeZone, Validate } from "class-validator";
|
||||
|
||||
export class UpdateManagedPlatformUserInput {
|
||||
export class UpdateManagedUserInput {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
email?: string;
|
||||
@@ -1,7 +1,7 @@
|
||||
import { PrismaReadService } from "@/modules/prisma/prisma-read.service";
|
||||
import { PrismaWriteService } from "@/modules/prisma/prisma-write.service";
|
||||
import { CreateManagedPlatformUserInput } from "@/modules/users/inputs/create-managed-platform-user.input";
|
||||
import { UpdateManagedPlatformUserInput } from "@/modules/users/inputs/update-managed-platform-user.input";
|
||||
import { CreateManagedUserInput } from "@/modules/users/inputs/create-managed-user.input";
|
||||
import { UpdateManagedUserInput } from "@/modules/users/inputs/update-managed-user.input";
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import type { Profile, User } from "@prisma/client";
|
||||
|
||||
@@ -14,7 +14,7 @@ export class UsersRepository {
|
||||
constructor(private readonly dbRead: PrismaReadService, private readonly dbWrite: PrismaWriteService) {}
|
||||
|
||||
async create(
|
||||
user: CreateManagedPlatformUserInput,
|
||||
user: CreateManagedUserInput,
|
||||
username: string,
|
||||
oAuthClientId: string,
|
||||
isPlatformManaged: boolean
|
||||
@@ -117,7 +117,7 @@ export class UsersRepository {
|
||||
});
|
||||
}
|
||||
|
||||
async update(userId: number, updateData: UpdateManagedPlatformUserInput) {
|
||||
async update(userId: number, updateData: UpdateManagedUserInput) {
|
||||
this.formatInput(updateData);
|
||||
|
||||
return this.dbWrite.prisma.user.update({
|
||||
@@ -126,13 +126,22 @@ export class UsersRepository {
|
||||
});
|
||||
}
|
||||
|
||||
async updateUsername(userId: number, newUsername: string) {
|
||||
return this.dbWrite.prisma.user.update({
|
||||
where: { id: userId },
|
||||
data: {
|
||||
username: newUsername,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async delete(userId: number): Promise<User> {
|
||||
return this.dbWrite.prisma.user.delete({
|
||||
where: { id: userId },
|
||||
});
|
||||
}
|
||||
|
||||
formatInput(userInput: CreateManagedPlatformUserInput | UpdateManagedPlatformUserInput) {
|
||||
formatInput(userInput: CreateManagedUserInput | UpdateManagedUserInput) {
|
||||
if (userInput.weekStart) {
|
||||
userInput.weekStart = capitalize(userInput.weekStart);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user