Skip to content

feat: centralize code table management and enum generation - #473

Merged
kyle1morel merged 1 commit into
masterfrom
feature/revamp-codes-cache
May 26, 2026
Merged

feat: centralize code table management and enum generation#473
kyle1morel merged 1 commit into
masterfrom
feature/revamp-codes-cache

Conversation

@qhanson55

Copy link
Copy Markdown
Contributor

Description

Refactors how database lookup code tables are cached, generated, and consumed. This eliminates manual code table arrays by dynamically building memory caches and types from Prisma metadata models ending in _code.

  • Relocate and refactor code enum generation script to src/db/codes/generate.ts
  • Implement dynamic cache manager in src/db/codes/cache.ts using a Proxy mapping
  • Dynamically fetch active lookup tables in services/code.ts via Prisma DMMF models
  • Update peachSync.ts to initialize caches prior to executing the synchronization loop
  • Replace hardcoded constant validation arrays in endpoints and Joi schemas with live cache lookups
  • Update unit tests and Jest deep mocks to support the central code cache system

Types of changes

New feature (non-breaking change which adds functionality)

Checklist

  • I have read the CONTRIBUTING doc
  • I have checked that unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

@qhanson55
qhanson55 requested a review from a team as a code owner May 20, 2026 15:43
@github-actions

Copy link
Copy Markdown

Coverage Report (Frontend)

Totals Coverage
Statements: 51.03% ( 6152 / 12056 )
Methods: 40.19% ( 854 / 2125 )
Lines: 60.26% ( 3814 / 6329 )
Branches: 41.2% ( 1484 / 3602 )

@github-actions

github-actions Bot commented May 20, 2026

Copy link
Copy Markdown

@qhanson55
qhanson55 force-pushed the feature/revamp-codes-cache branch from d3cf52b to 1c59400 Compare May 20, 2026 15:54
@github-actions

github-actions Bot commented May 20, 2026

Copy link
Copy Markdown

Coverage Report (Application)

Totals Coverage
Statements: 84.99% ( 4523 / 5322 )
Methods: 82.65% ( 581 / 703 )
Lines: 89.68% ( 3112 / 3470 )
Branches: 72.24% ( 830 / 1149 )

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors “code table” handling by generating enums and building a centralized, dynamic in-memory cache for DB lookup/code tables (Prisma models ending in _code), then updating validators/controllers/tests to consume that centralized cache rather than hardcoded arrays.

Changes:

  • Moved enum generation into src/db/codes/generate.ts and updated generated enum import paths across app + frontend.
  • Introduced a centralized code-table discovery (tables.ts), fetching (services/code.ts), and caching layer (cache.ts) with a codeTable accessor.
  • Replaced hardcoded validation arrays/constants with code-table cache lookups and updated unit tests/mocks accordingly.

Reviewed changes

Copilot reviewed 35 out of 36 changed files in this pull request and generated 14 comments.

Show a summary per file
File Description
frontend/src/utils/enums/codeEnums.ts Updates generator reference in the auto-generated frontend enum file header.
frontend/src/store/codeStore.ts Changes a computed getter to map code → definition instead of code → code.
app/tests/unit/validators/permit.spec.ts Updates enum import path to the new generated enums location.
app/tests/unit/validators/appliedPermits.spec.ts Updates enum import path to the new generated enums location.
app/tests/unit/services/code.spec.ts Updates service test expectations for the refactored code-table listing implementation.
app/tests/unit/parsers/peachParser.test.ts Updates enum import path to the new generated enums location.
app/tests/unit/data/index.ts Updates enum imports and adjusts expected note text fixture.
app/tests/unit/controllers/permit.spec.ts Updates enum import path and assertions to use codeTable display strings.
app/tests/unit/controllers/peach.test.ts Updates enum import path to the new generated enums location.
app/tests/unit/controllers/housingProject.spec.ts Updates enum import path to the new generated enums location.
app/tests/unit/controllers/generalProject.spec.ts Updates enum import path to the new generated enums location.
app/tests/mocks/prismaMock.ts Adds a mocked codeTable implementation derived from generated enums for unit tests.
app/src/validators/permit.ts Switches Joi .valid(...) lists from constants to codeTable.*.codes.
app/src/validators/noteHistory.ts Switches Joi .valid(...) lists from cached arrays to codeTable.*.codes.
app/src/validators/generalProject.ts Switches Joi .valid(...) lists from cached arrays to codeTable.*.codes.
app/src/validators/electrificationProject.ts Switches Joi .valid(...)/array item lists from cached arrays to codeTable.*.codes.
app/src/validators/appliedPermit.ts Switches Joi .valid(...) list from constants to codeTable.*.codes.
app/src/utils/constants/permit.ts Removes now-redundant hardcoded PERMIT_STAGE_LIST / PERMIT_STATE_LIST.
app/src/utils/cache/codes.ts Deletes the legacy code-cache module that managed per-table arrays.
app/src/types/stuff.d.ts Updates enum import path to the new generated enums location.
app/src/services/code.ts Refactors code-table listing to iterate discovered _code models dynamically.
app/src/parsers/peach.ts Updates enum import path to the new generated enums location.
app/src/db/utils/generate_enums.ts Removes the old enum generation script (replaced by src/db/codes/generate.ts).
app/src/db/codes/types.ts Introduces shared types for code-table names and code rows.
app/src/db/codes/tables.ts Adds code-table discovery via Prisma DMMF (*_code models, with exclusions).
app/src/db/codes/generate.ts Adds the new enum generation script under the centralized codes module.
app/src/db/codes/enums.ts Updates generator reference in the auto-generated backend enum file header.
app/src/db/codes/cache.ts Adds the centralized in-memory code-table cache and refresh function.
app/src/controllers/roadmap.ts Updates enum import path to the new generated enums location.
app/src/controllers/permit.ts Updates enum import path and uses codeTable display strings in status-change note generation.
app/src/controllers/peach.ts Updates enum import path to the new generated enums location.
app/src/controllers/housingProject.ts Updates enum import path to the new generated enums location.
app/src/controllers/generalProject.ts Updates enum import path to the new generated enums location.
app/server.ts Switches code-cache warmup to the new centralized cache module.
app/peachSync.ts Ensures code-cache warmup occurs before starting the PEACH sync loop.
app/package.json Updates the prisma:enums script to point at the new generator.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread app/src/validators/permit.ts Outdated
Comment thread app/src/validators/noteHistory.ts Outdated
Comment thread app/src/validators/noteHistory.ts Outdated
Comment thread app/src/validators/generalProject.ts Outdated
Comment thread app/src/validators/electrificationProject.ts
Comment thread app/src/db/codes/cache.ts Outdated
Comment thread app/tests/unit/services/code.spec.ts
Comment thread app/peachSync.ts Outdated
Comment thread frontend/src/store/codeStore.ts Outdated
Comment thread app/src/db/codes/generate.ts
@qhanson55
qhanson55 force-pushed the feature/revamp-codes-cache branch 3 times, most recently from c084187 to 2efdfc8 Compare May 20, 2026 23:13
@qhanson55
qhanson55 force-pushed the feature/revamp-codes-cache branch from 2efdfc8 to 252162a Compare May 26, 2026 16:36
Refactors database lookup code tables to dynamically build caches and types directly from Prisma metadata.

- Move enum generation script to `src/db/codes/generate.ts`
- Implement dynamic cache manager in `src/db/codes/cache.ts` with a fail-fast Proxy to catch type sync errors
- Fetch active tables dynamically via Prisma DMMF models
- Initialize caches in `peachSync.ts` prior to sync loop
- Replace static Joi `.valid()` calls with a `.custom()` Proxy to ensure code tables evaluate at runtime
- Update unit tests and Jest mocks to support the central cache system

Signed-off-by: qhanson55 <quinn.hanson@gov.bc.ca>
@qhanson55
qhanson55 force-pushed the feature/revamp-codes-cache branch from 252162a to 8abb3d8 Compare May 26, 2026 19:31
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Passed Quality Gate passed for 'nr-permitconnect-navigator-service app'

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
91.5% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarQube Cloud

@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Passed Quality Gate passed for 'nr-permitconnect-navigator-service frontend'

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarQube Cloud

@kyle1morel
kyle1morel merged commit 0b4ec47 into master May 26, 2026
24 checks passed
@kyle1morel
kyle1morel deleted the feature/revamp-codes-cache branch May 26, 2026 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants