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:5433/branch_db
DATABASE_URL=postgresql://branch_dev:password@localhost:5432/branch_db
38 changes: 38 additions & 0 deletions apps/backend/lambdas/projects/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,44 @@ export const handler = async (event: any): Promise<APIGatewayProxyResult> => {
const projects = await db.selectFrom("branch.projects").selectAll().execute();
return json(200, projects);
}

// 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' });
}

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 });
}

// POST /projects
if ((normalizedPath === '' || normalizedPath === '/' || normalizedPath === '/projects') && method === 'POST') {
Expand Down
44 changes: 43 additions & 1 deletion apps/backend/lambdas/projects/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ paths:
type: boolean

/projects:
get:
summary: GET /projects
responses:
'200':
description: OK
post:
summary: POST /projects
requestBody:
Expand All @@ -28,7 +33,7 @@ paths:
application/json:
schema:
type: object
required:
required:
- name
properties:
name:
Expand All @@ -38,3 +43,40 @@ paths:
responses:
'200':
description: OK

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

/projects/{id}/donors:
get:
summary: GET /projects/{id}/donors
parameters:
- in: path
name: id
required: true
schema:
type: string
responses:
'200':
description: OK
52 changes: 26 additions & 26 deletions apps/backend/lambdas/projects/package-lock.json

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

61 changes: 61 additions & 0 deletions apps/backend/lambdas/projects/test/example.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

test("health test 🌞", async () => {
let res = await fetch("http://localhost:3000/projects/health")
expect(res.status).toBe(200);
});


test("get projects no donors test 🌞", async () => {
const res = await fetch("http://localhost:3000/projects/4/donors");
expect(res.status).toBe(200);
let body = await res.json();
console.log(body);
expect(body.donors).toBeDefined();
expect(Array.isArray(body.donors)).toBe(true);
});

test("get projects yes donors test 🌞", async () => {
const res = await fetch("http://localhost:3000/projects/1/donors");
expect(res.status).toBe(200);
let body = await res.json();
console.log(body);
expect(body.donors).toBeDefined();
expect(Array.isArray(body.donors)).toBe(true);
if (body.donors.length > 0) {
const donor = body.donors[0];
expect(donor.project_id).toBeDefined();
expect(donor.name).toBeDefined();
expect(donor.total_budget).toBeDefined();
expect(donor.start_date).toBeDefined();
expect(donor.end_date).toBeDefined();
expect(donor.currency).toBeDefined();
expect(donor.created_at).toBeDefined();
expect(donor.donation_id).toBeDefined();
expect(donor.donor_id).toBeDefined();
expect(donor.amount).toBeDefined();
expect(donor.donated_at).toBeDefined();
expect(donor.organization).toBeDefined();
expect(donor.contact_name).toBeDefined();
expect(donor.contact_email).toBeDefined();
}
});


test("404 when invalid project id 🌞", async () => {
const res = await fetch("http://localhost:3000/projects/1000/donors");
expect(res.status).toBe(404);
});

test("400 when project id is not a number 🌞", async () => {
const res = await fetch("http://localhost:3000/projects/abc/donors");
expect(res.status).toBe(400);
const body = await res.json();
expect(body.message).toContain("must be a valid number");
});

test("400 when request has both body and query params 🌞", async () => {
const res = await fetch("http://localhost:3000/projects/1/donors?sort=desc");
expect(res.status).toBe(400);
const body = await res.json();
expect(body.message).toContain("Bad Request");
});
Loading