[action].post.ts
1.33 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
50
51
52
53
54
import { z } from 'zod'
import { SN_ACTION_VALUES } from '../../../../../app/types/sn'
import { mutateMockSnStatus } from '../../../../utils/mock-sns'
const actionValues = SN_ACTION_VALUES.filter(action => action !== 'import')
const paramsSchema = z.object({
action: z.enum(actionValues as [string, ...string[]])
})
const payloadSchema = z.object({
operator: z.string().trim().min(1),
reason: z.string().trim().min(1)
})
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: 'SN 标识不正确。'
}
}
const actionRaw = getRouterParam(event, 'action')
const parsedAction = paramsSchema.safeParse({ action: actionRaw })
if (!parsedAction.success) {
return {
success: false,
errorCode: 'VALIDATION_ERROR',
message: 'SN 动作不支持。'
}
}
const body = await readBody(event)
const parsedBody = payloadSchema.safeParse(body)
if (!parsedBody.success) {
return {
success: false,
errorCode: 'VALIDATION_ERROR',
message: 'SN 操作参数不正确。'
}
}
return mutateMockSnStatus(
id,
parsedAction.data.action as 'freeze' | 'unfreeze' | 'scrap',
parsedBody.data
)
})