65 lines
2.1 KiB
JavaScript
65 lines
2.1 KiB
JavaScript
function ensureArray(value) {
|
|
return Array.isArray(value) ? value : []
|
|
}
|
|
|
|
function pickFirstText() {
|
|
for (let index = 0; index < arguments.length; index += 1) {
|
|
const value = arguments[index]
|
|
if (value === null || value === undefined) continue
|
|
const text = String(value).trim()
|
|
if (text) return text
|
|
}
|
|
return ''
|
|
}
|
|
|
|
function normalizeNumber(value, fallback = 0) {
|
|
const num = Number(value)
|
|
return Number.isFinite(num) ? num : fallback
|
|
}
|
|
|
|
function normalizeTodoEntry(raw) {
|
|
if (raw && typeof raw === 'object') {
|
|
return {
|
|
...raw,
|
|
_todoCode: pickFirstText(raw.code, raw.no, raw.contractNo, raw.billNo, raw.orderNo, raw.referenceTypeNo)
|
|
}
|
|
}
|
|
const text = pickFirstText(raw)
|
|
return text ? { code: text, _todoCode: text } : null
|
|
}
|
|
|
|
function extractTodoEntries(item) {
|
|
const sourceList = [item && item.items, item && item.codes, item && item.list, item && item.data]
|
|
for (let index = 0; index < sourceList.length; index += 1) {
|
|
const current = sourceList[index]
|
|
if (!Array.isArray(current)) continue
|
|
return current.map(entry => normalizeTodoEntry(entry)).filter(Boolean)
|
|
}
|
|
return []
|
|
}
|
|
|
|
export function extractTodoRows(response) {
|
|
const payload = response && response.data !== undefined ? response.data : response
|
|
if (Array.isArray(payload)) return payload
|
|
if (!payload || typeof payload !== 'object') return []
|
|
if (Array.isArray(payload.data)) return payload.data
|
|
if (payload.data && Array.isArray(payload.data.data)) return payload.data.data
|
|
return []
|
|
}
|
|
|
|
export function normalizeTodoList(rows = []) {
|
|
return ensureArray(rows).map((item) => {
|
|
const entries = extractTodoEntries(item)
|
|
const count = Math.max(normalizeNumber(item && item.count, entries.length), entries.length)
|
|
return {
|
|
todoType: String(item && item.todoType !== undefined ? item.todoType : ''),
|
|
todoTypeDesc: pickFirstText(item && item.todoTypeDesc),
|
|
count,
|
|
entries,
|
|
codes: entries
|
|
.map(entry => ({ code: pickFirstText(entry && entry._todoCode) }))
|
|
.filter(entry => entry.code)
|
|
}
|
|
}).filter(item => item.count > 0 || item.entries.length > 0 || item.todoTypeDesc)
|
|
}
|