useWorkOrderApi.ts
6.01 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import type {
WorkOrder,
WorkOrderListQuery,
WorkOrderMutationResponse,
WorkOrderPayload,
WorkOrderTransitionAction
} from '~/types'
import { WORK_ORDER_STATUS_VALUES } from '~/types/work-order'
import { createSharedComposable } from '@vueuse/core'
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null
}
function isStatus(value: unknown): value is WorkOrder['status'] {
return typeof value === 'string' && WORK_ORDER_STATUS_VALUES.includes(value as WorkOrder['status'])
}
function normalizeString(value: unknown, fallback = '') {
return typeof value === 'string' ? value : fallback
}
function normalizeNumber(value: unknown, fallback = 0) {
const num = Number(value)
return Number.isFinite(num) ? num : fallback
}
function normalizeWorkOrders(payload: unknown): WorkOrder[] {
if (!Array.isArray(payload)) {
return []
}
return payload
.filter(isRecord)
.map((item) => {
const progress = isRecord(item.progress) ? item.progress : {}
const audit = isRecord(item.audit) ? item.audit : {}
const events = Array.isArray(item.events) ? item.events : []
return {
id: normalizeNumber(item.id),
orderNo: normalizeString(item.orderNo),
deviceCode: normalizeString(item.deviceCode),
batchNo: normalizeString(item.batchNo),
line: normalizeString(item.line),
ownerUsername: normalizeString(item.ownerUsername),
ownerName: normalizeString(item.ownerName),
plannedDate: normalizeString(item.plannedDate),
status: isStatus(item.status) ? item.status : 'draft',
progress: {
completedSn: normalizeNumber(progress.completedSn),
totalSn: normalizeNumber(progress.totalSn)
},
audit: {
createdBy: normalizeString(audit.createdBy),
createdAt: normalizeString(audit.createdAt),
updatedBy: normalizeString(audit.updatedBy),
updatedAt: normalizeString(audit.updatedAt),
lastAction: normalizeString(audit.lastAction) as WorkOrder['audit']['lastAction'],
lastActionAt: normalizeString(audit.lastActionAt),
lastActionBy: normalizeString(audit.lastActionBy)
},
events: events
.filter(isRecord)
.map(event => ({
id: normalizeString(event.id),
action: normalizeString(event.action) as WorkOrder['events'][number]['action'],
fromStatus: isStatus(event.fromStatus) ? event.fromStatus : null,
toStatus: isStatus(event.toStatus) ? event.toStatus : 'draft',
operator: normalizeString(event.operator),
at: normalizeString(event.at),
remark: normalizeString(event.remark, undefined)
}))
} satisfies WorkOrder
})
.filter(item => item.id > 0 && item.orderNo.length > 0)
}
function normalizeMutation(payload: unknown, fallbackMessage: string): WorkOrderMutationResponse {
if (!isRecord(payload)) {
return {
success: false,
errorCode: 'REQUEST_FAILED',
message: fallbackMessage
}
}
return {
success: Boolean(payload.success),
errorCode: typeof payload.errorCode === 'string' ? payload.errorCode : null,
message: typeof payload.message === 'string' && payload.message.length > 0
? payload.message
: fallbackMessage
}
}
function buildQuery(query: WorkOrderListQuery) {
const params = new URLSearchParams()
if (query.orderNo) {
params.set('orderNo', query.orderNo)
}
if (query.deviceCode) {
params.set('deviceCode', query.deviceCode)
}
if (query.batchNo) {
params.set('batchNo', query.batchNo)
}
if (query.ownerUsername) {
params.set('ownerUsername', query.ownerUsername)
}
if (query.status) {
params.set('status', query.status)
}
if (query.plannedStart) {
params.set('plannedStart', query.plannedStart)
}
if (query.plannedEnd) {
params.set('plannedEnd', query.plannedEnd)
}
const suffix = params.toString()
return suffix.length > 0 ? `?${suffix}` : ''
}
const _useWorkOrderApi = () => {
const api = useApiGateway()
const getWorkOrders = async (query: WorkOrderListQuery) => {
const result = await api.request<unknown>(`/api/work-orders${buildQuery(query)}`, {
method: 'GET'
})
return normalizeWorkOrders(result)
}
const createWorkOrder = async (payload: WorkOrderPayload & { operator: string }) => {
try {
const result = await api.request<unknown>('/api/work-orders', {
method: 'POST',
body: payload
})
return normalizeMutation(result, '创建工单失败。')
} catch {
return {
success: false,
errorCode: 'REQUEST_FAILED',
message: '创建工单失败。'
} satisfies WorkOrderMutationResponse
}
}
const updateWorkOrderDraft = async (id: number, payload: WorkOrderPayload & { operator: string }) => {
try {
const result = await api.request<unknown>(`/api/work-orders/${id}`, {
method: 'PUT',
body: payload
})
return normalizeMutation(result, '更新工单失败。')
} catch {
return {
success: false,
errorCode: 'REQUEST_FAILED',
message: '更新工单失败。'
} satisfies WorkOrderMutationResponse
}
}
const transitionWorkOrder = async (
id: number,
action: Exclude<WorkOrderTransitionAction, 'create' | 'edit_draft'>,
payload: { operator: string, remark?: string }
) => {
try {
const result = await api.request<unknown>(`/api/work-orders/${id}/actions/${action}`, {
method: 'POST',
body: payload
})
return normalizeMutation(result, '工单状态更新失败。')
} catch {
return {
success: false,
errorCode: 'REQUEST_FAILED',
message: '工单状态更新失败。'
} satisfies WorkOrderMutationResponse
}
}
return {
getWorkOrders,
createWorkOrder,
updateWorkOrderDraft,
transitionWorkOrder
}
}
export const useWorkOrderApi = createSharedComposable(_useWorkOrderApi)