Files
TREK/client/src/mobile/screens/vacay/vacayDayModel.ts
T
Maurice 2c2d008dc2 fix: clear every open Sonar security issue, and half the reliability ones (#2037)
* fix(security): mint idempotency keys properly, and resolve the extractor by path

Three findings out of a sweep of every open Sonar security issue. The other 49
turned out to be false positives and are documented in sonar-project.properties
rather than changed — each entry scoped to a file, never a whole rule, so a
future Math.random() on an auth path still fails.

The idempotency key is the one that can lose data. Both producers fell back to
Math.random when crypto.randomUUID was missing, and randomUUID needs a secure
context — TREK's own quickstart is http://localhost:3000 and LAN installs run on
http://192.168.x.x, so on the deployments the README documents the fallback is
what actually runs. The axios one was
`Math.random().toString(36).slice(2)`, which is not length-stable: 0.5 yields a
single character and 0 yields the empty string, which the interceptor reads as
"no key" and skips deduplication for entirely. Two writes sharing a key collapse
into one — the second gets the first one's cached response and the edit is gone
with no error anywhere. Both now come from utils/randomId, which has the
getRandomValues rung that works in insecure contexts too.

The extractor probe stored the bare binary name, so every extraction re-resolved
it through whatever PATH held at that moment. The official image sets
KITINERARY_EXTRACTOR_PATH and never reaches that branch, but a bare-metal install
without it ran an unqualified name from a PATH it does not control. It now walks
the search path itself and keeps the absolute path of the file it verified;
execFileSync also drops the /bin/sh hop the old probe needed. PATH comes through
app-config because process.env is walled off in src/.

* fix: four tests and sorts that were not doing what they claimed

Sonar reported 43 high-impact reliability issues. Reading all of them, four are
real; the rest are cosmetic or would be made worse by the suggested change.

budget.test.ts hand-built `paid: true`, but both emitters send `paid: body.paid
? 1 : 0` and the handler stores the value verbatim — the test asserted a payload
the server never sends. The line below it already used a number.

admin.controller.test.ts had a `.resolves` assertion with no await. The promise
was dropped, so that line could not fail the test.

The two to-do category lists sorted with a bare .sort(), which is byte order:
every accented name files behind the whole ASCII range, so in a German trip
"Übernachtung" lands after "Zelt". TREK ships 23 locales, so this is real data.
One side effect worth knowing: katColor picks a category's dot colour by its
index, so re-sorting can move which category shows which colour — it already
moves whenever a category is added, and the colour carries no meaning.

Deliberately not changed: the two `| 0` truncations (Math.trunc is not
equivalent — `| 0` wraps at 32 bits and these carry millisecond timestamps), the
two .map() callbacks whose second parameter is declared and used, and the sha1
in thumbnail.service (its own comment says a swap would orphan every existing
thumbnail).

* refactor: give every button a type, and prefer the Number statics

Two mechanical sweeps, no behaviour change intended in either.

A <button> with no type attribute is type="submit" per the HTML spec. Outside a
form that does nothing today, but it turns into a surprise the moment the markup
is wrapped in one, which is what Sonar's S9011 is warning about. 870 buttons get
an explicit type="button". Two things were left alone on purpose: buttons that
take a {...spread}, because the spread may carry a type and the merge order
decides which wins, and anything inside a <form> — there the answer is a
judgement call, and checking all eleven form files found every real submit button
already carrying type="submit".

The Number sweep covers only the three globals that are exactly equivalent to
their Number twin: parseInt, parseFloat and NaN. isNaN is NOT in it — the global
coerces its argument, so isNaN('abc') is true where Number.isNaN('abc') is false,
and renaming it would change what the code does. It reaches server and plugin-sdk
as well as the client; both copies of egress-policy.ts got the identical edit, so
the mirror the SDK's parity test guards stays intact.
2026-08-25 15:22:38 +02:00

175 lines
8.0 KiB
TypeScript

import { schoolHolidayWash } from '../../../components/Vacay/holidayVisual'
import type { HolidaysMap, VacayEntry } from '../../../types'
// Users created before picking a color have color=null; same fallback the
// desktop persons panel uses.
export const FALLBACK_PERSON_COLOR = '#6366f1'
export interface DayVisual {
background: string
numColor: string
// Set on hatched (comp/flex) cells, where the screen shows through between the
// stripes and the digit needs its own contrast (#1074).
textShadow?: string
boxShadow?: string
// At least one person logged this day as a half day (#552) — the cell shows a ½ badge.
half?: boolean
// Per-person fill segments for this day (#1074): each carries the person's tint and
// whether they logged a comp/flex day (hatched, kind='comp') vs. a vacation day
// (solid). Set only when someone is off; the cell renders overlays for 2+ people.
segments?: { color: string; comp: boolean }[]
// Colours of the school-holiday calendars covering this day — drawn as a rounded
// accent band under the number, on top of whatever fill the cell already has.
school?: string[]
}
export interface DayVisualContext {
todayStr: string
entryMap: Record<string, VacayEntry[]>
companyHolidaySet: Set<string>
companyHolidaysEnabled: boolean
holidays: HolidaysMap
weekendDays: number[]
// Shared read-only calendars per date (#444/#667) — drawn as inset rings on
// top of whatever the cell shows, never as fills.
sharedMap?: Record<string, { color: string }[]>
}
function hexToHsl(hex: string): { h: number; s: number; l: number } | null {
const m = /^#([0-9a-f]{6})$/i.exec(hex.trim())
if (!m) return null
const n = Number.parseInt(m[1], 16)
const r = ((n >> 16) & 255) / 255
const g = ((n >> 8) & 255) / 255
const b = (n & 255) / 255
const max = Math.max(r, g, b)
const min = Math.min(r, g, b)
const l = (max + min) / 2
const d = max - min
if (d === 0) return { h: 0, s: 0, l }
const s = d / (1 - Math.abs(2 * l - 1))
let h: number
if (max === r) h = ((g - b) / d) % 6
else if (max === g) h = (b - r) / d + 2
else h = (r - g) / d + 4
h = Math.round(h * 60)
if (h < 0) h += 360
return { h, s, l }
}
/**
* Light pastel tint of a person color for logged-day cells (the design's
* pink/teal pastels, derived from the real member colors).
*/
export function personTint(color: string): string {
const hsl = hexToHsl(color)
if (!hsl) return color
const s = Math.round(Math.min(hsl.s * 72, 80))
return `hsl(${hsl.h} ${s}% 85%)`
}
/**
* Dark readable ink for a day number on top of a pastel holiday-calendar
* color (the design's #C0392B on #FBE0E0, generalized to any calendar color).
*/
export function holidayInk(color: string): string {
const hsl = hexToHsl(color)
if (!hsl) return 'var(--m-ink)'
const s = Math.round(Math.min(Math.max(hsl.s * 80, 45), 75))
return `hsl(${hsl.h} ${s}% 42%)`
}
/** Diagonal hatch of a tint for comp/flex days (#1074), matching the desktop card. */
export function hatchTint(color: string): string {
return `repeating-linear-gradient(45deg, ${color} 0 2.5px, transparent 2.5px 5px)`
}
/**
* Day-cell color matrix, priority top-down: today ring > company holiday >
* logged persons (pastel, split for several) > public holiday > weekend >
* plain. Logged and company cells keep hard dark inks — the pastels are
* theme-independent surfaces.
*/
function baseDayVisual(dateStr: string, dayOfWeek: number, ctx: DayVisualContext): DayVisual {
const holidayMarkers = ctx.holidays[dateStr]
const holidays = Array.isArray(holidayMarkers) ? holidayMarkers : holidayMarkers ? [holidayMarkers] : []
const publicHoliday = holidays.find(holiday => (holiday.type ?? 'public_holiday') === 'public_holiday')
const schoolColors = holidays.filter(holiday => holiday.type === 'school_holiday').map(holiday => holiday.color)
// The school band sits on top of any fill (person / company / public), so tag it
// onto whatever visual this day resolves to rather than picking a single winner.
const withSchool = (v: DayVisual): DayVisual => (schoolColors.length > 0 ? { ...v, school: schoolColors } : v)
// Today is deliberately NOT decided here: it is a ring around whatever the day
// already is, applied in dayVisual. Resolving it as its own visual meant a
// vacation day or company holiday logged for today was stored and counted but
// drawn as an empty cell.
if (ctx.companyHolidaysEnabled && ctx.companyHolidaySet.has(dateStr)) {
return withSchool({ background: '#F5D9A6', numColor: '#8A5A00' })
}
const entries = ctx.entryMap[dateStr]
if (entries && entries.length > 0) {
// The fill still shows WHO is off; half days (#552) keep it and add a corner dot.
// Comp/flex days (#1074) hatch their segment. 1 person fills the cell directly
// (solid or hatched); several render segment overlays, so the background stays
// transparent underneath them.
const segments = entries.map(e => ({ color: personTint(e.person_color || FALLBACK_PERSON_COLOR), comp: e.kind === 'comp' }))
const single = segments.length === 1
const background = single ? (segments[0].comp ? hatchTint(segments[0].color) : segments[0].color) : 'transparent'
// The pastel fills are theme-independent surfaces, so every logged day keeps
// the same hard dark digit. An all-comp day is hatched, and the screen shows
// through between its stripes — a light shadow carries the contrast there so
// the digit survives dark mode without changing colour (#1074).
const allComp = segments.every(s => s.comp)
const visual: DayVisual = { background, numColor: '#101013', segments }
if (allComp) visual.textShadow = '0 1px 2px rgba(255,255,255,0.9), 0 0 3px rgba(255,255,255,0.6)'
if (entries.some(e => (e.fraction ?? 1) === 0.5)) visual.half = true
return withSchool(visual)
}
if (publicHoliday) {
return withSchool({ background: publicHoliday.color, numColor: holidayInk(publicHoliday.color) })
}
// Plain school-break day: soft wash + readable tinted ink, matching the desktop card.
if (schoolColors.length > 0) {
return { background: schoolHolidayWash(schoolColors[0]), numColor: holidayInk(schoolColors[0]), school: schoolColors }
}
if (ctx.weekendDays.includes(dayOfWeek)) {
return { background: 'var(--m-ic)', numColor: 'var(--m-faint)' }
}
return { background: 'transparent', numColor: 'var(--m-muted)' }
}
export function dayVisual(dateStr: string, dayOfWeek: number, ctx: DayVisualContext): DayVisual {
const visual = baseDayVisual(dateStr, dayOfWeek, ctx)
// Today rings whatever the day resolved to, so a vacation day or company
// holiday logged for today keeps its fill and is visible.
if (dateStr === ctx.todayStr) {
const ring = 'inset 0 0 0 1.5px var(--m-ink)'
visual.boxShadow = visual.boxShadow ? `${ring}, ${visual.boxShadow}` : ring
// The strong ink only where the cell has nothing of its own to say. On a
// pastel fill the fill's own ink is the readable one, and in dark mode
// --m-ink is light and would vanish on it.
if (!visual.segments && (visual.background === 'transparent' || visual.background === 'var(--m-ic)')) {
visual.numColor = 'var(--m-ink)'
}
}
// Shared calendars (#444/#667) draw inset rings over the base cell — capped at
// two so tiny mini-grid cells stay readable. Nested inside the today ring.
const rings = [...new Set((ctx.sharedMap?.[dateStr] || []).map(m => m.color))].slice(0, 2)
if (rings.length > 0) {
const shadows = visual.boxShadow ? [visual.boxShadow] : []
const base = visual.boxShadow ? 1.5 : 0
rings.forEach((c, i) => shadows.push(`inset 0 0 0 ${base + (i + 1) * 1.5}px ${c}`))
visual.boxShadow = shadows.join(', ')
}
return visual
}
/** Empty leading cells before the 1st, honoring the configured week start. */
export function monthLead(year: number, month: number, weekStart: number): number {
return (new Date(year, month, 1).getDay() - weekStart + 7) % 7
}
export function localDateStr(year: number, month: number, day: number): string {
return `${year}-${String(month + 1).padStart(2, '0')}-${String(day).padStart(2, '0')}`
}