Skip to content

Creating Inventory APIs#8

Open
davdwan21 wants to merge 10 commits intomainfrom
inventory-service-apis
Open

Creating Inventory APIs#8
davdwan21 wants to merge 10 commits intomainfrom
inventory-service-apis

Conversation

@davdwan21
Copy link
Copy Markdown
Collaborator

@davdwan21 davdwan21 commented Feb 15, 2026

Describe your changes

  • Created given folder structure, unsure about use case - implemented dir structure similar to demo
  • Created items.ts in services for generic services similar to the demo
  • route.ts is broken up between [id] and general for cleaner API management
  • Multiple schemas to enforce typing in items - currently implements highly strict typing
  • Created general routes for GET, POST
  • Created id specific routes for GET, PUT, DELETE by id

Issue ticket number and link

[ Insert Link & Ticket #]

Checklist before requesting a review

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • This change requires a documentation update

+ 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
@davdwan21 davdwan21 self-assigned this Feb 15, 2026
Copy link
Copy Markdown
Collaborator

@arnavjk007 arnavjk007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of error handling cases + GET filtering & pagination changes

return NextResponse.json({ message: "Invalid id" }, { status: 400 });
}

const updateData = await request.json();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Collaborator

@arnavjk007 arnavjk007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing, lets do the try catch with connectToDB before thsi try catch in all functs

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be filtered like they should be able to get by item name as well

// TODO ** Paginate and limit to 10 per page
// Build query with filters
export async function getItems(): Promise<Item[]> {
await connectToDatabase();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this since it will be in the route files

// Build query with filters
export async function getItems(): Promise<Item[]> {
await connectToDatabase();
const items = await ItemModel.find().exec();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also have .lean()


return toItem(item);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Collaborator

@arnavjk007 arnavjk007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

const connectionResponse = await connect();
if (connectionResponse) {
return connectionResponse;
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Collaborator

@arnavjk007 arnavjk007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

* @returns filtered items in the form of a JS object
*/
export async function filteredGet(options: getItemOptions) {
await connectToDatabase();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Collaborator

@arnavjk007 arnavjk007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

@arnavjk007 arnavjk007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DB connection changes + minor role changes.

import { connectToDatabase } from "./mongoose";
import { NextResponse } from "next/server";

export async function connect() {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The roles for this should be PI and LAB_MANAGER

Copy link
Copy Markdown
Collaborator

@arnavjk007 arnavjk007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor changes in scope + nit change services/items.ts to services/inventory.ts


// GET: fetch all items
export async function GET() {
const connectionResponse = await connect();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of connecting here lets just move this into the less exposed area in the services/items.ts for all functions


// GET: get an item by id
export async function GET(_: Request, { params }: { params: { id: string } }) {
const connectionResponse = await connect();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move connection to item.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants