S2: Duplicated SQL CASE/WHERE construction in signals route #7

Open
opened 2026-06-05 10:22:07 +00:00 by sebse · 0 comments
Owner

Code Smell

File: web/app/api/signals/route.ts

Both the delta branch and the value branch independently build the same SQL patterns:

  • seriesConditions map with parameterized combinator/item_key pairs
  • fullWhere combining original WHERE with series conditions via OR
  • orderCase for ORDER BY CASE preserving sort order
  • Final query execution

These ~16-18 lines are copy-pasted between the two branches (~35 lines total duplication). The inline parameter offset calculation (param.current + idx * 2) is fragile and must be kept in sync.

Why it matters

Any change to the series query pattern must be applied in two places. Easy to miss during refactoring.

Suggestion

Extract a helper function:

function buildSeriesQuery<T>(
  top: { combinator: string; item_key: string }[],
  selectCols: string,
  where: string,
  values: unknown[],
  param: { current: number },
): Promise<QueryResult<T>>
## Code Smell **File:** `web/app/api/signals/route.ts` Both the delta branch and the value branch independently build the same SQL patterns: - `seriesConditions` map with parameterized combinator/item_key pairs - `fullWhere` combining original WHERE with series conditions via OR - `orderCase` for ORDER BY CASE preserving sort order - Final query execution These ~16-18 lines are copy-pasted between the two branches (~35 lines total duplication). The inline parameter offset calculation (`param.current + idx * 2`) is fragile and must be kept in sync. ### Why it matters Any change to the series query pattern must be applied in two places. Easy to miss during refactoring. ### Suggestion Extract a helper function: ```ts function buildSeriesQuery<T>( top: { combinator: string; item_key: string }[], selectCols: string, where: string, values: unknown[], param: { current: number }, ): Promise<QueryResult<T>> ```
sebse added the code-smell label 2026-06-05 10:22:07 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sebse/factorio-signal-exporter#7