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) => { if (!clientLists || !Array.isArray(clientLists) || clientLists.length === 0) return []; const q = (query || '').toLowerCase().trim(); let processedData = clientLists.filter((c) => { if (!q) return true; const matchesClientName = c.name?.toLowerCase().includes(q); const matchesClientCode = c.code?.toLowerCase().includes(q); const matchesClientDesc = typeof c.description === 'string' && c.description.toLowerCase().includes(q); const matchesEmployee = c.users?.some((u) => u?.fullname?.toLowerCase().includes(q) || u?.nickname?.toLowerCase().includes(q) || u?.name?.toLowerCase().includes(q) ); return Boolean(matchesClientName || matchesClientCode || matchesClientDesc || matchesEmployee); }); // If searching by employee, prioritize matching users to the front of avatars if (q) { processedData = processedData.map((c) => { if (!c.users || c.users.length <= 1) return c; const hasMatchingUser = c.users.some((u) => u?.fullname?.toLowerCase().includes(q) || u?.nickname?.toLowerCase().includes(q) || u?.name?.toLowerCase().includes(q) ); if (!hasMatchingUser) return c; const sortedUsers = [...c.users].sort((a, b) => { const aMatch = (a?.fullname?.toLowerCase().includes(q) || a?.nickname?.toLowerCase().includes(q) || a?.name?.toLowerCase().includes(q)) ? 1 : 0; const bMatch = (b?.fullname?.toLowerCase().includes(q) || b?.nickname?.toLowerCase().includes(q) || b?.name?.toLowerCase().includes(q)) ? 1 : 0; return bMatch - aMatch; }); return { ...c, users: sortedUsers }; }); } 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 } // 'default', 'alphabet', or fallback: sort alphabetically by client name (A-Z) return (a.name || '').trim().localeCompare((b.name || '').trim(), undefined, { sensitivity: 'base', numeric: true }); }); return processedData; } export const getFilteredAssignments = (assignmentList: IProjectAssignment[], query?: string, sortBy?: string) => { if (assignmentList && assignmentList.length === 0) return [] const q = (query || '').toLowerCase().trim(); // 1. FILTERING let processedData = assignmentList?.filter((item) => { if (!q) return true; const matchesSearch = ( (item.client?.name?.toLowerCase().includes(q)) || (item.client?.code?.toLowerCase().includes(q)) || (item.end_user?.name?.toLowerCase().includes(q)) || (item.end_user?.code?.toLowerCase().includes(q)) || (item.im_name?.toLowerCase().includes(q)) || (item.im_code?.toLowerCase().includes(q)) || (item.im_description?.toLowerCase().includes(q)) || (item.project?.name?.toLowerCase().includes(q)) || (item.project?.code?.toLowerCase().includes(q)) || (item.project?.description?.toLowerCase().includes(q)) || (item.wo_name?.toLowerCase().includes(q)) || (item.wo_code?.toLowerCase().includes(q)) || (item.project_assignment_work_order_detail?.some((wo) => wo.user?.fullname?.toLowerCase().includes(q) || wo.user?.nickname?.toLowerCase().includes(q) || wo.user?.name?.toLowerCase().includes(q) )) ); // --- Status Filter --- // const itemStatus = item.project_assignment_status?.name?.toLowerCase(); // const matchesStatus = activeStatuses.length === 0 || (itemStatus && activeStatuses.includes(itemStatus)); // return matchesSearch && matchesStatus; return Boolean(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 } if (sortBy === 'alphabet') { const nameA = a.project?.name || a.wo_name || a.im_name || a.client?.name || ''; const nameB = b.project?.name || b.wo_name || b.im_name || b.client?.name || ''; return nameA.trim().localeCompare(nameB.trim(), undefined, { sensitivity: 'base', numeric: true }); } return 0; // 'default' - keeps original array order }); return processedData; }