Skip to content

[RFC] Centralize crime type constants — eliminate 5+ scattered definitions #2

@Khoa-Dam

Description

@Khoa-Dam

Problem

Crime type labels, danger weights, and severity mappings are duplicated across 5+ locations:

  • crime-reports.service.ts — inline dangerLevels objects (×3 occurrences)
  • ai/ai.service.tstypeLabels record
  • Frontend: use-map-crime-markers.ts, use-notifications.ts, map/page.tsx each define their own label/color maps

Two divergent danger scales coexist: getDefaultSeverityByType used 1–5, heatmap used 3–10. Any new crime type requires edits in 5+ files.

Proposed Solution — Two co-located constant files

Backend: src/shared/crime-types.constants.ts

export const CRIME_DANGER_WEIGHT: Record<CrimeType, number> = { ... } // single 1–10 scale
export const CRIME_TYPE_LABEL: Record<CrimeType, string> = { ... }
export function weightToSeverity(weight: number): number { ... } // maps to 1–5 for DB column

Frontend: src/constants/crime-constants.ts

export const CRIME_TYPE_LABEL: Record<CrimeType, string> = { ... }
export const CRIME_TYPE_COLOR: Record<CrimeType, string> = { ... }
export const CRIME_TYPE_OPTIONS = [ ... ]
export const SEVERITY_COLOR: Record<number, string> = { ... }

Files to migrate

File Action
src/crime-reports/crime-reports.service.ts Remove 3× inline dangerLevels, import CRIME_DANGER_WEIGHT
src/ai/ai.service.ts Remove typeLabels, import CRIME_TYPE_LABEL
src/app/(protected)/map/hooks/use-map-crime-markers.ts Remove local label map, import from constants
src/hooks/use-notifications.ts Remove crimeTypeLabels, import from constants
src/app/(protected)/map/page.tsx Remove CRIME_TYPE_OPTIONS definition, import from constants

Benefits

  • Adding a new crime type = edit 2 files, not 7
  • Heatmap danger score and DB severity column stay in sync via weightToSeverity()
  • Single source of truth for UI labels and colors

References

Architecture RFC session — 2026-04-12

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFCRequest for Comments — architecture proposals

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions