Skip to content

Reduce code duplication across form generation and routing#238

Open
subhranil2605 wants to merge 7 commits into
Capgemini:mainfrom
subhranil2605:refactor/fix_dupe_codes
Open

Reduce code duplication across form generation and routing#238
subhranil2605 wants to merge 7 commits into
Capgemini:mainfrom
subhranil2605:refactor/fix_dupe_codes

Conversation

@subhranil2605

@subhranil2605 subhranil2605 commented Jun 8, 2026

Copy link
Copy Markdown

Summary

  • Extracted OpenAI and Gemini client initialization into factory functions, eliminating 8 duplicate lines
  • Created pagination validation utility consolidating logic across 3 route files (admin, prototype, user)
  • Extracted Nunjucks filter setup into shared helper function to eliminate duplicate registration code
  • Reduced overall code duplication from 8.2% to 6.1% (587 lines remaining vs 750 original)

Test plan

  • ✅ All 628 unit tests pass (npm run test:unit)
  • ✅ All 27 E2E tests pass (npm run test:e2e)
  • ✅ No linting or formatting errors
  • ✅ No breaking changes to public APIs

subhranil2605 and others added 7 commits June 8, 2026 21:02
- Introduced new environment variables for Gemini API key and model ID in .env.example and validation schema.
- Created llm-provider.ts to handle form creation, updates, and suggestions using either Gemini or OpenAI.
- Refactored nunjucks setup into a separate utility file for better organization.
- Updated prototype routes to utilize the new LLM provider functions.
- Added new functions in gemini.ts for interacting with the Gemini API.
Create createOpenAIClient() helper to consolidate the identical OpenAI client
initialization pattern that was duplicated across 4 functions (createFormWithOpenAI,
generateSuggestionsWithOpenAI, judgeFormWithOpenAI, updateFormWithOpenAI).

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Consolidate the identical macro options generation for multiple_choice and
single_choice field types into buildChoiceFieldOptions() helper. The only
differences between them (checked expression and macro name) are now parameters.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Create validatePaginationParams() in validation-utils.ts to consolidate the
identical pagination parameter validation pattern that was duplicated across
admin-routes, prototype-routes, and user-routes.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Create createGeminiClient() helper to consolidate the identical Gemini client
initialization pattern that was duplicated across 4 functions in gemini.ts.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Consolidate the identical filter registration and view engine setup that was
duplicated in setupNunjucksEnv() and setupNunjucksEnvZipDownload(). The
zip-download version now re-exports from the shared utility.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
@subhranil2605 subhranil2605 requested a review from cmenon12 as a code owner June 8, 2026 16: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.

1 participant