RJBULK-APP/app/api/notes-for-statement/route.ts

26 lines
1.1 KiB
TypeScript
Raw Normal View History

import { NextRequest, NextResponse } from "next/server";
import { getAuth } from "@/lib/auth";
import { pbList } from "@/lib/pocketbase";
import type { DeliveryNote } from "@/lib/types";
export const dynamic = "force-dynamic";
export async function GET(req: NextRequest) {
const { token } = await getAuth();
if (!token) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
const { searchParams } = req.nextUrl;
const customer = searchParams.get("customer");
const start = searchParams.get("start");
const end = searchParams.get("end");
if (!customer || !start || !end) {
return NextResponse.json({ error: "customer, start, end required" }, { status: 400 });
}
try {
const filter = `customer = "${customer}" && date >= "${start}" && date <= "${end}" && (status = "Pending" || status = "")`;
const res = await pbList<DeliveryNote>("delivery_notes", { perPage: 1000, filter, sort: "date" }, token);
return NextResponse.json({ notes: res.items });
} catch (err: any) {
return NextResponse.json({ error: err?.message }, { status: 500 });
}
}