import { IProjectAssignment } from "./type.assignment"; import { IClient } from "./types"; export const STATUS_LIST = ["Not Started", "On Progress", "On Going", "Halted", "Issue", "SIT", "UAT", "PTR", "BAST", "Warranty", "End Of Support", "Canceled"] export const COMPLETE_STATUS_LIST = ['BAST', 'WARRANTY', 'END OF SUPPORT'] export const ONPROGRESS_STATUS_LIST = ['ON PROGRESS', 'ON GOING', 'HALTED', 'ISSUE', 'SIT', 'UAT', 'PTR'] export const getFilteredClients = (clientLists: IClient[], query?: string, sortBy?: string) => { let processedData = clientLists.filter((c) => c.name.toLowerCase().includes(query!) || c.code?.toLowerCase().includes(query!) || c.description?.toLowerCase().includes(query!) ); processedData = processedData.sort((a, b) => { if (sortBy === 'latest') { return new Date(b.created_at!).getTime() - new Date(a.created_at!).getTime(); } if (sortBy === 'oldest') { return new Date(a.created_at!).getTime() - new Date(b.created_at!).getTime(); } if (sortBy === 'participants_desc') { const aCount = a.users?.length || 0; const bCount = b.users?.length || 0; return bCount - aCount; // Highest count first } if (sortBy === 'participants_asc') { const aCount = a.users?.length || 0; const bCount = b.users?.length || 0; return aCount - bCount; // Lowest count first } return 0; // 'default' - keeps original array order }); return processedData; } export const getFilteredAssignments = (assignmentList: IProjectAssignment[], query?: string, sortBy?: string) => { if (assignmentList && assignmentList.length === 0) return [] // const activeStatuses = status ? status.toLowerCase()?.split(',') : []; // 1. FILTERING let processedData = assignmentList?.filter((item) => { const matchesSearch = query === '' || ( (item.client.name?.toLowerCase().includes(query!)) || (item.client.code?.toLowerCase().includes(query!)) || (item.end_user.name?.toLowerCase().includes(query!)) || (item.end_user.code?.toLowerCase().includes(query!)) || (item.im_name?.toLowerCase().includes(query)) || (item.im_code?.toLowerCase().includes(query)) || (item.im_description?.toLowerCase().includes(query)) || (item.project?.name?.toLowerCase().includes(query!)) || (item.project?.code?.toLowerCase().includes(query!)) || (item.project?.description?.toLowerCase().includes(query!)) || (item.wo_name?.toLowerCase().includes(query!)) || (item.wo_code?.toLowerCase().includes(query!)) ); // --- Status Filter --- // const itemStatus = item.project_assignment_status?.name?.toLowerCase(); // const matchesStatus = activeStatuses.length === 0 || (itemStatus && activeStatuses.includes(itemStatus)); // return matchesSearch && matchesStatus; return matchesSearch; }); // 2. SORTING processedData = processedData?.sort((a: IProjectAssignment, b: IProjectAssignment) => { if (sortBy === 'latest') { return new Date(b.created_at).getTime() - new Date(a.created_at).getTime(); } if (sortBy === 'oldest') { return new Date(a.created_at).getTime() - new Date(b.created_at).getTime(); } if (sortBy === 'participants_desc') { const aCount = a.project_assignment_work_order_detail?.length || 0; const bCount = b.project_assignment_work_order_detail?.length || 0; return bCount - aCount; // Highest count first } if (sortBy === 'participants_asc') { const aCount = a.project_assignment_work_order_detail?.length || 0; const bCount = b.project_assignment_work_order_detail?.length || 0; return aCount - bCount; // Lowest count first } return 0; // 'default' - keeps original array order }); return processedData; }