Invoicing & account-security polish bundle (#550)

* feat: invoicing & account-security polish bundle

Five independent improvements bundled to ship together:

- BankID/password lockout fix: BankID-only users could enroll MFA and
  brick themselves (Supabase requires AAL2 to change password or unenroll
  MFA, and AAL2 needs a password sign-in). New app_metadata.has_password
  flag tracks this; middleware gates /mfa/enroll behind it, /account/set-
  password is the unlock path, SecuritySettings shows a banner, and
  /api/account/password is the single write path that flips the flag.
  Backfill script for existing users.

- Swish invoice payment method: company_settings.swish + invoice_show_swish
  columns, validation in lib/api/schemas.ts (accepts 123XXXXXXX företag or
  07XXXXXXXX mobile, strips whitespace/hyphens), rendered on invoice PDFs.

- Send-reminders kill switch: per-company company_settings.send_invoice_
  reminders toggle in PdfPrintSettings/Automatisering. Reminder processor
  also tightened: positive status allowlist (sent + overdue) so terminal
  statuses can never match; skip when customer already responded via
  reminder link; race-window re-check before send.

- First-invoice logo prompt: one-shot dialog when creating the first
  invoice without a logo (issue #520). Self-limits via head-only count.

- SIE export opening-balance fallback: route IB through getOpeningBalances
  so the compute_prior_opening_balances RPC supplies #IB after multi-year
  imports where opening_balance_entry_id is intentionally NULL. Previously
  #IB silently went to zero and #UB collapsed to current-period movements.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(account-polish): address PR review feedback

- BankID-link path (extensions/general/tic/index.ts): read-merge-write
  app_metadata instead of passing { bankid_linked: true } alone.
  updateUserById REPLACES app_metadata wholesale, so the previous code
  would have wiped has_password for any user who later linked BankID,
  causing the set-password banner to (incorrectly) reappear and blocking
  the standard MFA enrollment button. The comment is now corrected.

- Middleware (lib/supabase/middleware.ts): thread inner returnTo through
  the /mfa/enroll → /account/set-password redirect so the user lands on
  their original destination after the full chain completes, not on /.

- safeReturnTo helper (lib/auth/safe-return-to.ts): replace the
  starts-with-/-but-not-// guard on mfa/enroll and set-password pages.
  The previous guard let /\evil.com and /@evil.com through. The new
  helper parses against a synthetic base origin and verifies it matches.

- set-password page (app/(auth)/account/set-password/page.tsx): remove
  CLAUDE.md design system violations — bg-gradient-to-b on page bg,
  inline shadow-md style on the card, space-y-5, font-medium on the h1,
  rounded-xl on the card. Flat surface, hairline border, font-display
  h1 per the design tokens.

- Swish dedup (lib/payments/swish.ts): extract normaliseSwish() and
  isValidSwish() helpers and use them in lib/api/schemas.ts,
  components/settings/BankDetailsForm.tsx, and the invoicing settings
  page. Single source of truth for the regex.

- Password route (app/api/account/password/route.ts): emit a structured
  success log so the audit pipeline can detect password-set events, not
  just failures.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-05-21 16:44:09 +02:00
committed by GitHub
co-authored by Claude Opus 4.7
parent 2879f6ed17
commit cc351158f8
32 changed files with 1327 additions and 76 deletions
+42
View File
@@ -1129,6 +1129,48 @@ describe('UpdateSettingsSchema', () => {
const result = UpdateSettingsSchema.safeParse({ invoice_default_days: 30.5 })
expect(result.success).toBe(false)
})
describe('swish', () => {
it('accepts a Swish-företag number (123XXXXXXX)', () => {
const result = UpdateSettingsSchema.safeParse({ swish: '1234567890' })
expect(result.success).toBe(true)
if (result.success) expect(result.data.swish).toBe('1234567890')
})
it('accepts a Swedish mobile number (07XXXXXXXX)', () => {
const result = UpdateSettingsSchema.safeParse({ swish: '0701234567' })
expect(result.success).toBe(true)
if (result.success) expect(result.data.swish).toBe('0701234567')
})
it('strips whitespace and hyphens before validating', () => {
const result = UpdateSettingsSchema.safeParse({ swish: '123 456 78 90' })
expect(result.success).toBe(true)
if (result.success) expect(result.data.swish).toBe('1234567890')
})
it('rejects a non-Swish-företag, non-mobile number', () => {
const result = UpdateSettingsSchema.safeParse({ swish: '0123456789' })
expect(result.success).toBe(false)
})
it('accepts empty string for clearing the value', () => {
const result = UpdateSettingsSchema.safeParse({ swish: '' })
expect(result.success).toBe(true)
})
it('accepts invoice_show_swish toggle', () => {
const result = UpdateSettingsSchema.safeParse({ invoice_show_swish: false })
expect(result.success).toBe(true)
})
})
describe('send_invoice_reminders', () => {
it('accepts the kill-switch toggle', () => {
const result = UpdateSettingsSchema.safeParse({ send_invoice_reminders: false })
expect(result.success).toBe(true)
})
})
})
// ============================================================
+14
View File
@@ -1,4 +1,5 @@
import { z } from 'zod'
import { normaliseSwish, isValidSwish } from '@/lib/payments/swish'
// ============================================================
// Shared primitives
@@ -481,6 +482,16 @@ export const UpdateSettingsSchema = z.object({
account_number: z.string().regex(/^\d{6,12}$/, 'Kontonummer måste vara 6-12 siffror').optional().or(z.literal('')),
bankgiro: z.string().regex(/^(\d{3,4}-\d{4}|\d{7,8})$/, 'Ogiltigt bankgironummer (7-8 siffror)').nullable().optional().or(z.literal('')),
plusgiro: z.string().regex(/^\d{1,7}-\d{1}$/, 'Ogiltigt plusgironummer').nullable().optional().or(z.literal('')),
swish: z.string()
.transform(normaliseSwish)
.pipe(
z.string().refine(
isValidSwish,
'Ogiltigt Swish-nummer (företagsnummer 123XXXXXXX eller mobilnummer 07XXXXXXXX)',
),
)
.nullable()
.optional(),
iban: z.string().optional(),
bic: z.string().optional(),
accounting_method: AccountingMethodSchema.optional(),
@@ -503,11 +514,14 @@ export const UpdateSettingsSchema = z.object({
invoice_show_ocr: z.boolean().optional(),
invoice_show_bankgiro: z.boolean().optional(),
invoice_show_plusgiro: z.boolean().optional(),
invoice_show_swish: z.boolean().optional(),
invoice_show_logo: z.boolean().optional(),
invoice_show_company_name: z.boolean().optional(),
invoice_company_name_position: z.enum(['header', 'footer']).optional(),
invoice_late_fee_text: z.string().nullable().optional(),
invoice_credit_terms_text: z.string().nullable().optional(),
// Automation
send_invoice_reminders: z.boolean().optional(),
// AI agent flow
ai_flow_enabled: z.boolean().optional(),
// Salary payment file