Repository navigation
Expand file tree
/
Copy pathroute.ts
More file actions
56 lines (50 loc) · 1.45 KB
/
Copy pathroute.ts
File metadata and controls
56 lines (50 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { auth } from "@/auth";
import DonorOfferService from "@/services/donorOfferService";
import UserService from "@/services/userService";
import {
ArgumentError,
AuthenticationError,
errorResponse,
} from "@/util/errors";
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
const paramSchema = z.object({
donorOfferId: z.string().transform((val) => {
const parsed = parseInt(val, 10);
if (Number.isNaN(parsed)) {
throw new Error("donorOfferId must be a valid number");
}
return parsed;
}),
});
export async function GET(
_: NextRequest,
{ params }: { params: Promise<{ donorOfferId: string }> }
) {
try {
const session = await auth();
if (!session?.user) {
throw new AuthenticationError("Session required");
}
UserService.checkPermission(session.user, "offerWrite");
const resolvedParams = await params;
const parsed = paramSchema.safeParse(resolvedParams);
if (!parsed.success) {
throw new ArgumentError(parsed.error.message);
}
const { csv, fileName } =
await DonorOfferService.getUnfinalizedDonorOfferCsv(
parsed.data.donorOfferId
);
return new NextResponse(csv, {
status: 200,
headers: {
"Content-Type": "text/csv; charset=utf-8",
"Content-Disposition": `attachment; filename="${fileName}"`,
"Cache-Control": "no-store",
},
});
} catch (error) {
return errorResponse(error);
}
}