Conversation
+ Created given folder structure, unsure about use case + Created items.ts in services for generic services similar to the demo + route.ts mimics demo routing for cleaner API management + multiple schemas to enforce typing in items Next steps: - Create id specific routes for GET, PUT, DELETE by id
+ Added GET, PUT, DELETE by id
arnavjk007
left a comment
There was a problem hiding this comment.
Lots of error handling cases + GET filtering & pagination changes
app/api/inventory/[id]/route.ts
Outdated
| return NextResponse.json({ message: "Invalid id" }, { status: 400 }); | ||
| } | ||
|
|
||
| const updateData = await request.json(); |
There was a problem hiding this comment.
Let's use zod schema to validate that the item to be updated is being updated with a valid schema so something like this:
const updateSchema = z.object({
name: z.string().optional(),
quantity: z.number().min(0).optional(),
threshold: z
.object({
minQuantity: z.number().min(0),
enabled: z.boolean(),
})
.optional(),
});
There was a problem hiding this comment.
How strict should the schema validation be? For example should we include all items like category and event as well?
+ Added exception handling to services and APIs + Adjusted typing to handle ItemInput (DB) vs Item (query) Next steps: - Implement pagination and search queries
arnavjk007
left a comment
There was a problem hiding this comment.
FilteredGet needs to be implemented so users can query by item name. Moved DB connection from services to route files. Some other nit comments
|
|
||
| // GET: fetch all items | ||
| // implement page, limit, labid search params | ||
| export async function GET() { |
There was a problem hiding this comment.
Same thing, lets do the try catch with connectToDB before thsi try catch in all functs
There was a problem hiding this comment.
This should also be filtered like they should be able to get by item name as well
services/items.ts
Outdated
| // TODO ** Paginate and limit to 10 per page | ||
| // Build query with filters | ||
| export async function getItems(): Promise<Item[]> { | ||
| await connectToDatabase(); |
There was a problem hiding this comment.
remove this since it will be in the route files
services/items.ts
Outdated
| // Build query with filters | ||
| export async function getItems(): Promise<Item[]> { | ||
| await connectToDatabase(); | ||
| const items = await ItemModel.find().exec(); |
|
|
||
| return toItem(item); | ||
| } | ||
|
|
There was a problem hiding this comment.
Add get filteredItems for querying by item id + pagination
+ General route.ts no longer uses a cached connection. Also returns NextResponse upon failed connection attempt. + GET and POST now use new connection attempt + POST utilizes safeParse instead of parse to validate body with schema. + Model exports cleaned up and condensed. + Services no longer attempt to connect to database + Comment descriptor for each method + Methods no longer throw errors, now simply return null upon nonexistent item Next steps: - implement getFiltered service and API - fix id specific routing
arnavjk007
left a comment
There was a problem hiding this comment.
Comments about usage of connect. We only have to do await connect() and dont need to return anything. Also some comments aren't addressed likek using .lean when using .exec
app/api/inventory/route.ts
Outdated
| const connectionResponse = await connect(); | ||
| if (connectionResponse) { | ||
| return connectionResponse; | ||
| } |
There was a problem hiding this comment.
Not really sure what this is doing, we only have to await connect() here...
+ Created filteredGet in services + Implemented .lean() so services return plain objects rather than documents + Proper item update is enforced with Zod schema
arnavjk007
left a comment
There was a problem hiding this comment.
connectToDatabase() should be moved to route files. I put a comment on what it shoudl look like. Also, please add unit testing that mocks DB calls to make sure our functions work. You can create a tests folder in each directory with your files like api/inventory/tests/ and in there you will have tests that test all route files.
Also you should change the name of the route.ts pathway since it is app/api/demo/rout.ts, it shouldn't be demo.
services/items.ts
Outdated
| * @returns filtered items in the form of a JS object | ||
| */ | ||
| export async function filteredGet(options: getItemOptions) { | ||
| await connectToDatabase(); |
There was a problem hiding this comment.
All DB connections should be done in route files through a try catch like this
try {
await connectToDatabase();
} catch {
return NextResponse.json(
{ success: false, message: "Error connecting to database." },
{ status: 500 }
);
}
+ Moved connect to lib/db file for proper testing + Created test files for route and route id with jest + Added jest config and setup files
There was a problem hiding this comment.
Mostly looks good but for jest I already put the config and setup flies in the main branch so on your local setup switch to the main branch and git pull it and then go back to the feature branch and do git merge main so you get the updated changes and resolve the merge conflicts since in this PR you made your own setup and config. Or you could just resolve the conflicts in git itself by accepting changes from the main branch over the feat.
arnavjk007
left a comment
There was a problem hiding this comment.
DB connection changes + minor role changes.
| import { connectToDatabase } from "./mongoose"; | ||
| import { NextResponse } from "next/server"; | ||
|
|
||
| export async function connect() { |
There was a problem hiding this comment.
We don't need this file, the connectToDatabase function is in the mongoose.ts in this directory use that instead everywhere.
models/Item.ts
Outdated
There was a problem hiding this comment.
The roles for this should be PI and LAB_MANAGER
arnavjk007
left a comment
There was a problem hiding this comment.
Minor changes in scope + nit change services/items.ts to services/inventory.ts
app/api/inventory/route.ts
Outdated
|
|
||
| // GET: fetch all items | ||
| export async function GET() { | ||
| const connectionResponse = await connect(); |
There was a problem hiding this comment.
Instead of connecting here lets just move this into the less exposed area in the services/items.ts for all functions
app/api/inventory/[id]/route.ts
Outdated
|
|
||
| // GET: get an item by id | ||
| export async function GET(_: Request, { params }: { params: { id: string } }) { | ||
| const connectionResponse = await connect(); |
There was a problem hiding this comment.
move connection to item.ts
Describe your changes
Issue ticket number and link
[ Insert Link & Ticket #]
Checklist before requesting a review