Skip to content

Commit e20a54f

Browse files
authored
Merge pull request #37 from GTBitsOfGood/davidgu/ListPartnersUnallocatedItemRequestsQuery
Finished partner associated unallocated item requests
2 parents ce4f382 + b00ea26 commit e20a54f

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { testApiHandler } from "next-test-api-route-handler";
2+
import * as appHandler from "./route";
3+
import { expect, test } from "@jest/globals";
4+
import { validateSession, invalidateSession } from "@/test/util/authMockUtils";
5+
import { fillDbMockWithUnallocatedItemRequestsForPartnerIdFilter } from "@/test/util/dbMockUtils";
6+
import { dbMock } from "@/test/dbMock";
7+
8+
test("Should return 401 for invalid session", async () => {
9+
await testApiHandler({
10+
params: { partnerId: "1" },
11+
appHandler,
12+
async test({ fetch }) {
13+
invalidateSession();
14+
15+
const res = await fetch({ method: "GET" });
16+
await expect(res.status).toBe(401);
17+
const json = await res.json();
18+
await expect(json).toEqual({ message: "Session required" });
19+
},
20+
});
21+
});
22+
23+
test("Should return 403 if not STAFF, ADMIN, or SUPER_ADMIN", async () => {
24+
await testApiHandler({
25+
params: { partnerId: "1" },
26+
appHandler,
27+
async test({ fetch }) {
28+
validateSession("PARTNER");
29+
30+
const res = await fetch({ method: "GET" });
31+
await expect(res.status).toBe(403);
32+
const json = await res.json();
33+
await expect(json).toEqual({ message: "Unauthorized" });
34+
},
35+
});
36+
});
37+
38+
test("Should return 200 if STAFF", async () => {
39+
await testApiHandler({
40+
params: { partnerId: "1" },
41+
appHandler,
42+
async test({ fetch }) {
43+
validateSession("STAFF");
44+
45+
const res = await fetch({ method: "GET" });
46+
await expect(res.status).toBe(200);
47+
},
48+
});
49+
});
50+
51+
test("Should return 200 if ADMIN", async () => {
52+
await testApiHandler({
53+
params: { partnerId: "1" },
54+
appHandler,
55+
async test({ fetch }) {
56+
validateSession("ADMIN");
57+
58+
const res = await fetch({ method: "GET" });
59+
await expect(res.status).toBe(200);
60+
},
61+
});
62+
});
63+
64+
test("Should return 200 if SUPER_ADMIN", async () => {
65+
await testApiHandler({
66+
params: { partnerId: "1" },
67+
appHandler,
68+
async test({ fetch }) {
69+
validateSession("SUPER_ADMIN");
70+
71+
const res = await fetch({ method: "GET" });
72+
await expect(res.status).toBe(200);
73+
},
74+
});
75+
});
76+
77+
// This method doesn't have a mock db implemented such that it verifies that the only items being pulled are those associated with the partner id.
78+
test("Should return 200 and unallocated item requests associated with partnerId", async () => {
79+
await testApiHandler({
80+
params: { partnerId: "1" },
81+
appHandler,
82+
async test({ fetch }) {
83+
validateSession("STAFF");
84+
85+
fillDbMockWithUnallocatedItemRequestsForPartnerIdFilter(10);
86+
const expectedResponse = await dbMock.unallocatedItemRequest.findMany();
87+
88+
const res = await fetch({ method: "GET" });
89+
expect(res.status).toBe(200);
90+
91+
const json = await res.json();
92+
expect(json).toEqual({ unallocatedItemRequests: expectedResponse });
93+
},
94+
});
95+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { auth } from "@/auth";
2+
import { db } from "@/db";
3+
import {
4+
argumentError,
5+
authenticationError,
6+
authorizationError,
7+
} from "@/util/responses";
8+
import { UserType } from "@prisma/client";
9+
import { NextResponse } from "next/server";
10+
11+
interface UnallocatedItemRequestsResponse {
12+
unallocatedItemRequests: {
13+
id: number;
14+
itemId: number;
15+
quantity: number;
16+
comments: string;
17+
}[];
18+
}
19+
20+
/**
21+
* Handles GET requests to retrieve unallocated item requests that relate to a partner id.
22+
* @param req - the incoming request (unused)
23+
* @param params - the partner id to retrieve unallocated item requests for
24+
* @returns 401 if the session is invalid
25+
* @returns 403 if the user type isn't staff, admin, or super admin
26+
* @returns 400 if the partner id is not an integer
27+
* @returns 200 and a json response with the unallocated item requests associated with the partnerId
28+
*/
29+
export async function GET(
30+
req: Request,
31+
{ params }: { params: Promise<{ partnerId: string }> }
32+
) {
33+
const session = await auth();
34+
if (!session) return authenticationError("Session required");
35+
if (!session?.user) return authenticationError("User not found");
36+
if (
37+
session.user.type !== UserType.STAFF &&
38+
session.user.type !== UserType.ADMIN &&
39+
session.user.type !== UserType.SUPER_ADMIN
40+
)
41+
return authorizationError("Unauthorized");
42+
43+
const partnerId = parseInt((await params).partnerId);
44+
if (isNaN(partnerId)) return argumentError("Partner Id must be an integer");
45+
46+
const unallocatedItemRequests = await db.unallocatedItemRequest.findMany({
47+
where: { partnerId },
48+
select: {
49+
id: true,
50+
itemId: true,
51+
quantity: true,
52+
comments: true,
53+
},
54+
});
55+
56+
return NextResponse.json({
57+
unallocatedItemRequests,
58+
} as UnallocatedItemRequestsResponse);
59+
}

src/test/util/dbMockUtils.ts

+17
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,24 @@ export async function fillDbMockWithUnallocatedItemRequestsForItemIdFiltering(
6767
comments: "Test unallocated item request " + i,
6868
});
6969
}
70+
}
7071

72+
export async function fillDbMockWithUnallocatedItemRequestsForPartnerIdFilter(
73+
num: number
74+
) {
75+
const partnerId = 1;
76+
const numOfItems = 10;
77+
78+
const unallocatedItemRequests = [];
79+
for (let i = 0; i < num; i++) {
80+
unallocatedItemRequests.push({
81+
id: i,
82+
partnerId: partnerId,
83+
itemId: Math.floor(Math.random() * numOfItems),
84+
quantity: Math.floor(Math.random() * 100),
85+
comments: `Test comment ${i}`,
86+
});
87+
}
7188
dbMock.unallocatedItemRequest.findMany.mockResolvedValue(
7289
unallocatedItemRequests
7390
);

0 commit comments

Comments
 (0)