feat(bot): add observable notification management

This commit is contained in:
2026-03-24 03:58:00 +04:00
parent 7e9ae75a41
commit 83ffd7df72
18 changed files with 1267 additions and 58 deletions

View File

@@ -63,6 +63,7 @@ describe('createAdHocNotificationJobsHandler', () => {
},
listUpcomingNotifications: async () => [],
cancelNotification: async () => ({ status: 'not_found' }),
updateNotification: async () => ({ status: 'not_found' }),
listDueNotifications: async () => [dueNotification()],
claimDueNotification: async () => true,
releaseDueNotification: async () => {},

View File

@@ -316,6 +316,9 @@ describe('registerAdHocNotifications', () => {
async cancelNotification() {
return { status: 'not_found' }
},
async updateNotification() {
return { status: 'not_found' }
},
async listDueNotifications() {
return []
},
@@ -415,6 +418,9 @@ describe('registerAdHocNotifications', () => {
async cancelNotification() {
return { status: 'not_found' }
},
async updateNotification() {
return { status: 'not_found' }
},
async listDueNotifications() {
return []
},
@@ -497,6 +503,9 @@ describe('registerAdHocNotifications', () => {
async cancelNotification() {
return { status: 'not_found' }
},
async updateNotification() {
return { status: 'not_found' }
},
async listDueNotifications() {
return []
},

View File

@@ -217,26 +217,53 @@ export function formatReminderWhen(input: {
: formatScheduledFor(input.locale, input.scheduledForIso, input.timezone)
}
function deliveryModeLabel(locale: BotLocale, mode: AdHocNotificationDeliveryMode): string {
if (locale === 'ru') {
switch (mode) {
case 'topic':
return 'в этот топик'
case 'dm_all':
return 'всем в личку'
case 'dm_selected':
return 'выбранным в личку'
function listedNotificationLine(input: {
locale: BotLocale
timezone: string
item: Awaited<ReturnType<AdHocNotificationService['listUpcomingNotifications']>>[number]
}): string {
const when = formatReminderWhen({
locale: input.locale,
scheduledForIso: input.item.scheduledFor.toString(),
timezone: input.timezone
})
const details: string[] = []
if (input.item.assigneeDisplayName) {
details.push(
input.locale === 'ru'
? `для ${input.item.assigneeDisplayName}`
: `for ${input.item.assigneeDisplayName}`
)
}
if (input.item.deliveryMode !== 'topic') {
if (input.item.deliveryMode === 'dm_all') {
details.push(input.locale === 'ru' ? 'всем в личку' : 'DM to everyone')
} else {
const names = input.item.dmRecipientDisplayNames.join(', ')
details.push(
input.locale === 'ru'
? names.length > 0
? `в личку: ${names}`
: 'в выбранные лички'
: names.length > 0
? `DM: ${names}`
: 'DM selected members'
)
}
}
switch (mode) {
case 'topic':
return 'this topic'
case 'dm_all':
return 'DM all members'
case 'dm_selected':
return 'DM selected members'
if (input.item.creatorDisplayName !== input.item.assigneeDisplayName) {
details.push(
input.locale === 'ru'
? `создал ${input.item.creatorDisplayName}`
: `created by ${input.item.creatorDisplayName}`
)
}
const suffix = details.length > 0 ? `\n${details.join(' · ')}` : ''
return `${when}\n${input.item.notificationText}${suffix}`
}
function notificationSummaryText(input: {
@@ -635,36 +662,42 @@ export function registerAdHocNotifications(options: {
await replyInTopic(
ctx,
locale === 'ru'
? 'Пока нет будущих напоминаний, которые вы можете отменить.'
: 'There are no upcoming notifications you can cancel yet.'
? 'Пока будущих напоминаний нет.'
: 'There are no upcoming notifications yet.'
)
return
}
const lines = items.slice(0, 10).map((item, index) => {
const when = formatScheduledFor(
locale,
item.scheduledFor.toString(),
reminderContext.timezone
)
return `${index + 1}. ${item.notificationText}\n${when}\n${deliveryModeLabel(locale, item.deliveryMode)}`
})
const listedItems = items.slice(0, 10).map((item, index) => ({
item,
index
}))
const lines = listedItems.map(
({ item, index }) =>
`${index + 1}. ${listedNotificationLine({
locale,
timezone: reminderContext.timezone,
item
})}`
)
const keyboard: InlineKeyboardMarkup = {
inline_keyboard: items.slice(0, 10).map((item, index) => [
{
text: locale === 'ru' ? `Отменить ${index + 1}` : `Cancel ${index + 1}`,
callback_data: `${AD_HOC_NOTIFICATION_CANCEL_SAVED_PREFIX}${item.id}`
}
])
inline_keyboard: listedItems
.filter(({ item }) => item.canCancel)
.map(({ item, index }) => [
{
text: locale === 'ru' ? `Отменить ${index + 1}` : `Cancel ${index + 1}`,
callback_data: `${AD_HOC_NOTIFICATION_CANCEL_SAVED_PREFIX}${item.id}`
}
])
}
await replyInTopic(
ctx,
[locale === 'ru' ? 'Ближайшие напоминания:' : 'Upcoming notifications:', '', ...lines].join(
'\n'
'\n\n'
),
keyboard
keyboard.inline_keyboard.length > 0 ? keyboard : undefined
)
})

View File

@@ -73,6 +73,10 @@ import {
createMiniAppUpdateUtilityBillHandler
} from './miniapp-billing'
import { createMiniAppLocalePreferenceHandler } from './miniapp-locale'
import {
createMiniAppCancelNotificationHandler,
createMiniAppUpdateNotificationHandler
} from './miniapp-notifications'
import { createNbgExchangeRateProvider } from './nbg-exchange-rates'
import { createOpenAiChatAssistant } from './openai-chat-assistant'
import { createOpenAiAdHocNotificationInterpreter } from './openai-ad-hoc-notification-interpreter'
@@ -635,10 +639,31 @@ export async function createBotRuntimeApp(): Promise<BotRuntimeApp> {
allowedOrigins: runtime.miniAppAllowedOrigins,
botToken: runtime.telegramBotToken,
financeServiceForHousehold,
adHocNotificationService: adHocNotificationService!,
onboardingService: householdOnboardingService,
logger: getLogger('miniapp-dashboard')
})
: undefined,
miniAppUpdateNotification:
householdOnboardingService && adHocNotificationService
? createMiniAppUpdateNotificationHandler({
allowedOrigins: runtime.miniAppAllowedOrigins,
botToken: runtime.telegramBotToken,
onboardingService: householdOnboardingService,
adHocNotificationService,
logger: getLogger('miniapp-notifications')
})
: undefined,
miniAppCancelNotification:
householdOnboardingService && adHocNotificationService
? createMiniAppCancelNotificationHandler({
allowedOrigins: runtime.miniAppAllowedOrigins,
botToken: runtime.telegramBotToken,
onboardingService: householdOnboardingService,
adHocNotificationService,
logger: getLogger('miniapp-notifications')
})
: undefined,
miniAppPendingMembers: householdOnboardingService
? createMiniAppPendingMembersHandler({
allowedOrigins: runtime.miniAppAllowedOrigins,

View File

@@ -1,6 +1,7 @@
import { describe, expect, test } from 'bun:test'
import {
type AdHocNotificationService,
createFinanceCommandService,
createHouseholdOnboardingService
} from '@household/application'
@@ -314,6 +315,23 @@ function onboardingRepository(): HouseholdConfigurationRepository {
}
}
function notificationService(
items: Awaited<ReturnType<AdHocNotificationService['listUpcomingNotifications']>> = []
): AdHocNotificationService {
return {
scheduleNotification: async () => {
throw new Error('not implemented')
},
listUpcomingNotifications: async () => items,
cancelNotification: async () => ({ status: 'not_found' }),
updateNotification: async () => ({ status: 'not_found' }),
listDueNotifications: async () => [],
claimDueNotification: async () => false,
releaseDueNotification: async () => {},
markNotificationSent: async () => null
}
}
describe('createMiniAppDashboardHandler', () => {
test('returns a dashboard for an authenticated household member', async () => {
const authDate = Math.floor(Date.now() / 1000)
@@ -344,10 +362,28 @@ describe('createMiniAppDashboardHandler', () => {
isAdmin: true
}
]
const adHocNotificationService = notificationService([
{
id: 'notification-1',
creatorMemberId: 'member-1',
creatorDisplayName: 'Stan',
assigneeMemberId: null,
assigneeDisplayName: null,
notificationText: 'Stan, breakfast time.',
scheduledFor: instantFromIso('2026-03-25T06:00:00.000Z'),
deliveryMode: 'topic',
dmRecipientMemberIds: [],
dmRecipientDisplayNames: [],
status: 'scheduled',
canCancel: true,
canEdit: true
}
])
const dashboard = createMiniAppDashboardHandler({
allowedOrigins: ['http://localhost:5173'],
botToken: 'test-bot-token',
financeServiceForHousehold: () => financeService,
adHocNotificationService,
onboardingService: createHouseholdOnboardingService({
repository: householdRepository
})
@@ -415,6 +451,17 @@ describe('createMiniAppDashboardHandler', () => {
currency: 'GEL',
displayCurrency: 'GEL'
}
],
notifications: [
{
id: 'notification-1',
summaryText: 'Stan, breakfast time.',
deliveryMode: 'topic',
dmRecipientMemberIds: [],
creatorDisplayName: 'Stan',
canCancel: true,
canEdit: true
}
]
}
})
@@ -550,6 +597,7 @@ describe('createMiniAppDashboardHandler', () => {
allowedOrigins: ['http://localhost:5173'],
botToken: 'test-bot-token',
financeServiceForHousehold: () => financeService,
adHocNotificationService: notificationService(),
onboardingService: createHouseholdOnboardingService({
repository: householdRepository
})
@@ -644,6 +692,7 @@ describe('createMiniAppDashboardHandler', () => {
allowedOrigins: ['http://localhost:5173'],
botToken: 'test-bot-token',
financeServiceForHousehold: () => financeService,
adHocNotificationService: notificationService(),
onboardingService: createHouseholdOnboardingService({
repository: householdRepository
})
@@ -706,6 +755,7 @@ describe('createMiniAppDashboardHandler', () => {
allowedOrigins: ['http://localhost:5173'],
botToken: 'test-bot-token',
financeServiceForHousehold: () => financeService,
adHocNotificationService: notificationService(),
onboardingService: createHouseholdOnboardingService({
repository: householdRepository
})

View File

@@ -1,4 +1,8 @@
import type { FinanceCommandService, HouseholdOnboardingService } from '@household/application'
import type {
AdHocNotificationService,
FinanceCommandService,
HouseholdOnboardingService
} from '@household/application'
import { Money } from '@household/domain'
import type { Logger } from '@household/observability'
@@ -14,6 +18,7 @@ export function createMiniAppDashboardHandler(options: {
allowedOrigins: readonly string[]
botToken: string
financeServiceForHousehold: (householdId: string) => FinanceCommandService
adHocNotificationService: AdHocNotificationService
onboardingService: HouseholdOnboardingService
logger?: Logger
}): {
@@ -74,6 +79,10 @@ export function createMiniAppDashboardHandler(options: {
const dashboard = await options
.financeServiceForHousehold(session.member.householdId)
.generateDashboard()
const notifications = await options.adHocNotificationService.listUpcomingNotifications({
householdId: session.member.householdId,
viewerMemberId: session.member.id
})
if (!dashboard) {
return miniAppJsonResponse(
{ ok: false, error: 'No billing cycle available' },
@@ -178,6 +187,21 @@ export function createMiniAppDashboardHandler(options: {
})) ?? []
}
: {})
})),
notifications: notifications.map((notification) => ({
id: notification.id,
summaryText: notification.notificationText,
scheduledFor: notification.scheduledFor.toString(),
status: notification.status,
deliveryMode: notification.deliveryMode,
dmRecipientMemberIds: notification.dmRecipientMemberIds,
dmRecipientDisplayNames: notification.dmRecipientDisplayNames,
creatorMemberId: notification.creatorMemberId,
creatorDisplayName: notification.creatorDisplayName,
assigneeMemberId: notification.assigneeMemberId,
assigneeDisplayName: notification.assigneeDisplayName,
canCancel: notification.canCancel,
canEdit: notification.canEdit
}))
}
},

View File

@@ -0,0 +1,216 @@
import type { AdHocNotificationService, HouseholdOnboardingService } from '@household/application'
import { Temporal } from '@household/domain'
import type { Logger } from '@household/observability'
import type { AdHocNotificationDeliveryMode } from '@household/ports'
import {
allowedMiniAppOrigin,
createMiniAppSessionService,
miniAppErrorResponse,
miniAppJsonResponse,
readMiniAppRequestPayload,
type MiniAppSessionResult
} from './miniapp-auth'
async function authenticateMemberSession(
request: Request,
sessionService: ReturnType<typeof createMiniAppSessionService>,
origin: string | undefined
): Promise<
| Response
| {
member: NonNullable<MiniAppSessionResult['member']>
}
> {
const payload = await readMiniAppRequestPayload(request)
if (!payload.initData) {
return miniAppJsonResponse({ ok: false, error: 'Missing initData' }, 400, origin)
}
const session = await sessionService.authenticate(payload)
if (!session) {
return miniAppJsonResponse({ ok: false, error: 'Invalid Telegram init data' }, 401, origin)
}
if (!session.authorized || !session.member || session.member.status !== 'active') {
return miniAppJsonResponse(
{ ok: false, error: 'Access limited to active household members' },
403,
origin
)
}
return {
member: session.member
}
}
async function parseJsonBody<T>(request: Request): Promise<T> {
const text = await request.clone().text()
if (text.trim().length === 0) {
return {} as T
}
try {
return JSON.parse(text) as T
} catch {
throw new Error('Invalid JSON body')
}
}
function parseScheduledLocal(localValue: string, timezone: string): Temporal.Instant {
return Temporal.ZonedDateTime.from(`${localValue}[${timezone}]`).toInstant()
}
function isDeliveryMode(value: string | undefined): value is AdHocNotificationDeliveryMode {
return value === 'topic' || value === 'dm_all' || value === 'dm_selected'
}
export function createMiniAppUpdateNotificationHandler(options: {
allowedOrigins: readonly string[]
botToken: string
onboardingService: HouseholdOnboardingService
adHocNotificationService: AdHocNotificationService
logger?: Logger
}): {
handler: (request: Request) => Promise<Response>
} {
const sessionService = createMiniAppSessionService({
botToken: options.botToken,
onboardingService: options.onboardingService
})
return {
handler: async (request) => {
const origin = allowedMiniAppOrigin(request, options.allowedOrigins)
if (request.method === 'OPTIONS') {
return miniAppJsonResponse({ ok: true }, 204, origin)
}
if (request.method !== 'POST') {
return miniAppJsonResponse({ ok: false, error: 'Method Not Allowed' }, 405, origin)
}
try {
const auth = await authenticateMemberSession(request, sessionService, origin)
if (auth instanceof Response) {
return auth
}
const parsed = await parseJsonBody<{
notificationId?: string
scheduledLocal?: string
timezone?: string
deliveryMode?: string
dmRecipientMemberIds?: string[]
}>(request)
const notificationId = parsed.notificationId?.trim()
if (!notificationId) {
return miniAppJsonResponse({ ok: false, error: 'Missing notificationId' }, 400, origin)
}
const scheduledLocal = parsed.scheduledLocal?.trim()
const timezone = parsed.timezone?.trim()
const deliveryMode = parsed.deliveryMode?.trim()
const result = await options.adHocNotificationService.updateNotification({
notificationId,
viewerMemberId: auth.member.id,
...(scheduledLocal && timezone
? {
scheduledFor: parseScheduledLocal(scheduledLocal, timezone),
timePrecision: 'exact' as const
}
: {}),
...(deliveryMode && isDeliveryMode(deliveryMode)
? {
deliveryMode,
dmRecipientMemberIds: parsed.dmRecipientMemberIds ?? []
}
: {})
})
switch (result.status) {
case 'updated':
return miniAppJsonResponse({ ok: true, authorized: true }, 200, origin)
case 'invalid':
return miniAppJsonResponse({ ok: false, error: result.reason }, 400, origin)
case 'forbidden':
return miniAppJsonResponse({ ok: false, error: 'Forbidden' }, 403, origin)
case 'not_found':
return miniAppJsonResponse({ ok: false, error: 'Notification not found' }, 404, origin)
case 'already_handled':
case 'past_due':
return miniAppJsonResponse({ ok: false, error: result.status }, 409, origin)
}
} catch (error) {
return miniAppErrorResponse(error, origin, options.logger)
}
}
}
}
export function createMiniAppCancelNotificationHandler(options: {
allowedOrigins: readonly string[]
botToken: string
onboardingService: HouseholdOnboardingService
adHocNotificationService: AdHocNotificationService
logger?: Logger
}): {
handler: (request: Request) => Promise<Response>
} {
const sessionService = createMiniAppSessionService({
botToken: options.botToken,
onboardingService: options.onboardingService
})
return {
handler: async (request) => {
const origin = allowedMiniAppOrigin(request, options.allowedOrigins)
if (request.method === 'OPTIONS') {
return miniAppJsonResponse({ ok: true }, 204, origin)
}
if (request.method !== 'POST') {
return miniAppJsonResponse({ ok: false, error: 'Method Not Allowed' }, 405, origin)
}
try {
const auth = await authenticateMemberSession(request, sessionService, origin)
if (auth instanceof Response) {
return auth
}
const parsed = await parseJsonBody<{
notificationId?: string
}>(request)
const notificationId = parsed.notificationId?.trim()
if (!notificationId) {
return miniAppJsonResponse({ ok: false, error: 'Missing notificationId' }, 400, origin)
}
const result = await options.adHocNotificationService.cancelNotification({
notificationId,
viewerMemberId: auth.member.id
})
switch (result.status) {
case 'cancelled':
return miniAppJsonResponse({ ok: true, authorized: true }, 200, origin)
case 'forbidden':
return miniAppJsonResponse({ ok: false, error: 'Forbidden' }, 403, origin)
case 'not_found':
return miniAppJsonResponse({ ok: false, error: 'Notification not found' }, 404, origin)
case 'already_handled':
case 'past_due':
return miniAppJsonResponse({ ok: false, error: result.status }, 409, origin)
}
} catch (error) {
return miniAppErrorResponse(error, origin, options.logger)
}
}
}
}

View File

@@ -14,6 +14,18 @@ export interface BotWebhookServerOptions {
handler: (request: Request) => Promise<Response>
}
| undefined
miniAppUpdateNotification?:
| {
path?: string
handler: (request: Request) => Promise<Response>
}
| undefined
miniAppCancelNotification?:
| {
path?: string
handler: (request: Request) => Promise<Response>
}
| undefined
miniAppJoin?:
| {
path?: string
@@ -226,6 +238,10 @@ export function createBotWebhookServer(options: BotWebhookServerOptions): {
: `/${options.webhookPath}`
const miniAppAuthPath = options.miniAppAuth?.path ?? '/api/miniapp/session'
const miniAppDashboardPath = options.miniAppDashboard?.path ?? '/api/miniapp/dashboard'
const miniAppUpdateNotificationPath =
options.miniAppUpdateNotification?.path ?? '/api/miniapp/notifications/update'
const miniAppCancelNotificationPath =
options.miniAppCancelNotification?.path ?? '/api/miniapp/notifications/cancel'
const miniAppJoinPath = options.miniAppJoin?.path ?? '/api/miniapp/join'
const miniAppPendingMembersPath =
options.miniAppPendingMembers?.path ?? '/api/miniapp/admin/pending-members'
@@ -301,6 +317,14 @@ export function createBotWebhookServer(options: BotWebhookServerOptions): {
return await options.miniAppDashboard.handler(request)
}
if (options.miniAppUpdateNotification && url.pathname === miniAppUpdateNotificationPath) {
return await options.miniAppUpdateNotification.handler(request)
}
if (options.miniAppCancelNotification && url.pathname === miniAppCancelNotificationPath) {
return await options.miniAppCancelNotification.handler(request)
}
if (options.miniAppJoin && url.pathname === miniAppJoinPath) {
return await options.miniAppJoin.handler(request)
}

View File

@@ -5,7 +5,7 @@ import { cn } from '../../lib/cn'
type InputProps = {
value?: string
placeholder?: string
type?: 'text' | 'number' | 'email'
type?: 'text' | 'number' | 'email' | 'datetime-local'
min?: string | number
max?: string | number
step?: string | number

View File

@@ -450,7 +450,39 @@ function createDashboard(state: {
rentFxEffectiveDate: '2026-03-17',
members: state.members,
paymentPeriods,
ledger: state.ledger ?? baseLedger()
ledger: state.ledger ?? baseLedger(),
notifications: [
{
id: 'notification-breakfast',
summaryText: 'Stas, breakfast is waiting for your attention.',
scheduledFor: '2026-03-25T05:00:00.000Z',
status: 'scheduled',
deliveryMode: 'topic',
dmRecipientMemberIds: [],
dmRecipientDisplayNames: [],
creatorMemberId: 'demo-member',
creatorDisplayName: 'Stas',
assigneeMemberId: 'demo-member',
assigneeDisplayName: 'Stas',
canCancel: true,
canEdit: true
},
{
id: 'notification-call-georgiy',
summaryText: 'Dima, time to check whether Georgiy has called back.',
scheduledFor: '2026-03-25T16:00:00.000Z',
status: 'scheduled',
deliveryMode: 'dm_selected',
dmRecipientMemberIds: ['member-chorb', 'demo-member'],
dmRecipientDisplayNames: ['Dima', 'Stas'],
creatorMemberId: 'member-chorb',
creatorDisplayName: 'Chorbanaut',
assigneeMemberId: null,
assigneeDisplayName: null,
canCancel: true,
canEdit: true
}
]
}
}

View File

@@ -187,6 +187,21 @@ export interface MiniAppDashboard {
}[]
payerMemberId?: string
}[]
notifications: {
id: string
summaryText: string
scheduledFor: string
status: 'scheduled' | 'sent' | 'cancelled'
deliveryMode: 'topic' | 'dm_all' | 'dm_selected'
dmRecipientMemberIds: readonly string[]
dmRecipientDisplayNames: readonly string[]
creatorMemberId: string
creatorDisplayName: string
assigneeMemberId: string | null
assigneeDisplayName: string | null
canCancel: boolean
canEdit: boolean
}[]
}
export interface MiniAppAdminSettingsPayload {
@@ -339,6 +354,64 @@ export async function fetchMiniAppDashboard(initData: string): Promise<MiniAppDa
return payload.dashboard
}
export async function updateMiniAppNotification(
initData: string,
input: {
notificationId: string
scheduledLocal?: string
timezone?: string
deliveryMode?: 'topic' | 'dm_all' | 'dm_selected'
dmRecipientMemberIds?: readonly string[]
}
): Promise<void> {
const response = await fetch(`${apiBaseUrl()}/api/miniapp/notifications/update`, {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
initData,
...input
})
})
const payload = (await response.json()) as {
ok: boolean
authorized?: boolean
error?: string
}
if (!response.ok || !payload.authorized) {
throw new Error(payload.error ?? 'Failed to update notification')
}
}
export async function cancelMiniAppNotification(
initData: string,
notificationId: string
): Promise<void> {
const response = await fetch(`${apiBaseUrl()}/api/miniapp/notifications/cancel`, {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
initData,
notificationId
})
})
const payload = (await response.json()) as {
ok: boolean
authorized?: boolean
error?: string
}
if (!response.ok || !payload.authorized) {
throw new Error(payload.error ?? 'Failed to cancel notification')
}
}
export async function fetchMiniAppPendingMembers(
initData: string
): Promise<readonly MiniAppPendingMember[]> {

View File

@@ -23,7 +23,12 @@ import {
nextCyclePeriod,
parseCalendarDate
} from '../lib/dates'
import { submitMiniAppUtilityBill, addMiniAppPayment } from '../miniapp-api'
import {
submitMiniAppUtilityBill,
addMiniAppPayment,
updateMiniAppNotification,
cancelMiniAppNotification
} from '../miniapp-api'
import type { MiniAppDashboard } from '../miniapp-api'
function sumMemberPaymentsByKind(
@@ -76,6 +81,117 @@ function paymentRemainingMinor(
return remainingMinor > 0n ? remainingMinor : 0n
}
function zonedDateTimeParts(date: Date, timeZone: string) {
const parts = new Intl.DateTimeFormat('en-CA', {
timeZone,
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
hourCycle: 'h23'
}).formatToParts(date)
const read = (type: string) => Number(parts.find((part) => part.type === type)?.value ?? '0')
return {
year: read('year'),
month: read('month'),
day: read('day'),
hour: read('hour'),
minute: read('minute')
}
}
function dateKey(input: { year: number; month: number; day: number }) {
return [
String(input.year).padStart(4, '0'),
String(input.month).padStart(2, '0'),
String(input.day).padStart(2, '0')
].join('-')
}
function shiftDateKey(currentKey: string, days: number): string {
const [yearText = '1970', monthText = '01', dayText = '01'] = currentKey.split('-')
const year = Number(yearText)
const month = Number(monthText)
const day = Number(dayText)
const shifted = new Date(Date.UTC(year, month - 1, day + days))
return [
shifted.getUTCFullYear(),
String(shifted.getUTCMonth() + 1).padStart(2, '0'),
String(shifted.getUTCDate()).padStart(2, '0')
].join('-')
}
function formatNotificationTimeOfDay(locale: 'en' | 'ru', hour: number, minute: number) {
const exact = `${String(hour).padStart(2, '0')}:${String(minute).padStart(2, '0')}`
if (locale !== 'ru' || minute !== 0) {
return locale === 'ru' ? `в ${exact}` : `at ${exact}`
}
if (hour >= 5 && hour <= 11) return `в ${hour} утра`
if (hour >= 12 && hour <= 16) return hour === 12 ? 'в 12 дня' : `в ${hour} дня`
if (hour >= 17 && hour <= 23) return `в ${hour > 12 ? hour - 12 : hour} вечера`
return `в ${hour} ночи`
}
function formatNotificationWhen(
locale: 'en' | 'ru',
scheduledForIso: string,
timeZone: string
): string {
const now = zonedDateTimeParts(new Date(), timeZone)
const target = zonedDateTimeParts(new Date(scheduledForIso), timeZone)
const nowKey = dateKey(now)
const sleepAwareBaseKey = now.hour <= 4 ? shiftDateKey(nowKey, -1) : nowKey
const targetKey = dateKey(target)
const timeText = formatNotificationTimeOfDay(locale, target.hour, target.minute)
if (targetKey === sleepAwareBaseKey) {
return locale === 'ru' ? `Сегодня ${timeText}` : `Today ${timeText}`
}
if (targetKey === shiftDateKey(sleepAwareBaseKey, 1)) {
return locale === 'ru' ? `Завтра ${timeText}` : `Tomorrow ${timeText}`
}
if (targetKey === shiftDateKey(sleepAwareBaseKey, 2)) {
return locale === 'ru' ? `Послезавтра ${timeText}` : `The day after tomorrow ${timeText}`
}
const dateText =
locale === 'ru'
? `${String(target.day).padStart(2, '0')}.${String(target.month).padStart(2, '0')}.${target.year}`
: `${target.year}-${String(target.month).padStart(2, '0')}-${String(target.day).padStart(2, '0')}`
return `${dateText} ${timeText}`
}
function formatNotificationDelivery(
locale: 'en' | 'ru',
notification: MiniAppDashboard['notifications'][number]
) {
if (notification.deliveryMode === 'topic') {
return locale === 'ru' ? 'В этот топик' : 'This topic'
}
if (notification.deliveryMode === 'dm_all') {
return locale === 'ru' ? 'Всем в личку' : 'DM to everyone'
}
return locale === 'ru'
? notification.dmRecipientDisplayNames.length > 0
? `В личку: ${notification.dmRecipientDisplayNames.join(', ')}`
: 'В выбранные лички'
: notification.dmRecipientDisplayNames.length > 0
? `DM: ${notification.dmRecipientDisplayNames.join(', ')}`
: 'DM selected members'
}
function notificationInputValue(iso: string, timeZone: string) {
const target = zonedDateTimeParts(new Date(iso), timeZone)
return `${dateKey(target)}T${String(target.hour).padStart(2, '0')}:${String(target.minute).padStart(2, '0')}`
}
export default function HomeRoute() {
const navigate = useNavigate()
const { readySession, initData, refreshHouseholdData } = useSession()
@@ -104,12 +220,30 @@ export default function HomeRoute() {
)
const [quickPaymentAmount, setQuickPaymentAmount] = createSignal('')
const [submittingPayment, setSubmittingPayment] = createSignal(false)
const [notificationEditorOpen, setNotificationEditorOpen] = createSignal(false)
const [editingNotificationId, setEditingNotificationId] = createSignal<string | null>(null)
const [notificationScheduleDraft, setNotificationScheduleDraft] = createSignal('')
const [notificationDeliveryModeDraft, setNotificationDeliveryModeDraft] = createSignal<
'topic' | 'dm_all' | 'dm_selected'
>('topic')
const [notificationRecipientsDraft, setNotificationRecipientsDraft] = createSignal<string[]>([])
const [savingNotification, setSavingNotification] = createSignal(false)
const [cancellingNotificationId, setCancellingNotificationId] = createSignal<string | null>(null)
const [toastState, setToastState] = createSignal<{
visible: boolean
message: string
type: 'success' | 'info' | 'error'
}>({ visible: false, message: '', type: 'info' })
const selectedNotification = createMemo(
() =>
dashboard()?.notifications.find(
(notification) => notification.id === editingNotificationId()
) ?? null
)
const activeHouseholdMembers = createMemo(() => dashboard()?.members ?? [])
async function copyText(value: string): Promise<boolean> {
try {
await navigator.clipboard.writeText(value)
@@ -331,6 +465,94 @@ export default function HomeRoute() {
}
}
function openNotificationEditor(notification: MiniAppDashboard['notifications'][number]) {
const data = dashboard()
if (!data) return
setEditingNotificationId(notification.id)
setNotificationScheduleDraft(notificationInputValue(notification.scheduledFor, data.timezone))
setNotificationDeliveryModeDraft(notification.deliveryMode)
setNotificationRecipientsDraft(
notification.deliveryMode === 'dm_selected' ? [...notification.dmRecipientMemberIds] : []
)
setNotificationEditorOpen(true)
}
function toggleNotificationRecipient(memberId: string) {
setNotificationRecipientsDraft((current) =>
current.includes(memberId)
? current.filter((value) => value !== memberId)
: [...current, memberId]
)
}
async function handleNotificationSave() {
const data = initData()
const current = dashboard()
const notification = selectedNotification()
if (!data || !current || !notification || !notification.canEdit || savingNotification()) return
setSavingNotification(true)
try {
await updateMiniAppNotification(data, {
notificationId: notification.id,
scheduledLocal: notificationScheduleDraft(),
timezone: current.timezone,
deliveryMode: notificationDeliveryModeDraft(),
dmRecipientMemberIds:
notificationDeliveryModeDraft() === 'dm_selected' ? notificationRecipientsDraft() : []
})
setNotificationEditorOpen(false)
setToastState({
visible: true,
message: locale() === 'ru' ? 'Напоминание обновлено.' : 'Notification updated.',
type: 'success'
})
await refreshHouseholdData(true, true)
} catch {
setToastState({
visible: true,
message:
locale() === 'ru'
? 'Не получилось обновить напоминание.'
: 'Failed to update notification.',
type: 'error'
})
} finally {
setSavingNotification(false)
}
}
async function handleNotificationCancel(notificationId: string) {
const data = initData()
if (!data || cancellingNotificationId()) return
setCancellingNotificationId(notificationId)
try {
await cancelMiniAppNotification(data, notificationId)
if (editingNotificationId() === notificationId) {
setNotificationEditorOpen(false)
}
setToastState({
visible: true,
message: locale() === 'ru' ? 'Напоминание отменено.' : 'Notification cancelled.',
type: 'success'
})
await refreshHouseholdData(true, true)
} catch {
setToastState({
visible: true,
message:
locale() === 'ru'
? 'Не получилось отменить напоминание.'
: 'Failed to cancel notification.',
type: 'error'
})
} finally {
setCancellingNotificationId(null)
}
}
return (
<div class="route route--home">
{/* ── Welcome hero ────────────────────────────── */}
@@ -918,6 +1140,105 @@ export default function HomeRoute() {
</Card>
</Show>
<Card>
<div class="balance-card">
<div class="balance-card__header">
<span class="balance-card__label">
{locale() === 'ru' ? 'Напоминания' : 'Notifications'}
</span>
<Badge variant="muted">{data().notifications.length}</Badge>
</div>
<Show
when={data().notifications.length > 0}
fallback={
<p class="empty-state">
{locale() === 'ru'
? 'Пока нет запланированных напоминаний.'
: 'There are no scheduled notifications yet.'}
</p>
}
>
<div class="balance-card__amounts">
<For each={data().notifications}>
{(notification) => (
<div
class="balance-card__row"
style={{
'align-items': 'flex-start',
'flex-direction': 'column',
gap: '10px',
padding: '12px 0'
}}
>
<div
style={{
display: 'flex',
width: '100%',
'justify-content': 'space-between',
gap: '12px',
'align-items': 'flex-start'
}}
>
<div style={{ display: 'grid', gap: '6px' }}>
<strong>{notification.summaryText}</strong>
<span>
{formatNotificationWhen(
locale(),
notification.scheduledFor,
data().timezone
)}
</span>
<span>{formatNotificationDelivery(locale(), notification)}</span>
<Show when={notification.assigneeDisplayName}>
<span>
{(locale() === 'ru' ? 'Для: ' : 'For: ') +
notification.assigneeDisplayName}
</span>
</Show>
<span>
{(locale() === 'ru' ? 'Создал: ' : 'Created by: ') +
notification.creatorDisplayName}
</span>
</div>
<div
style={{
display: 'flex',
gap: '8px',
'flex-wrap': 'nowrap',
'justify-content': 'flex-end',
'align-items': 'center',
'flex-shrink': '0'
}}
>
<Show when={notification.canEdit}>
<Button
variant="secondary"
size="sm"
onClick={() => openNotificationEditor(notification)}
>
{locale() === 'ru' ? 'Управлять' : 'Manage'}
</Button>
</Show>
<Show when={notification.canCancel}>
<Button
variant="ghost"
size="sm"
loading={cancellingNotificationId() === notification.id}
onClick={() => void handleNotificationCancel(notification.id)}
>
{locale() === 'ru' ? 'Отменить' : 'Cancel'}
</Button>
</Show>
</div>
</div>
</div>
)}
</For>
</div>
</Show>
</div>
</Card>
{/* Latest activity */}
<Card>
<div class="activity-card">
@@ -972,6 +1293,122 @@ export default function HomeRoute() {
</Match>
</Switch>
<Modal
open={notificationEditorOpen()}
title={locale() === 'ru' ? 'Управление напоминанием' : 'Manage notification'}
{...(selectedNotification()
? {
description: formatNotificationWhen(
locale(),
selectedNotification()!.scheduledFor,
dashboard()?.timezone ?? 'UTC'
)
}
: {})}
closeLabel={copy().showLessAction}
onClose={() => {
setNotificationEditorOpen(false)
}}
footer={
<>
<Show when={selectedNotification()?.canCancel}>
<Button
variant="ghost"
loading={cancellingNotificationId() === selectedNotification()?.id}
onClick={() =>
selectedNotification() &&
void handleNotificationCancel(selectedNotification()!.id)
}
>
{locale() === 'ru' ? 'Отменить напоминание' : 'Cancel notification'}
</Button>
</Show>
<Button variant="ghost" onClick={() => setNotificationEditorOpen(false)}>
{copy().showLessAction}
</Button>
<Button
variant="primary"
loading={savingNotification()}
disabled={
!notificationScheduleDraft().trim() ||
(notificationDeliveryModeDraft() === 'dm_selected' &&
notificationRecipientsDraft().length === 0)
}
onClick={() => void handleNotificationSave()}
>
{locale() === 'ru' ? 'Сохранить' : 'Save'}
</Button>
</>
}
>
<div style={{ display: 'grid', gap: '16px' }}>
<Field label={locale() === 'ru' ? 'Когда' : 'When'}>
<Input
type="datetime-local"
value={notificationScheduleDraft()}
onInput={(event) => setNotificationScheduleDraft(event.currentTarget.value)}
/>
</Field>
<Field label={locale() === 'ru' ? 'Куда отправлять' : 'Delivery'}>
<div style={{ display: 'flex', gap: '8px', 'flex-wrap': 'wrap' }}>
<Button
variant={notificationDeliveryModeDraft() === 'topic' ? 'primary' : 'secondary'}
size="sm"
onClick={() => setNotificationDeliveryModeDraft('topic')}
>
{locale() === 'ru' ? 'В топик' : 'Topic'}
</Button>
<Button
variant={notificationDeliveryModeDraft() === 'dm_all' ? 'primary' : 'secondary'}
size="sm"
onClick={() => setNotificationDeliveryModeDraft('dm_all')}
>
{locale() === 'ru' ? 'Всем в личку' : 'DM all'}
</Button>
<Button
variant={
notificationDeliveryModeDraft() === 'dm_selected' ? 'primary' : 'secondary'
}
size="sm"
onClick={() => setNotificationDeliveryModeDraft('dm_selected')}
>
{locale() === 'ru' ? 'Выбрать получателей' : 'Select recipients'}
</Button>
</div>
</Field>
<Show when={notificationDeliveryModeDraft() === 'dm_selected'}>
<Field
label={locale() === 'ru' ? 'Получатели' : 'Recipients'}
hint={
locale() === 'ru'
? 'Выберите, кому отправить в личку.'
: 'Choose who should receive the DM.'
}
>
<div style={{ display: 'flex', gap: '8px', 'flex-wrap': 'wrap' }}>
<For each={activeHouseholdMembers()}>
{(member) => (
<Button
variant={
notificationRecipientsDraft().includes(member.memberId)
? 'primary'
: 'secondary'
}
size="sm"
onClick={() => toggleNotificationRecipient(member.memberId)}
>
{member.displayName}
</Button>
)}
</For>
</div>
</Field>
</Show>
</div>
</Modal>
{/* Quick Payment Modal */}
<Modal
open={quickPaymentOpen()}