* feat: cloud backup to Google Drive + full-archive all-scope Adds a cloud-backup extension that uploads a full-company backup ZIP to the user's own Google Drive via OAuth (drive.file scope only). Refresh tokens are AES-256-GCM encrypted before being stored in extension_data. The full-archive export gains a scope=all mode for whole-company backups (per-period SIE under sie/, per-period rapporter/ subfolders, flat dokument/ manifest tagged with fiscal_period_id). An 80 MB size guard short-circuits generation before the platform response limit. Also fixes a latent bug in lib/core/audit/audit-service.ts where the parameter was named userId while the query filtered by company_id; the audit-trail API route was passing user.id so audit queries returned empty unless user and company shared a UUID. Drive-by: scope the dashboard "fresh start" localStorage key per companyId so dismissing the setup checklist in one company no longer carries over to others. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: address review comments on cloud backup + archive export - Extend audit trail to_date to end-of-day so last-day entries aren't silently excluded from period-scoped archives. - Apply 413 size-limit guard regardless of include_documents, using the overhead-only figure when documents are excluded. - Use crypto.randomUUID() for Drive multipart boundary to eliminate any collision risk with ZIP payload bytes. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: migrate legacy setup-gate localStorage keys on dashboard Users who previously dismissed the setup checklist via the old global erp_setup_fresh_start or erp_checklist_dismissed keys were re-gated after the switch to a company-scoped key. Fall back to the legacy keys on read and migrate them to the scoped key on first hit. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: update customer email handling and anonymization rules in supportmail-to-ticket skill * test: update audit trail to_date expectation for end-of-day timestamp Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
257 lines
10 KiB
TypeScript
257 lines
10 KiB
TypeScript
import { createClient } from '@/lib/supabase/server'
|
|
import { redirect } from 'next/navigation'
|
|
import { cookies } from 'next/headers'
|
|
import DashboardContent from '@/components/dashboard/DashboardContent'
|
|
import { getActiveCompanyId } from '@/lib/company/context'
|
|
import type { Deadline, ReceiptQueueSummary, OnboardingProgress } from '@/types'
|
|
|
|
export const dynamic = 'force-dynamic'
|
|
|
|
export default async function DashboardPage() {
|
|
const supabase = await createClient()
|
|
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
redirect('/login')
|
|
}
|
|
|
|
const cookieStore = await cookies()
|
|
const rawCompanyId = cookieStore.get('gnubok-company-id')?.value
|
|
?? await getActiveCompanyId(supabase, user.id)
|
|
|
|
// Validate the cookie/preference points to a company the user can access
|
|
let companyId = rawCompanyId
|
|
if (companyId) {
|
|
const { data: membership } = await supabase
|
|
.from('company_members')
|
|
.select('company_id')
|
|
.eq('company_id', companyId)
|
|
.eq('user_id', user.id)
|
|
.maybeSingle()
|
|
if (!membership) companyId = null
|
|
}
|
|
|
|
if (!companyId) {
|
|
redirect('/onboarding')
|
|
}
|
|
|
|
// Fetch current year date boundaries
|
|
const startOfYearStr = new Date(new Date().getFullYear(), 0, 1).toISOString().split('T')[0]
|
|
const startOfMonthStr = new Date(new Date().getFullYear(), new Date().getMonth(), 1).toISOString().split('T')[0]
|
|
const now = new Date()
|
|
const today = now.toISOString().split('T')[0]
|
|
const nextWeek = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000).toISOString().split('T')[0]
|
|
|
|
// Source types that require supporting documents
|
|
const needsDocSourceTypes = [
|
|
'manual',
|
|
'bank_transaction',
|
|
'supplier_invoice_registered',
|
|
'supplier_invoice_paid',
|
|
'supplier_invoice_cash_payment',
|
|
'import',
|
|
]
|
|
|
|
// Fetch all data in parallel
|
|
const [
|
|
{ data: profile },
|
|
{ data: settings },
|
|
{ count: customerCount },
|
|
{ count: invoiceCount },
|
|
{ count: receiptCount },
|
|
{ count: transactionCount },
|
|
{ data: journalLines },
|
|
{ data: transactions },
|
|
{ data: unpaidInvoices },
|
|
{ data: bankConnections },
|
|
{ data: deadlines },
|
|
{ count: pendingReviewCount },
|
|
{ count: unmatchedReceiptsCount },
|
|
{ count: unmatchedTransactionsCount },
|
|
{ count: postedEntriesCount },
|
|
{ data: entriesWithDocs },
|
|
{ data: recentReceiptActivity },
|
|
{ count: sieImportCount },
|
|
{ count: staleUncategorizedCount },
|
|
{ count: uncategorizedCount },
|
|
] = await Promise.all([
|
|
supabase.from('profiles').select('full_name').eq('id', user.id).single(),
|
|
supabase.from('company_settings').select('*').eq('company_id', companyId).single(),
|
|
supabase.from('customers').select('*', { count: 'exact', head: true }).eq('company_id', companyId),
|
|
supabase.from('invoices').select('*', { count: 'exact', head: true }).eq('company_id', companyId),
|
|
supabase.from('receipts').select('*', { count: 'exact', head: true }).eq('company_id', companyId),
|
|
supabase.from('transactions').select('*', { count: 'exact', head: true }).eq('company_id', companyId),
|
|
supabase.from('journal_entry_lines')
|
|
.select('account_number, debit_amount, credit_amount, journal_entry:journal_entries!inner(entry_date, status, company_id)')
|
|
.eq('journal_entry.status', 'posted')
|
|
.eq('journal_entry.company_id', companyId)
|
|
.gte('journal_entry.entry_date', startOfYearStr),
|
|
supabase.from('transactions').select('amount, amount_sek, is_business').eq('company_id', companyId).gte('date', startOfYearStr),
|
|
supabase.from('invoices').select('total, total_sek, vat_amount, vat_amount_sek, status').eq('company_id', companyId).in('status', ['sent', 'overdue']),
|
|
supabase.from('bank_connections').select('id, accounts_data, status, consent_expires, bank_name').eq('company_id', companyId).eq('status', 'active'),
|
|
supabase.from('deadlines').select('*, customer:customers(id, name)').eq('company_id', companyId).eq('is_completed', false)
|
|
.or(`due_date.lt.${today},due_date.lte.${nextWeek}`).order('due_date', { ascending: true }),
|
|
supabase.from('receipts').select('*', { count: 'exact', head: true }).eq('company_id', companyId).eq('status', 'extracted'),
|
|
supabase.from('receipts').select('*', { count: 'exact', head: true }).eq('company_id', companyId).eq('status', 'confirmed').is('matched_transaction_id', null),
|
|
supabase.from('transactions').select('*', { count: 'exact', head: true }).eq('company_id', companyId).lt('amount', 0).is('receipt_id', null),
|
|
supabase.from('journal_entries').select('*', { count: 'exact', head: true }).eq('company_id', companyId).eq('status', 'posted').in('source_type', needsDocSourceTypes),
|
|
supabase.from('document_attachments').select('journal_entry_id').eq('company_id', companyId).eq('is_current_version', true).not('journal_entry_id', 'is', null),
|
|
supabase.from('receipts').select('created_at').eq('company_id', companyId).eq('status', 'confirmed').order('created_at', { ascending: false }).limit(30),
|
|
supabase.from('sie_imports').select('*', { count: 'exact', head: true }).eq('company_id', companyId).eq('status', 'completed'),
|
|
supabase.from('transactions').select('*', { count: 'exact', head: true }).eq('company_id', companyId).is('journal_entry_id', null).not('is_business', 'eq', false).lt('date', new Date(now.getTime() - 14 * 24 * 60 * 60 * 1000).toISOString().split('T')[0]),
|
|
supabase.from('transactions').select('*', { count: 'exact', head: true }).eq('company_id', companyId).is('is_business', null),
|
|
])
|
|
|
|
const firstName = profile?.full_name?.split(' ')[0] || null
|
|
|
|
// If onboarding is not complete, redirect to onboarding
|
|
if (!settings?.onboarding_complete) {
|
|
redirect('/onboarding')
|
|
}
|
|
|
|
const onboardingProgress: OnboardingProgress = {
|
|
hasCustomers: (customerCount || 0) > 0,
|
|
hasInvoices: (invoiceCount || 0) > 0,
|
|
hasBankConnected: (transactionCount || 0) > 0,
|
|
hasSIEImport: (sieImportCount || 0) > 0,
|
|
}
|
|
|
|
// Calculate totals from journal entry lines using account classes
|
|
const calculateTotals = (lines: typeof journalLines, fromDate: string) => {
|
|
const filtered = (lines || []).filter((l) => {
|
|
const entry = l.journal_entry as unknown as { entry_date: string; status: string }
|
|
return entry.entry_date >= fromDate
|
|
})
|
|
|
|
let revenue = 0
|
|
let expenses = 0
|
|
|
|
for (const line of filtered) {
|
|
const acct = line.account_number
|
|
if (acct.startsWith('3')) {
|
|
// Revenue: class 3 — credit-normal accounts
|
|
revenue += Math.round(((line.credit_amount || 0) - (line.debit_amount || 0)) * 100) / 100
|
|
} else if (acct.startsWith('4') || acct.startsWith('5') || acct.startsWith('6') || acct.startsWith('7')) {
|
|
// Expenses: classes 4-7 — debit-normal accounts
|
|
expenses += Math.round(((line.debit_amount || 0) - (line.credit_amount || 0)) * 100) / 100
|
|
}
|
|
}
|
|
|
|
revenue = Math.round(revenue * 100) / 100
|
|
expenses = Math.round(expenses * 100) / 100
|
|
|
|
return { income: revenue, expenses, net: Math.round((revenue - expenses) * 100) / 100 }
|
|
}
|
|
|
|
const ytdTotals = calculateTotals(journalLines, startOfYearStr)
|
|
const mtdTotals = calculateTotals(journalLines, startOfMonthStr)
|
|
|
|
const uncategorizedTxns = (transactions || []).filter(
|
|
(t) => t.is_business === null
|
|
)
|
|
const uncategorizedIncome = uncategorizedTxns
|
|
.filter((t) => t.amount > 0)
|
|
.reduce((sum, t) => sum + Number(t.amount_sek || t.amount), 0)
|
|
const uncategorizedExpenses = uncategorizedTxns
|
|
.filter((t) => t.amount < 0)
|
|
.reduce((sum, t) => sum + Math.abs(Number(t.amount_sek || t.amount)), 0)
|
|
|
|
const unpaidTotal = (unpaidInvoices || []).reduce(
|
|
(sum, inv) => sum + Number(inv.total_sek || inv.total),
|
|
0
|
|
)
|
|
|
|
const unpaidVatTotal = (unpaidInvoices || []).reduce(
|
|
(sum, inv) => sum + Number(inv.vat_amount_sek || inv.vat_amount || 0),
|
|
0
|
|
)
|
|
|
|
const overdueCount = (unpaidInvoices || []).filter(
|
|
(inv) => inv.status === 'overdue'
|
|
).length
|
|
|
|
let bankBalance: number | null = null
|
|
if (bankConnections && bankConnections.length > 0) {
|
|
const allBalances = bankConnections.flatMap(conn => {
|
|
const accounts = conn.accounts_data as { balance: number }[] | null
|
|
return accounts || []
|
|
})
|
|
if (allBalances.length > 0) {
|
|
bankBalance = allBalances.reduce((sum, acc) => sum + (acc.balance || 0), 0)
|
|
}
|
|
}
|
|
|
|
const nowMs = new Date().getTime()
|
|
const expiringBankConnections = (bankConnections || [])
|
|
.filter(conn => {
|
|
if (!conn.consent_expires) return false
|
|
const daysLeft = Math.ceil(
|
|
(new Date(conn.consent_expires).getTime() - nowMs) / (1000 * 60 * 60 * 24)
|
|
)
|
|
return daysLeft > 0 && daysLeft <= 14
|
|
})
|
|
.map(conn => ({
|
|
id: conn.id as string,
|
|
bank_name: conn.bank_name as string,
|
|
days_left: Math.ceil(
|
|
(new Date(conn.consent_expires!).getTime() - nowMs) / (1000 * 60 * 60 * 24)
|
|
),
|
|
}))
|
|
|
|
const uniqueEntriesWithDocs = new Set(
|
|
(entriesWithDocs || []).map((d) => d.journal_entry_id)
|
|
).size
|
|
|
|
const missingUnderlagCount = Math.max(0, (postedEntriesCount || 0) - uniqueEntriesWithDocs)
|
|
|
|
let streakCount = 0
|
|
if (recentReceiptActivity && recentReceiptActivity.length > 0) {
|
|
const todayDate = new Date()
|
|
todayDate.setHours(0, 0, 0, 0)
|
|
|
|
const activityDates = new Set(
|
|
recentReceiptActivity.map((r) => new Date(r.created_at).toISOString().split('T')[0])
|
|
)
|
|
|
|
const checkDate = new Date(todayDate)
|
|
while (activityDates.has(checkDate.toISOString().split('T')[0])) {
|
|
streakCount++
|
|
checkDate.setDate(checkDate.getDate() - 1)
|
|
}
|
|
}
|
|
|
|
const receiptQueue: ReceiptQueueSummary = {
|
|
unmatched_receipts_count: unmatchedReceiptsCount || 0,
|
|
unmatched_transactions_count: unmatchedTransactionsCount || 0,
|
|
pending_review_count: pendingReviewCount || 0,
|
|
streak_count: streakCount,
|
|
}
|
|
|
|
return (
|
|
<DashboardContent
|
|
firstName={firstName}
|
|
companyId={companyId}
|
|
settings={settings}
|
|
summary={{
|
|
ytd: ytdTotals,
|
|
mtd: mtdTotals,
|
|
uncategorizedCount: uncategorizedCount || 0,
|
|
uncategorizedIncome,
|
|
uncategorizedExpenses,
|
|
unpaidInvoicesCount: (unpaidInvoices || []).length,
|
|
unpaidInvoicesTotal: unpaidTotal,
|
|
unpaidVatTotal,
|
|
overdueInvoicesCount: overdueCount,
|
|
bankBalance,
|
|
expiringBankConnections,
|
|
deadlines: (deadlines || []) as Deadline[],
|
|
receiptQueue,
|
|
missingUnderlagCount,
|
|
staleUncategorizedCount: staleUncategorizedCount || 0,
|
|
}}
|
|
onboardingProgress={onboardingProgress}
|
|
/>
|
|
)
|
|
}
|