Skip to content

fix: use user_id/current_score in score and simulation controllers - #1835

Open
flare-999 wants to merge 3 commits into
LabsCrypt:mainfrom
flare-999:fix/1801-score-simulation-columns
Open

flare-999 wants to merge 3 commits into
LabsCrypt:mainfrom
flare-999:fix/1801-score-simulation-columns

Conversation

@flare-999

Copy link
Copy Markdown

Overview

scoreController.ts and simulationController.ts read and wrote the scores table through columns that do not exist: SELECT score FROM scores WHERE borrower = $1 and INSERT INTO scores (borrower, score) ... ON CONFLICT (borrower). The table created by 1771691269865_initial-schema.js only has user_id and current_score, so GET /api/score/:userId, POST /api/score/update, GET /api/simulation/:userId/history and POST /api/simulation/simulate-payment all failed with a 500 (column "borrower" does not exist).

Related Issue

Closes #1801

Changes

  • [MODIFY] backend/src/controllers/scoreController.ts

    • getScore / updateScore: SELECT current_score AS score FROM scores WHERE user_id = $1.
    • updateScore upsert: INSERT INTO scores (user_id, current_score) ... ON CONFLICT (user_id) DO UPDATE SET current_score = LEAST(850, GREATEST(300, scores.current_score + $3)) ... RETURNING current_score AS score.
    • getScoreBreakdown: the current_score_cte now selects COALESCE(current_score, 500) with WHERE user_id = $1.
  • [MODIFY] backend/src/controllers/simulationController.ts

    • getRemittanceHistory / simulatePayment: SELECT current_score AS score FROM scores WHERE user_id = $1.

The score and current_score row shapes read by the controllers are preserved via the AS score alias, so response payloads are unchanged.

Verification Results

Implemented via GitHub Contents/Git API (no local clone).
Acceptance criteria mapping:
scoreController.ts queries target user_id / current_score (getScore, updateScore, getScoreBreakdown)
simulationController.ts queries target user_id / current_score (getRemittanceHistory, simulatePayment)
No remaining `borrower` / `score` column references against the scores table in either controller
Existing controller tests assert behaviour (not the legacy column names), so no test changes were required
Acceptance Criteria Status
borrower replaced with user_id All scores-table reads/writes in both controllers
score replaced with current_score Selects, upsert and conflict branch
Response contract preserved current_score AS score keeps rows[0].score working

@flare-999

Copy link
Copy Markdown
Author

@LabsCrypt both failing checks are green now.

Before: backend, env-docs-check
After: both pass.

Root cause & fix

  • backend failed on prettier/prettier in the new query() calls added to scoreController.ts / simulationController.ts — reformatted them (no behavioural change).
  • env-docs-check (the env-documentation guard) went green once the branch was merged up with main, which brought the documented env vars back in sync with the code.

Head cfe4ce479d → 11691cc466.

This branch has not been deployed

No deployments
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.

[Bug][Backend] scoreController and simulationController query non-existent columns (borrower, score) on table scores

1 participant