* feat: add Accounted MCP namespace * fix(bookkeeping): stop flagging verifikat whose underlag lives on a referenced supplier invoice The missing-underlag surfaces only accepted a document directly linked to the entry, so payment verifikat for supplier invoices (doc on the registration entry per design) and entries whose doc was pinned to the bank transaction before matching were falsely flagged; opening the entry showed the referenced doc and cleared the warning client-side, and it came back on reload. - verifikat_without_documents + transactions_without_documents now treat an entry as covered when a supplier invoice referencing it (registration or payment FK, or a supplier_invoice_payments row) carries a document anchored to a journal entry (BFL 5 kap 7 paragraf hänvisning till underlag; anchoring required because the WORM deletion guards key on document_attachments.journal_entry_id) - match-supplier-invoice routes (dashboard + v1) propagate the transaction's pinned document onto the payment verifikat, mirroring the categorize route; migration backfills rows already written (open unlocked periods, company-guarded, never steals a linked doc) - /api/documents/counts, the transactions-page badges, the bulk "Inget underlag krävs" count and the push-notification scheduler share the same reference-aware predicate, so every surface agrees with the RPC - counts route validates journal_entry_ids as UUIDs (they are interpolated into a PostgREST or-filter) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(transactions): align table columns flush with page edges Collapse the checkbox gutter column to zero width and hang the hover-revealed checkbox/expand chevron in the page margins, drop the outer padding so DATUM sits flush left and STATUS flush right, and tuck the overflow-menu dots under the middle of the STATUS header. Applied to both the inbox and history tables so they stay identical. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(arsredovisning): tie anlaggningstillgangar note to booked depreciation The ARL 5:8 roll-forward note recomputed depreciation from its own day-based linear formula (365.25/12 month length, non-inclusive day count, linear only), drifting ~20 kr per year per asset from the ledger-driven resultat- and balansrakning and misstating non-linear methods entirely. Note figures now come from posted depreciation_schedules rows (the same source disposeAsset reverses), falling back to the engine's computeAnnualDepreciation when nothing is posted; pre-onboarding opening balances iterate prior years through the engine. Adds a note-vs-trial-balance tie-out warning (accounts 1000-1299, over 1 kr) surfaced before download. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * refactor(stripe): move connect and sync surface from settings to import page Stripe's transaction feed is a continuous import source in the same category as the PSD2 bank connection, so its connect/sync surface now lives on the import page as a source card (mode=stripe), gated "kommer snart" on hosted like before; self-hosted keeps the full panel. - Import page: Stripe card after Koppla bank, renders the existing StripeSettingsPanel via the settings-panel registry - OAuth callback and panel cleanup return to /import?mode=stripe - Settings > Betalningar retired: nav item removed, route redirects, PaymentsSettingsContent deleted, legacy ?tab=payments mapped - New import.stripe_* strings in sv+en; dead settings_nav.payments removed Crons and sync logic unchanged; payment-link settings stay in the invoicing section. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(underlag): paginate missing-underlag cron and harden doc-surface queries Resolve PR review findings on bug/invalid-imports: - notification-scheduler: fetchAllRows on all 5 global reads; past 1000 rows the capped reads produced false "saknade underlag" notifications - bulk-missing: LOOKUP_CHUNK 300->150 so the twice-embedded .or() id list stays under the PostgREST URL limit - bulk-missing + transactions page: UUID-guard the .or()-interpolated id lists, matching documents/counts - match-supplier-invoice (dashboard + v1): log documentId/journalEntryId on the non-fatal doc-link warning - well-known/oauth-protected-resource: document the tool_namespace allow-list - messages/en: reword stripe_description - DECISIONS.md: record the asset ibAck tie-out and Tailwind !important calls Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(tic): convert registrationDate from Unix seconds to millisecond epoch in lookup and profile tests --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
284 lines
9.9 KiB
TypeScript
284 lines
9.9 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
|
|
// Mock the tic-client functions. /lookup now only calls
|
|
// searchCompanyByOrgNumber: the Phase 2 fetchers stay mocked because the
|
|
// /profile handler shares the module, but we don't expect /lookup to invoke
|
|
// them.
|
|
vi.mock('../lib/tic-client', () => ({
|
|
searchCompanyByOrgNumber: vi.fn(),
|
|
getBankAccounts: vi.fn(),
|
|
getIndustryCodes: vi.fn(),
|
|
getEmails: vi.fn(),
|
|
getPhones: vi.fn(),
|
|
getFiscalYears: vi.fn(),
|
|
}))
|
|
|
|
import { ticExtension } from '../index'
|
|
import {
|
|
searchCompanyByOrgNumber,
|
|
getBankAccounts,
|
|
getIndustryCodes,
|
|
getEmails,
|
|
getPhones,
|
|
getFiscalYears,
|
|
} from '../lib/tic-client'
|
|
import { TICAPIError } from '../lib/tic-types'
|
|
import type { TICCompanyDocument } from '../lib/tic-types'
|
|
|
|
const mockSearch = vi.mocked(searchCompanyByOrgNumber)
|
|
const mockBank = vi.mocked(getBankAccounts)
|
|
const mockIndustries = vi.mocked(getIndustryCodes)
|
|
const mockEmails = vi.mocked(getEmails)
|
|
const mockPhones = vi.mocked(getPhones)
|
|
const mockFiscalYears = vi.mocked(getFiscalYears)
|
|
|
|
function makeRequest(orgNumber?: string): Request {
|
|
const url = orgNumber
|
|
? `http://localhost/api/extensions/ext/tic/lookup?org_number=${encodeURIComponent(orgNumber)}`
|
|
: 'http://localhost/api/extensions/ext/tic/lookup'
|
|
return new Request(url)
|
|
}
|
|
|
|
const lookupHandler = ticExtension.apiRoutes![0].handler
|
|
|
|
// Search-public document now carries everything /lookup needs at the top
|
|
// level: sniCodes, bankAccounts, emailAddresses, phoneNumbers,
|
|
// mostRecentFinancialSummary. The previous Phase 2 fan-out duplicated these.
|
|
const mockDoc: TICCompanyDocument = {
|
|
companyId: 42,
|
|
registrationNumber: '5560360793',
|
|
names: [
|
|
{ nameOrIdentifier: 'Registered Name', companyNamingType: 'registeredName' },
|
|
{ nameOrIdentifier: 'Test AB', companyNamingType: 'name' },
|
|
],
|
|
legalEntityType: 'AB',
|
|
// 2026-02-02 in Unix seconds (TIC's native unit; the route converts to ms)
|
|
registrationDate: Math.floor(Date.UTC(2026, 1, 2) / 1000),
|
|
mostRecentRegisteredAddress: {
|
|
streetAddress: 'Storgatan 1',
|
|
postalCode: '111 22',
|
|
city: 'Stockholm',
|
|
},
|
|
isRegisteredForFTax: true,
|
|
isRegisteredForVAT: true,
|
|
isCeased: false,
|
|
activityStatus: 'isActive',
|
|
sniCodes: [
|
|
{ rank: 1, sni_2007Code: '62010', sni_2007Name: 'Dataprogrammering', sni_2007Section: 'J' },
|
|
],
|
|
bankAccounts: [{ accountNumber: '1234567', bankAccountType: 'bankgiro' }],
|
|
emailAddresses: [{ emailAddress: 'info@test.se', emailAddressType: 'general' }],
|
|
phoneNumbers: [{ phoneNumberFormatted: '08-1234567', e164PhoneNumber: '+4681234567' }],
|
|
mostRecentFinancialSummary: {
|
|
// 2024-01-01 → 2024-12-31 (Unix seconds, UTC)
|
|
periodStart: Math.floor(Date.UTC(2024, 0, 1) / 1000),
|
|
periodEnd: Math.floor(Date.UTC(2024, 11, 31) / 1000),
|
|
},
|
|
}
|
|
|
|
describe('TIC lookup route', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('returns 400 when org_number is missing', async () => {
|
|
const res = await lookupHandler(makeRequest())
|
|
expect(res.status).toBe(400)
|
|
})
|
|
|
|
it('returns 404 when company not found', async () => {
|
|
mockSearch.mockResolvedValue(null)
|
|
|
|
const res = await lookupHandler(makeRequest('000000-0000'))
|
|
expect(res.status).toBe(404)
|
|
})
|
|
|
|
it('returns full lookup result from the search doc alone', async () => {
|
|
mockSearch.mockResolvedValue(mockDoc)
|
|
|
|
const res = await lookupHandler(makeRequest('556036-0793'))
|
|
expect(res.status).toBe(200)
|
|
|
|
const { data } = await res.json()
|
|
expect(data.companyName).toBe('Test AB')
|
|
expect(data.isCeased).toBe(false)
|
|
expect(data.address).toEqual({
|
|
street: 'Storgatan 1',
|
|
postalCode: '111 22',
|
|
city: 'Stockholm',
|
|
})
|
|
expect(data.registration).toEqual({ fTax: true, vat: true })
|
|
expect(data.bankAccounts).toEqual([
|
|
{ type: 'bankgiro', accountNumber: '1234567', bic: null },
|
|
])
|
|
expect(data.sniCodes).toEqual([{ code: '62010', name: 'Dataprogrammering' }])
|
|
expect(data.email).toBe('info@test.se')
|
|
expect(data.phone).toBe('08-1234567')
|
|
expect(data.fiscalYear).toEqual({ startMonthDay: '01-01', endMonthDay: '12-31' })
|
|
expect(data.registrationDate).toBe(Date.UTC(2026, 1, 2))
|
|
})
|
|
|
|
it('converts registrationDate from Unix seconds to a millisecond epoch', async () => {
|
|
mockSearch.mockResolvedValue(mockDoc)
|
|
|
|
const res = await lookupHandler(makeRequest('556036-0793'))
|
|
const { data } = await res.json()
|
|
// Regression: fed raw seconds into `new Date()`, a 2026 registration
|
|
// rendered as 1970-01-21 in onboarding's fiscal-year step.
|
|
expect(new Date(data.registrationDate).toISOString().slice(0, 10)).toBe('2026-02-02')
|
|
})
|
|
|
|
it('returns registrationDate null when the doc lacks one', async () => {
|
|
mockSearch.mockResolvedValue({
|
|
...mockDoc,
|
|
registrationDate: undefined as unknown as number,
|
|
})
|
|
|
|
const res = await lookupHandler(makeRequest('556036-0793'))
|
|
const { data } = await res.json()
|
|
expect(data.registrationDate).toBeNull()
|
|
})
|
|
|
|
it('does NOT fan out to Phase 2 endpoints', async () => {
|
|
mockSearch.mockResolvedValue(mockDoc)
|
|
await lookupHandler(makeRequest('556036-0793'))
|
|
expect(mockBank).not.toHaveBeenCalled()
|
|
expect(mockIndustries).not.toHaveBeenCalled()
|
|
expect(mockEmails).not.toHaveBeenCalled()
|
|
expect(mockPhones).not.toHaveBeenCalled()
|
|
expect(mockFiscalYears).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('derives fiscal year MM-DD from mostRecentFinancialSummary', async () => {
|
|
mockSearch.mockResolvedValue({
|
|
...mockDoc,
|
|
mostRecentFinancialSummary: {
|
|
// 2024-07-01 → 2025-06-30 (broken fiscal year)
|
|
periodStart: Math.floor(Date.UTC(2024, 6, 1) / 1000),
|
|
periodEnd: Math.floor(Date.UTC(2025, 5, 30) / 1000),
|
|
},
|
|
})
|
|
|
|
const res = await lookupHandler(makeRequest('556036-0793'))
|
|
const { data } = await res.json()
|
|
expect(data.fiscalYear).toEqual({ startMonthDay: '07-01', endMonthDay: '06-30' })
|
|
})
|
|
|
|
it('returns fiscalYear null when the company has no closed period yet', async () => {
|
|
mockSearch.mockResolvedValue({ ...mockDoc, mostRecentFinancialSummary: undefined })
|
|
|
|
const res = await lookupHandler(makeRequest('556036-0793'))
|
|
const { data } = await res.json()
|
|
expect(data.fiscalYear).toBeNull()
|
|
})
|
|
|
|
it('filters non-bankgiro entries from doc.bankAccounts', async () => {
|
|
mockSearch.mockResolvedValue({
|
|
...mockDoc,
|
|
bankAccounts: [
|
|
{ accountNumber: '1234567', bankAccountType: 'bankgiro' },
|
|
{ accountNumber: 'SE45 5000', bankAccountType: 'iban' },
|
|
],
|
|
})
|
|
|
|
const res = await lookupHandler(makeRequest('556036-0793'))
|
|
const { data } = await res.json()
|
|
expect(data.bankAccounts).toEqual([
|
|
{ type: 'bankgiro', accountNumber: '1234567', bic: null },
|
|
])
|
|
})
|
|
|
|
it('falls back to e164 when phoneNumberFormatted is missing', async () => {
|
|
mockSearch.mockResolvedValue({
|
|
...mockDoc,
|
|
phoneNumbers: [{ e164PhoneNumber: '+4681234567' }],
|
|
})
|
|
|
|
const res = await lookupHandler(makeRequest('556036-0793'))
|
|
const { data } = await res.json()
|
|
expect(data.phone).toBe('+4681234567')
|
|
})
|
|
|
|
it('prefers name type over other naming types', async () => {
|
|
mockSearch.mockResolvedValue(mockDoc)
|
|
|
|
const res = await lookupHandler(makeRequest('556036-0793'))
|
|
const { data } = await res.json()
|
|
expect(data.companyName).toBe('Test AB')
|
|
})
|
|
|
|
it('handles missing optional fields gracefully', async () => {
|
|
mockSearch.mockResolvedValue({
|
|
...mockDoc,
|
|
sniCodes: undefined,
|
|
bankAccounts: undefined,
|
|
emailAddresses: undefined,
|
|
phoneNumbers: undefined,
|
|
})
|
|
|
|
const res = await lookupHandler(makeRequest('556036-0793'))
|
|
expect(res.status).toBe(200)
|
|
|
|
const { data } = await res.json()
|
|
expect(data.companyName).toBe('Test AB')
|
|
expect(data.bankAccounts).toEqual([])
|
|
expect(data.sniCodes).toEqual([])
|
|
expect(data.email).toBeNull()
|
|
expect(data.phone).toBeNull()
|
|
})
|
|
|
|
it('detects ceased companies via isCeased boolean', async () => {
|
|
mockSearch.mockResolvedValue({ ...mockDoc, isCeased: true, activityStatus: 'isNoLongerActive' })
|
|
|
|
const res = await lookupHandler(makeRequest('556036-0793'))
|
|
const { data } = await res.json()
|
|
expect(data.isCeased).toBe(true)
|
|
})
|
|
|
|
it('returns 503 when TIC is not configured', async () => {
|
|
mockSearch.mockRejectedValue(
|
|
new TICAPIError('TIC_API_PROXY_URL is not configured', undefined, 'NOT_CONFIGURED')
|
|
)
|
|
const res = await lookupHandler(makeRequest('556036-0793'))
|
|
expect(res.status).toBe(503)
|
|
})
|
|
|
|
it('returns 429 when TIC rate-limits us', async () => {
|
|
mockSearch.mockRejectedValue(
|
|
new TICAPIError('Rate limit exceeded', 429, 'RATE_LIMIT_EXCEEDED')
|
|
)
|
|
const res = await lookupHandler(makeRequest('556036-0793'))
|
|
expect(res.status).toBe(429)
|
|
})
|
|
|
|
it('returns 504 when TIC times out', async () => {
|
|
mockSearch.mockRejectedValue(new TICAPIError('Request timeout', undefined, 'TIMEOUT'))
|
|
const res = await lookupHandler(makeRequest('556036-0793'))
|
|
expect(res.status).toBe(504)
|
|
})
|
|
|
|
it('returns 400 when upstream rejects the org number (4xx)', async () => {
|
|
mockSearch.mockRejectedValue(new TICAPIError('TIC API error: Bad Request', 400))
|
|
const res = await lookupHandler(makeRequest('1234567-1234'))
|
|
expect(res.status).toBe(400)
|
|
})
|
|
|
|
it('returns 502 when upstream returns 5xx', async () => {
|
|
mockSearch.mockRejectedValue(new TICAPIError('TIC API error: Bad Gateway', 502))
|
|
const res = await lookupHandler(makeRequest('556036-0793'))
|
|
expect(res.status).toBe(502)
|
|
})
|
|
|
|
it('returns 502 when fetch fails (network error)', async () => {
|
|
mockSearch.mockRejectedValue(new TICAPIError('Failed to fetch from TIC: ECONNRESET'))
|
|
const res = await lookupHandler(makeRequest('556036-0793'))
|
|
expect(res.status).toBe(502)
|
|
})
|
|
|
|
it('returns 500 for non-TICAPIError unexpected errors', async () => {
|
|
mockSearch.mockRejectedValue(new Error('boom'))
|
|
const res = await lookupHandler(makeRequest('556036-0793'))
|
|
expect(res.status).toBe(500)
|
|
})
|
|
})
|