Skip to content

Replace raw state integers with named enum scopes in jobs#72

Open
JuanVqz wants to merge 2 commits intofix-brakemanfrom
fix-raw-state-integers
Open

Replace raw state integers with named enum scopes in jobs#72
JuanVqz wants to merge 2 commits intofix-brakemanfrom
fix-raw-state-integers

Conversation

@JuanVqz
Copy link
Member

@JuanVqz JuanVqz commented Mar 6, 2026

Problem

Both DailyPuzzleJob and PuzzleInventoryCheckJob queried for approved puzzles using the raw integer state: 0 instead of the named enum scope:

# Fragile — breaks silently if enum values change
Puzzle.where(sent_at: nil, state: 0)
Puzzle.where(state: 0, sent_at: nil)

The integer 0 maps to approved today per the enum definition, but this is implicit and will silently return wrong results if the enum ever changes.

Fix

Puzzle.approved.where(sent_at: nil)

Tests

Added test files for both jobs (neither had any tests):

DailyPuzzleJobTest

  • Only selects approved puzzles, not pending or rejected
  • Only selects puzzles that have not been sent yet
  • Marks the selected puzzle as archived and sets sent_at
  • Does nothing when no approved unsent puzzles exist

PuzzleInventoryCheckJobTest

  • Sends notification when fewer than 5 approved unsent puzzles exist
  • Does not send notification when 5 or more approved unsent puzzles exist
  • Only counts approved puzzles, not pending or rejected
  • Only counts unsent puzzles

Using state: 0 relied on knowing the integer value of the approved enum,
which breaks silently if values change. Both DailyPuzzleJob and
PuzzleInventoryCheckJob now use Puzzle.approved.where(sent_at: nil).
@JuanVqz JuanVqz changed the base branch from main to fix-brakeman March 6, 2026 01:01

def send_low_inventory_notification(count)
channel_id = ENV["SHIELD_NOTIFICATIONS_CHANNEL"]
return Rails.logger.warn("SHIELD_NOTIFICATIONS_CHANNEL is not configured, skipping low inventory notification") unless channel_id
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nil guard does not fix the issue but discarding factors.

@JuanVqz JuanVqz requested review from mateusdeap and torresga March 6, 2026 01:12
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