398c734b93
Webhook lifecycle: GET hub.challenge handshake (constant-time verify-token compare); POST verifies X-Hub-Signature-256 over the RAW body before any parse, Zod-parses the envelope, persists inbound rows (partial-unique wamid = dedupe against Meta's up-to-7-day redelivery), acks 200 fast and defers media processing via the after() idiom. Rejected and rate-limited content always acks 200 and lands as skipped/error rows, never a retryable status. Linking: the settings panel (Installningar -> WhatsApp) mints AC- one-time codes (sha256 stored, 10 min TTL, single use, ambiguity-free alphabet); the webhook consumes the code, binds phone to user (HMAC-peppered hash + AES-256- GCM at rest) and confirms with M3. Keyword commands stopp/start/hjalp; unknown senders get one throttled M1 greeting (1/h, 3/day) behind the sender-quota RPC, with no media download and no content persistence. Intake worker: atomic claim on the message row (the durable job record), company resolution (default -> sole membership -> M6 fallback, no item), per-company inbox quota (ack-and-drop, M17 once per 10 min per sender), MIME allowlist, 10 MB stream-checked media download, exact sha256 duplicate check, then the shared uploadAndExtract funnel (source 'whatsapp', channel_context caption, whatsapp_message_id) and the M4 ack with extracted merchant/total/date. Failures wrap to 'error' + error_message + one M18. uploadAndExtract widened: source 'whatsapp', optional channelMeta + actorId; email/upload paths behaviorally unchanged. Deferred to PR4: burst debounce + combined ack (M5), in-chat company choice (M6 buttons + 8h pin), clarifying questions M7-M10, interpret-answer LLM call, sweep cron, retention cron. Co-authored-by: Jakob Wennberg <jakob.wennberg@gmail.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
87 lines
4.1 KiB
TypeScript
87 lines
4.1 KiB
TypeScript
'use client'
|
|
|
|
import { useTranslations } from 'next-intl'
|
|
import { useCompany } from '@/contexts/CompanyContext'
|
|
import { useAgentSheet } from '@/components/agent/AgentSheetProvider'
|
|
import { ENABLED_EXTENSION_IDS } from '@/lib/extensions/_generated/enabled-extensions'
|
|
|
|
export type SettingsGroupKey = 'account' | 'company' | 'accounting' | 'sales' | 'tools'
|
|
|
|
export interface SettingsNavItem {
|
|
id: string
|
|
href: string
|
|
label: string
|
|
group: SettingsGroupKey
|
|
}
|
|
|
|
export interface SettingsNavGroup {
|
|
key: SettingsGroupKey
|
|
label: string
|
|
items: SettingsNavItem[]
|
|
}
|
|
|
|
// Rail group order: personal first (Konto), then company-scoped buckets.
|
|
const GROUP_ORDER: SettingsGroupKey[] = ['account', 'company', 'accounting', 'sales', 'tools']
|
|
|
|
/**
|
|
* Single source of truth for the settings sections, their conditional
|
|
* visibility, and their grouping. Consumed by both the full-page rail and the
|
|
* routed settings modal so the two can never drift on which sections show for
|
|
* AB vs EF, sandbox, identity-verified, or enabled extensions.
|
|
*
|
|
* Visibility is derived from client context (no extra fetch): `isSandbox`
|
|
* comes from CompanyContext, identity from the agent sheet, and extension
|
|
* availability from the generated enabled-extensions set.
|
|
*/
|
|
export function useSettingsNavItems(): { items: SettingsNavItem[]; groups: SettingsNavGroup[] } {
|
|
const { company, isSandbox } = useCompany()
|
|
const { identity } = useAgentSheet()
|
|
const t = useTranslations('settings_nav')
|
|
|
|
const hasCompany = !!company
|
|
const hasBankingExtension = ENABLED_EXTENSION_IDS.has('enable-banking')
|
|
const hasMcpExtension = ENABLED_EXTENSION_IDS.has('mcp-server')
|
|
const hasWhatsAppExtension = ENABLED_EXTENSION_IDS.has('whatsapp-inbox')
|
|
|
|
// Företagsprofil (TIC-snapshot) lives under Företag; Skatteverket under Skatt;
|
|
// assistentens minne + kunskap under Assistenten; säkerhetsbackup under
|
|
// Importera/Exportera. Team stays hidden (show:false) until enabled.
|
|
const defs: Array<SettingsNavItem & { show: boolean }> = [
|
|
{ id: 'account', href: '/settings/account', label: t('account'), group: 'account', show: true },
|
|
{ id: 'billing', href: '/settings/billing', label: t('billing'), group: 'account', show: true },
|
|
{ id: 'company', href: '/settings/company', label: t('company'), group: 'company', show: hasCompany },
|
|
{ id: 'bookkeeping', href: '/settings/bookkeeping', label: t('bookkeeping'), group: 'accounting', show: hasCompany },
|
|
{ id: 'tax', href: '/settings/tax', label: t('tax'), group: 'accounting', show: hasCompany },
|
|
// Lön settings follow the sidebar: every aktiebolag, plus any company that
|
|
// has registered as an employer (pays_salaries): e.g. an enskild firma
|
|
// with staff. #782
|
|
{ id: 'salary', href: '/settings/salary', label: t('salary'), group: 'accounting', show: hasCompany && (company?.entity_type === 'aktiebolag' || !!company?.pays_salaries) },
|
|
{ id: 'invoicing', href: '/settings/invoicing', label: t('invoicing'), group: 'sales', show: hasCompany },
|
|
{ id: 'templates', href: '/settings/templates', label: t('templates'), group: 'sales', show: hasCompany },
|
|
{ id: 'banking', href: '/settings/banking', label: t('banking'), group: 'tools', show: hasCompany && !isSandbox && hasBankingExtension },
|
|
{ id: 'whatsapp', href: '/settings/whatsapp', label: t('whatsapp'), group: 'tools', show: hasCompany && !isSandbox && hasWhatsAppExtension },
|
|
{ id: 'assistant', href: '/settings/assistant', label: t('assistant'), group: 'tools', show: hasCompany && identity.isVerified },
|
|
{ id: 'api', href: '/settings/api', label: t('api'), group: 'tools', show: hasCompany && hasMcpExtension },
|
|
]
|
|
|
|
const items: SettingsNavItem[] = defs
|
|
.filter((d) => d.show)
|
|
.map(({ show: _show, ...item }) => item)
|
|
|
|
const groupLabels: Record<SettingsGroupKey, string> = {
|
|
account: t('group_account'),
|
|
company: t('group_company'),
|
|
accounting: t('group_accounting'),
|
|
sales: t('group_sales'),
|
|
tools: t('group_tools'),
|
|
}
|
|
|
|
const groups: SettingsNavGroup[] = GROUP_ORDER.map((key) => ({
|
|
key,
|
|
label: groupLabels[key],
|
|
items: items.filter((i) => i.group === key),
|
|
})).filter((g) => g.items.length > 0)
|
|
|
|
return { items, groups }
|
|
}
|