Skip to content
Open
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
2 changes: 1 addition & 1 deletion apps/backend/db/.env.dev
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DATABASE_URL=postgresql://branch_dev:password@localhost:5432/branch_db
DATABASE_URL=postgresql://branch_dev:password@localhost:5432/branch_db
77 changes: 77 additions & 0 deletions apps/backend/lambdas/projects/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/projects/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;
41 changes: 40 additions & 1 deletion apps/backend/lambdas/projects/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,45 @@ 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 /projects/{id}/donors
const parts = normalizedPath.split('/');
if (parts.length === 3 && parts[2] === 'donors' && method === 'GET') {
const id = parts[1];
if (!id) return json(400, { message: 'id is required' });

if (isNaN(Number(id))) {
return json(400, { message: 'Project id must be a valid number' });
}

const queryString = event.rawQueryString || event.queryStringParameters;
if (queryString && (typeof queryString === 'string' ? queryString.length > 0 : Object.keys(queryString).length > 0)) {
return json(400, { message: 'Bad Request: Query parameters are not allowed' });
}

// TODO: Add your business logic here
const project = await db
.selectFrom("branch.projects as p")
.where("p.project_id", "=", Number(id))
.selectAll()
.executeTakeFirst();

if (!project) {
return json(404, { message: 'Project not found' });
}

const donors = await db.selectFrom("branch.projects as p").where("p.project_id", "=", Number(id)).innerJoin(
"branch.project_donations as bpd",
"bpd.project_id",
"p.project_id"
).innerJoin(
"branch.donors as bd",
"bd.donor_id",
"bpd.donor_id"
).selectAll().execute();
return json(200, { donors });
}
// <<< ROUTES-END

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

/projects/{id}/donors:
get:
summary: GET /projects/{id}/donors
parameters:
- in: path
name: id
required: true
schema:
type: string
responses:
'200':
description: OK
Loading