Repository navigation
feat(audit-trail): cross-collection timeline route [PRD-1257] #1911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bexchauveto
merged 7 commits into
main
from
feature/prd-1257-agent-nodejs-cross-collection-audit-trail-route-for-the
Oct 9, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5c5b3dc
feat(audit-trail): withhold project-level audit values from non-admins
bexchauveto a9baee6
feat(audit-trail): cross-collection timeline route for the project-le…
bexchauveto 5fa8ffc
fix(audit-trail): end the timeline walk when the cursor cannot move p…
bexchauveto 9f76388
refactor(audit-trail): qualify the permission scope in the timeline r…
bexchauveto 79333bb
docs(audit-trail): the timeline route has no target collection to gat…
bexchauveto e7b7c0f
perf(audit-trail): index (timestamp, id) for the cross-collection tim…
bexchauveto 6c72f5a
feat(audit-trail): serve the timeline's values to every reader, annou…
bexchauveto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import type { AuditOperation } from './types'; | ||
|
|
||
| import { ValidationError } from '@forestadmin/datasource-toolkit'; | ||
| import { DateTime } from 'luxon'; | ||
|
|
||
| const DATE_ONLY = /^\d{4}-\d{2}-\d{2}$/; | ||
| const DATE_TIME = /^(\d{4}-\d{2}-\d{2})[T ](\d{2}):(\d{2})(?::(\d{2}))?$/; | ||
| // ISO 8601 instant: carries its own timezone designator (`Z` or `±HH:mm` / `±HHMM`). | ||
| const ISO_INSTANT = /[Zz]$|[+-]\d{2}:?\d{2}$/; | ||
|
|
||
| export const DEFAULT_PAGE_SIZE = 20; | ||
| export const MAX_PAGE_SIZE = 100; | ||
|
|
||
| export const AUDIT_OPERATIONS: readonly AuditOperation[] = [ | ||
| 'create', | ||
| 'update', | ||
| 'delete', | ||
| 'action', | ||
| 'action_failed', | ||
| ]; | ||
|
|
||
| // Comma-separated integer ids; non-numeric tokens are dropped. Empty after parsing → no filter. | ||
| export function parseUserIds(raw?: string): number[] | undefined { | ||
| if (!raw) return undefined; | ||
|
|
||
| const ids = raw | ||
| .split(',') | ||
| .map(token => token.trim()) | ||
| .filter(token => /^\d+$/.test(token)) | ||
| .map(token => Number.parseInt(token, 10)); | ||
|
|
||
| return ids.length > 0 ? ids : undefined; | ||
| } | ||
|
|
||
| // Comma-separated, from a closed set. An unrecognized value is rejected rather than dropped: a | ||
| // silently ignored filter returns unfiltered rows into a list the caller believes is filtered, | ||
| // which is worse than an error. | ||
| export function parseOperations(raw?: string): AuditOperation[] | undefined { | ||
| if (!raw) return undefined; | ||
|
|
||
| const tokens = raw | ||
| .split(',') | ||
| .map(token => token.trim()) | ||
| .filter(token => token.length > 0); | ||
|
|
||
| const unknown = tokens.find(token => !AUDIT_OPERATIONS.includes(token as AuditOperation)); | ||
|
|
||
| if (unknown) { | ||
| throw new ValidationError( | ||
| `Invalid operation: "${unknown}" (expected one of ${AUDIT_OPERATIONS.join(', ')})`, | ||
| ); | ||
| } | ||
|
|
||
| return tokens.length > 0 ? (tokens as AuditOperation[]) : undefined; | ||
| } | ||
|
|
||
| export function parseFields(raw?: string): string[] | undefined { | ||
| if (!raw) return undefined; | ||
|
|
||
| const fields = raw | ||
| .split(',') | ||
| .map(token => token.trim()) | ||
| .filter(token => token.length > 0); | ||
|
|
||
| return fields.length > 0 ? fields : undefined; | ||
| } | ||
|
|
||
| // Trimmed; empty after trimming is treated the same as absent. | ||
| export function parseSearch(raw?: string): string | undefined { | ||
| const trimmed = raw?.trim(); | ||
|
|
||
| return trimmed || undefined; | ||
| } | ||
|
|
||
| // 1-based `page[size]` (default 20, capped at 100). Invalid values fall back to the default rather | ||
| // than erroring. | ||
| export function parsePageSize(raw?: string): number { | ||
| const size = Number.parseInt(raw ?? '', 10); | ||
|
|
||
| if (Number.isNaN(size) || size < 1) return DEFAULT_PAGE_SIZE; | ||
|
|
||
| return Math.min(size, MAX_PAGE_SIZE); | ||
| } | ||
|
|
||
| function toLocalInstant(raw: string, timezone: string, boundary: 'start' | 'end'): DateTime { | ||
| // An embedded offset already pins the instant — the request timezone and start/end boundary | ||
| // don't apply. | ||
| if (ISO_INSTANT.test(raw)) return DateTime.fromISO(raw, { setZone: true }); | ||
|
|
||
| if (DATE_ONLY.test(raw)) { | ||
| const day = DateTime.fromISO(raw, { zone: timezone }); | ||
|
|
||
| return boundary === 'end' ? day.endOf('day') : day.startOf('day'); | ||
| } | ||
|
|
||
| const match = DATE_TIME.exec(raw); | ||
|
|
||
| if (!match) return DateTime.invalid('unparsable'); | ||
|
|
||
| const [, date, hours, minutes, seconds] = match; | ||
| const base = DateTime.fromISO(`${date}T${hours}:${minutes}`, { zone: timezone }); | ||
|
|
||
| if (seconds !== undefined) return base.set({ second: Number(seconds), millisecond: 0 }); | ||
|
|
||
| // Minutes-only: end snaps to :59.999 to stay inclusive; start stays at :00.000. | ||
| return boundary === 'end' ? base.set({ second: 59, millisecond: 999 }) : base; | ||
| } | ||
|
|
||
| // Bare day (`YYYY-MM-DD`) or wall-clock datetime (`YYYY-MM-DD[T| ]HH:mm[:ss]`), interpreted as | ||
| // local time in the request timezone and returned as a UTC instant so the store can compare it | ||
| // to stored timestamps. | ||
| export function parseDateBoundary( | ||
| raw: string | undefined, | ||
| timezone: string, | ||
| boundary: 'start' | 'end', | ||
| ): string | undefined { | ||
| if (!raw) return undefined; | ||
|
|
||
| const instant = toLocalInstant(raw, timezone, boundary); | ||
|
|
||
| if (!instant.isValid) { | ||
| throw new ValidationError( | ||
| instant.invalidReason === 'unsupported zone' | ||
| ? `Invalid timezone: "${timezone}"` | ||
| : `Invalid date: "${raw}" (expected YYYY-MM-DD, YYYY-MM-DDTHH:mm, or an ISO 8601 instant)`, | ||
| ); | ||
| } | ||
|
|
||
| return instant.toUTC().toISO() ?? undefined; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.