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
|
"use server";
import type { Filter, ExtendedSortingState, JoinOperator } from "@/types/table";
import { getAllVendorsContractsCompletionSummary } from "@/lib/edp-progress/vendor-completion-stats";
import type { EDPVendorRow } from "./table/edp-progress-table-columns";
type GetListsArgs = {
filters?: Filter<EDPVendorRow>[]
sort?: ExtendedSortingState<EDPVendorRow>
joinOperator?: JoinOperator
search?: string
}
function applyFilters(rows: EDPVendorRow[], filters?: Filter<EDPVendorRow>[], _joinOperator?: JoinOperator): EDPVendorRow[] {
if (!filters || filters.length === 0) return rows
return rows.filter((row) => {
return filters.every((f) => {
const id = f.id as keyof EDPVendorRow
const value = (row as any)[id]
const v = f.value
switch (f.type) {
case "text":
return typeof value === "string" && String(value).toLowerCase().includes(String(v).toLowerCase())
case "number":
if (typeof value !== "number") return false
if (f.operator === "gte") return value >= Number(v)
if (f.operator === "lte") return value <= Number(v)
if (f.operator === "gt") return value > Number(v)
if (f.operator === "lt") return value < Number(v)
return value === Number(v)
case "select":
return String(value) === String(v)
default:
return true
}
})
})
}
function applyGlobalSearch(rows: EDPVendorRow[], search?: string): EDPVendorRow[] {
if (!search) return rows
const s = search.toLowerCase()
return rows.filter((r) =>
(r.vendorName || "").toLowerCase().includes(s)
)
}
function applySorting(rows: EDPVendorRow[], sort?: ExtendedSortingState<EDPVendorRow>): EDPVendorRow[] {
if (!sort || sort.length === 0) return rows
const copy = [...rows]
copy.sort((a, b) => {
for (const s of sort) {
const aVal = (a as any)[s.id]
const bVal = (b as any)[s.id]
if (aVal === bVal) continue
if (aVal == null) return s.desc ? 1 : -1
if (bVal == null) return s.desc ? -1 : 1
if (aVal < bVal) return s.desc ? 1 : -1
if (aVal > bVal) return s.desc ? -1 : 1
}
return 0
})
return copy
}
export async function getEDPProgressLists(args: GetListsArgs = {}): Promise<{ data: EDPVendorRow[]; pageCount: number }> {
const res = await getAllVendorsContractsCompletionSummary();
const rows: EDPVendorRow[] = (res?.vendors || []).map((v) => ({
vendorId: v.vendorId,
vendorName: v.vendorName,
totalForms: v.totalForms,
totalTags: v.totalTags,
totalRequiredFields: v.totalRequiredFields,
totalFilledFields: v.totalFilledFields,
completionPercentage: v.overallCompletionPercentage,
}));
const searched = applyGlobalSearch(rows, args.search)
const filtered = applyFilters(searched, args.filters, args.joinOperator)
const sorted = applySorting(filtered, args.sort)
return { data: sorted, pageCount: 1 };
}
|