diff --git a/app/helpers/shop_helper.rb b/app/helpers/shop_helper.rb index 5af27d2..e2477a4 100644 --- a/app/helpers/shop_helper.rb +++ b/app/helpers/shop_helper.rb @@ -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 diff --git a/app/helpers/sticker_promise_helper.rb b/app/helpers/sticker_promise_helper.rb index 90ca225..907a2cc 100644 --- a/app/helpers/sticker_promise_helper.rb +++ b/app/helpers/sticker_promise_helper.rb @@ -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) diff --git a/app/models/order.rb b/app/models/order.rb index b20c5a4..4e3a5ba 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -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 @@ -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! @@ -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") diff --git a/app/models/project.rb b/app/models/project.rb index 623384b..df32674 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -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. @@ -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 diff --git a/db/migrate/20261109210050_let_cancelled_orders_free_their_prize.rb b/db/migrate/20261109210050_let_cancelled_orders_free_their_prize.rb new file mode 100644 index 0000000..5f0a081 --- /dev/null +++ b/db/migrate/20261109210050_let_cancelled_orders_free_their_prize.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index bf8dfc7..890949c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.1].define(version: 2026_11_09_171000) do +ActiveRecord::Schema[8.1].define(version: 2026_11_09_210050) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" @@ -197,7 +197,7 @@ t.datetime "airtable_failed_at" t.string "theseus_letter_id" t.index ["ordered_by_id"], name: "index_orders_on_ordered_by_id" - t.index ["project_id", "prize_key"], name: "index_orders_on_project_id_and_prize_key", unique: true, where: "((prize_key)::text <> 'stickers'::text)" + t.index ["project_id", "prize_key"], name: "index_orders_on_project_id_and_prize_key", unique: true, where: "(((prize_key)::text <> 'stickers'::text) AND ((status)::text <> 'cancelled'::text))" end create_table "pairs", force: :cascade do |t| diff --git a/test/controllers/admin/mcp_controller_test.rb b/test/controllers/admin/mcp_controller_test.rb index 446ea08..6fb6a82 100644 --- a/test/controllers/admin/mcp_controller_test.rb +++ b/test/controllers/admin/mcp_controller_test.rb @@ -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 diff --git a/test/controllers/orders_controller_test.rb b/test/controllers/orders_controller_test.rb index 6326136..baa634f 100644 --- a/test/controllers/orders_controller_test.rb +++ b/test/controllers/orders_controller_test.rb @@ -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 diff --git a/test/controllers/shop_controller_test.rb b/test/controllers/shop_controller_test.rb index a6f837c..86512cc 100644 --- a/test/controllers/shop_controller_test.rb +++ b/test/controllers/shop_controller_test.rb @@ -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 @@ -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) diff --git a/test/helpers/shop_helper_test.rb b/test/helpers/shop_helper_test.rb index 1749ac0..11fbcd1 100644 --- a/test/helpers/shop_helper_test.rb +++ b/test/helpers/shop_helper_test.rb @@ -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 diff --git a/test/helpers/sticker_promise_helper_test.rb b/test/helpers/sticker_promise_helper_test.rb index 6db5908..b457a30 100644 --- a/test/helpers/sticker_promise_helper_test.rb +++ b/test/helpers/sticker_promise_helper_test.rb @@ -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)) diff --git a/test/jobs/airtable_sync_job_test.rb b/test/jobs/airtable_sync_job_test.rb index 951b8c6..adb86c3 100644 --- a/test/jobs/airtable_sync_job_test.rb +++ b/test/jobs/airtable_sync_job_test.rb @@ -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 @@ -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) diff --git a/test/models/dev_my_project_test.rb b/test/models/dev_my_project_test.rb index 760f7ec..1d399df 100644 --- a/test/models/dev_my_project_test.rb +++ b/test/models/dev_my_project_test.rb @@ -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) diff --git a/test/models/order_test.rb b/test/models/order_test.rb index 22ec486..7a5adde 100644 --- a/test/models/order_test.rb +++ b/test/models/order_test.rb @@ -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 @@ -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 diff --git a/test/models/project_goal_test.rb b/test/models/project_goal_test.rb index 9b4cb66..0203437 100644 --- a/test/models/project_goal_test.rb +++ b/test/models/project_goal_test.rb @@ -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