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
13 changes: 13 additions & 0 deletions app/assets/stylesheets/save_point/pages/admin-ships.css
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@
.pg-admin-ships__table tr[data-status="awaiting_fraud"] .pg-admin-ships__status::before { --icon: url("/save_point/icons/search-fill.svg"); }
.pg-admin-ships__table td[data-decision] small { margin-top: 8px; font-weight: 700; }

/* The Unified YSWS DB's side of an approved ship: a small soft pill under the decision, no outline, its edge a darker shade of
its own fill. Mint is in Unified, gold is on its way, coral needs a look, and a ship not sent yet is a plain sunken pill. */
.pg-admin-ships__table td[data-decision] small.pg-admin-ships__unified {
--fill: var(--surface-sunken); --ink-on: var(--ink-muted);
display: block; width: fit-content; max-width: 260px; overflow: hidden; text-overflow: ellipsis;
padding: 2px 10px; border-radius: 999px; font-size: 12px; line-height: 18px;
background: var(--fill); color: var(--ink-on); box-shadow: inset 0 -2px 0 var(--shade);
}
.pg-admin-ships__unified[data-unified="sent"] { --fill: var(--mint-soft); --ink-on: var(--mint-ink); }
.pg-admin-ships__unified[data-unified="processing"] { --fill: var(--gold-soft); --ink-on: var(--gold-ink); }
.pg-admin-ships__unified[data-unified="held"],
.pg-admin-ships__unified[data-unified="error"] { --fill: var(--coral-soft); --ink-on: var(--coral-ink); }

/* ---------- Narrow: each row is a card ---------- */
@media (max-width: 759px) {
.pg-admin-ships__panel { overflow: visible; background: none; border: 0; box-shadow: none; border-radius: 0; }
Expand Down
29 changes: 28 additions & 1 deletion app/jobs/airtable/sync_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def sync(scope, table, prefix, read_at: Time.current, rows: nil, &fields)

# Only approved ships cross, only when marked unsynced, and never a row the Unified YSWS DB has taken.
def sync_ships
refresh_unified_status
read_at = Time.current
scope = Ship.approved.where(in_unified: false, synced_at: nil).includes(:screenshot_attachment, project: :user)
ships = stale(due(scope))
Expand All @@ -72,8 +73,34 @@ def sync_ships
ships -= unsent
taken, fresh = ships.partition { existing.dig("ship-#{it.id}", "fields", AirtableSchema::UNIFIED_FIELD).present? }
Ship.where(id: taken.map(&:id)).update_all(in_unified: true)
fresh.each { store_unified_status(it, existing["ship-#{it.id}"]) }
# D-156: a row the Unified automation turned away (an error, with the box still ticked) only runs again when the box is ticked
# anew. It goes out unticked this tick and stays unsynced, so the next tick ticks it.
reticked = fresh.select { existing.dig("ship-#{it.id}", "fields", AirtableFields::SUBMIT_TO_UNIFIED_FIELD) && existing.dig("ship-#{it.id}", "fields", AirtableSchema::ERROR_FIELD).present? }
# D-111: the row Airtable has already is read, so a deflation sentence the ship no longer has is cleared rather than left (AirtableFields.clearing_stale_deflation).
sync(scope, @ship_table, "ship-", read_at:, rows: fresh) { AirtableFields.clearing_stale_deflation(AirtableFields.ship(it, submit_to_unified: it.submit_to_unified?), existing["ship-#{it.id}"]) }
sync(scope, @ship_table, "ship-", read_at:, rows: fresh) do
AirtableFields.clearing_stale_deflation(AirtableFields.ship(it, submit_to_unified: it.submit_to_unified? && reticked.exclude?(it)), existing["ship-#{it.id}"])
end
Ship.where(id: reticked.map(&:id)).update_all(synced_at: nil)
end

# What the Airtable rows say about the Unified automation, for the admin ships list (Ship#unified_state). A row already sent and
# waiting on the automation (or in an error) is not read by the send above, so the oldest-read ones are read here, 20 a tick.
def refresh_unified_status
ships = Ship.approved.where(in_unified: false).where.not(airtable_record_id: nil)
.order(Arel.sql("ships.unified_checked_at ASC NULLS FIRST")).limit(20).to_a
return if ships.empty?

found = @client.find_by(@ship_table, ID, ships.map { "ship-#{it.id}" })
ships.each { store_unified_status(it, found["ship-#{it.id}"]) }
rescue AirtableClient::Error => e
Rails.logger.warn("airtable sync: unified status not read, #{e.message}")
end

def store_unified_status(ship, record)
fields = record&.dig("fields").to_h
ship.update_columns(unified_status: fields[AirtableSchema::STATUS_FIELD].presence, unified_error: fields[AirtableSchema::ERROR_FIELD].presence,
unified_checked_at: Time.current)
end

# Upserts the rows. Returns [rows done, rows failed]. A failed batch is tried one row at a time.
Expand Down
26 changes: 25 additions & 1 deletion app/models/airtable_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,31 @@ def duplicate(s, earlier, window = nil)
"(#{same.map { "#{us_date(it.created_at.utc.to_date)}, #{hours_text(cumulative_hours(it))} since Save Point began" }.join("; ")})."
parts << "This record claims only the #{hours_text(new_hours(s, window))} since then, #{date_range(s, window)}."
end
parts.join(" ").presence
parts << teammate_text(s, others, window)
parts.compact.join(" ").presence
end

# D-156: Unified's submit automation turns a row away when its code URL is on a record already, unless this field holds text.
# A teammate on the same repo is common practice and goes through as is, so the sync writes the sentence itself. It names the
# program and the approval date of each record, never the other person.
def teammate_text(s, others, window)
user = s.project.user
records = others.select { it.other_person?(user) }.map { record_text(it) }
records += teammate_ships(s).map { "Save Point (approved #{us_date((it.decided_at || it.updated_at).utc.to_date)})" }
return if records.empty?

"This code URL is also in the Unified YSWS DB for a teammate on the same repo (#{records.join("; ")}). " \
"This record is a different person and claims only their own Hackatime time, #{date_range(s, window)}."
end

# Approved Save Point ships of other accounts on the same repo that the Unified YSWS DB has taken, one per project.
def teammate_ships(s)
key = Ship::EarlierShip.normalize(s.repo_url)
path = key.split("/", 2).last.to_s
return [] unless path.include?("/")

Ship.approved.joins(:project).where.not(projects: { user_id: s.project.user_id }).where("ships.repo_url ILIKE ?", "%#{Ship.sanitize_sql_like(path)}%")
.order(:decided_at, :id).select { Ship::EarlierShip.normalize(it.repo_url) == key && it.sent_to_unified? }.uniq(&:project_id)
end

# D-149: what dates a record covers and whether they meet the time counted here (start to the ship), said plainly. Never a guess:
Expand Down
3 changes: 3 additions & 0 deletions app/models/airtable_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
module AirtableSchema
ID_FIELD = "save_point_id"
UNIFIED_FIELD = "Automation - YSWS Record ID"
# What the Unified automation writes back (checked 2026-11-08): a formula such as "2–Submitted", and the error text.
STATUS_FIELD = "Automation - Status"
ERROR_FIELD = "Automation - Error"

DATETIME = { dateFormat: { name: "iso" }, timeFormat: { name: "24hour" }, timeZone: "utc" }.freeze
DATE = { dateFormat: { name: "iso" } }.freeze
Expand Down
18 changes: 18 additions & 0 deletions app/models/ship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,24 @@ def unified_holds
holds
end

# The Unified YSWS DB has this ship: it took the row (in_unified), or the Airtable row's "Automation - Status" reads
# "2–Submitted" (the sync copies it into unified_status).
def sent_to_unified? = in_unified? || unified_status.to_s.start_with?("2")

# Where an approved ship stands on its way to the Unified YSWS DB, from stored data only (the sync keeps it, no Airtable call).
# Returns [key, label, detail]: key is sent, held, error, processing or not_synced; detail is the full text for a hover.
def unified_state
return [ "sent", "Sent to Unified", nil ] if sent_to_unified?

holds = unified_holds
return [ "held", "Held: #{holds.first.delete_suffix(".")}", holds.join(" ") ] if holds.any?
return [ "error", "Error: #{unified_error.squish.truncate(60)}", unified_error ] if unified_error.present?
return [ "error", "Error: sync to Airtable failed", "The sync to Airtable failed and tries again within the hour." ] if airtable_failed_at && synced_at.nil?
return [ "processing", "Processing", unified_status.presence ] if synced_at

[ "not_synced", "Not synced yet", nil ]
end

# The fields the Unified YSWS row needs that are empty, by the row's own names.
def missing_unified_fields
user = project.user
Expand Down
3 changes: 3 additions & 0 deletions app/models/ship/earlier_ship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def same_person?(user)
(hackatime_id.present? && user.hackatime_uid.present? && hackatime_id.to_s == user.hackatime_uid.to_s)
end

# D-156: a record that names someone who is not this user: a teammate on the same repo. One that names nobody is not known to be.
def other_person?(user) = (email.present? || hackatime_id.present?) && !same_person?(user)

# Whether the dates the record covers meet from..to: true, false, or nil when no range is readable (never guessed, D-144).
def overlaps?(from, to)
return if ranges.empty?
Expand Down
5 changes: 5 additions & 0 deletions app/views/admin/ships/_row.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,10 @@
<% elsif !ship.pending? %>
<small class="admin__muted"><%= ship.decided_by&.first_name.presence || "someone" %> · <%= ship.decided_at.strftime("%b %-d") %></small>
<% end %>
<% if ship.approved? %>
<%# The Unified YSWS DB's side, from what the Airtable sync stored (Ship#unified_state). %>
<% state, label, detail = ship.unified_state %>
<small class="pg-admin-ships__unified" data-unified="<%= state %>" title="<%= detail || label %>"><%= label %></small>
<% end %>
</td>
</tr>
9 changes: 9 additions & 0 deletions db/migrate/20261108190000_add_unified_status_to_ships.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# What the Airtable row says about the Unified YSWS automation: "Automation - Status" (for example "2–Submitted") and
# "Automation - Error". The sync reads them back, so the admin ships list can show them with no Airtable call per row.
class AddUnifiedStatusToShips < ActiveRecord::Migration[8.1]
def change
add_column :ships, :unified_status, :string
add_column :ships, :unified_error, :text
add_column :ships, :unified_checked_at, :datetime
end
end
19 changes: 19 additions & 0 deletions db/migrate/20261108190100_resend_ship_50_with_teammate_note.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# D-156: ship 50 (a teammate's ship on a repo ship 51 already put in Unified) was rejected as a duplicate, because its row had no
# "Optional - Override Duplicate Justification". The sync now fills it. So the ship goes through the sync once more, and
# Airtable::SyncJob unticks and reticks a row that sits in an error so the automation runs again. As with D-141's resync,
# the old pods of a rolling deploy still run the old code, so airtable_failed_at is put 50 minutes back: ten minutes on,
# the new code sends it.
class ResendShip50WithTeammateNote < ActiveRecord::Migration[8.1]
def up
execute <<~SQL
UPDATE ships
SET synced_at = NULL,
airtable_failed_at = GREATEST(airtable_failed_at, now() - interval '50 minutes'),
updated_at = now()
WHERE id = 50 AND status = 'approved' AND in_unified = false
SQL
end

def down
end
end
5 changes: 4 additions & 1 deletion db/schema.rb

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

39 changes: 39 additions & 0 deletions test/controllers/admin/ships_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,45 @@ class Admin::ShipsControllerTest < ActionDispatch::IntegrationTest
assert_select "tr##{dom_id(@ship)} [data-decision=approved]", /Approved.*Orpheus/m
end

test "an approved ship shows where it stands with the Unified YSWS DB, from what the sync stored" do
@ship.update_columns(approved_columns(7.5, decided_at: Time.current, decided_by_id: users(:orpheus).id))
sign_in_as(users(:orpheus))
badge = "tr##{dom_id(@ship)} .pg-admin-ships__unified"

get admin_ships_path
assert_select badge, text: "Held: Real name is not ticked"
assert_select "#{badge}[data-unified=held]"
assert_select "#{badge}[title*='Real name is not ticked.']"

@ship.update_columns(in_unified: true)
get admin_ships_path
assert_select "#{badge}[data-unified=sent]", "Sent to Unified"

@ship.update_columns(in_unified: false, unified_status: "2–Submitted")
get admin_ships_path
assert_select "#{badge}[data-unified=sent]", "Sent to Unified"
end

test "Ship#unified_state names each state" do
ship = @ship
ship.update_columns(approved_columns(7.5))
ship.define_singleton_method(:unified_holds) { [] }
assert_equal [ "not_synced", "Not synced yet", nil ], ship.unified_state
ship.synced_at = Time.current
assert_equal [ "processing", "Processing", nil ], ship.unified_state
ship.unified_error = "Possible duplicate of recABC. Fill 'Optional - Override Duplicate Justification' to override."
key, label, detail = ship.unified_state
assert_equal "error", key
assert_match(/\AError: Possible duplicate of recABC/, label)
assert_match "to override.", detail
ship.synced_at = nil
ship.unified_error = nil
ship.airtable_failed_at = Time.current
assert_equal "error", ship.unified_state.first
ship.in_unified = true
assert_equal "sent", ship.unified_state.first
end

test "the ships page has a tab for each stage, with its count, and a tab narrows the list" do
sign_in_as(users(:orpheus))
done = projects(:orpheus).ships.create!(@ship.attributes.slice("title", "description", "repo_url", "demo_url").merge(hours: 3, screenshot: screenshot_upload,
Expand Down
27 changes: 27 additions & 0 deletions test/jobs/airtable_sync_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,33 @@ def approved_ship(approved_hours: 10)
assert_empty @fake.rows(SUBMISSIONS), "a taken ship is not looked at again"
end

test "D-156: the sync copies the automation's status and error onto a sent ship, for the admin list" do
ship = approved_ship
ship.update_columns(airtable_record_id: "recX", synced_at: Time.current)
@fake.existing = { "ship-#{ship.id}" => { "id" => "recX", "fields" => { "Automation - Status" => "1–Error", "Automation - Error" => "Possible duplicate of recABC." } } }
Airtable::SyncJob.perform_now
ship.reload
assert_equal "1–Error", ship.unified_status
assert_equal "Possible duplicate of recABC.", ship.unified_error
assert ship.unified_checked_at
assert_empty @fake.rows(SUBMISSIONS), "reading the status sends nothing"
end

test "D-156: a row the automation turned away goes out unticked first and ticked on the next tick, so the automation runs again" do
ship = approved_ship
ship.update_columns(real_name_first: "Orpheus", real_name_last: "the Dino", review_checklist: { "real_name" => true })
assert ship.submit_to_unified?
@fake.existing = { "ship-#{ship.id}" => { "id" => "recX", "fields" => { AirtableFields::SUBMIT_TO_UNIFIED_FIELD => true, "Automation - Error" => "Possible duplicate of recABC." } } }
Airtable::SyncJob.perform_now
assert_equal false, @fake.rows(SUBMISSIONS).last[AirtableFields::SUBMIT_TO_UNIFIED_FIELD]
assert_nil ship.reload.synced_at, "still unsynced, so the next tick sends it again"

@fake.existing = { "ship-#{ship.id}" => { "id" => "recX", "fields" => { "Automation - Error" => "Possible duplicate of recABC." } } }
Airtable::SyncJob.perform_now
assert_equal true, @fake.rows(SUBMISSIONS).last[AirtableFields::SUBMIT_TO_UNIFIED_FIELD]
assert ship.reload.synced_at
end

test "a ship goes once and is not sent again until it changes, and its screenshot goes only the first time" do
ENV["APP_URL"] = "https://savepoint.hackclub.com"
ship = approved_ship
Expand Down
44 changes: 44 additions & 0 deletions test/models/airtable_fields_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,50 @@ def unexplained(**review)
assert_not AirtableFields.ship(first).key?("Optional - Override Duplicate Justification"), "the first ship has nothing before it"
end

# D-156: a teammate's Save Point ship on the same repo, approved and taken by the Unified YSWS DB (or not yet).
def teammate_ship(in_unified: true, unified_status: nil)
ship = travel_to(Program::STARTS_AT + 2.days) do
projects(:ana).ships.create!(title: "Beat Sheet", description: "A rhythm game where every beat is a cell lighting up in time.",
repo_url: REPO, demo_url: "https://example.com/play", hackatime_projects: [ "rhythm-game" ], hours: 5,
screenshot: Rack::Test::UploadedFile.new(file_fixture("screenshot.png"), "image/png"), screenshot_shows_game: true)
end
ship.update_columns(approved_columns(5).merge(in_unified:, unified_status:, decided_at: Time.utc(2026, 10, 26, 12)))
ship
end

test "D-156: a teammate's Save Point record in Unified fills the note, with no name or email" do
teammate_ship
text = AirtableFields.ship(first_ship)["Optional - Override Duplicate Justification"]
assert_equal "This code URL is also in the Unified YSWS DB for a teammate on the same repo (Save Point (approved 10/26/2026)). " \
"This record is a different person and claims only their own Hackatime time, 10/23/2026-10/25/2026.", text
assert_no_match(/Ana|Lovelace|@/, text)
end

test "D-156: a Save Point teammate the Unified YSWS DB has not taken writes nothing" do
teammate_ship(in_unified: false)
assert_not AirtableFields.ship(first_ship).key?("Optional - Override Duplicate Justification")
end

test "D-156: the sync's copy of the automation status counts as taken" do
teammate_ship(in_unified: false, unified_status: "2–Submitted")
assert_match "a teammate on the same repo (Save Point (approved 10/26/2026))", AirtableFields.ship(first_ship)["Optional - Override Duplicate Justification"]
end

test "D-156: another program's record of someone else is a teammate's, one with no person is not" do
@unified = [ unified_record("Sleepover", "2026-10-31", 3, email: "friend@example.com"), unified_record("jame gam", "2026-10-20", 4) ]
text = AirtableFields.ship(first_ship)["Optional - Override Duplicate Justification"]
assert_match "a teammate on the same repo (Sleepover (approved 10/31/2026, 3 hours)). This record is a different person", text
assert_no_match "friend@example.com", text
assert_equal 1, text.scan("a teammate on the same repo").size
end

test "D-156: a same-person record keeps its D-149 text and the teammate sentence follows it" do
@unified = [ unified_record("Sleepover", "2026-10-31", nil, email: @project.user.email, range: "10/22/2026-10/24/2026") ]
teammate_ship
text = AirtableFields.ship(first_ship)["Optional - Override Duplicate Justification"]
assert_match "The Sleepover record covers 10/22/2026-10/24/2026, which overlaps the time counted here (10/23/2026-10/25/2026). This code URL is also in the Unified YSWS DB for a teammate on the same repo (Save Point (approved 10/26/2026)).", text
end

test "an update with a new code URL is no duplicate, but its hours still start after the earlier ship" do
first_ship
row = AirtableFields.ship(approved(at: Program::STARTS_AT + 5.days, hours: 15, repo: "https://github.com/orpheus/beat-sheet-2"))
Expand Down
Loading