Skip to content

Commit 1e317d2

Browse files
authored
Merge pull request #34 from GTBitsOfGood/davidgu/ListItemsRequestQuery
Davidgu/list items request query
2 parents 21006bf + 05b935c commit 1e317d2

File tree

3 files changed

+187
-1
lines changed

3 files changed

+187
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 { fillDbMockWithUnallocatedItemRequestsForItemIdFiltering } from "@/test/util/dbMockUtils";
6+
import { dbMock } from "@/test/dbMock";
7+
8+
test("Should return 401 for no session", async () => {
9+
await testApiHandler({
10+
params: { unallocatedItemId: "1" },
11+
appHandler,
12+
async test({ fetch }) {
13+
invalidateSession();
14+
15+
const res = await fetch({ method: "GET" });
16+
expect(res.status).toBe(401);
17+
const json = await res.json();
18+
expect(json).toEqual({ message: "Session required" });
19+
},
20+
});
21+
});
22+
23+
test("Should return 403 for being PARTNER", async () => {
24+
await testApiHandler({
25+
appHandler,
26+
async test({ fetch }) {
27+
validateSession("PARTNER");
28+
29+
const res = await fetch({ method: "GET" });
30+
expect(res.status).toBe(403);
31+
const json = await res.json();
32+
expect(json).toEqual({ message: "Unauthorized" });
33+
},
34+
});
35+
});
36+
37+
test("Should return 200 for being STAFF", async () => {
38+
await testApiHandler({
39+
params: { unallocatedItemId: "1" },
40+
appHandler,
41+
async test({ fetch }) {
42+
validateSession("STAFF");
43+
44+
const res = await fetch({ method: "GET" });
45+
expect(res.status).toBe(200);
46+
},
47+
});
48+
});
49+
50+
test("Should return 200 for being ADMIN", async () => {
51+
await testApiHandler({
52+
params: { unallocatedItemId: "1" },
53+
appHandler,
54+
async test({ fetch }) {
55+
validateSession("ADMIN");
56+
57+
const res = await fetch({ method: "GET" });
58+
expect(res.status).toBe(200);
59+
},
60+
});
61+
});
62+
63+
test("Should return 200 for being SUPER_ADMIN", async () => {
64+
await testApiHandler({
65+
params: { unallocatedItemId: "1" },
66+
appHandler,
67+
async test({ fetch }) {
68+
validateSession("SUPER_ADMIN");
69+
70+
const res = await fetch({ method: "GET" });
71+
expect(res.status).toBe(200);
72+
},
73+
});
74+
});
75+
76+
test("For an authorized session, should give all unallocated item requests pointed to the specificed item", async () => {
77+
await testApiHandler({
78+
params: { unallocatedItemId: "1" },
79+
appHandler,
80+
async test({ fetch }) {
81+
fillDbMockWithUnallocatedItemRequestsForItemIdFiltering(10);
82+
83+
const unallocatedItemRequests =
84+
await dbMock.unallocatedItemRequest.findMany();
85+
86+
validateSession("STAFF");
87+
const res = await fetch({ method: "GET" });
88+
expect(res.status).toBe(200);
89+
const json = await res.json();
90+
expect(json).toEqual({ unallocatedItemRequests });
91+
// expect(json).toEqual({
92+
// unallocatedItemRequests: unallocatedItemRequests.forEach((uir) => {
93+
// return {
94+
// id: uir.id,
95+
// quantity: uir.quantity,
96+
// comments: uir.comments,
97+
// };
98+
// }),
99+
// });
100+
},
101+
});
102+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
partnerId: number;
15+
quantity: number;
16+
comments: string;
17+
}[];
18+
}
19+
20+
/**
21+
* Handles GET requests to retrieve unallocated item requests that relate to an item id.
22+
* @param request - the incoming request (unused)
23+
* @param params - the item 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 item id is not an integer
27+
* @returns 200 and a json response with the unallocated item requests
28+
*/
29+
export async function GET(
30+
request: Request,
31+
{ params }: { params: Promise<{ unallocatedItemId: string }> }
32+
) {
33+
// Validate session
34+
const session = await auth();
35+
if (!session?.user) return authenticationError("Session required");
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+
// Get item id from request parameters
44+
const itemId = parseInt((await params).unallocatedItemId);
45+
if (isNaN(itemId)) return argumentError("Item Id must be an integer");
46+
47+
// Get all unallocated item requests for the specified item
48+
const unallocatedItemRequests = await db.unallocatedItemRequest.findMany({
49+
where: { itemId },
50+
select: {
51+
id: true,
52+
partnerId: true,
53+
quantity: true,
54+
comments: true,
55+
},
56+
});
57+
58+
return NextResponse.json({
59+
unallocatedItemRequests,
60+
} as UnallocatedItemRequestsResponse);
61+
}

src/test/util/dbMockUtils.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { dbMock } from "@/test/dbMock";
2-
import { UnclaimedItem } from "@prisma/client";
2+
import { UnallocatedItemRequest, UnclaimedItem } from "@prisma/client";
33

44
// Helper util methods for testing
55

@@ -34,3 +34,26 @@ export async function fillDbMockWithManyUnclaimedItems(
3434
dbMock.unclaimedItem.findMany.mockResolvedValue(items);
3535
return items;
3636
}
37+
38+
export async function fillDbMockWithUnallocatedItemRequestsForItemIdFiltering(
39+
num: number
40+
) {
41+
// const numberOfItems = 10;
42+
const numberOfPartners = 10;
43+
44+
const unallocatedItemRequests: UnallocatedItemRequest[] = [];
45+
46+
for (let i = 0; i < num; i++) {
47+
unallocatedItemRequests.push({
48+
id: i,
49+
quantity: Math.floor(Math.random() * 1000),
50+
partnerId: Math.floor(Math.random() * numberOfPartners),
51+
itemId: 1,
52+
comments: "Test unallocated item request " + i,
53+
});
54+
}
55+
56+
dbMock.unallocatedItemRequest.findMany.mockResolvedValue(
57+
unallocatedItemRequests
58+
);
59+
}

0 commit comments

Comments
 (0)