feat: 同步报销流程与工作台改动
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { apiRequest } from './api.js'
|
||||
|
||||
export const REIMBURSEMENT_LIST_PREVIEW_PARAMS = Object.freeze({ page: 1, pageSize: 100 })
|
||||
export const REIMBURSEMENT_LIST_FULL_PAGE_SIZE = 100
|
||||
const REIMBURSEMENT_LIST_MAX_PAGES = 200
|
||||
|
||||
function buildListQuery(params = {}) {
|
||||
const search = new URLSearchParams()
|
||||
@@ -27,18 +29,73 @@ export function extractExpenseClaimItems(payload) {
|
||||
return Array.isArray(payload?.items) ? payload.items : []
|
||||
}
|
||||
|
||||
function isPaginatedExpenseClaimPayload(payload) {
|
||||
return Boolean(
|
||||
payload
|
||||
&& typeof payload === 'object'
|
||||
&& Array.isArray(payload.items)
|
||||
&& Number.isFinite(Number(payload.page))
|
||||
&& Number.isFinite(Number(payload.page_size))
|
||||
)
|
||||
}
|
||||
|
||||
async function fetchAllExpenseClaimPages(fetchPage, params = {}) {
|
||||
const pageSize = Number(params.pageSize || params.page_size || REIMBURSEMENT_LIST_FULL_PAGE_SIZE)
|
||||
const normalizedPageSize = Number.isFinite(pageSize) && pageSize > 0
|
||||
? Math.min(REIMBURSEMENT_LIST_FULL_PAGE_SIZE, Math.floor(pageSize))
|
||||
: REIMBURSEMENT_LIST_FULL_PAGE_SIZE
|
||||
let page = Math.max(1, Math.floor(Number(params.page || 1) || 1))
|
||||
const items = []
|
||||
|
||||
for (let index = 0; index < REIMBURSEMENT_LIST_MAX_PAGES; index += 1) {
|
||||
const payload = await fetchPage({
|
||||
...params,
|
||||
page,
|
||||
pageSize: normalizedPageSize
|
||||
})
|
||||
|
||||
if (!isPaginatedExpenseClaimPayload(payload)) {
|
||||
return extractExpenseClaimItems(payload)
|
||||
}
|
||||
|
||||
items.push(...extractExpenseClaimItems(payload))
|
||||
|
||||
const total = Number(payload.total || 0)
|
||||
const totalPages = Number(payload.total_pages || 0)
|
||||
const hasNext = Boolean(payload.has_next)
|
||||
if (!hasNext || (totalPages && page >= totalPages) || (total && items.length >= total)) {
|
||||
break
|
||||
}
|
||||
page += 1
|
||||
}
|
||||
|
||||
return items
|
||||
}
|
||||
|
||||
export function fetchExpenseClaims(params = {}) {
|
||||
return apiRequest(`/reimbursements/claims${buildListQuery(params)}`)
|
||||
}
|
||||
|
||||
export function fetchAllExpenseClaims(params = {}) {
|
||||
return fetchAllExpenseClaimPages(fetchExpenseClaims, params)
|
||||
}
|
||||
|
||||
export function fetchApprovalExpenseClaims(params = {}) {
|
||||
return apiRequest(`/reimbursements/claims/approvals${buildListQuery(params)}`)
|
||||
}
|
||||
|
||||
export function fetchAllApprovalExpenseClaims(params = {}) {
|
||||
return fetchAllExpenseClaimPages(fetchApprovalExpenseClaims, params)
|
||||
}
|
||||
|
||||
export function fetchArchivedExpenseClaims(params = {}) {
|
||||
return apiRequest(`/reimbursements/claims/archives${buildListQuery(params)}`)
|
||||
}
|
||||
|
||||
export function fetchAllArchivedExpenseClaims(params = {}) {
|
||||
return fetchAllExpenseClaimPages(fetchArchivedExpenseClaims, params)
|
||||
}
|
||||
|
||||
export function fetchExpenseClaimDetail(claimId) {
|
||||
return apiRequest(`/reimbursements/claims/${encodeURIComponent(String(claimId || '').trim())}`)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user