fix(stock-quick-draft): parseInt limit + exhaustive status map
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
parent
0515b1ce7c
commit
921ea4e63d
2 changed files with 11 additions and 3 deletions
|
|
@ -1,6 +1,11 @@
|
|||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getAdminPocketBase } from "@/lib/server/pocketbase-admin";
|
||||
import { addQuickDraftLine } from "@/lib/server/stock-quick-draft-service";
|
||||
import {
|
||||
addQuickDraftLine,
|
||||
type AddQuickDraftLineResult,
|
||||
} from "@/lib/server/stock-quick-draft-service";
|
||||
|
||||
type FailureReason = Extract<AddQuickDraftLineResult, { ok: false }>["reason"];
|
||||
|
||||
/**
|
||||
* POST /api/v1/stock-quick-draft/add-line
|
||||
|
|
@ -31,7 +36,7 @@ import { addQuickDraftLine } from "@/lib/server/stock-quick-draft-service";
|
|||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
const STATUS_BY_REASON: Record<string, number> = {
|
||||
const STATUS_BY_REASON: Record<FailureReason, number> = {
|
||||
validation: 400,
|
||||
supplier_not_found: 404,
|
||||
stock_item_not_found: 404,
|
||||
|
|
|
|||
|
|
@ -21,7 +21,10 @@ export async function GET(req: NextRequest) {
|
|||
const { searchParams } = new URL(req.url);
|
||||
const q = (searchParams.get("q") ?? "").trim();
|
||||
const limitRaw = searchParams.get("limit");
|
||||
const limit = limitRaw ? Number(limitRaw) : undefined;
|
||||
const parsed = limitRaw ? Number.parseInt(limitRaw, 10) : NaN;
|
||||
// NaN / non-finite → undefined so the service uses its documented default
|
||||
// rather than propagating an invalid `perPage` to PocketBase (which 500s).
|
||||
const limit = Number.isFinite(parsed) ? parsed : undefined;
|
||||
|
||||
if (q.length < 2) {
|
||||
return NextResponse.json(
|
||||
|
|
|
|||
Loading…
Reference in a new issue