[action].post.ts
1.42 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
import { z } from 'zod'
import { WORK_ORDER_TRANSITION_ACTION_VALUES } from '../../../../../app/types/work-order'
import { transitionMockWorkOrder } from '../../../../utils/mock-work-orders'
const transitionActions = WORK_ORDER_TRANSITION_ACTION_VALUES.filter(action => action !== 'create' && action !== 'edit_draft')
const paramsSchema = z.object({
action: z.enum(transitionActions as [string, ...string[]])
})
const payloadSchema = z.object({
operator: z.string().trim().min(1),
remark: z.string().trim().optional()
})
export default eventHandler(async (event) => {
const idRaw = getRouterParam(event, 'id')
const id = Number(idRaw)
if (!Number.isInteger(id) || id <= 0) {
return {
success: false,
errorCode: 'VALIDATION_ERROR',
message: '工单标识不正确。'
}
}
const actionRaw = getRouterParam(event, 'action')
const actionResult = paramsSchema.safeParse({ action: actionRaw })
if (!actionResult.success) {
return {
success: false,
errorCode: 'VALIDATION_ERROR',
message: '工单动作不支持。'
}
}
const body = await readBody(event)
const parsed = payloadSchema.safeParse(body)
if (!parsed.success) {
return {
success: false,
errorCode: 'VALIDATION_ERROR',
message: '操作参数不正确。'
}
}
return transitionMockWorkOrder(id, actionResult.data.action as 'dispatch' | 'pause' | 'resume' | 'close', parsed.data)
})