Skip to content
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

feat(api): implement bulk inventory creation admin API endpoint #1159

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from

Conversation

lemilonkh
Copy link
Contributor

@lemilonkh lemilonkh commented Feb 27, 2025

Description by Korbit AI

What change is being made?

Implement a new API endpoint for bulk inventory creation aimed at admin users, incorporate priority handling for data sources, refactor DataSourceService to improve code reusability, and update the database schema to include a priority column in the DataSourceI18n model.

Why are these changes being made?

These changes enhance the system's functionality by allowing administrators to efficiently create multiple inventories at once, automatically determining the best data sources based on priority. The refactoring in DataSourceService improves code organization and maintainability, promoting future scalability, while the database schema update supports priority sorting for more precise data sourcing.

Is this description stale? Ask me to generate a new description by commenting /korbit-generate-pr-description

@lemilonkh lemilonkh self-assigned this Feb 27, 2025
Copy link

@korbit-ai korbit-ai bot left a comment

Choose a reason for hiding this comment

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

Review by Korbit AI

Korbit automatically attempts to detect when you fix issues in new commits.
Category Issue Fix Detected
Error Handling Incorrect String Type Check ▹ view
Functionality Incorrect Error Array Type Definition ▹ view
Functionality Overly Restrictive Population Check ▹ view
Functionality Missing Type Enforcement for Priority Range ▹ view
Performance Inefficient Data Type for Priority Field ▹ view
Functionality Insufficient Locode Format Validation ▹ view
Performance Inefficient Request Body Handling ▹ view
Files scanned
File Path Reviewed
app/src/app/api/v0/city/[city]/user/[user]/route.ts
app/src/app/api/v0/admin/bulk/route.ts
app/src/backend/AdminService.ts
app/src/app/api/v0/datasource/[inventoryId]/route.ts
app/src/backend/DataSourceService.ts
app/src/models/DataSourceI18n.ts

Explore our documentation to understand the languages and file types we support and the files we ignore.

Need a new review? Comment /korbit-review on this PR and I'll review your latest changes.

Korbit Guide: Usage and Customization

Interacting with Korbit

  • You can manually ask Korbit to review your PR using the /korbit-review command in a comment at the root of your PR.
  • You can ask Korbit to generate a new PR description using the /korbit-generate-pr-description command in any comment on your PR.
  • Too many Korbit comments? I can resolve all my comment threads if you use the /korbit-resolve command in any comment on your PR.
  • Chat with Korbit on issues we post by tagging @korbit-ai in your reply.
  • Help train Korbit to improve your reviews by giving a 👍 or 👎 on the comments Korbit posts.

Customizing Korbit

  • Check out our docs on how you can make Korbit work best for you and your team.
  • Customize Korbit for your organization through the Korbit Console.

Current Korbit Configuration

General Settings
Setting Value
Review Schedule Automatic excluding drafts
Max Issue Count 10
Automatic PR Descriptions
Issue Categories
Category Enabled
Documentation
Logging
Error Handling
Readability
Design
Performance
Security
Functionality

Feedback and Support

Note

Korbit Pro is free for open source projects 🎉

Looking to add Korbit to your team? Get started with a free 2 week trial here

inventoryId: string,
cityLocode: string,
): Promise<{ locode: string; error: string }[]> {
const errors: any[] = [];
Copy link

Choose a reason for hiding this comment

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

Incorrect Error Array Type Definition category Functionality

Tell me more
What is the issue?

The errors array type in connectAllDataSources is defined as any[], but the function's return type and actual usage indicates it should be an array of objects with locode and error properties.

Why this matters

This type mismatch could lead to runtime errors if the wrong error structure is pushed to the array, as TypeScript's type checking is bypassed with 'any'.

Suggested change ∙ Feature Preview

Define a proper type for the errors array:

const errors: { locode: string; error: string }[] = [];

Report a problem with this comment

💬 Chat with Korbit by mentioning @korbit-ai.

inventory.year!,
) as PopulationAttributes;
// TODO allow country downscaling to work if there is no region population?
if (!cityPopulation || !countryPopulation || !regionPopulation) {
Copy link

Choose a reason for hiding this comment

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

Overly Restrictive Population Check category Functionality

Tell me more
What is the issue?

The code requires all population data (city, country, and region) to be present, but there's a TODO comment suggesting that country downscaling should work without region population.

Why this matters

This restrictive check prevents valid use cases where population-based downscaling could still work with just country data, reducing the functionality of the system unnecessarily.

Suggested change ∙ Feature Preview

Modify the check to allow country-based downscaling when region data is missing:

if (!cityPopulation || !countryPopulation) {
  populationIssue = "missing-population";
} else {
  countryPopulationScaleFactor = cityPopulation.population / countryPopulation.countryPopulation!;
  if (regionPopulation) {
    regionPopulationScaleFactor = cityPopulation.population / regionPopulation.regionPopulation!;
  }
}

Report a problem with this comment

💬 Chat with Korbit by mentioning @korbit-ai.

import { z } from "zod";

const createBulkInventoriesRequest = z.object({
cityLocodes: z.array(z.string()), // List of city locodes
Copy link

Choose a reason for hiding this comment

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

Insufficient Locode Format Validation category Functionality

Tell me more
What is the issue?

The cityLocodes validation accepts any string without validating if it's a proper locode format.

Why this matters

Invalid locode formats could cause downstream processing issues or data inconsistencies in the inventory creation process.

Suggested change ∙ Feature Preview

Add a regex pattern to validate locode format:

const LOCODE_PATTERN = /^[A-Z]{5}$/;
const createBulkInventoriesRequest = z.object({
  cityLocodes: z.array(z.string().regex(LOCODE_PATTERN, "Invalid locode format")).min(1),
  // ... rest of the schema
});

Report a problem with this comment

💬 Chat with Korbit by mentioning @korbit-ai.

Copy link
Contributor

@cephaschapa cephaschapa left a comment

Choose a reason for hiding this comment

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

Great work 👍

import { NextResponse } from "next/server";
import { z } from "zod";

const createBulkInventoriesRequest = z.object({
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be moved to our validations.

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