AuditLogs.utils.ts 2.1 KB

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