feat(status): add estimate customer actions
This commit is contained in:
parent
efdc4d5be1
commit
6f6891134e
9 changed files with 699 additions and 3 deletions
36
app/api/status/[token]/estimate/action-response.ts
Normal file
36
app/api/status/[token]/estimate/action-response.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import { NextResponse } from "next/server";
|
||||
import type { EstimateActionResult } from "@/lib/olympus/status";
|
||||
|
||||
export function estimateActionResponse(result: EstimateActionResult, successMessage: string) {
|
||||
if (result.ok) {
|
||||
return NextResponse.json({ message: successMessage });
|
||||
}
|
||||
|
||||
if (result.reason === "invalid") {
|
||||
return NextResponse.json(
|
||||
{ message: "Dieser Statuslink ist ungültig oder nicht mehr aktiv." },
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
if (result.reason === "unavailable") {
|
||||
return NextResponse.json(
|
||||
{ message: "Der Reparaturstatus ist momentan nicht abrufbar." },
|
||||
{ status: 502 },
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ message: "Die Aktion konnte nicht abgeschlossen werden. Bitte versuchen Sie es erneut." },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
export async function readOptionalMessage(request: Request) {
|
||||
try {
|
||||
const body = await request.json() as { message?: unknown };
|
||||
return typeof body.message === "string" ? body.message : "";
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
15
app/api/status/[token]/estimate/approve/route.ts
Normal file
15
app/api/status/[token]/estimate/approve/route.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import { submitOlympusEstimateAction } from "@/lib/olympus/status";
|
||||
import { estimateActionResponse } from "../action-response";
|
||||
|
||||
type RouteContext = {
|
||||
params: Promise<{
|
||||
token: string;
|
||||
}>;
|
||||
};
|
||||
|
||||
export async function POST(_request: Request, { params }: RouteContext) {
|
||||
const { token } = await params;
|
||||
const result = await submitOlympusEstimateAction(token, "approve");
|
||||
|
||||
return estimateActionResponse(result, "Kostenvoranschlag wurde freigegeben.");
|
||||
}
|
||||
16
app/api/status/[token]/estimate/decline/route.ts
Normal file
16
app/api/status/[token]/estimate/decline/route.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { submitOlympusEstimateAction } from "@/lib/olympus/status";
|
||||
import { estimateActionResponse, readOptionalMessage } from "../action-response";
|
||||
|
||||
type RouteContext = {
|
||||
params: Promise<{
|
||||
token: string;
|
||||
}>;
|
||||
};
|
||||
|
||||
export async function POST(request: Request, { params }: RouteContext) {
|
||||
const { token } = await params;
|
||||
const message = await readOptionalMessage(request);
|
||||
const result = await submitOlympusEstimateAction(token, "decline", message);
|
||||
|
||||
return estimateActionResponse(result, "Kostenvoranschlag wurde abgelehnt.");
|
||||
}
|
||||
16
app/api/status/[token]/estimate/question/route.ts
Normal file
16
app/api/status/[token]/estimate/question/route.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { submitOlympusEstimateAction } from "@/lib/olympus/status";
|
||||
import { estimateActionResponse, readOptionalMessage } from "../action-response";
|
||||
|
||||
type RouteContext = {
|
||||
params: Promise<{
|
||||
token: string;
|
||||
}>;
|
||||
};
|
||||
|
||||
export async function POST(request: Request, { params }: RouteContext) {
|
||||
const { token } = await params;
|
||||
const message = await readOptionalMessage(request);
|
||||
const result = await submitOlympusEstimateAction(token, "question", message);
|
||||
|
||||
return estimateActionResponse(result, "Ihre Rückfrage wurde gesendet.");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue