Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/helpers/shop_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
module ShopHelper
CLAIMED_TAGS = { "waiting" => "in the queue", "ordered" => "ordered" }.freeze

# Your order for a prize, if you've claimed it.
# Your order for a prize, if you've claimed it. A cancelled order doesn't count: the prize is back on the shelf.
def shop_order_for(project, prize)
project.orders.find { |order| order.persisted? && order.prize_key == prize.key }
project.orders.find { |order| order.live? && order.prize_key == prize.key }
end

# Where a card is: claimed (you've an order for it), ready (your approved hours cover it: claim it) or locked (they
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/sticker_promise_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def sticker_promise_state(user, now: Time.current)

if user.sticker_packs_unclaimed.positive?
{ kind: :earned }
elsif project.orders.any? { |order| order.persisted? && order.prize_key == Prize::STICKERS.key }
elsif project.orders.any? { |order| order.live? && order.prize_key == Prize::STICKERS.key }
{ kind: :claimed }
elsif now < user.created_at + Reward::STICKERS_WINDOW
sticker_counting_state(user, project)
Expand Down
7 changes: 5 additions & 2 deletions app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Order < ApplicationRecord
validates :status, inclusion: { in: STATUSES }
validates :prize_key, inclusion: { in: Prize::CLAIMABLE.map(&:key) }
# The free stickers can be claimed up to Reward::STICKER_PACKS_MAX times, once for each pack earned (stickers_earned).
validates :prize_key, uniqueness: { scope: :project_id, message: "is one you've bought already" }, allow_nil: true, unless: :stickers?
validates :prize_key, uniqueness: { scope: :project_id, conditions: -> { where.not(status: "cancelled") }, message: "is one you've bought already" }, allow_nil: true, unless: :stickers?
validate :shop_open, :not_banned, on: :create
validate :stickers_earned, :covered_by_approved_hours, :addressed, :deliverable, :old_enough, :has_email, on: :create

Expand Down Expand Up @@ -88,6 +88,9 @@ def ordered!(by:, theseus_letter_id: nil)

def cancelled? = status == "cancelled"

# Saved and not cancelled: an order that holds its prize and its hours.
def live? = persisted? && !cancelled?

# A waiting order is cancelled: the row stays, off the hours spent, so the hours come back. Under the row lock, so two
# cancels at once cancel it once. Answers false, changing nothing, when it is no longer waiting.
def cancel!
Expand Down Expand Up @@ -202,7 +205,7 @@ def stickers_earned
return unless stickers? && project

user = project.user
claimed = project.orders.where(prize_key: Prize::STICKERS.key).where.not(id:).count
claimed = project.orders.where(prize_key: Prize::STICKERS.key).where.not(id:).where.not(status: "cancelled").count
earned = Reward.sticker_packs_earned(user)
if earned.zero? then errors.add(:prize_key, "isn't one you've earned")
elsif claimed >= [ earned, Reward::STICKER_PACKS_MAX ].min then errors.add(:prize_key, "is one you've bought already")
Expand Down
4 changes: 2 additions & 2 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def hours_unspent_logged

# You've claimed the prize you're aiming for already.
def goal_claimed?
chosen_prize.present? && orders.any? { |order| order.persisted? && order.prize_key == prize_key }
chosen_prize.present? && orders.any? { |order| order.live? && order.prize_key == prize_key }
end

# Your approved hours to spend cover your goal, and you haven't claimed it: it's yours to claim.
Expand All @@ -307,7 +307,7 @@ def goal_reached_seen!
# Nothing when your goal is the dearest.
def upsell_prize
goal = chosen_prize or return
claimed = orders.select(&:persisted?).filter_map(&:prize_key)
claimed = orders.select(&:live?).filter_map(&:prize_key)
Prize::ALL.select { |prize| prize.cost > goal.cost && claimed.exclude?(prize.key) }.min_by { |prize| [ prize.cost, prize.hours ] }
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# A cancelled order (Order#cancel!) gives its prize back, so it can be claimed again: the one-per-prize index skips it.
class LetCancelledOrdersFreeTheirPrize < ActiveRecord::Migration[8.1]
def change
remove_index :orders, [ :project_id, :prize_key ], unique: true, where: "((prize_key)::text <> 'stickers'::text)", name: "index_orders_on_project_id_and_prize_key"
add_index :orders, [ :project_id, :prize_key ], unique: true, where: "((prize_key)::text <> 'stickers'::text) AND ((status)::text <> 'cancelled'::text)", name: "index_orders_on_project_id_and_prize_key"
end
end
4 changes: 2 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/controllers/admin/mcp_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,7 @@ class Admin::McpControllerTest < ActionDispatch::IntegrationTest
end

test "cancel_order refunds a waiting order's hours, refuses ordered and cancelled ones, and lets them claim another prize" do
travel_to Time.utc(2026, 11, 9, 12) # the claim at the end needs the shop open
project = projects(:ana)
order = make_order("micro", "waiting")
assert_equal 0, project.reload.hours_to_spend
Expand Down
3 changes: 3 additions & 0 deletions test/controllers/orders_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

# POST /shop/order: the claim itself. The shop's page around it is in ShopControllerTest.
class OrdersControllerTest < ActionDispatch::IntegrationTest
# The shop closed at Program::SHOP_CLOSES_AT. These tests claim prizes, so they run on the last shop day.
setup { travel_to Time.utc(2026, 11, 9, 12) }

include ActiveJob::TestHelper

setup do
Expand Down
22 changes: 22 additions & 0 deletions test/controllers/shop_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require "test_helper"

class ShopControllerTest < ActionDispatch::IntegrationTest
# The shop closed at Program::SHOP_CLOSES_AT. These tests claim prizes, so they run on the last shop day.
setup { travel_to Time.utc(2026, 11, 9, 12) }

include ActiveJob::TestHelper

setup do
Expand Down Expand Up @@ -97,6 +100,25 @@ class ShopControllerTest < ActionDispatch::IntegrationTest
assert_select ".shop__stat--ship .shop__stat-value", "1 hrs"
end

test "a cancelled order puts its prize back on the shelf, and it can be claimed again" do
micro = Prize.find("micro")
approve(hours: micro.hours)
sign_in_orpheus
post shop_order_path, params: { order: { prize_key: "micro" } }
assert @project.orders.last.cancel!

get shop_path
assert_response :success
assert_select "#shop-micro[data-state=ready] form[action=?]", shop_order_path

assert_difference -> { @project.orders.count }, 1 do
post shop_order_path, params: { order: { prize_key: "micro" } }
end
assert_equal %w[cancelled waiting], @project.orders.order(:id).pluck(:status)
get shop_path
assert_select "#shop-micro[data-state=claimed] .shop-prize__status", /in the queue/
end

test "the shop page has no parcel name field: it sits in each claimable prize's sheet, prefilled" do
micro = Prize.find("micro")
approve(hours: micro.hours + 1)
Expand Down
3 changes: 3 additions & 0 deletions test/helpers/shop_helper_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require "test_helper"

class ShopHelperTest < ActionView::TestCase
# The shop closed at Program::SHOP_CLOSES_AT. These tests claim prizes, so they run on the last shop day.
setup { travel_to Time.utc(2026, 11, 9, 12) }

include ProjectsHelper

test "the shelf is every prize, fewest hours first, with a project's goal marked and no state without one" do
Expand Down
3 changes: 3 additions & 0 deletions test/helpers/sticker_promise_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

# The free stickers' card on the project page (D-051). Fixtures are stamped at the real clock, so every test pins created_at.
class StickerPromiseHelperTest < ActionView::TestCase
# The shop closed at Program::SHOP_CLOSES_AT. These tests claim prizes, so they run on the last shop day.
setup { travel_to Time.utc(2026, 11, 9, 12) }

setup do
@user = users(:orpheus)
@user.update_column(:created_at, Time.utc(2026, 10, 25, 10))
Expand Down
2 changes: 2 additions & 0 deletions test/jobs/airtable_sync_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def approved_ship(approved_hours: 10)
end

test "copies orders to their own table" do
travel_to Time.utc(2026, 11, 9, 12) # it claims a prize, so the shop must be open
approved_ship(approved_hours: 100)
order = @orpheus.reload.orders.create!(prize_key: @orpheus.prize_key, address: HQ_ADDRESS_WITH_PHONE)
Airtable::SyncJob.perform_now
Expand Down Expand Up @@ -234,6 +235,7 @@ def approved_ship(approved_hours: 10)
end

test "every field a row writes exists in the schema" do
travel_to Time.utc(2026, 11, 9, 12) # it claims a prize, so the shop must be open
ship = approved_ship(approved_hours: 100)
order = @orpheus.reload.orders.create!(prize_key: @orpheus.prize_key, address: HQ_ADDRESS_WITH_PHONE)
assert_empty AirtableFields.user(ship.project.user).keys - AirtableSchema.field_names(:users)
Expand Down
3 changes: 3 additions & 0 deletions test/models/dev_my_project_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

# /dev's controls on your own project (Dev::MyProject), and the fake Hackatime hours they pin.
class DevMyProjectTest < ActiveSupport::TestCase
# The shop closed at Program::SHOP_CLOSES_AT. These tests claim prizes, so they run on the last shop day.
setup { travel_to Time.utc(2026, 11, 9, 12) }

setup do
@user = users(:orpheus)
@project = projects(:orpheus)
Expand Down
6 changes: 6 additions & 0 deletions test/models/order_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require "test_helper"

class OrderTest < ActiveSupport::TestCase
# The shop closed at Program::SHOP_CLOSES_AT. These tests claim prizes, so they run on the last shop day.
setup { travel_to Time.utc(2026, 11, 9, 12) }

include ActiveJob::TestHelper

setup do
Expand Down Expand Up @@ -295,6 +298,9 @@ def approve_at_start(hours:)
# under the project's row lock, so only one is saved. Threads each need their own database connection, so nothing here rolls
# back by itself: the test removes what it made.
class OrderConcurrencyTest < ActiveSupport::TestCase
# The shop closed at Program::SHOP_CLOSES_AT. These tests claim prizes, so they run on the last shop day.
setup { travel_to Time.utc(2026, 11, 9, 12) }

self.use_transactional_tests = false

# Holds a claim between its check and its insert until the other one has also checked (or a second has passed, which is
Expand Down
3 changes: 3 additions & 0 deletions test/models/project_goal_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# The goal prize: when your approved hours reach it (the earned-it popup, shown once per goal), the next prize up to
# offer, and what claiming a cheaper prize leaves of your hours.
class ProjectGoalTest < ActiveSupport::TestCase
# The shop closed at Program::SHOP_CLOSES_AT. These tests claim prizes, so they run on the last shop day.
setup { travel_to Time.utc(2026, 11, 9, 12) }

setup do
@project = projects(:orpheus)
@project.update!(prize_key: "u2c") # 8BitDo Ultimate 2C
Expand Down
Loading