Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/backend/db/db_setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
1 change: 1 addition & 0 deletions apps/backend/lambdas/users/db-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export interface BranchUsers {
is_admin: Generated<boolean | null>;
name: string;
user_id: Generated<number>;
profile_image: string | null;
}

export interface DB {
Expand Down
11 changes: 7 additions & 4 deletions apps/backend/lambdas/users/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ export const handler = async (event: any): Promise<APIGatewayProxyResult> => {
userId: user.user_id,
email: user.email,
name: user.name,
isAdmin: user.is_admin
isAdmin: user.is_admin,
profile_image: user.profile_image,
}
});
}
Expand All @@ -127,17 +128,18 @@ export const handler = async (event: any): Promise<APIGatewayProxyResult> => {
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}
Expand Down Expand Up @@ -173,6 +175,7 @@ export const handler = async (event: any): Promise<APIGatewayProxyResult> => {
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
Expand All @@ -189,7 +192,7 @@ export const handler = async (event: any): Promise<APIGatewayProxyResult> => {
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);
Expand Down
4 changes: 4 additions & 0 deletions apps/backend/lambdas/users/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ paths:
type: string
isAdmin:
type: boolean
profileImage:
type: string
responses:
'201':
description: Success
Expand Down Expand Up @@ -78,6 +80,8 @@ paths:
type: string
isAdmin:
type: boolean
profileImage:
type: string
responses:
'200':
description: OK
Expand Down
21 changes: 21 additions & 0 deletions apps/backend/lambdas/users/test/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading