60 lines
2.2 KiB
TypeScript
60 lines
2.2 KiB
TypeScript
import { NextRequest } from 'next/server'
|
|
import { bomCostingScenarioCalculationSchema } from '@/lib/bom-costing/contracts'
|
|
import { AppError, ErrorCode } from '@/server/core/errors'
|
|
import { handleRequest, jsonError, jsonSuccess } from '@/server/core/response'
|
|
import { bomCostingService } from '@/server/services/bom-costing/bom-costing.service'
|
|
import { bomCostingScenarioService } from '@/server/services/bom-costing/bom-costing-scenario.service'
|
|
|
|
type RouteContext = { params: Promise<{ id: string }> }
|
|
|
|
export async function GET(request: NextRequest, context: RouteContext) {
|
|
return handleRequest(
|
|
request,
|
|
async (ctx) => {
|
|
const { id } = await context.params
|
|
const caseId = decodeURIComponent(id)
|
|
const skuId = request.nextUrl.searchParams.get('skuId')?.trim() || undefined
|
|
try {
|
|
await bomCostingService.getCase(caseId, ctx.user)
|
|
return jsonSuccess(
|
|
{ scenarios: await bomCostingScenarioService.listScenarios({ caseId, skuId }) },
|
|
200,
|
|
ctx.requestId,
|
|
)
|
|
} catch (error) {
|
|
if (error instanceof AppError) {
|
|
return jsonError(error.code, error.message, error.statusCode, ctx.requestId)
|
|
}
|
|
throw error
|
|
}
|
|
},
|
|
{ requireAuth: true },
|
|
)
|
|
}
|
|
|
|
export async function POST(request: NextRequest, context: RouteContext) {
|
|
return handleRequest(
|
|
request,
|
|
async (ctx) => {
|
|
const { id } = await context.params
|
|
const caseId = decodeURIComponent(id)
|
|
const body = await request.json().catch(() => null)
|
|
const parsed = bomCostingScenarioCalculationSchema.safeParse(body)
|
|
if (!parsed.success) {
|
|
return jsonError(ErrorCode.VALIDATION_FAILED, '目标材质参数不正确', 400, ctx.requestId)
|
|
}
|
|
try {
|
|
await bomCostingService.getCase(caseId, ctx.user)
|
|
const result = await bomCostingScenarioService.calculate({ caseId, ...parsed.data })
|
|
return jsonSuccess(result, 200, ctx.requestId)
|
|
} catch (error) {
|
|
if (error instanceof AppError) {
|
|
return jsonError(error.code, error.message, error.statusCode, ctx.requestId)
|
|
}
|
|
throw error
|
|
}
|
|
},
|
|
{ requireAuth: true },
|
|
)
|
|
}
|