Commit 3d9af10
committed
fix: catchup shows no codes after startup backfill
Three bugs prevented /catchup from showing codes to newly registered users:
1. backfillHandler never persisted pending codes - incremented stats.pendingCodes
counter but never called codeManager.addPendingCode(), so no codes were
stored in the database for /catchup to find.
2. backfillHandler skipped all user redemptions - checked 'success' in
userDetailsResponse which is never true for valid PlayerData responses
(they have a 'details' property, not 'success'). Fixed by checking
!playerData?.details instead.
3. codeManager was missing several methods used by production code and tests:
normalizeCodeStatus, isCodeRedeemedByUser, getAllValidCodes,
addNewPendingCodes, getRedeemedCodeCount, deleteUserRedeemedCodes,
getRedeemedCodesByUsers, getServerCodeStats, getAggregateLoot (now backed
by loot_totals table), and backfillLootTotals.
Additional fixes in codeManager:
- addRedeemedCode: normalize numeric status codes via /^\d+$/ guard,
fix onConflictDoUpdate target to [code, discordId], propagate isPublic
flag correctly, write loot aggregates to loot_totals on Success.
- addPendingCode: add onConflictDoNothing() to make it idempotent.
- getRedeemedCodeDetails: add optional offset parameter.
- addNewPendingCodes: chunk inserts at 499 rows (2 params/row) to stay
within SQLite 999-variable limit.
- getRedeemedCodesFromList: chunk IN queries at 997 codes (+2 status
params) to stay within SQLite 999-variable limit.
- deleteUserRedeemedCodes: COUNT(*) + DELETE instead of RETURNING to
avoid materialising a large deleted-row array.
- getRedeemedCodesByUsers: chunk discordIds within per-query param budget.
- Export CHEST_TYPE_NAMES and LootSummary type (used by codes.ts/stats.ts).
backfillHandler fixes:
- Validate instance_id is present and non-'0' before calling submitCode,
matching the guard in autoRedeemer.ts.
- Replace N+1 isCodeRedeemed/addPendingCode loop with a single
getRedeemedCodesFromList query + bulk addNewPendingCodes insert.
Adds end-to-end tests covering the startup-backfill -> setup -> catchup
flow, including API failure fallback, duplicate backfill idempotency,
invalid instance_id skip, and SQLite chunking boundary paths.
Signed-off-by: Michael Cramer <michael@bigmichi1.de>1 parent 8f207e4 commit 3d9af10
4 files changed
Lines changed: 655 additions & 238 deletions
File tree
- src/bot
- database
- handlers
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
62 | 66 | | |
63 | 67 | | |
64 | 68 | | |
| |||
447 | 451 | | |
448 | 452 | | |
449 | 453 | | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
450 | 509 | | |
451 | 510 | | |
452 | 511 | | |
| |||
511 | 570 | | |
512 | 571 | | |
513 | 572 | | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
514 | 581 | | |
515 | 582 | | |
516 | 583 | | |
| |||
786 | 853 | | |
787 | 854 | | |
788 | 855 | | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
789 | 866 | | |
| 867 | + | |
0 commit comments