260617_01_ENH: 배정 테스트를 위해 개발 중도 커밋
This commit is contained in:
100
src/app/api/monitoring/[[...slug]]/route.ts
Normal file
100
src/app/api/monitoring/[[...slug]]/route.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
import {NextRequest, NextResponse} from "next/server";
|
||||
import {RouteContext} from "@/types/common";
|
||||
import {errorToResponse} from "@/lib/http-util";
|
||||
import {createBackApi} from "@/lib/server-api";
|
||||
import {MonitoringGpsRequest, PerformanceMonitoringVisit} from "@/types/gwcf";
|
||||
|
||||
export const GET = async (
|
||||
req: NextRequest,
|
||||
context: RouteContext
|
||||
) => {
|
||||
try {
|
||||
void req;
|
||||
const {slug = []} = await context.params;
|
||||
const [action, subAction, third, fourth, fifth] = slug;
|
||||
const backApi = await createBackApi();
|
||||
|
||||
if (action === "visit" && subAction === "evidence" && third && fourth && slug.length === 4) {
|
||||
const result = await backApi.get<PerformanceMonitoringVisit[]>(
|
||||
`/monitoring/visit/evidence/${encodeURIComponent(third)}/${encodeURIComponent(fourth)}`
|
||||
);
|
||||
return NextResponse.json(result, { status: 200 });
|
||||
}
|
||||
|
||||
if (action === "visit" && subAction === "evidence" && third === "zip" && fourth && fifth && slug.length === 5) {
|
||||
const result = await backApi.get<ArrayBuffer>(
|
||||
`/monitoring/visit/evidence/zip/${encodeURIComponent(fourth)}/${encodeURIComponent(fifth)}`,
|
||||
{ expectBinary: true }
|
||||
);
|
||||
|
||||
return new NextResponse(result, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/zip",
|
||||
"Content-Disposition": `attachment; filename="monitoring-evidence-${fourth}-${fifth}.zip"`,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
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, subAction, performanceId] = slug;
|
||||
const backApi = await createBackApi();
|
||||
|
||||
if (action === "visit" && subAction === "departure" && performanceId && slug.length === 3) {
|
||||
const body = await req.json().catch(() => null) as MonitoringGpsRequest | null;
|
||||
if (!body) {
|
||||
return NextResponse.json({ message: "Request body is required." }, { status: 400 });
|
||||
}
|
||||
|
||||
const result = await backApi.post<PerformanceMonitoringVisit>(
|
||||
`/monitoring/visit/departure/${encodeURIComponent(performanceId)}`,
|
||||
body
|
||||
);
|
||||
return NextResponse.json(result, { status: 200 });
|
||||
}
|
||||
|
||||
if (action === "visit" && subAction === "arrival" && performanceId && slug.length === 3) {
|
||||
const formData = await req.formData();
|
||||
const gpsPart = formData.get("gps");
|
||||
|
||||
if (!gpsPart) {
|
||||
return NextResponse.json({ message: "gps field is required." }, { status: 400 });
|
||||
}
|
||||
|
||||
const backendForm = new FormData();
|
||||
|
||||
if (gpsPart instanceof File) {
|
||||
backendForm.append("gps", gpsPart, gpsPart.name);
|
||||
} else {
|
||||
backendForm.append("gps", new Blob([String(gpsPart)], {type: "application/json"}));
|
||||
}
|
||||
|
||||
formData.getAll("files").forEach((file) => {
|
||||
if (file instanceof File) {
|
||||
backendForm.append("files", file, file.name);
|
||||
}
|
||||
});
|
||||
|
||||
const result = await backApi.postForm<PerformanceMonitoringVisit>(
|
||||
`/monitoring/visit/arrival/${encodeURIComponent(performanceId)}`,
|
||||
backendForm
|
||||
);
|
||||
return NextResponse.json(result, { status: 200 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
||||
} catch (error: unknown) {
|
||||
return errorToResponse(error);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user