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
149 changes: 148 additions & 1 deletion apps/backend/db/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion apps/backend/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"description": "",
"dependencies": {
"kysely": "^0.28.8",
"kysely-codegen": "^0.19.0"
"kysely-codegen": "^0.19.0",
"pg": "^8.16.3"
}
}
77 changes: 77 additions & 0 deletions apps/backend/lambdas/users/db-types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* This file was generated by kysely-codegen.
* Please do not edit it manually.
*/

import type { ColumnType } from "kysely";

export type Generated<T> = T extends ColumnType<infer S, infer I, infer U>
? ColumnType<S, I | undefined, U>
: ColumnType<T, T | undefined, T>;

export type Numeric = ColumnType<string, number | string, number | string>;

export type Timestamp = ColumnType<Date, Date | string, Date | string>;

export interface BranchDonors {
contact_email: string | null;
contact_name: string | null;
created_at: Generated<Timestamp | null>;
donor_id: Generated<number>;
organization: string;
}

export interface BranchExpenditures {
amount: Numeric;
category: string | null;
created_at: Generated<Timestamp | null>;
description: string | null;
entered_by: number | null;
expenditure_id: Generated<number>;
project_id: number;
spent_on: Generated<Timestamp>;
}

export interface BranchProjectDonations {
amount: Numeric;
donated_at: Generated<Timestamp | null>;
donation_id: Generated<number>;
donor_id: number;
project_id: number;
}

export interface BranchProjectMemberships {
hours: Numeric | null;
membership_id: Generated<number>;
project_id: number;
role: string;
start_date: Timestamp | null;
user_id: number;
}

export interface BranchProjects {
created_at: Generated<Timestamp | null>;
currency: Generated<string | null>;
end_date: Timestamp | null;
name: string;
project_id: Generated<number>;
start_date: Timestamp | null;
total_budget: Numeric | null;
}

export interface BranchUsers {
created_at: Generated<Timestamp | null>;
email: string;
is_admin: Generated<boolean | null>;
name: string;
user_id: Generated<number>;
}

export interface DB {
"branch.donors": BranchDonors;
"branch.expenditures": BranchExpenditures;
"branch.project_donations": BranchProjectDonations;
"branch.project_memberships": BranchProjectMemberships;
"branch.projects": BranchProjects;
"branch.users": BranchUsers;
}
19 changes: 19 additions & 0 deletions apps/backend/lambdas/users/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Kysely, PostgresDialect } from 'kysely'
import { Pool } from 'pg'
import type { DB } from './db-types'


const db = new Kysely<DB>({
dialect: new PostgresDialect({
pool: new Pool({
host: process.env.DB_HOST ?? 'localhost',
port: Number(process.env.DB_PORT ?? 5432),
user: process.env.DB_USER ?? 'branch_dev',
password: process.env.DB_PASSWORD ?? 'password',
database: process.env.DB_NAME ?? 'branch_db',
ssl: false,
}),
}),
})

export default db
51 changes: 50 additions & 1 deletion apps/backend/lambdas/users/handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import db from './db';

export const handler = async (event: any): Promise<APIGatewayProxyResult> => {
try {
Expand All @@ -16,7 +17,55 @@ export const handler = async (event: any): Promise<APIGatewayProxyResult> => {

// >>> ROUTES-START (do not remove this marker)
// CLI-generated routes will be inserted here
// <<< ROUTES-END

// GET /{userId} (dev server strips /users prefix)
if (normalizedPath.startsWith('/') && normalizedPath.split('/').length === 2 && method === 'GET') {
const userId = normalizedPath.split('/')[1];
if (!userId) return json(400, { message: 'userId is required' });

const user = await db.selectFrom("branch.users").where("user_id", "=", Number(userId)).selectAll().executeTakeFirst();
if (!user) return json(404, { message: 'User not found' });

return json(200, {
ok: true,
route: 'GET /users/{userId}',
pathParams: { userId },
body: {
userId: user.user_id,
email: user.email,
name: user.name,
isAdmin: user.is_admin
}
});
}

// PATCH /{userId} (dev server strips /users prefix)
if (normalizedPath.startsWith('/') && normalizedPath.split('/').length === 2 && method === 'PATCH') {
const userId = normalizedPath.split('/')[1];
if (!userId) return json(400, { message: 'userId is required' });
const body = event.body ? JSON.parse(event.body) as Record<string, unknown> : {};

// make sure user exists
let user = await db.selectFrom("branch.users").where("user_id", "=", Number(userId)).selectAll().executeTakeFirst();
if (!user) return json(404, { message: 'User not found' });

// vars to update
let email = body.email as string;
let name = body.name as string;
let isAdmin = body.isAdmin as boolean;

// update
await db.updateTable('branch.users')
.set({ email, name, is_admin: isAdmin })
.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 } });
}
// <<< ROUTES-END

return json(404, { message: 'Not Found', path: normalizedPath, method });
} catch (err) {
Expand Down
28 changes: 28 additions & 0 deletions apps/backend/lambdas/users/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,31 @@ paths:
properties:
ok:
type: boolean

/users/{userId}:
patch:
summary: PATCH /users/{userId}
parameters:
- in: path
name: userId
required: true
schema:
type: string
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
name:
type: string
email:
type: string
isAdmin:
type: boolean
responses:
'200':
description: OK
'404':
description: User not found
Loading