* feat: multi-tenant company refactor (GNU-19) Introduce companies table, company_members, and user_preferences to support multiple companies per user. All data scoping changes from user_id to company_id across the entire codebase. Key changes: - Database migration: new tables, company_id on 40+ tables, backfill, RLS rewrite from user_id to company-member-based, updated RPCs - Types: Company, CompanyMember, CompanyRole, UserPreferences types; company_id added to all entity interfaces; companyId on all events - Engine: all 7 core functions take companyId; storno, period, year-end services updated; 16 report generators updated - Middleware: company context resolution (cookie → prefs → first company) - API routes: ~120 routes updated with requireCompanyId() - Frontend: CompanyProvider context, layout/dashboard/onboarding updated - Extensions: context factory, 9 extensions, all lib files updated - Tests: 1880 tests passing, all helpers updated with company_id defaults Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add database migrations for multi-tenant company and team system (GNU-19) Adds company_invitations, company creation RPC, team_members, account deletion RPC, and teams table refactor migrations. Updates base multi-tenant migration with cascading FKs and onboarding_step column. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add team types and update core infrastructure for multi-tenancy (GNU-19) Adds TeamRole, MemberSource, and Team types. Refactors Supabase service client to be stateless, updates middleware for team-aware routing, extends CompanyContext with team/role fields, and updates extension service types to accept companyId. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * refactor: thread company_id through business logic functions (GNU-19) Replaces user_id scoping with company_id across all lib modules: bookkeeping, documents, transactions, invoices, reconciliation, tax, deadlines, and import. Updates corresponding tests. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * refactor: thread company_id through API routes and extensions (GNU-19) Updates all existing API routes to extract and pass companyId. Updates enable-banking and arcim-migration extensions for company-scoped transaction ingestion and sync. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add company and team management API routes (GNU-19) Adds CRUD endpoints for company members, company invitations, team members, and team invitations. Includes invite token utilities, email templates, and company switch server action. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add team/company UI components, pages, and dashboard updates (GNU-19) Adds CompanySwitcher, ConsultantEmptyState, Step0RoleChoice, company members and team management panels. Updates dashboard layout for team-aware routing, onboarding for multi-step role choice, and auth callback for team invite acceptance. Ignores supabase/.branches/. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add null guards for company in import page (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: move appUrl declaration to outer scope in invite route (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add optional chaining for company.name in members section (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add optional chaining for second company.name in members section (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add null guards for company in extension components (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: pass companyId to executeSIEImport in arcim-migration extension (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: update tests to use companyId instead of userId and improve type handling --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
264 lines
8.3 KiB
TypeScript
264 lines
8.3 KiB
TypeScript
/**
|
|
* Push Notifications Extension
|
|
*
|
|
* Converts system events into instant push notifications.
|
|
* Also provides cron-based scheduling for time-dependent checks
|
|
* (tax deadlines, invoice due/overdue).
|
|
*
|
|
* Event handlers follow the gate pattern:
|
|
* check extension setting -> build payload -> send via unified pipeline
|
|
*/
|
|
|
|
import type { Extension, ExtensionContext } from '@/lib/extensions/types'
|
|
import type { EventPayload } from '@/lib/events/types'
|
|
import { pushNotificationsApiRoutes } from './api-routes'
|
|
import { sendNotificationToUser } from './notification-sender'
|
|
import {
|
|
createPeriodLockedPayload,
|
|
createYearClosedPayload,
|
|
createInvoiceSentPayload,
|
|
createReceiptExtractedPayload,
|
|
createReceiptMatchedPayload,
|
|
} from './payload-builders'
|
|
|
|
// ============================================================
|
|
// Settings
|
|
// ============================================================
|
|
|
|
export interface PushNotificationSettings {
|
|
periodLockedEnabled: boolean
|
|
periodYearClosedEnabled: boolean
|
|
invoiceSentEnabled: boolean
|
|
receiptExtractedEnabled: boolean
|
|
receiptMatchedEnabled: boolean
|
|
missingUnderlagEnabled: boolean
|
|
}
|
|
|
|
const DEFAULT_SETTINGS: PushNotificationSettings = {
|
|
periodLockedEnabled: true,
|
|
periodYearClosedEnabled: true,
|
|
invoiceSentEnabled: false,
|
|
receiptExtractedEnabled: true,
|
|
receiptMatchedEnabled: true,
|
|
missingUnderlagEnabled: true,
|
|
}
|
|
|
|
/** Get settings via ExtensionContext (preferred in event handlers) */
|
|
async function getSettingsViaCtx(ctx: ExtensionContext): Promise<PushNotificationSettings> {
|
|
const stored = await ctx.settings.get<Partial<PushNotificationSettings>>()
|
|
return { ...DEFAULT_SETTINGS, ...(stored || {}) }
|
|
}
|
|
|
|
/** Get settings for external callers (settings routes, cron jobs) */
|
|
export async function getSettings(userId: string): Promise<PushNotificationSettings> {
|
|
const { createClient } = await import('@/lib/supabase/server')
|
|
const supabase = await createClient()
|
|
|
|
const { data } = await supabase
|
|
.from('notification_settings')
|
|
.select(
|
|
'period_locked_enabled, period_year_closed_enabled, invoice_sent_enabled, receipt_extracted_enabled, receipt_matched_enabled, missing_underlag_enabled'
|
|
)
|
|
.eq('company_id', userId)
|
|
.single()
|
|
|
|
if (!data) return { ...DEFAULT_SETTINGS }
|
|
return {
|
|
periodLockedEnabled: data.period_locked_enabled ?? DEFAULT_SETTINGS.periodLockedEnabled,
|
|
periodYearClosedEnabled: data.period_year_closed_enabled ?? DEFAULT_SETTINGS.periodYearClosedEnabled,
|
|
invoiceSentEnabled: data.invoice_sent_enabled ?? DEFAULT_SETTINGS.invoiceSentEnabled,
|
|
receiptExtractedEnabled: data.receipt_extracted_enabled ?? DEFAULT_SETTINGS.receiptExtractedEnabled,
|
|
receiptMatchedEnabled: data.receipt_matched_enabled ?? DEFAULT_SETTINGS.receiptMatchedEnabled,
|
|
missingUnderlagEnabled: data.missing_underlag_enabled ?? DEFAULT_SETTINGS.missingUnderlagEnabled,
|
|
}
|
|
}
|
|
|
|
export async function saveSettings(
|
|
userId: string,
|
|
partial: Partial<PushNotificationSettings>
|
|
): Promise<PushNotificationSettings> {
|
|
const current = await getSettings(userId)
|
|
const merged = { ...current, ...partial }
|
|
|
|
const { createClient } = await import('@/lib/supabase/server')
|
|
const supabase = await createClient()
|
|
|
|
await supabase
|
|
.from('notification_settings')
|
|
.upsert(
|
|
{
|
|
user_id: userId,
|
|
period_locked_enabled: merged.periodLockedEnabled,
|
|
period_year_closed_enabled: merged.periodYearClosedEnabled,
|
|
invoice_sent_enabled: merged.invoiceSentEnabled,
|
|
receipt_extracted_enabled: merged.receiptExtractedEnabled,
|
|
receipt_matched_enabled: merged.receiptMatchedEnabled,
|
|
missing_underlag_enabled: merged.missingUnderlagEnabled,
|
|
},
|
|
{ onConflict: 'user_id' }
|
|
)
|
|
|
|
return merged
|
|
}
|
|
|
|
// ============================================================
|
|
// Event Handlers
|
|
// ============================================================
|
|
|
|
async function handlePeriodLocked(
|
|
payload: EventPayload<'period.locked'>,
|
|
ctx?: ExtensionContext
|
|
): Promise<void> {
|
|
const { period, userId } = payload
|
|
|
|
const settings = ctx ? await getSettingsViaCtx(ctx) : await getSettings(userId)
|
|
if (!settings.periodLockedEnabled) return
|
|
|
|
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
|
|
const notificationPayload = createPeriodLockedPayload(period.name, period.id)
|
|
|
|
const result = await sendNotificationToUser(
|
|
supabase,
|
|
userId,
|
|
notificationPayload,
|
|
'period_locked',
|
|
period.id
|
|
)
|
|
|
|
if (result.sent) {
|
|
(ctx?.log ?? console).info(`Period locked notification sent for ${period.name}`)
|
|
}
|
|
}
|
|
|
|
async function handleYearClosed(
|
|
payload: EventPayload<'period.year_closed'>,
|
|
ctx?: ExtensionContext
|
|
): Promise<void> {
|
|
const { period, userId } = payload
|
|
|
|
const settings = ctx ? await getSettingsViaCtx(ctx) : await getSettings(userId)
|
|
if (!settings.periodYearClosedEnabled) return
|
|
|
|
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
|
|
const notificationPayload = createYearClosedPayload(period.name, period.id)
|
|
|
|
const result = await sendNotificationToUser(
|
|
supabase,
|
|
userId,
|
|
notificationPayload,
|
|
'period_year_closed',
|
|
period.id
|
|
)
|
|
|
|
if (result.sent) {
|
|
(ctx?.log ?? console).info(`Year closed notification sent for ${period.name}`)
|
|
}
|
|
}
|
|
|
|
async function handleInvoiceSent(
|
|
payload: EventPayload<'invoice.sent'>,
|
|
ctx?: ExtensionContext
|
|
): Promise<void> {
|
|
const { invoice, userId } = payload
|
|
|
|
const settings = ctx ? await getSettingsViaCtx(ctx) : await getSettings(userId)
|
|
if (!settings.invoiceSentEnabled) return
|
|
|
|
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
|
|
const notificationPayload = createInvoiceSentPayload(
|
|
invoice.invoice_number,
|
|
invoice.id
|
|
)
|
|
|
|
const result = await sendNotificationToUser(
|
|
supabase,
|
|
userId,
|
|
notificationPayload,
|
|
'invoice_sent',
|
|
invoice.id
|
|
)
|
|
|
|
if (result.sent) {
|
|
(ctx?.log ?? console).info(`Invoice sent notification for #${invoice.invoice_number}`)
|
|
}
|
|
}
|
|
|
|
async function handleReceiptExtracted(
|
|
payload: EventPayload<'receipt.extracted'>,
|
|
ctx?: ExtensionContext
|
|
): Promise<void> {
|
|
const { receipt, userId } = payload
|
|
|
|
const settings = ctx ? await getSettingsViaCtx(ctx) : await getSettings(userId)
|
|
if (!settings.receiptExtractedEnabled) return
|
|
|
|
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
|
|
const notificationPayload = createReceiptExtractedPayload(
|
|
receipt.merchant_name,
|
|
receipt.id
|
|
)
|
|
|
|
const result = await sendNotificationToUser(
|
|
supabase,
|
|
userId,
|
|
notificationPayload,
|
|
'receipt_extracted',
|
|
receipt.id
|
|
)
|
|
|
|
if (result.sent) {
|
|
(ctx?.log ?? console).info(`Receipt extracted notification for ${receipt.id}`)
|
|
}
|
|
}
|
|
|
|
async function handleReceiptMatched(
|
|
payload: EventPayload<'receipt.matched'>,
|
|
ctx?: ExtensionContext
|
|
): Promise<void> {
|
|
const { receipt, transaction, userId } = payload
|
|
|
|
const settings = ctx ? await getSettingsViaCtx(ctx) : await getSettings(userId)
|
|
if (!settings.receiptMatchedEnabled) return
|
|
|
|
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
|
|
const notificationPayload = createReceiptMatchedPayload(receipt.id, transaction.id)
|
|
|
|
const result = await sendNotificationToUser(
|
|
supabase,
|
|
userId,
|
|
notificationPayload,
|
|
'receipt_matched',
|
|
receipt.id
|
|
)
|
|
|
|
if (result.sent) {
|
|
(ctx?.log ?? console).info(`Receipt matched notification for ${receipt.id}`)
|
|
}
|
|
}
|
|
|
|
// ============================================================
|
|
// Extension Object
|
|
// ============================================================
|
|
|
|
export const pushNotificationsExtension: Extension = {
|
|
id: 'push-notifications',
|
|
name: 'Push-notiser',
|
|
version: '1.0.0',
|
|
sector: 'general',
|
|
apiRoutes: pushNotificationsApiRoutes,
|
|
eventHandlers: [
|
|
{ eventType: 'period.locked', handler: handlePeriodLocked },
|
|
{ eventType: 'period.year_closed', handler: handleYearClosed },
|
|
{ eventType: 'invoice.sent', handler: handleInvoiceSent },
|
|
{ eventType: 'receipt.extracted', handler: handleReceiptExtracted },
|
|
{ eventType: 'receipt.matched', handler: handleReceiptMatched },
|
|
],
|
|
settingsPanel: {
|
|
label: 'Push-notiser',
|
|
path: '/settings/extensions/push-notifications',
|
|
},
|
|
async onInstall(ctx) {
|
|
await ctx.settings.set('settings', DEFAULT_SETTINGS)
|
|
},
|
|
}
|