fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s

Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
This commit is contained in:
2026-11-10 09:37:22 +01:00
parent 077251ac94
commit e7f0070e84
6135 changed files with 1718997 additions and 4 deletions
@@ -0,0 +1,78 @@
import { describe, expect, it } from 'vitest'
import {
ALL_CURRENCIES,
articleCurrency,
listArticleCurrencies,
matchesCurrencyScope,
resolveCurrencyScope,
} from '@/lib/articles/currency-scope'
describe('articleCurrency', () => {
it('normalizes the code', () => {
expect(articleCurrency({ currency: 'eur' })).toBe('EUR')
})
it('treats a missing or blank currency as SEK', () => {
expect(articleCurrency({})).toBe('SEK')
expect(articleCurrency({ currency: null })).toBe('SEK')
expect(articleCurrency({ currency: '' })).toBe('SEK')
})
})
describe('listArticleCurrencies', () => {
it('lists each present currency once, SEK first then alphabetically', () => {
expect(
listArticleCurrencies([
{ currency: 'USD' },
{ currency: 'EUR' },
{ currency: 'SEK' },
{ currency: 'EUR' },
{ currency: 'NOK' },
]),
).toEqual(['SEK', 'EUR', 'NOK', 'USD'])
})
it('folds legacy blank rows into SEK', () => {
expect(listArticleCurrencies([{ currency: null }, { currency: 'sek' }])).toEqual(['SEK'])
})
it('is empty for an empty register, so the picker can stay hidden', () => {
expect(listArticleCurrencies([])).toEqual([])
})
})
describe('resolveCurrencyScope', () => {
const available = ['SEK', 'EUR']
it('defaults to no scope', () => {
expect(resolveCurrencyScope(null, available)).toBe(ALL_CURRENCIES)
expect(resolveCurrencyScope(undefined, available)).toBe(ALL_CURRENCIES)
})
it('accepts a present currency, case-insensitively', () => {
expect(resolveCurrencyScope('eur', available)).toBe('EUR')
})
// A stale or hand-edited link must not empty the register: the fallback is
// "show everything", which the user can see and recover from.
it('falls back to no scope for a currency that is not in the register', () => {
expect(resolveCurrencyScope('USD', available)).toBe(ALL_CURRENCIES)
expect(resolveCurrencyScope('nonsense', available)).toBe(ALL_CURRENCIES)
expect(resolveCurrencyScope('EUR', [])).toBe(ALL_CURRENCIES)
})
})
describe('matchesCurrencyScope', () => {
it('keeps every article when unscoped', () => {
expect(matchesCurrencyScope({ currency: 'USD' }, ALL_CURRENCIES)).toBe(true)
})
it('keeps only the scoped currency', () => {
expect(matchesCurrencyScope({ currency: 'EUR' }, 'EUR')).toBe(true)
expect(matchesCurrencyScope({ currency: 'USD' }, 'EUR')).toBe(false)
})
it('matches legacy blank rows under SEK', () => {
expect(matchesCurrencyScope({ currency: null }, 'SEK')).toBe(true)
})
})
+71
View File
@@ -0,0 +1,71 @@
import { describe, it, expect } from 'vitest'
import { compareArticles, sortArticles } from '@/lib/articles/sort'
describe('sortArticles', () => {
it('orders by article number numerically, not alphabetically', () => {
const sorted = sortArticles([
{ article_number: '10', name: 'Tio' },
{ article_number: '2', name: 'Två' },
{ article_number: '1', name: 'Ett' },
])
expect(sorted.map((a) => a.article_number)).toEqual(['1', '2', '10'])
})
it('reproduces issue #1053: numbered register no longer sorts by name', () => {
const sorted = sortArticles([
{ article_number: '2', name: 'Delbehandling' },
{ article_number: '4', name: 'Fotvårdsremiss 85+' },
{ article_number: '5', name: 'Fotvårdsremiss ej frikort' },
{ article_number: '3', name: 'Fotvårdsremiss frikort' },
{ article_number: '1', name: 'Medicinsk fotvård' },
])
expect(sorted.map((a) => a.article_number)).toEqual(['1', '2', '3', '4', '5'])
})
it('puts articles without a number last, sorted by name', () => {
const sorted = sortArticles([
{ article_number: null, name: 'Zeta' },
{ article_number: '7', name: 'Sju' },
{ article_number: null, name: 'Alfa' },
{ article_number: undefined, name: 'Beta' },
])
expect(sorted.map((a) => a.name)).toEqual(['Sju', 'Alfa', 'Beta', 'Zeta'])
})
it('treats blank article numbers as unnumbered', () => {
const sorted = sortArticles([
{ article_number: ' ', name: 'Blank' },
{ article_number: '1', name: 'Ett' },
])
expect(sorted.map((a) => a.name)).toEqual(['Ett', 'Blank'])
})
it('breaks ties on equal numbers by name', () => {
const sorted = sortArticles([
{ article_number: '1', name: 'B' },
{ article_number: '1', name: 'A' },
])
expect(sorted.map((a) => a.name)).toEqual(['A', 'B'])
})
it('handles alphanumeric numbers with embedded digits', () => {
const sorted = sortArticles([
{ article_number: 'A10', name: 'x' },
{ article_number: 'A2', name: 'y' },
])
expect(sorted.map((a) => a.article_number)).toEqual(['A2', 'A10'])
})
it('does not mutate the input array', () => {
const input = [
{ article_number: '2', name: 'b' },
{ article_number: '1', name: 'a' },
]
sortArticles(input)
expect(input.map((a) => a.article_number)).toEqual(['2', '1'])
})
it('compareArticles is exported for column sorters', () => {
expect(compareArticles({ article_number: '2', name: 'x' }, { article_number: '10', name: 'y' })).toBeLessThan(0)
})
})
+65
View File
@@ -0,0 +1,65 @@
/**
* Currency scoping for the article register (#1189).
*
* Article prices became first-class in non-SEK currencies (#1166), so a mixed
* register needs a way to look at one currency at a time. The scope is offered
* as a ContextPicker chip and lives in the URL, which means it arrives as
* untrusted text: everything here treats an unknown or stale code as "no
* scope" rather than as a filter that hides every row.
*/
/** Sentinel for "no currency scope". Kept out of the URL when it is the value. */
export const ALL_CURRENCIES = 'all'
/**
* The register's own default. Rows written before multi-currency support, and
* any row with a blank currency, are SEK: pricing in Sweden without saying so
* means kronor.
*/
export const DEFAULT_CURRENCY = 'SEK'
interface CurrencyBearingArticle {
currency?: string | null
}
/** Normalized currency code of one article. */
export function articleCurrency(article: CurrencyBearingArticle): string {
return (article.currency || DEFAULT_CURRENCY).toUpperCase()
}
/**
* The currencies actually present in the register, SEK first and the rest
* alphabetically. Only these may be offered: a picker listing currencies
* nobody priced anything in would filter to an empty table.
*/
export function listArticleCurrencies(articles: CurrencyBearingArticle[]): string[] {
const present = new Set(articles.map(articleCurrency))
return [...present].sort((a, b) => {
if (a === DEFAULT_CURRENCY) return -1
if (b === DEFAULT_CURRENCY) return 1
return a.localeCompare(b)
})
}
/**
* Resolve the scope to apply from the URL parameter. A code that is not in the
* register (last EUR article deleted, hand-edited or shared link) falls back to
* ALL_CURRENCIES: showing everything is recoverable, showing nothing reads as
* data loss.
*/
export function resolveCurrencyScope(
param: string | null | undefined,
availableCurrencies: string[],
): string {
if (!param) return ALL_CURRENCIES
const normalized = param.toUpperCase()
return availableCurrencies.includes(normalized) ? normalized : ALL_CURRENCIES
}
/** True when the article belongs in the given scope. */
export function matchesCurrencyScope(
article: CurrencyBearingArticle,
scope: string,
): boolean {
return scope === ALL_CURRENCIES || articleCurrency(article) === scope
}
+30
View File
@@ -0,0 +1,30 @@
import type { SupabaseClient } from '@supabase/supabase-js'
/**
* Assign an article number to an article row via the generate_article_number
* RPC. Idempotent: if the row already has a number, the RPC returns it unchanged
* without consuming a sequence number. Concurrency is handled inside the RPC via
* a row lock on the article plus an atomic counter on company_settings: two
* callers racing on the same article both return the same number and the counter
* advances by exactly one.
*
* Mirrors lib/invoices/ensure-invoice-number.ts. Unlike invoice numbers, article
* numbers are master data and carry no BFL sequence/immutability obligation, so
* a gap is harmless.
*/
export async function ensureArticleNumber(
supabase: SupabaseClient,
companyId: string,
articleId: string,
): Promise<string> {
const { data, error } = await supabase.rpc('generate_article_number', {
p_company_id: companyId,
p_article_id: articleId,
})
if (error || !data) {
throw new Error(`Failed to assign article number: ${error?.message ?? 'no value returned'}`)
}
return data as string
}
+31
View File
@@ -0,0 +1,31 @@
/**
* Canonical ordering for article lists and pickers: by article number with
* numeric-aware comparison ('2' before '10'), articles without a number last,
* name as tie-breaker. Users number their articles to control this order
* (issue #1053), so a plain string sort ('10' < '2') or name sort breaks it.
*/
interface SortableArticle {
article_number?: string | null
name: string
}
const numberCollator = new Intl.Collator('sv', { numeric: true, sensitivity: 'base' })
export function compareArticles(a: SortableArticle, b: SortableArticle): number {
const aNum = a.article_number?.trim()
const bNum = b.article_number?.trim()
if (aNum && bNum) {
const byNumber = numberCollator.compare(aNum, bNum)
if (byNumber !== 0) return byNumber
} else if (aNum) {
return -1
} else if (bNum) {
return 1
}
return numberCollator.compare(a.name, b.name)
}
export function sortArticles<T extends SortableArticle>(articles: T[]): T[] {
return [...articles].sort(compareArticles)
}
+70
View File
@@ -0,0 +1,70 @@
import type { SupabaseClient } from '@supabase/supabase-js'
import { getBASReference } from '@/lib/bookkeeping/bas-reference'
/**
* Classify a per-article invoice posting-account override against the company's chart.
*
* - 'ok' : active class 1-3 account in the chart; accept as-is.
* - 'activatable' : a class 1-3 account that is merely missing/inactive: either
* an inactive chart row or a known BAS class 1-3 number not yet
* in the chart. Routes translate this to ACCOUNTS_NOT_IN_CHART
* so the standard activate-and-retry dialog flow applies
* (same UX as the journal entry form).
* - 'invalid' : anything else: an account outside classes 1-3 or a number unknown to
* both the chart and the BAS catalogue. Never bookable.
*
* Throws on an unexpected DB error so the route wrapper maps it to the canonical
* envelope.
*/
export type RevenueAccountStatus = 'ok' | 'activatable' | 'invalid'
export async function checkRevenueAccount(
supabase: SupabaseClient,
companyId: string,
account: string,
): Promise<RevenueAccountStatus> {
const { data, error } = await supabase
.from('chart_of_accounts')
.select('account_class, is_active')
.eq('company_id', companyId)
.eq('account_number', account)
.maybeSingle()
if (error) throw error
if (data) {
if (data.account_class < 1 || data.account_class > 3) return 'invalid'
return data.is_active ? 'ok' : 'activatable'
}
const ref = getBASReference(account)
return ref && ref.account_class >= 1 && ref.account_class <= 3 ? 'activatable' : 'invalid'
}
/**
* True when `account` exists in the company's chart of accounts as an ACTIVE
* class 1-3 account. Used to guard the optional per-article posting-account
* override so a typo or an unsuitable account can never be
* pinned to an article (and later booked). Never trust the client.
*
* Throws on an unexpected DB error so the route wrapper maps it to the canonical
* envelope; a simple "account not found" resolves to `false`, not an error.
*/
export async function isValidRevenueAccount(
supabase: SupabaseClient,
companyId: string,
account: string,
): Promise<boolean> {
const { data, error } = await supabase
.from('chart_of_accounts')
.select('account_number')
.eq('company_id', companyId)
.gte('account_class', 1)
.lte('account_class', 3)
.eq('is_active', true)
.eq('account_number', account)
.maybeSingle()
if (error) throw error
return !!data
}