148 lines
4.3 KiB
TypeScript
148 lines
4.3 KiB
TypeScript
import {NextRequest, NextResponse} from "next/server";
|
|
import {RouteContext} from "@/types/common";
|
|
import {errorToResponse} from "@/lib/http-util";
|
|
import {createBackApi} from "@/lib/server-api";
|
|
import {ExcelUploadResult} from "@/types/common";
|
|
import {UserFull} from "@/types/gwcf";
|
|
|
|
export const GET = async (
|
|
req: NextRequest,
|
|
context: RouteContext
|
|
) => {
|
|
try {
|
|
const {slug = []} = await context.params;
|
|
const [action] = slug;
|
|
const backApi = await createBackApi();
|
|
|
|
if (action === "list" && slug.length === 2) {
|
|
const result = await backApi.get<UserFull[]>(`/user/list/${encodeURIComponent(slug[1])}`);
|
|
return NextResponse.json(result, { status: 200 });
|
|
}
|
|
|
|
if (slug.length === 1 && action) {
|
|
const result = await backApi.get<UserFull>(`/user/${encodeURIComponent(action)}`);
|
|
return NextResponse.json(result, { status: 200 });
|
|
}
|
|
|
|
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
|
} catch (error: unknown) {
|
|
return errorToResponse(error);
|
|
}
|
|
};
|
|
|
|
export const POST = async (
|
|
req: NextRequest,
|
|
context: RouteContext
|
|
) => {
|
|
try {
|
|
const {slug = []} = await context.params;
|
|
const [action] = slug;
|
|
const backApi = await createBackApi();
|
|
|
|
if (action === "excel-upload" && slug.length === 2) {
|
|
const formData = await req.formData();
|
|
const file = formData.get("file") as File | null;
|
|
|
|
if (!file) {
|
|
return NextResponse.json({ message: "file field is required." }, { status: 400 });
|
|
}
|
|
|
|
const backendForm = new FormData();
|
|
backendForm.append("file", file, file.name);
|
|
|
|
const result = await backApi.postForm<ExcelUploadResult>(
|
|
`/user/excel/upload/${encodeURIComponent(slug[1])}`,
|
|
backendForm
|
|
);
|
|
return NextResponse.json(result, { status: 200 });
|
|
}
|
|
|
|
if (action === "excel-template" && slug.length === 2) {
|
|
const result = await backApi.post<ArrayBuffer>(
|
|
`/user/excel/template/${encodeURIComponent(slug[1])}`,
|
|
undefined,
|
|
{ expectBinary: true }
|
|
);
|
|
|
|
return new NextResponse(result, {
|
|
status: 200,
|
|
headers: {
|
|
"Content-Type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
"Content-Disposition": `attachment; filename="user_${slug[1]}_template.xlsx"`,
|
|
},
|
|
});
|
|
}
|
|
|
|
if (action === "excel-download" && slug.length === 2) {
|
|
const body = await req.json().catch(() => null);
|
|
|
|
if (!Array.isArray(body)) {
|
|
return NextResponse.json({ message: "Request body must be an array." }, { status: 400 });
|
|
}
|
|
|
|
const result = await backApi.post<ArrayBuffer>(
|
|
`/user/excel/download/${encodeURIComponent(slug[1])}`,
|
|
body,
|
|
{ expectBinary: true }
|
|
);
|
|
|
|
return new NextResponse(result, {
|
|
status: 200,
|
|
headers: {
|
|
"Content-Type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
"Content-Disposition": `attachment; filename="user_${slug[1]}.xlsx"`,
|
|
},
|
|
});
|
|
}
|
|
|
|
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
|
} catch (error: unknown) {
|
|
return errorToResponse(error);
|
|
}
|
|
};
|
|
|
|
export const PATCH = async (
|
|
req: NextRequest,
|
|
context: RouteContext
|
|
) => {
|
|
try {
|
|
const {slug = []} = await context.params;
|
|
const [action] = slug;
|
|
const backApi = await createBackApi();
|
|
|
|
if (!action) {
|
|
const body = await req.json().catch(() => null);
|
|
if (!body) {
|
|
return NextResponse.json({ message: "Request body is required." }, { status: 400 });
|
|
}
|
|
|
|
await backApi.patch<void>("/user", body);
|
|
return new NextResponse(null, { status: 204 });
|
|
}
|
|
|
|
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
|
} catch (error: unknown) {
|
|
return errorToResponse(error);
|
|
}
|
|
};
|
|
|
|
export const DELETE = async (
|
|
req: NextRequest,
|
|
context: RouteContext
|
|
) => {
|
|
try {
|
|
const {slug = []} = await context.params;
|
|
const [userId] = slug;
|
|
const backApi = await createBackApi();
|
|
|
|
if (userId && slug.length === 1) {
|
|
await backApi.del<void>(`/user/${encodeURIComponent(userId)}`);
|
|
return new NextResponse(null, { status: 204 });
|
|
}
|
|
|
|
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
|
} catch (error: unknown) {
|
|
return errorToResponse(error);
|
|
}
|
|
};
|