@@ -3,15 +3,23 @@ import { NextRequest, NextResponse } from "next/server";
33/**
44 * PATCH /api/invoices/bulk
55 *
6- * Bulk archive/unarchive operation endpoint.
7- * Accepts up to 200 invoice IDs per request with archive status.
6+ * Bulk operation endpoint for archive, delete, and tag operations.
7+ * Accepts up to 200 invoice IDs per request.
8+ *
9+ * Request body:
10+ * - invoiceIds: string[] (required)
11+ * - action: 'archive' | 'delete' | 'tag' (required)
12+ * - archived?: boolean (for archive action)
13+ * - tags?: string[] (for tag action)
814 */
915export async function PATCH ( request : NextRequest ) {
1016 try {
1117 const body = await request . json ( ) ;
12- const { invoiceIds, archived } = body as {
18+ const { invoiceIds, action , archived, tags } = body as {
1319 invoiceIds : string [ ] ;
14- archived : boolean ;
20+ action : string ;
21+ archived ?: boolean ;
22+ tags ?: string [ ] ;
1523 } ;
1624
1725 // Validate inputs
@@ -29,23 +37,53 @@ export async function PATCH(request: NextRequest) {
2937 ) ;
3038 }
3139
32- if ( typeof archived !== "boolean" ) {
40+ if ( ! [ "archive" , "delete" , "tag" ] . includes ( action ) ) {
3341 return NextResponse . json (
34- { error : "archived must be a boolean " } ,
42+ { error : "action must be 'archive', 'delete', or 'tag' " } ,
3543 { status : 400 } ,
3644 ) ;
3745 }
3846
39- // TODO: Implement actual database storage of archived status
40- // For now, this endpoint validates the request and returns success.
41- // In production, store archived status in database alongside invoice data.
47+ if ( action === "archive" && typeof archived !== "boolean" ) {
48+ return NextResponse . json (
49+ { error : "archived must be a boolean for archive action" } ,
50+ { status : 400 } ,
51+ ) ;
52+ }
53+
54+ if ( action === "tag" && ! Array . isArray ( tags ) ) {
55+ return NextResponse . json (
56+ { error : "tags must be an array for tag action" } ,
57+ { status : 400 } ,
58+ ) ;
59+ }
60+
61+ // TODO: Implement actual database operations
62+ // For now, validate the request and return success.
63+ // In production:
64+ // - archive: Update archived status in database
65+ // - delete: Mark invoices as deleted or remove them
66+ // - tag: Apply tags to invoices
67+
68+ let message = "" ;
69+ switch ( action ) {
70+ case "archive" :
71+ message = `${ invoiceIds . length } invoices ${ archived ? "archived" : "unarchived" } ` ;
72+ break ;
73+ case "delete" :
74+ message = `${ invoiceIds . length } invoices deleted` ;
75+ break ;
76+ case "tag" :
77+ message = `${ invoiceIds . length } invoices tagged with: ${ tags ?. join ( ", " ) } ` ;
78+ break ;
79+ }
4280
4381 return NextResponse . json (
4482 {
4583 success : true ,
4684 count : invoiceIds . length ,
47- archived ,
48- message : ` ${ invoiceIds . length } invoices ${ archived ? "archived" : "unarchived" } ` ,
85+ action ,
86+ message,
4987 } ,
5088 { status : 200 } ,
5189 ) ;
0 commit comments