From f9d8e83f14a0f697828a4d11db869f24a9314359 Mon Sep 17 00:00:00 2001 From: Anudeep <94232161+anudeeps352@users.noreply.github.com> Date: Sun, 13 Oct 2024 16:48:56 +0530 Subject: [PATCH] feat(api-client): Create controller for User module (#484) --- packages/api-client/src/controllers/user.ts | 66 ++++++++++++ packages/api-client/src/types/user.types.d.ts | 34 ++++++ packages/api-client/tests/user.spec.ts | 100 ++++++++++++++++++ 3 files changed, 200 insertions(+) create mode 100644 packages/api-client/src/controllers/user.ts create mode 100644 packages/api-client/src/types/user.types.d.ts create mode 100644 packages/api-client/tests/user.spec.ts diff --git a/packages/api-client/src/controllers/user.ts b/packages/api-client/src/controllers/user.ts new file mode 100644 index 00000000..472da504 --- /dev/null +++ b/packages/api-client/src/controllers/user.ts @@ -0,0 +1,66 @@ +import { APIClient } from '@api-client/core/client' +import { parseResponse } from '@api-client/core/response-parser' +import { ClientResponse } from '@api-client/types/index.types' +import { + GetSelfResponse, + UpdateSelfRequest, + UpdateSelfResponse, + ValidateEmailChangeOTPRequest, + ValidateEmailChangeOTPResponse, + ResendEmailChangeOTPRequest, + DeleteSelfResponse, + ResendEmailChangeOTPResponse +} from '@api-client/types/user.types' + +export default class UserController { + private apiClient: APIClient + + constructor(private readonly backendURL: string) { + this.apiClient = new APIClient(this.backendURL) + } + async getSelf( + headers?: Record + ): Promise> { + const response = await this.apiClient.get(`/api/user`, headers) + return await parseResponse(response) + } + + async updateSelf( + request: UpdateSelfRequest, + headers?: Record + ): Promise> { + const response = await this.apiClient.put(`./api/user`, request, headers) + return await parseResponse(response) + } + + async deleteSelf( + headers?: Record + ): Promise> { + const response = await this.apiClient.delete(`./api/user`, headers) + return await parseResponse(response) + } + + async validateEmailChangeOTP( + request: ValidateEmailChangeOTPRequest, + headers?: Record + ): Promise> { + const response = await this.apiClient.post( + `./api/user/validate-email-change-otp?otp=${request.otp}`, + request, + headers + ) + return await parseResponse(response) + } + + async resendEmailChangeOTP( + request: ResendEmailChangeOTPRequest, + headers?: Record + ): Promise> { + const response = await this.apiClient.post( + `./api/user/resend-email-change-otp`, + request, + headers + ) + return await parseResponse(response) + } +} diff --git a/packages/api-client/src/types/user.types.d.ts b/packages/api-client/src/types/user.types.d.ts new file mode 100644 index 00000000..b5a553bc --- /dev/null +++ b/packages/api-client/src/types/user.types.d.ts @@ -0,0 +1,34 @@ +import { PageRequest, PageResponse } from '@api-client/index' +import { Workspace } from '@api-client/types/workspace.types' + +export interface GetSelfResponse { + id: string + email: string + name: string + profilePictureUrl: string | null + isActive: boolean + isOnboardingFinished: boolean + isAdmin: boolean + authProvider: string + defaultWorkspace: Workspace +} +export interface UpdateSelfRequest { + name?: string + profilePictureUrl?: string + isOnboardingFinished?: boolean + email?: string +} +export interface UpdateSelfResponse + extends Partial> {} + +export interface DeleteSelfRequest {} +export interface DeleteSelfResponse {} + +export interface ValidateEmailChangeOTPRequest { + otp: string +} +export interface ValidateEmailChangeOTPResponse + extends Partial> {} + +export interface ResendEmailChangeOTPRequest {} +export interface ResendEmailChangeOTPResponse {} diff --git a/packages/api-client/tests/user.spec.ts b/packages/api-client/tests/user.spec.ts new file mode 100644 index 00000000..d2f9bc55 --- /dev/null +++ b/packages/api-client/tests/user.spec.ts @@ -0,0 +1,100 @@ +import { APIClient } from '@api-client/core/client' +import UserController from '@api-client/controllers/user' + +describe('User Controller Tests', () => { + const backendURL = process.env.BACKEND_URL + + const client = new APIClient(backendURL) + const userController = new UserController(backendURL) + + const email = 'johndoe@example.com' + let projectSlug: string | null + let workspaceSlug: string | null + let environmentSlug: string | null + + beforeAll(async () => { + //Create the user's workspace + const workspaceResponse = (await ( + await client.post( + '/api/workspace', + { + name: 'My Workspace' + }, + { + 'x-e2e-user-email': email + } + ) + ).json()) as any + + workspaceSlug = workspaceResponse.slug + + // Create a project + const projectResponse = (await ( + await client.post( + `/api/project/${workspaceSlug}`, + { + name: 'Project', + storePrivateKey: true + }, + { + 'x-e2e-user-email': email + } + ) + ).json()) as any + + projectSlug = projectResponse.slug + + const createEnvironmentResponse = (await ( + await client.post( + `/api/environment/${projectSlug}`, + { + name: 'Dev' + }, + { + 'x-e2e-user-email': email + } + ) + ).json()) as any + + environmentSlug = createEnvironmentResponse.slug + }) + + afterAll(async () => { + // Delete the workspace + await client.delete(`/api/workspace/${workspaceSlug}`, { + 'x-e2e-user-email': email + }) + }) + + // Get Current User + it('should get current user', async () => { + const user = await userController.getSelf({ 'x-e2e-user-email': email }) + + expect(user.data.defaultWorkspace.name).toBe('My Workspace') + expect(user.data.email).toBe('johndoe@example.com') + }) + + // Update Current User + it('should update current user', async () => { + const user = await userController.updateSelf( + { name: 'Jane Doe', email: 'janedoe@example.com' }, + { 'x-e2e-user-email': email } + ) + + expect(user.data.name).toBe('Jane Doe') + expect(user.data.email).toBe('janedoe@example.com') + }) + + // Delete Current User + it('should update current user', async () => { + const deleteUser = await userController.updateSelf( + { name: 'Jane Doe', email: 'janedoe@example.com' }, + { 'x-e2e-user-email': email } + ) + + expect(deleteUser.success).toBe(true) + }) + + // Validate email change OTP + // resend validate email OTP tests +})