From 9b84b2d76a4ded965904f692a6022bdd830835e0 Mon Sep 17 00:00:00 2001 From: Dylan Jones Date: Wed, 11 Dec 2024 23:27:38 +0000 Subject: [PATCH] Set the hat to be the jumper and cache donations into db to prevent spamming hats Add to cron --- app/controllers/static_pages_controller.rb | 1 + app/models/christmas_jumper_donation.rb | 31 +++++++++++++++++-- ...30936_create_christmas_jumper_donations.rb | 10 ++++++ db/schema.rb | 30 +++++++++++++++++- 4 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20241211230936_create_christmas_jumper_donations.rb diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index 8dec49d..8939b82 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -154,6 +154,7 @@ def webfinger # GET /cron.json # GET /cron.xml def cron + ChristmasJumperDonation.update Track.update BbcTwitter.update PringlesPrice.update diff --git a/app/models/christmas_jumper_donation.rb b/app/models/christmas_jumper_donation.rb index 785c3ef..019f904 100644 --- a/app/models/christmas_jumper_donation.rb +++ b/app/models/christmas_jumper_donation.rb @@ -1,9 +1,10 @@ -class ChristmasJumperDonation +class ChristmasJumperDonation < ApplicationRecord PROJECT = "pub-tracker-live" BASE_URL = "https://firestore.googleapis.com/v1/projects/#{PROJECT}/databases/(default)/documents" DONATIONS_URL = "https://api.justgiving.com/904af047/v1/fundraising/pages/cjd240033091/donations" + def self.get_donations begin response = JSON.parse(RestClient.get(DONATIONS_URL, accept: "application/json").body) @@ -16,7 +17,19 @@ def self.get_donations def self.set_christmas_jumper_hat user_id - puts "Give hat to #{user_id}" + data = { + "fields": { + "equipped": { + "mapValue": { + "fields": { + "hat": { "stringValue": "https://dyl.anjon.es/images/jumper.png" } + } + } + } + } + } + url = "#{BASE_URL}/profiles/#{user_id}?mask.fieldPaths=equipped.hat&updateMask.fieldPaths=equipped.hat" + response = RestClient.patch(url, data.to_json, content_type: :json) end @@ -34,8 +47,20 @@ def self.update_pub_thursday_user justgiving_name def self.update self.get_donations().each do |donation| + justgiving_id = donation["id"].to_s justgiving_name = donation["donorDisplayName"] - self.update_pub_thursday_user(justgiving_name) + next if ChristmasJumperDonation.exists?(donation_id: justgiving_id) + + begin + self.update_pub_thursday_user(justgiving_name) + rescue => e + logger.info "Failed to update pub thursday_user #{justgiving_name}: " + e.message + end + + ChristmasJumperDonation.create({ + donation_id: justgiving_id, + name: justgiving_name + }) end return end diff --git a/db/migrate/20241211230936_create_christmas_jumper_donations.rb b/db/migrate/20241211230936_create_christmas_jumper_donations.rb new file mode 100644 index 0000000..9a6ac9d --- /dev/null +++ b/db/migrate/20241211230936_create_christmas_jumper_donations.rb @@ -0,0 +1,10 @@ +class CreateChristmasJumperDonations < ActiveRecord::Migration[7.0] + def change + create_table :christmas_jumper_donations do |t| + t.string :donation_id + t.string :name + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 2c1e425..b949f53 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[7.0].define(version: 2023_08_07_225945) do +ActiveRecord::Schema[7.0].define(version: 2024_12_11_230936) do create_table "accounts", id: :integer, charset: "utf8mb3", force: :cascade do |t| t.string "number" t.string "name" @@ -154,6 +154,13 @@ t.index ["naptan_code"], name: "index_bus_stops_on_naptan_code" end + create_table "christmas_jumper_donations", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t| + t.string "donation_id" + t.string "name" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "credentials", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t| t.integer "user_id" t.string "external_id" @@ -191,6 +198,27 @@ t.datetime "updated_at", precision: nil end + create_table "duo_leaderboards", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t| + t.string "name" + t.string "url" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["url"], name: "index_duo_leaderboards_on_url", unique: true + end + + create_table "duo_leaderboards_participants", id: false, charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t| + t.bigint "duo_participant_id", null: false + t.bigint "duo_leaderboard_id", null: false + t.index ["duo_participant_id", "duo_leaderboard_id"], name: "index_duo" + end + + create_table "duo_participants", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t| + t.string "name" + t.string "photo" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "episodes", id: :integer, charset: "utf8mb3", force: :cascade do |t| t.string "pid", null: false t.string "title", null: false