-
-
Notifications
You must be signed in to change notification settings - Fork 327
Description
As part of the work in #9621, we created a new shared constants library (@medic/constants) to reduce the use of magic strings throughout the codebase. The initial work added DOC_IDS.SERVICE_WORKER_META, DOC_TYPES.TRANSLATIONS, HTTP_HEADERS.REQUEST_ID, SENTINEL_METADATA.TRANSITIONS_SEQ, SENTINEL_METADATA.BACKGROUND_SEQ, and USER_ROLES.ONLINE.
However, there are many more magic strings used throughout the codebase that should be converted to use shared constants. This parent issue tracks the work to add constants across multiple categories including document IDs, document types, and other common strings.
Why this matters
Magic strings scattered throughout the codebase can lead to typos and make refactoring difficult. By centralizing these constants in a shared library, we:
- Reduce the risk of typos (if you misspell
DOC_IDS.SETTINGS, you'll get a clear error) - Make it easier to find all usages of a particular document ID
- Create a single source of truth for important values
- Make the code more maintainable and self-documenting
Constants to add
Based on an analysis of the codebase, here are the most common magic strings that could be converted to constants:
Document IDs (DOC_IDS)
settingsresourcesprivacy-policiesbrandingpartnerstarget-aggregatesmigration-logextension-libszscore-charts
Document Types (DOC_TYPES)
data_recordpersoncliniccontactdistrict_hospitalhealth_centertasktargetuser-settingsformtoken_login
Other constant categories
Additional constants may be identified in other categories:
- HTTP Headers - Common header names
- User Roles - Role strings used throughout the application
- Sentinel Metadata - Local document IDs and sentinel-specific constants
- Common Prefixes/Patterns - Reusable string patterns like
org.couchdb.user:ormessages-
Implementation approach
Each constant (or small group of related constants) should be added as a separate sub-issue to keep pull requests focused and easy to review.
To succeed on this ticket, please:
-
Set up your development environment and ensure it works by logging into your CHT instance.
-
Read the shared constants library file at
shared-libs/constants/src/index.jsto understand its structure. Notice how it exports several constant categories likeDOC_IDS,DOC_TYPES,HTTP_HEADERS, etc. -
Search for how existing constants are used throughout the codebase to understand the pattern:
- For example,
DOC_IDS.SERVICE_WORKER_METAis used in files likeapi/src/generate-service-worker.js,webapp/src/ts/services/db-sync.service.ts, andapi/src/services/authorization.js - Notice how the constant is imported from
@medic/constantsand used in place of the magic string
- For example,
-
For the specific constant(s) you're working on:
- Add the constant to
shared-libs/constants/src/index.jsin the appropriate category object (e.g.,DOC_IDS,DOC_TYPES, etc.) - Follow the pattern of existing constants in that category
- Search for all usages of the magic string throughout the codebase (use grep or your IDE's search)
- Replace each occurrence with the new constant
- Ensure imports from
@medic/constantsare added where needed
- Add the constant to
-
Build the project and run unit tests to ensure everything compiles and works correctly:
npm ci npm run build-dev npm run unit
Note: The constants library itself doesn't have unit tests, but running the full unit test suite should verify that your changes work correctly throughout the codebase.
-
If all tests pass, create a git commit with your changes following the contribution guidelines.
Proposed sub-issues
The following sub-issues could be added to track individual constants or groups of constants:
Document ID constants
High-frequency document IDs (individual issues):
- Add constant for 'settings' document ID
- Add constant for 'resources' document ID
- Add constant for 'privacy-policies' document ID
- Add constant for 'branding' document ID
- Add constant for 'partners' document ID
Lower-frequency document IDs (grouped):
- Add constants for 'target-aggregates', 'migration-log', 'extension-libs', and 'zscore-charts' document IDs
Document Type constants
Very high-frequency document types (individual issues):
- Add constant for 'data_record' document type
- Add constant for 'person' document type
- Add constant for 'clinic' document type
- Add constant for 'contact' document type
- Add constant for 'district_hospital' document type
- Add constant for 'health_center' document type
Lower-frequency document types (grouped):
- Add constants for 'task', 'target', and 'user-settings' document types
- Add constants for 'form' and 'token_login' document types
Other constants
Based on the existing constant categories in the shared constants library:
- Add constants for HTTP headers (e.g., common header names beyond
REQUEST_ID) - Add constants for user roles (e.g., common role strings beyond
ONLINE) - Add constants for Sentinel metadata (e.g., additional local document IDs)
- Add constants for common prefixes or patterns (e.g.,
org.couchdb.user:,messages-for translations)
Additional constant categories can be added as sub-issues as they are identified.