feat(notifications): Phase C — Don't Disturb (quiet hours window)

User задаёт quiet window (e.g. 22:00–08:00 Europe/Moscow) — external
channels (email + express) DROP'аются dispatcher'ом. In-app bell badge
остаётся (юзер видит при следующем визите UI). Cross-midnight aware.

## Backend

- Migration 0026: user_notification_settings table (user_id PK + quiet_hours_start/end LocalTime + tz)
- Entity UserNotificationSettings + Repository (stock CRUD)
- UserPreferencesGate extended:
  - Constructor takes settingsRepo
  - allows() checks isQuiet(userId) первым for external channels (EMAIL/EXPRESS/TELEGRAM)
  - isQuiet: load settings → if hasEffectiveQuietHours() → check current LocalTime в user's TZ
  - inWindow() helper: cross-midnight semantics (22:00–08:00 wraps midnight)
  - Invalid TZ → fallback к DEFAULT_TZ (Europe/Moscow) с WARN log
  - Test seam: setClockForTesting(Clock) для deterministic tests
- MeNotificationsSettingsController: GET/PUT /api/v1/me/notifications/settings
  - HH:mm input parsing, IANA TZ validation, enabled=false clears window
  - Auth required (JWT sub claim)

## Frontend

- NotificationSettings type в client.ts
- useMyNotificationSettings query (60s stale)
- useUpdateNotificationSettings mutation
- QuietHoursSection в /me/notifications/preferences route:
  - Switch toggle + 2 time inputs + TZ select (12 RU/UTC zones)
  - Optimistic local state synced from query
  - Save button с pending/success/error states
- i18n RU + EN

## Tests

- 9 new tests UserPreferencesGateQuietHoursTest:
  - Same-day window match (inclusive start, exclusive end)
  - Cross-midnight window match (22:00–08:00)
  - No settings row → не блокирует
  - Quiet window active → email + express dropped
  - Outside window → email allowed
  - start == end → disabled (zero-duration treated as off)
  - Reviewer pool bypasses (always ON)
  - Invalid TZ → fallback default
  - Specific pref ON + quiet ON → quiet wins (drop)
- NotificationsDispatcherTest constructor updated к new signature
- Total notifications tests: 53 → 62, все green

## Design rationale (drop vs defer)

Drop chosen за defer (queue + re-fire scheduler) потому что:
- Defer needs persistent queue + scheduled retry — high complexity
- Most notifications time-relevant (draft N hours ago less actionable)
- Bell badge показывает full feed — ничего не «теряется»
- Phase D summary digest можно build later если demand surfaces
This commit is contained in:
Zimin A.N.
2026-05-17 11:11:21 +03:00
parent 6ed0b459de
commit 74704dbf5b
13 changed files with 804 additions and 4 deletions
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<!-- Phase C: Don't Disturb (time-of-day quiet hours).
User может задать window (e.g. 22:0008:00 Europe/Moscow) в течение
которой emails + express пуши DROP'аются. In-app bell badge остаётся
(user видит при следующем визите UI). Telegram TBD.
Why drop, not defer:
- Defer requires queue + re-fire scheduler — complex
- Most notifications are time-relevant (draft submitted N hours ago
less actionable, чем «just now»)
- User видит full feed через bell badge — ничего не теряется
- Phase D можно build summary digest если demand surfaces
Per-user single window (одна quiet zone per user). Cross-midnight OK
(e.g. 22:0008:00 = quiet когда now is 22:0023:59 OR 00:0008:00).
New table вместо extending user_notification_prefs — schemas разные:
prefs = per (user, event, channel), settings = per user global.
Splitting cleaner чем denormalising. -->
<changeSet id="0026-1-notification-quiet-hours" author="ordinis">
<comment>Per-user quiet hours window + timezone — Phase C Don't Disturb</comment>
<createTable tableName="user_notification_settings">
<column name="user_id" type="VARCHAR(128)">
<constraints primaryKey="true" nullable="false"/>
</column>
<!-- Quiet hours: NULL означает не задано (always deliver).
Format LOCAL TIME (HH:mm:ss) — interpreted в указанной TZ.
Если start == end → disabled (treat as no quiet hours). -->
<column name="quiet_hours_start" type="TIME"/>
<column name="quiet_hours_end" type="TIME"/>
<!-- IANA timezone, e.g. 'Europe/Moscow'. NULL → server default 'Europe/Moscow'. -->
<column name="quiet_hours_tz" type="VARCHAR(64)"/>
<column name="created_at" type="TIMESTAMPTZ" defaultValueComputed="NOW()">
<constraints nullable="false"/>
</column>
<column name="updated_at" type="TIMESTAMPTZ" defaultValueComputed="NOW()">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
</databaseChangeLog>
@@ -35,5 +35,6 @@
<include file="changes/0023-user-display-cache.xml" relativeToChangelogFile="true"/>
<include file="changes/0024-notification-read-state.xml" relativeToChangelogFile="true"/>
<include file="changes/0025-notification-prefs-event-type.xml" relativeToChangelogFile="true"/>
<include file="changes/0026-notification-quiet-hours.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>