feat(collection-confirm): add GET /api/v1/collection-confirm route
Co-Authored-By: Claude <noreply@anthropic.com> Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
parent
c856db1617
commit
bb10f1350b
2 changed files with 54 additions and 0 deletions
40
src/app/api/v1/collection-confirm/route.test.ts
Normal file
40
src/app/api/v1/collection-confirm/route.test.ts
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("@/lib/server/collection-confirm-service", () => ({
|
||||
listCollectedItems: vi.fn(),
|
||||
}));
|
||||
|
||||
import { GET } from "./route";
|
||||
import { listCollectedItems } from "@/lib/server/collection-confirm-service";
|
||||
|
||||
describe("GET /api/v1/collection-confirm", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("returns the rows from listCollectedItems as JSON", async () => {
|
||||
const rows = [
|
||||
{
|
||||
id: "row-1",
|
||||
source: "customer" as const,
|
||||
description: "Collected item",
|
||||
},
|
||||
];
|
||||
vi.mocked(listCollectedItems).mockResolvedValue(rows);
|
||||
|
||||
const response = await GET();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(listCollectedItems).toHaveBeenCalledOnce();
|
||||
await expect(response.json()).resolves.toEqual(rows);
|
||||
});
|
||||
|
||||
it("returns a 500 with an error body when the service throws", async () => {
|
||||
vi.mocked(listCollectedItems).mockRejectedValue(new Error("PB down"));
|
||||
|
||||
const response = await GET();
|
||||
|
||||
expect(response.status).toBe(500);
|
||||
await expect(response.json()).resolves.toEqual({ error: "PB down" });
|
||||
});
|
||||
});
|
||||
14
src/app/api/v1/collection-confirm/route.ts
Normal file
14
src/app/api/v1/collection-confirm/route.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import { NextResponse } from "next/server";
|
||||
import { listCollectedItems } from "@/lib/server/collection-confirm-service";
|
||||
|
||||
export async function GET(): Promise<NextResponse> {
|
||||
try {
|
||||
const rows = await listCollectedItems();
|
||||
return NextResponse.json(rows);
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : "internal error" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue