Skip to content

feat: show sent alerts from the alerts endpoint (TRA-1298) - #792

Merged
NicholusMuwonge merged 2 commits into
mainfrom
feat/tra-1298-alerts-hook-endpoint
Nov 21, 2026
Merged

NicholusMuwonge merged 2 commits into
mainfrom
feat/tra-1298-alerts-hook-endpoint

Conversation

@edmacabebe

@edmacabebe edmacabebe commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Requested by edmacabebe · project thread

Switches the Notifications screens from mock alerts to GET /api/v1/notification-alerts (#748), and makes the alert id readable.

Hook switch

  • useNotificationAlerts now calls notificationsApi.listAlerts through TanStack Query, keyed by org and query. It keeps the previous page while the next one loads, but never across an org switch. It returns the server's page and pagination.total.
  • Unset filters are left out of the query, because the server rejects a present-but-empty parameter.
  • A bare YYYY-MM-DD from the date filters becomes the start (00:00:00.000) or end (23:59:59.999999) of that local day, written with that day's own offset, since the server can't know the viewer's zone. The end keeps microseconds because the server stores them and treats to as inclusive.
  • src/lib/notifications/mockAlerts.ts is removed: after the switch nothing used it.
  • The alarm details no longer documents direction, which the endpoint never sends.

Alert id

id changes from a number to a string with meaning:

alert-20261117T004422Z-alarm-2814749767106561
      sent time (UTC)  source  delivery public id

The time and source alone are not unique: one event fans out to several recipients and channels in the same second. The suffix is the delivery's non-sequential public id from migration 000057, so it never exposes the shared sequence. No migration in this PR.

Tests

  • Backend: FormatID unit test; the storage and handler integration tests pass and assert the new id.
  • Frontend: hook tests (page and total, error path, previous page kept within an org but not across a switch, filter omission, local-day bounds checked under America/New_York on both 2026 DST days), API client test for listAlerts, fixtures updated to string ids. pnpm typecheck and pnpm lint clean.

Refs: TRA-1298

https://claude.ai/code/session_01QrZ7zb1j4kqUQoTC3WT6qT


Generated by Claude Code

useNotificationAlerts now reads GET /notification-alerts instead of mock
data. Unset filters are left out, and a bare YYYY-MM-DD from the date
filters becomes the start or end of that local day, since the server
takes only RFC 3339 instants. The mock alerts module is removed, as
nothing else used it.

An alert's id is now a readable string: "alert-", the sent time in UTC
to the second, the event source, and the delivery's public id, e.g.
"alert-20261117T004422Z-alarm-2814749767106561". The public id keeps it
unique, because one event fans out to several deliveries in the same
second.

Refs: TRA-1298

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QrZ7zb1j4kqUQoTC3WT6qT
@edmacabebe edmacabebe self-assigned this Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

@NicholusMuwonge, requesting your review because this is a small change under the team rule that Mike takes DB or significant changes: it switches the alerts hook your Notifications screens use from mock data to the merged endpoint (#748), and changes the alert id to a readable string. There is no migration or DB change.


Generated by Claude Code

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Preview Deployment Update

✅ This PR has been successfully merged into the preview branch.

The preview environment will update shortly at: https://app.preview.trakrf.id

@NicholusMuwonge NicholusMuwonge left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, and it supersedes #793, which I've closed. I found two things in useNotificationAlerts.ts while reviewing the same change on my side:

1. The previous org's alerts show after an org switch. placeholderData: keepPreviousData keeps the last result across any key change, including currentOrg.id. So after switching org, org A's alerts stay on screen until org B's request comes back. Keep the previous page only when the org is unchanged:

placeholderData: (prev, prevQuery) =>
  prevQuery?.queryKey[1] === currentOrg?.id ? prev : undefined,

A test: render with org 7 and resolve page 1, switch the store to org 8 with the request still pending, then expect alerts to be [] and isLoading to be true.

2. The end-of-day cutoff stops a millisecond early. The backend stores microseconds and treats to as inclusive, adding 1µs. 23:59:59.999 therefore misses anything sent between .999 and .999999 on the "to" day. toISOString() can't go finer than milliseconds, so the bound needs the local offset written out:

const pad = (n: number, width = 2) => String(n).padStart(width, '0');

export function localDayBound(date: string, edge: 'start' | 'end'): string {
  const [y, m, d] = date.split('-').map(Number);
  const dt = edge === 'start' ? new Date(y, m - 1, d, 0, 0, 0, 0) : new Date(y, m - 1, d, 23, 59, 59, 999);
  const offset = -dt.getTimezoneOffset();
  const sign = offset < 0 ? '-' : '+';
  const abs = Math.abs(offset);
  const frac = edge === 'start' ? '000' : '999999';
  return (
    `${pad(dt.getFullYear(), 4)}-${pad(dt.getMonth() + 1)}-${pad(dt.getDate())}` +
    `T${pad(dt.getHours())}:${pad(dt.getMinutes())}:${pad(dt.getSeconds())}.${frac}` +
    `${sign}${pad(Math.floor(abs / 60))}:${pad(abs % 60)}`
  );
}

The offset comes from that day, so DST days come out right. Pin process.env.TZ = 'America/New_York' in the test and check 2026-03-08 and 2026-11-01. Under the default UTC zone a DST test passes without proving anything.

The full versions, with tests, are on the closed #793 branch (feat/tra-1366-real-notification-alerts), in useNotificationAlerts.ts and its test.

Review fixes for the alerts hook:

- The previous page is kept while the next loads only when the org is
  unchanged, so another org's alerts no longer show after an org switch.
- A bare "to" date now ends at 23:59:59.999999 with that day's own
  offset. The server stores microseconds and treats "to" as inclusive,
  and toISOString() stopped at .999, missing the day's last 999 µs.
  Tests pin America/New_York to check both DST days.

Also restores the repo's single-quote formatting that an unconfigured
Prettier run had rewritten in the previous commit.

Refs: TRA-1298

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QrZ7zb1j4kqUQoTC3WT6qT

edmacabebe commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor Author

@NicholusMuwonge thanks, both were real, and both are fixed in 79a3441:

  1. placeholderData now keeps the previous page only when queryKey[1] matches the current org. I added your org-switch test and checked it fails with keepPreviousData.
  2. A bare to date now ends at 23:59:59.999999 with that day's own offset. The tests pin America/New_York and check both 2026-03-08 and 2026-11-01, start and end.

I kept the existing toInstant/alertQuery names rather than renaming to localDayBound/toAlertQuery. That push also undoes a quote-style reformat my previous commit made by accident, so the diff against main is smaller now.


Generated by Claude Code

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Preview Deployment Update

✅ This PR has been successfully merged into the preview branch.

The preview environment will update shortly at: https://app.preview.trakrf.id

@NicholusMuwonge

Copy link
Copy Markdown
Contributor

Thanks @edmacabebe , merging now.

@NicholusMuwonge
NicholusMuwonge merged commit a3ed1d8 into main Nov 21, 2026
17 checks passed
@NicholusMuwonge
NicholusMuwonge deleted the feat/tra-1298-alerts-hook-endpoint branch November 21, 2026 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants