From 2fac4143b8423feddf413f791ed88834b5acec60 Mon Sep 17 00:00:00 2001 From: mehanana Date: Mon, 1 Jun 2026 23:03:02 -0400 Subject: [PATCH] added profile_image to the users db; fixed get/post/patch endpoints to include the new column; wrote a test for this --- apps/backend/db/db_setup.sql | 3 ++- apps/backend/lambdas/users/db-types.d.ts | 1 + apps/backend/lambdas/users/handler.ts | 11 ++++++---- apps/backend/lambdas/users/openapi.yaml | 4 ++++ apps/backend/lambdas/users/test/users.test.ts | 21 +++++++++++++++++++ 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/apps/backend/db/db_setup.sql b/apps/backend/db/db_setup.sql index c25cd888..5b82e7fb 100644 --- a/apps/backend/db/db_setup.sql +++ b/apps/backend/db/db_setup.sql @@ -8,7 +8,8 @@ CREATE TABLE users ( name VARCHAR(100) NOT NULL, email VARCHAR(150) UNIQUE NOT NULL, is_admin BOOLEAN DEFAULT FALSE, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + profile_image TEXT ); CREATE TABLE projects ( diff --git a/apps/backend/lambdas/users/db-types.d.ts b/apps/backend/lambdas/users/db-types.d.ts index 5ca734e3..f08f3c38 100644 --- a/apps/backend/lambdas/users/db-types.d.ts +++ b/apps/backend/lambdas/users/db-types.d.ts @@ -67,6 +67,7 @@ export interface BranchUsers { is_admin: Generated; name: string; user_id: Generated; + profile_image: string | null; } export interface DB { diff --git a/apps/backend/lambdas/users/handler.ts b/apps/backend/lambdas/users/handler.ts index 2f7eaa78..aa6fc503 100644 --- a/apps/backend/lambdas/users/handler.ts +++ b/apps/backend/lambdas/users/handler.ts @@ -105,7 +105,8 @@ export const handler = async (event: any): Promise => { userId: user.user_id, email: user.email, name: user.name, - isAdmin: user.is_admin + isAdmin: user.is_admin, + profile_image: user.profile_image, } }); } @@ -127,17 +128,18 @@ export const handler = async (event: any): Promise => { let email = body.email as string; let name = body.name as string; let isAdmin = body.isAdmin as boolean; + let profileImage = body.profileImage as string | undefined; // update await db.updateTable('branch.users') - .set({ email, name, is_admin: isAdmin }) + .set({ email, name, is_admin: isAdmin, profile_image: profileImage }) .where('user_id', '=', Number(userId)) .execute(); // get updated user let updatedUser = await db.selectFrom("branch.users").where("user_id", "=", Number(userId)).selectAll().executeTakeFirst(); - return json(200, { ok: true, route: 'PATCH /users/{userId}', pathParams: { userId }, body: { email: updatedUser!.email, name: updatedUser!.name, isAdmin: updatedUser!.is_admin } }); + return json(200, { ok: true, route: 'PATCH /users/{userId}', pathParams: { userId }, body: { email: updatedUser!.email, name: updatedUser!.name, isAdmin: updatedUser!.is_admin, profileImage: updatedUser!.profile_image } }); } // DELETE /users/{userId} @@ -173,6 +175,7 @@ export const handler = async (event: any): Promise => { if (!email || !name || typeof isAdmin !== 'boolean') { return json(400, { message: 'email, name, and isAdmin are required' }); } + let profile_image = body.profileImage as string; // Check if user with this email already exists const existingUser = await db @@ -189,7 +192,7 @@ export const handler = async (event: any): Promise => { try { await db .insertInto('branch.users') - .values({ email, name, is_admin: isAdmin }) + .values({ email, name, is_admin: isAdmin, profile_image }) .execute(); } catch (err) { console.error('Database insert error:', err); diff --git a/apps/backend/lambdas/users/openapi.yaml b/apps/backend/lambdas/users/openapi.yaml index 671ecd9a..54fe702e 100644 --- a/apps/backend/lambdas/users/openapi.yaml +++ b/apps/backend/lambdas/users/openapi.yaml @@ -49,6 +49,8 @@ paths: type: string isAdmin: type: boolean + profileImage: + type: string responses: '201': description: Success @@ -78,6 +80,8 @@ paths: type: string isAdmin: type: boolean + profileImage: + type: string responses: '200': description: OK diff --git a/apps/backend/lambdas/users/test/users.test.ts b/apps/backend/lambdas/users/test/users.test.ts index a7796070..6d111334 100644 --- a/apps/backend/lambdas/users/test/users.test.ts +++ b/apps/backend/lambdas/users/test/users.test.ts @@ -162,6 +162,27 @@ test("patch user test 🌞", async () => { } }); +test("patch user profile_image test 🌞", async () => { + mockAdminAuth(); + + const patchEvent = createEvent({ + method: 'PATCH', + path: '/1', + body: { + name: "Ashley Duggan", + email: "ashley@branch.org", + isAdmin: true, + profileImage: "https://s3.amazonaws.com/branch-avatars/ashley.png" + }, + }); + + const res = await handler(patchEvent); + expect(res.statusCode).toBe(200); + + const body = JSON.parse(res.body).body; + expect(body.profileImage).toBe("https://s3.amazonaws.com/branch-avatars/ashley.png"); +}); + test("patch user 404 test 🌞", async () => { mockAdminAuth();