* fix(payroll): expose jämkning percentage and validity on the employee tax form (#1913) An employee with a Skatteverket jämkning decision could not have the adjusted withholding percentage set anywhere in the app: model, API and engine supported jamkning_percentage / jamkning_valid_from / jamkning_valid_to end to end, but EmployeeTaxCard never exposed them. - EmployeeTaxCard: percentage input plus required from/to dates in the A-skatt branch; null (= clear the beslut) when emptied or when no table applies, mirroring tax_table_number. Both dates are required because isJamkningValid only applies a beslut when both are set. - Edit page: PATCH body sends the three fields as explicit values (guarded on the card having reported), card initial seeded from the employee, read-only Jämkning row in the tax section. - NewEmployeeDialog: initial tax state and POST body carry the fields. - Legacy PATCH /api/salary/employees/[id]: merged-state jämkning check (start date required, dates ordered), same rule and messages as v1 and employee-commands, gated on the PATCH touching a jamkning key. - lib/api/schemas.ts: truthful comment on the engine's both-dates gate. - i18n: salary_employee.tax_jamkning_* in sv and en. - Tests on the legacy PATCH route (400 x4, 200 x3) and the POST route. The engine is deliberately untouched; the API/MCP contract (valid_to optional) stays as is, follow-up filed in the PR body. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FkUfWtuFCUkNtRAgMQCse2 * fix(payroll): jämkning keys reach the employee PATCH only when visible and edited (#1913) Review findings on #1988: the card reported null for the three jämkning fields whenever its inputs were hidden (sidoinkomst, F-skatt, FA-skatt, ej verifierad) and the edit page forwarded those nulls, so toggling sidoinkomst or fixing a phone number on an FA-skatt employee silently wiped a stored beslut (which the engine still applies for FA-skatt). The two date inputs were also natively required whenever a percentage was present, so a beslut stored via the API/MCP without valid_to (allowed by the schema) blocked the whole form on unrelated edits. - lib/salary/jamkning-patch.ts (new): isJamkningEditable() and jamkningPatch(); the keys are spread into the PATCH body with explicit values (null = clear) only when the inputs were visible and edited, otherwise omitted like every other sparse field. - EmployeeTaxCard: jamkning_touched flag on EmployeeTaxValue, set by the three handlers; required on both dates gated on it; non-blocking hint (tax_jamkning_incomplete_hint, sv + en) on a seeded beslut missing a date. - Edit page spreads jamkningPatch(tax); NewEmployeeDialog initial state carries the flag. - Tests: lib/salary/__tests__/jamkning-patch.test.ts (keys omitted for sidoinkomst / f_skatt / fa_skatt / not_verified / untouched seeded row, explicit nulls when cleared, spread shape). - DECISIONS.md: one line. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FkUfWtuFCUkNtRAgMQCse2 * test(payroll): type the insert mock's payload so the typecheck ratchet accepts the jamkning tests vi.fn(() => ...) infers an empty parameter tuple, so insert.mock.calls[0][0] failed TS2493 under the new check:types gate (#1980) on CI. Declaring the payload parameter keeps the assertions and makes the tuple indexable. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FkUfWtuFCUkNtRAgMQCse2 --------- Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
236 lines
8.6 KiB
TypeScript
236 lines
8.6 KiB
TypeScript
/**
|
|
* Regression test for the plaintext-personnummer crash, plus auth wiring.
|
|
*
|
|
* GET /api/salary/employees decrypts every employee's personnummer on read and
|
|
* maps over the whole roster. A row whose personnummer was stored UNENCRYPTED
|
|
* (a pre-fix v1 REST create, or a seed) used to throw
|
|
* ERR_CRYPTO_INVALID_AUTH_TAG ("Invalid authentication tag length: 6") inside
|
|
* the .map(), 500-ing the entire endpoint for the affected company. The decrypt
|
|
* helper now passes a raw 12-digit value through unchanged, so a mixed
|
|
* encrypted/plaintext table no longer takes the roster down.
|
|
*
|
|
* The route now runs through the withRouteContext wrapper, so we mock its
|
|
* auth/company/write dependencies and inject the Supabase client via requireAuth.
|
|
*/
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { NextResponse } from 'next/server'
|
|
|
|
vi.mock('@/lib/init', () => ({ ensureInitialized: vi.fn() }))
|
|
vi.mock('@/lib/auth/require-auth', () => ({ requireAuth: vi.fn() }))
|
|
vi.mock('@/lib/company/context', () => ({
|
|
getActiveCompanyId: vi.fn().mockResolvedValue('company-1'),
|
|
requireCompanyId: vi.fn().mockResolvedValue('company-1'),
|
|
getCompanyEntityType: vi.fn(),
|
|
}))
|
|
vi.mock('@/lib/auth/require-write', () => ({
|
|
requireWritePermission: vi.fn().mockResolvedValue({ ok: true }),
|
|
}))
|
|
|
|
import { GET, POST } from '../route'
|
|
import { requireAuth } from '@/lib/auth/require-auth'
|
|
import { encryptPersonnummer } from '@/lib/salary/personnummer'
|
|
|
|
// Synthetic 12-digit values (year 1900 / 1902, zero suffix): obviously not
|
|
// real birthdates. ISO A.5.34 / GDPR Art.5(1)(c): fixtures must not look like
|
|
// production PII.
|
|
const PLAINTEXT_PNR = '190001010000'
|
|
const ENCRYPTED_PNR = encryptPersonnummer('190203040000')
|
|
|
|
function supabaseWithRows(rows: unknown[]) {
|
|
const query: Record<string, unknown> = {}
|
|
query.select = vi.fn(() => query)
|
|
query.eq = vi.fn(() => query)
|
|
query.order = vi.fn(() => Promise.resolve({ data: rows, error: null }))
|
|
return { from: vi.fn(() => query) }
|
|
}
|
|
|
|
function authed(supabase: unknown) {
|
|
vi.mocked(requireAuth).mockResolvedValue({
|
|
user: { id: 'user-1' } as never,
|
|
supabase: supabase as never,
|
|
error: null,
|
|
} as never)
|
|
}
|
|
|
|
function req() {
|
|
return new Request('https://x.test/api/salary/employees')
|
|
}
|
|
|
|
const params = { params: Promise.resolve({}) } as never
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
describe('GET /api/salary/employees', () => {
|
|
it('returns 401 when unauthenticated', async () => {
|
|
vi.mocked(requireAuth).mockResolvedValue({
|
|
user: null,
|
|
supabase: null,
|
|
error: NextResponse.json({ error: 'Unauthorized' }, { status: 401 }),
|
|
} as never)
|
|
|
|
const res = await GET(req(), params)
|
|
expect(res.status).toBe(401)
|
|
})
|
|
|
|
it('does not 500 on a mixed plaintext + encrypted roster; masks both', async () => {
|
|
authed(
|
|
supabaseWithRows([
|
|
{ id: 'e1', last_name: 'A', personnummer: PLAINTEXT_PNR, personnummer_last4: '0000' },
|
|
{ id: 'e2', last_name: 'B', personnummer: ENCRYPTED_PNR, personnummer_last4: '0000' },
|
|
]),
|
|
)
|
|
|
|
const res = await GET(req(), params)
|
|
expect(res.status).toBe(200)
|
|
const body = await res.json()
|
|
// Both rows masked birthdate-visible, last-4 hidden.
|
|
expect(body.data[0].personnummer_masked).toBe('19000101-XXXX')
|
|
expect(body.data[1].personnummer_masked).toBe('19020304-XXXX')
|
|
// Neither the plaintext nor the stored ciphertext may leak.
|
|
expect(JSON.stringify(body)).not.toContain(PLAINTEXT_PNR)
|
|
expect(JSON.stringify(body)).not.toContain(ENCRYPTED_PNR)
|
|
})
|
|
|
|
it('never returns the mask under the writable `personnummer` key', async () => {
|
|
// The roster feeds edit forms. If the mask came back as `personnummer`, a
|
|
// client that reads a row and writes it back would post 'ÅÅÅÅMMDD-XXXX'
|
|
// into the encrypt path. The masked value lives under `personnummer_masked`
|
|
// so the read key and the write key can never be confused.
|
|
authed(supabaseWithRows([{ id: 'e1', last_name: 'A', personnummer: ENCRYPTED_PNR }]))
|
|
|
|
const res = await GET(req(), params)
|
|
const body = await res.json()
|
|
expect(body.data[0].personnummer).toBeUndefined()
|
|
expect('personnummer' in body.data[0]).toBe(false)
|
|
expect(body.data[0].personnummer_masked).toBe('19020304-XXXX')
|
|
})
|
|
|
|
it('strips personnummer_last4 so the mask cannot be reassembled', async () => {
|
|
// The mask is YYYYMMDD-XXXX. A response carrying the mask AND the last
|
|
// four digits hands the client the full personnummer by concatenation, so
|
|
// personnummer_last4 must never ride along with the roster rows.
|
|
authed(
|
|
supabaseWithRows([
|
|
{ id: 'e1', last_name: 'A', personnummer: ENCRYPTED_PNR, personnummer_last4: '0000' },
|
|
]),
|
|
)
|
|
|
|
const res = await GET(req(), params)
|
|
const body = await res.json()
|
|
expect(body.data[0]).not.toHaveProperty('personnummer_last4')
|
|
expect(body.data[0]).not.toHaveProperty('personnummer')
|
|
expect(body.data[0].personnummer_masked).toBe('19020304-XXXX')
|
|
})
|
|
})
|
|
|
|
describe('POST /api/salary/employees', () => {
|
|
// Luhn-valid synthetic personnummer (checksum verified in personnummer.test.ts).
|
|
const NEW_PNR = '199001019802'
|
|
|
|
function supabaseWithInsert(returned: Record<string, unknown>) {
|
|
const single = vi.fn(() => Promise.resolve({ data: returned, error: null }))
|
|
const select = vi.fn(() => ({ single }))
|
|
const insert = vi.fn((_payload: Record<string, unknown>) => ({ select }))
|
|
return { supabase: { from: vi.fn(() => ({ insert })) }, insert }
|
|
}
|
|
|
|
it('create response carries the mask only: no ciphertext, no last4', async () => {
|
|
const inserted = {
|
|
id: 'emp-new',
|
|
company_id: 'company-1',
|
|
first_name: 'Test',
|
|
last_name: 'Testsson',
|
|
personnummer: encryptPersonnummer(NEW_PNR),
|
|
personnummer_last4: '9802',
|
|
employment_type: 'employee',
|
|
}
|
|
const { supabase } = supabaseWithInsert(inserted)
|
|
authed(supabase)
|
|
|
|
const res = await POST(
|
|
new Request('https://x.test/api/salary/employees', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
first_name: 'Test',
|
|
last_name: 'Testsson',
|
|
personnummer: NEW_PNR,
|
|
employment_start: '2026-01-01',
|
|
monthly_salary: 30000,
|
|
tax_table_number: 34,
|
|
tax_municipality: 'Stockholm',
|
|
}),
|
|
}),
|
|
params,
|
|
)
|
|
expect(res.status).toBe(201)
|
|
const body = await res.json()
|
|
expect(body.data.personnummer_masked).toBe('19900101-XXXX')
|
|
expect(body.data).not.toHaveProperty('personnummer')
|
|
expect(body.data).not.toHaveProperty('personnummer_last4')
|
|
// Neither the full personnummer nor its suffix may appear anywhere in the
|
|
// serialized response: mask + last4 would reassemble the identity.
|
|
expect(JSON.stringify(body)).not.toContain(NEW_PNR)
|
|
expect(JSON.stringify(body)).not.toContain('9802')
|
|
})
|
|
|
|
// #1913: the NewEmployeeDialog sends the jämkning beslut on create; pin that
|
|
// the insert carries it, and that omitting it inserts nulls (no beslut).
|
|
const CREATE_BASE = {
|
|
first_name: 'Test',
|
|
last_name: 'Testsson',
|
|
personnummer: NEW_PNR,
|
|
employment_start: '2026-01-01',
|
|
monthly_salary: 30000,
|
|
tax_table_number: 34,
|
|
tax_municipality: 'Stockholm',
|
|
}
|
|
|
|
function postRequest(body: Record<string, unknown>) {
|
|
return new Request('https://x.test/api/salary/employees', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(body),
|
|
})
|
|
}
|
|
|
|
it('inserts the jämkning percentage and validity when provided', async () => {
|
|
const { supabase, insert } = supabaseWithInsert({ id: 'emp-new', personnummer: encryptPersonnummer(NEW_PNR) })
|
|
authed(supabase)
|
|
|
|
const res = await POST(
|
|
postRequest({
|
|
...CREATE_BASE,
|
|
jamkning_percentage: 12.5,
|
|
jamkning_valid_from: '2026-01-01',
|
|
jamkning_valid_to: '2026-12-31',
|
|
}),
|
|
params,
|
|
)
|
|
|
|
expect(res.status).toBe(201)
|
|
expect(insert).toHaveBeenCalledTimes(1)
|
|
expect(insert.mock.calls[0][0]).toMatchObject({
|
|
jamkning_percentage: 12.5,
|
|
jamkning_valid_from: '2026-01-01',
|
|
jamkning_valid_to: '2026-12-31',
|
|
})
|
|
})
|
|
|
|
it('inserts null jämkning fields when the body omits them', async () => {
|
|
const { supabase, insert } = supabaseWithInsert({ id: 'emp-new', personnummer: encryptPersonnummer(NEW_PNR) })
|
|
authed(supabase)
|
|
|
|
const res = await POST(postRequest(CREATE_BASE), params)
|
|
|
|
expect(res.status).toBe(201)
|
|
expect(insert.mock.calls[0][0]).toMatchObject({
|
|
jamkning_percentage: null,
|
|
jamkning_valid_from: null,
|
|
jamkning_valid_to: null,
|
|
})
|
|
})
|
|
})
|