AuditLogs.utils.ts52 lines · main
| 1 | import dayjs from 'dayjs' |
| 2 | |
| 3 | import { DatePickerToFrom } from '@/components/interfaces/Settings/Logs/Logs.types' |
| 4 | import type { AuditLog } from '@/data/organizations/organization-audit-logs-query' |
| 5 | |
| 6 | export function sortAuditLogs(logs: AuditLog[], descending: boolean): AuditLog[] { |
| 7 | return [...logs].sort((a, b) => |
| 8 | descending ? b.timestamp - a.timestamp : a.timestamp - b.timestamp |
| 9 | ) |
| 10 | } |
| 11 | |
| 12 | export function filterByUsers(logs: AuditLog[], userIds: string[]): AuditLog[] { |
| 13 | if (userIds.length === 0) return logs |
| 14 | return logs.filter((log) => userIds.includes(log.actor.user_id ?? '')) |
| 15 | } |
| 16 | |
| 17 | export function filterByProjects(logs: AuditLog[], projectRefs: string[]): AuditLog[] { |
| 18 | if (projectRefs.length === 0) return logs |
| 19 | return logs.filter((log) => projectRefs.includes(log.project_ref ?? '')) |
| 20 | } |
| 21 | |
| 22 | // [Joshen] Mainly to handle if a single date is selected - currently just for Audit Logs as |
| 23 | // i'm on the fence if this logic should be within the DatePicker component itself |
| 24 | // e.g for Logs.DatePicker which uses this component, the component itself has its own time selection UI |
| 25 | // JFYI currentDate is just a parameter so that I can run tests for this |
| 26 | |
| 27 | export const formatSelectedDateRange = (value: DatePickerToFrom) => { |
| 28 | const current = dayjs() |
| 29 | const from = dayjs(value.from) |
| 30 | .hour(current.hour()) |
| 31 | .minute(current.minute()) |
| 32 | .second(current.second()) |
| 33 | const to = dayjs(value.to).hour(current.hour()).minute(current.minute()).second(current.second()) |
| 34 | |
| 35 | if (from.date() === to.date()) { |
| 36 | // [Joshen] If a single date is selected, we either set the "from" to start from 00:00 |
| 37 | // or "to" to end at 23:59 depending on which date was selected |
| 38 | if (from.date() === current.date()) { |
| 39 | return { |
| 40 | from: from.set('hour', 0).set('minute', 0).set('second', 0).utc().toISOString(), |
| 41 | to: to.utc().toISOString(), |
| 42 | } |
| 43 | } else { |
| 44 | return { |
| 45 | from: from.utc().toISOString(), |
| 46 | to: to.set('hour', 23).set('minute', 59).set('second', 59).utc().toISOString(), |
| 47 | } |
| 48 | } |
| 49 | } else { |
| 50 | return { from: from.utc().toISOString(), to: to.utc().toISOString() } |
| 51 | } |
| 52 | } |