feat(capa): add pure list helpers for filtering, saved views, sorting and paging
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,111 @@
|
|||||||
|
export type CapaStatus =
|
||||||
|
| 'open' | 'in_progress' | 'overdue' | 'pending_verification' | 'verified' | 'reopened' | 'closed'
|
||||||
|
export type CapaPriority = 'low' | 'med' | 'high'
|
||||||
|
|
||||||
|
export interface CapaRow {
|
||||||
|
id: string
|
||||||
|
incident_id: string
|
||||||
|
description: string
|
||||||
|
department: string
|
||||||
|
due_date: string
|
||||||
|
priority: CapaPriority
|
||||||
|
status: CapaStatus
|
||||||
|
incident_ref: string | null
|
||||||
|
owner_name: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
export const PAGE_SIZE = 25
|
||||||
|
|
||||||
|
export const STATUS_ORDER: CapaStatus[] = [
|
||||||
|
'open', 'in_progress', 'overdue', 'reopened', 'pending_verification', 'verified', 'closed',
|
||||||
|
]
|
||||||
|
|
||||||
|
export type CapaViewKey = 'all' | 'active' | 'overdue' | 'verification' | 'done'
|
||||||
|
|
||||||
|
export const VIEWS: Array<{ key: CapaViewKey; label: string; statuses: CapaStatus[] | null }> = [
|
||||||
|
{ key: 'all', label: 'All', statuses: null },
|
||||||
|
{ key: 'active', label: 'Active', statuses: ['open', 'in_progress', 'reopened'] },
|
||||||
|
{ key: 'overdue', label: 'Overdue', statuses: ['overdue'] },
|
||||||
|
{ key: 'verification', label: 'Verification', statuses: ['pending_verification'] },
|
||||||
|
{ key: 'done', label: 'Done', statuses: ['verified', 'closed'] },
|
||||||
|
]
|
||||||
|
|
||||||
|
export interface CapaFilters {
|
||||||
|
q: string
|
||||||
|
owner: string
|
||||||
|
department: string
|
||||||
|
priority: string
|
||||||
|
view: CapaViewKey
|
||||||
|
}
|
||||||
|
|
||||||
|
export const EMPTY_FILTERS: CapaFilters = { q: '', owner: '', department: '', priority: '', view: 'all' }
|
||||||
|
|
||||||
|
export type CapaSortKey = 'due_date' | 'priority' | 'status'
|
||||||
|
export type CapaSort = { key: CapaSortKey; dir: 'asc' | 'desc' }
|
||||||
|
|
||||||
|
function matchesOthers(r: CapaRow, f: CapaFilters): boolean {
|
||||||
|
if (f.owner && r.owner_name !== f.owner) return false
|
||||||
|
if (f.department && r.department !== f.department) return false
|
||||||
|
if (f.priority && r.priority !== f.priority) return false
|
||||||
|
if (f.q) {
|
||||||
|
const q = f.q.toLowerCase()
|
||||||
|
const hay = `${r.description} ${r.incident_ref ?? ''} ${r.owner_name ?? ''}`.toLowerCase()
|
||||||
|
if (!hay.includes(q)) return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
function inView(r: CapaRow, view: CapaViewKey): boolean {
|
||||||
|
const statuses = VIEWS.find(v => v.key === view)?.statuses ?? null
|
||||||
|
return statuses === null || statuses.includes(r.status)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function filterCapas(rows: CapaRow[], f: CapaFilters): CapaRow[] {
|
||||||
|
return rows.filter(r => matchesOthers(r, f) && inView(r, f.view))
|
||||||
|
}
|
||||||
|
|
||||||
|
export function viewCounts(rows: CapaRow[], f: CapaFilters): Record<CapaViewKey, number> {
|
||||||
|
const scoped = rows.filter(r => matchesOthers(r, f))
|
||||||
|
const counts = {} as Record<CapaViewKey, number>
|
||||||
|
for (const v of VIEWS) counts[v.key] = scoped.filter(r => inView(r, v.key)).length
|
||||||
|
return counts
|
||||||
|
}
|
||||||
|
|
||||||
|
const PRIORITY_RANK: Record<CapaPriority, number> = { high: 0, med: 1, low: 2 }
|
||||||
|
|
||||||
|
export function sortCapas(rows: CapaRow[], sort: CapaSort): CapaRow[] {
|
||||||
|
const sign = sort.dir === 'asc' ? 1 : -1
|
||||||
|
const key = (r: CapaRow): number | string =>
|
||||||
|
sort.key === 'priority' ? PRIORITY_RANK[r.priority]
|
||||||
|
: sort.key === 'status' ? STATUS_ORDER.indexOf(r.status)
|
||||||
|
: r.due_date
|
||||||
|
return [...rows].sort((a, b) => {
|
||||||
|
const ka = key(a)
|
||||||
|
const kb = key(b)
|
||||||
|
return ka < kb ? -sign : ka > kb ? sign : 0
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export function nextSort(current: CapaSort, key: CapaSortKey): CapaSort {
|
||||||
|
if (current.key === key) return { key, dir: current.dir === 'asc' ? 'desc' : 'asc' }
|
||||||
|
return { key, dir: 'asc' }
|
||||||
|
}
|
||||||
|
|
||||||
|
export function paginate<T>(rows: T[], page: number, size: number) {
|
||||||
|
const total = rows.length
|
||||||
|
const pageCount = Math.max(1, Math.ceil(total / size))
|
||||||
|
const p = Math.min(Math.max(1, page), pageCount)
|
||||||
|
const items = rows.slice((p - 1) * size, p * size)
|
||||||
|
const from = items.length === 0 ? 0 : (p - 1) * size + 1
|
||||||
|
const to = items.length === 0 ? 0 : from + items.length - 1
|
||||||
|
return { items, from, to, total, page: p, pageCount }
|
||||||
|
}
|
||||||
|
|
||||||
|
export function distinct(values: Array<string | null | undefined>): string[] {
|
||||||
|
return [...new Set(values.filter((v): v is string => Boolean(v)))].sort()
|
||||||
|
}
|
||||||
|
|
||||||
|
export function daysLate(due: string, today: string): number {
|
||||||
|
const diff = Math.round((Date.parse(today) - Date.parse(due)) / 86_400_000)
|
||||||
|
return Math.max(0, diff)
|
||||||
|
}
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
import { describe, it, expect } from 'vitest'
|
||||||
|
import {
|
||||||
|
EMPTY_FILTERS, PAGE_SIZE, STATUS_ORDER, VIEWS,
|
||||||
|
daysLate, distinct, filterCapas, nextSort, paginate, sortCapas, viewCounts,
|
||||||
|
type CapaRow,
|
||||||
|
} from '@/lib/capa/list'
|
||||||
|
|
||||||
|
const row = (over: Partial<CapaRow>): CapaRow => ({
|
||||||
|
id: 'x', incident_id: 'i', description: 'Fit guard rail', department: 'Facilities', due_date: '2026-10-10',
|
||||||
|
priority: 'med', status: 'open', incident_ref: 'SC1-202607-0001', owner_name: 'Tan Mei Ling', ...over,
|
||||||
|
})
|
||||||
|
|
||||||
|
const rows: CapaRow[] = [
|
||||||
|
row({ id: '1', description: 'Anti-slip coating', due_date: '2026-09-12', priority: 'high', status: 'overdue', owner_name: 'Tan Mei Ling', department: 'Facilities' }),
|
||||||
|
row({ id: '2', description: 'Forklift refresher', due_date: '2026-10-20', priority: 'low', status: 'in_progress', owner_name: 'Ravi Kumar', department: 'Operations', incident_ref: 'SC3-202607-0002' }),
|
||||||
|
row({ id: '3', description: 'Guard rail repair', due_date: '2026-10-01', priority: 'med', status: 'pending_verification', owner_name: 'Tan Mei Ling', department: 'Facilities' }),
|
||||||
|
row({ id: '4', description: 'Signage', due_date: '2026-08-30', priority: 'low', status: 'closed', owner_name: null, department: 'Operations' }),
|
||||||
|
]
|
||||||
|
|
||||||
|
describe('constants', () => {
|
||||||
|
it('orders statuses for the board and defines the saved views', () => {
|
||||||
|
expect(STATUS_ORDER).toEqual(['open', 'in_progress', 'overdue', 'reopened', 'pending_verification', 'verified', 'closed'])
|
||||||
|
expect(VIEWS.map(v => v.key)).toEqual(['all', 'active', 'overdue', 'verification', 'done'])
|
||||||
|
expect(PAGE_SIZE).toBe(25)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('filterCapas', () => {
|
||||||
|
it('returns everything for the empty filters', () => {
|
||||||
|
expect(filterCapas(rows, EMPTY_FILTERS)).toHaveLength(4)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('searches description, incident reference and owner, case-insensitively', () => {
|
||||||
|
expect(filterCapas(rows, { ...EMPTY_FILTERS, q: 'GUARD' }).map(r => r.id)).toEqual(['3'])
|
||||||
|
expect(filterCapas(rows, { ...EMPTY_FILTERS, q: 'sc3-' }).map(r => r.id)).toEqual(['2'])
|
||||||
|
expect(filterCapas(rows, { ...EMPTY_FILTERS, q: 'ravi' }).map(r => r.id)).toEqual(['2'])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('filters by owner, department and priority', () => {
|
||||||
|
expect(filterCapas(rows, { ...EMPTY_FILTERS, owner: 'Tan Mei Ling' }).map(r => r.id)).toEqual(['1', '3'])
|
||||||
|
expect(filterCapas(rows, { ...EMPTY_FILTERS, department: 'Operations' }).map(r => r.id)).toEqual(['2', '4'])
|
||||||
|
expect(filterCapas(rows, { ...EMPTY_FILTERS, priority: 'low' }).map(r => r.id)).toEqual(['2', '4'])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('filters by saved view', () => {
|
||||||
|
expect(filterCapas(rows, { ...EMPTY_FILTERS, view: 'overdue' }).map(r => r.id)).toEqual(['1'])
|
||||||
|
expect(filterCapas(rows, { ...EMPTY_FILTERS, view: 'active' }).map(r => r.id)).toEqual(['2'])
|
||||||
|
expect(filterCapas(rows, { ...EMPTY_FILTERS, view: 'verification' }).map(r => r.id)).toEqual(['3'])
|
||||||
|
expect(filterCapas(rows, { ...EMPTY_FILTERS, view: 'done' }).map(r => r.id)).toEqual(['4'])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('combines filters', () => {
|
||||||
|
expect(filterCapas(rows, { ...EMPTY_FILTERS, owner: 'Tan Mei Ling', view: 'overdue' }).map(r => r.id)).toEqual(['1'])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('viewCounts', () => {
|
||||||
|
it('counts each view with the other filters applied', () => {
|
||||||
|
expect(viewCounts(rows, EMPTY_FILTERS)).toEqual({ all: 4, active: 1, overdue: 1, verification: 1, done: 1 })
|
||||||
|
expect(viewCounts(rows, { ...EMPTY_FILTERS, owner: 'Tan Mei Ling', view: 'done' })).toEqual({ all: 2, active: 0, overdue: 1, verification: 1, done: 0 })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('sortCapas', () => {
|
||||||
|
it('sorts by due date both ways without mutating the input', () => {
|
||||||
|
const copy = [...rows]
|
||||||
|
expect(sortCapas(rows, { key: 'due_date', dir: 'asc' }).map(r => r.id)).toEqual(['4', '1', '3', '2'])
|
||||||
|
expect(sortCapas(rows, { key: 'due_date', dir: 'desc' }).map(r => r.id)).toEqual(['2', '3', '1', '4'])
|
||||||
|
expect(rows).toEqual(copy)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('sorts priority high first and status in board order', () => {
|
||||||
|
expect(sortCapas(rows, { key: 'priority', dir: 'asc' }).map(r => r.id)).toEqual(['1', '3', '2', '4'])
|
||||||
|
expect(sortCapas(rows, { key: 'status', dir: 'asc' }).map(r => r.id)).toEqual(['2', '1', '3', '4'])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('nextSort', () => {
|
||||||
|
it('flips the direction on the same column and starts a new one ascending', () => {
|
||||||
|
expect(nextSort({ key: 'due_date', dir: 'asc' }, 'due_date')).toEqual({ key: 'due_date', dir: 'desc' })
|
||||||
|
expect(nextSort({ key: 'due_date', dir: 'desc' }, 'priority')).toEqual({ key: 'priority', dir: 'asc' })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('paginate', () => {
|
||||||
|
const many = Array.from({ length: 27 }, (_, i) => i + 1)
|
||||||
|
|
||||||
|
it('slices a page and reports the range', () => {
|
||||||
|
expect(paginate(many, 1, 25)).toMatchObject({ from: 1, to: 25, total: 27, page: 1, pageCount: 2 })
|
||||||
|
expect(paginate(many, 2, 25)).toMatchObject({ from: 26, to: 27, items: [26, 27], page: 2 })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('clamps an out-of-range page and handles empty input', () => {
|
||||||
|
expect(paginate(many, 9, 25).page).toBe(2)
|
||||||
|
expect(paginate(many, 0, 25).page).toBe(1)
|
||||||
|
expect(paginate([], 1, 25)).toMatchObject({ from: 0, to: 0, total: 0, page: 1, pageCount: 1, items: [] })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('distinct and daysLate', () => {
|
||||||
|
it('returns sorted unique non-empty values', () => {
|
||||||
|
expect(distinct(['b', 'a', 'b', null, undefined, ''])).toEqual(['a', 'b'])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('counts whole days past the due date, never negative', () => {
|
||||||
|
expect(daysLate('2026-09-12', '2026-09-27')).toBe(15)
|
||||||
|
expect(daysLate('2026-10-10', '2026-09-27')).toBe(0)
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user