-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Davidgu/list unclaimed items query #12
Conversation
Created documentation and GET for api/unclaimedItems/route.ts Created authMockUtil and dbMockUtils tools for easier verification and db testing No idea what I did to authMock but it is working properly
✅ Deploy Preview for hope-for-haiti ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
src/test/authMock.ts
Outdated
@@ -13,4 +13,4 @@ beforeEach(() => { | |||
mockReset(authMock); | |||
}); | |||
|
|||
export const authMock = auth as unknown as jest.Mock<() => Session | null>; | |||
export const authMock = auth as unknown as jest.Mock<() => Session | null>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
iirc prettier should add a newline? if you are using vs code, you can install the prettier extension and configure it to run on save.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it
src/app/api/unclaimedItems/route.ts
Outdated
* Handles GET requests to retrieve unclaimed items from the unclaimedItem database. | ||
* | ||
* @returns {NextResponse} On success, returns a status of 200 and a JSON object containing an array of unclaimed items. | ||
* @returns {NextResponse} On authentication error, returns a status of 401 and a JSON object containing an error message. | ||
* @returns {NextResponse} On error, returns a status of 500 and a JSON object containing an error message. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can you reformat to match the example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it and I think it matches the example
src/app/api/unclaimedItems/route.ts
Outdated
* @example | ||
* // Example response: | ||
* { | ||
* "unclaimedItems": [ | ||
* { | ||
* "id": 1, | ||
* "name": "Banana", | ||
* "quantity": 10, | ||
* "expirationDate": "2025-01-22T14:45:43.241Z" | ||
* }, | ||
* { | ||
* "id": 2, | ||
* "name": "Apple", | ||
* "quantity": 100, | ||
* "expirationDate": "2025-01-22T14:45:43.243Z" | ||
* } | ||
* ] | ||
* } | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's replace this doc example with a typescript interface:
// at the top of the file somewhere
interface Response {
numberOfPatients: number;
organizationType: OrganizationType;
// ...
}
// handler function
export async function GET(...): Promise<NextResponse<Response>> {
// ...
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created an interface
src/app/api/unclaimedItems/route.ts
Outdated
export async function GET() { | ||
try { | ||
const session = await auth(); | ||
if (!session) return authenticationError('Session required'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could be more extensive (also lowkey this is my bad for giving a bad example 💀 it's updated now)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a if (!session?.user) return error
src/app/api/unclaimedItems/route.ts
Outdated
} catch (err: unknown) { | ||
if (err instanceof Error) { | ||
console.error(err.message); | ||
} else { | ||
console.error('An unknown error occurred'); | ||
} | ||
return internalError(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nextjs already returns a 500 and logs uncaught exceptions from route handlers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got rid of the try catch statement
src/test/util/authMockUtils.ts
Outdated
* @param expires Optional, default is "" | ||
*/ | ||
export async function validateSession( | ||
user: { id: string, type: UserType } = { id: "1234", type: "ADMIN" }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: i think this session helper could be more useful if it:
- didn't provide a default user value but instead..
- accepted a default user type value
- randomly generated a user ID and returned it (this is to prevent magic numbers from existing in tests)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to provide user type now, default expiration is now a day from current day, and randomly generates a user id. Returns the generated session.
src/util/responses.ts
Outdated
@@ -27,3 +27,7 @@ export function internalError() { | |||
export function ok() { | |||
return NextResponse.json({ message: "OK" }, { status: 200 }); | |||
} | |||
|
|||
export function successResponse<T>(data: T) { | |||
return NextResponse.json(data, { status: 200 }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would prefer if you just used NextResponse.json()
in the route handler as it defaults to status 200.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deleted successResponse
dbMock.unclaimedItem.create({ data: { id: 1, name: "Test Item 1", quantity: 1, expirationDate: new Date() } }); | ||
const items: UnclaimedItem[] = await manyUnclaimedItems(3); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you might be confused on what the mock does. for every call to db
in your route code, there should be the same call to dbMock
in the test. e.g. if you only do db.unclaimedItem.findMany
in the route, then you should only call dbMock
once in your test which will be dbMock.unclaimedItem.findMany
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if I did it correctly but I changed the test
src/app/api/unclaimedItems/route.ts
Outdated
unclaimedItems: | ||
| { | ||
id: number; | ||
name: string; | ||
quantity: number; | ||
expirationDate: Date | null; | ||
}[] | ||
| []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this is kinda wonky. why is this a union type with empty array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was an error earlier because when I pulled it in the tests it would be null and not an empty array, but I'm not getting the error anymore so I've changed it back to normal.
src/app/api/unclaimedItems/route.ts
Outdated
|
||
// Response for GET /api/unclaimedItems | ||
interface UnclaimedItemsResponse { | ||
numberOfItems: number | 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this can just be 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!
src/app/api/unclaimedItems/route.ts
Outdated
const unclaimedItems = await db.unclaimedItem.findMany(); | ||
|
||
return NextResponse.json({ | ||
numberOfItems: unclaimedItems?.length, // ?.length for when unclaimedItems is undefined |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to return the number of items
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
Description
Create a
GET
route handler at/api/unclaimedItems
that returns a list of allUnclaimedItem
records.Success Criteria
GET /api/unclaimedItems
returns a401
status code with JSON body{"message": "Session required"}
GET /api/unclaimedItems
returns a200
status code and allUnclaimedItem
records as JSON objects (includeid
,name
,quantity
, andexpirationDate
as an ISO-8601 string)Checklist