FEATURE: Gift Exchange 2019

FEATURE: Gift Exchange 2019

Creates the “gift-exchange-2019” group that will be used to run the 2019 Discourse Holiday Gift Exchange.

Adds a scheduled job that will run the Gift Exchange every Nov 20th.

The job will send a PM to every members of the “gift-exchange-{year}” group with their match and a small explanation on what to do.

diff --git a/jobs/scheduled/check_cakeday.rb b/jobs/scheduled/check_cakeday.rb
index 807b1dc..be1015e 100644
--- a/jobs/scheduled/check_cakeday.rb
+++ b/jobs/scheduled/check_cakeday.rb
@@ -67,4 +67,4 @@ module Jobs
     end
   end
 end
-    
+
diff --git a/jobs/scheduled/check_logs_game.rb b/jobs/scheduled/check_logs_game.rb
index eb416ee..a4230e9 100644
--- a/jobs/scheduled/check_logs_game.rb
+++ b/jobs/scheduled/check_logs_game.rb
@@ -114,7 +114,6 @@ module Jobs
       def assign_next!
         TopicAssigner.new(@topic, Discourse.system_user).assign(nominee)
         update_assigned_to_in_round(users_assigned_to_in_round + [nominee.id])
-
       end
     end
   end
diff --git a/jobs/scheduled/gift_exchange.rb b/jobs/scheduled/gift_exchange.rb
new file mode 100644
index 0000000..f85edff
--- /dev/null
+++ b/jobs/scheduled/gift_exchange.rb
@@ -0,0 +1,58 @@
+# frozen_string_literal: true
+
+module Jobs
+
+  class ::DevAdditions::GiftExchange < ::Jobs::Scheduled
+    every 6.hours
+
+    GIFT_EXCHANGE_CUSTOM_FIELD ||= "gifts-were-exchanged"
+
+    def execute(args = nil)
+      return unless SiteSetting.dev_additions_enabled
+
+      now = Time.zone.now
+      return if now.month != 11 && now.day != 20
+
+      group_name = "gift-exchange-#{now.year}"
+      return unless group = Group.find_by(name: group_name)
+
+      return if group.custom_fields[GIFT_EXCHANGE_CUSTOM_FIELD].present?
+
+      usernames = group.users.pluck(:username)
+      return if usernames.size < 3
+
+      usernames.shuffle!
+      usernames << usernames[0]
+
+      usernames.each_cons(2).each do |gifter, giftee|
+        raw = <<~MD
+        Thanks for signing up for the **#{now.year} Discourse Holiday Secret Gift Exchange**!
+
+        You have been matched with @#{giftee}!
+
+        Here's what you need to do:
+
+        - Order them a gift by **Dec 1st**.
+        - The recommended spend is **$20 USD**. You may spend less or more; just try to make your gift thoughtful! If you don't know much about your match, now is a great time to find out!
+        - Your match's address will be [here](https://dev.discourse.org/t/1882).
+
+        Since the team is remote, it might be easier and cheaper to find an online retailer closer to them to reduce shipping costs and time.
+
+        Thanks for participating and have fun!
+        MD
+
+        PostCreator.new(
+          Discourse.system_user,
+          archetype: Archetype.private_message,
+          target_usernames: gifter,
+          title: "#{now.year} Discourse Holiday Secret Gift :gift: Exchange",
+          raw: raw,
+        ).create
+      end
+
+      group.custom_fields[GIFT_EXCHANGE_CUSTOM_FIELD] = true
+      group.save_custom_fields
+    end
+  end
+
+end
diff --git a/plugin.rb b/plugin.rb
index 3d83e40..ecf237c 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -9,17 +9,28 @@
 enabled_site_setting :dev_additions_enabled
 
 module ::DevAdditions
-  MANAGER_CUSTOM_FIELD = "user_manager"
-  CAKEDAY_NOTICE_CUSTOM_FIELD = "cakeday_notice"
-  CAKEDAY_NOTICE_TOPIC_CUSTOM_FIELD = "review_manager"
+  MANAGER_CUSTOM_FIELD ||= "user_manager"
+  CAKEDAY_NOTICE_CUSTOM_FIELD ||= "cakeday_notice"
+  CAKEDAY_NOTICE_TOPIC_CUSTOM_FIELD ||= "review_manager"
 end
 
 after_initialize do
   [
     "../jobs/scheduled/check_logs_game.rb",
     "../jobs/scheduled/check_cakeday.rb",
+    "../jobs/scheduled/gift_exchange.rb",
   ].each { |path| require File.expand_path(path, __FILE__) }
 
   register_editable_user_custom_field(DevAdditions::MANAGER_CUSTOM_FIELD)
   whitelist_staff_user_custom_field(DevAdditions::MANAGER_CUSTOM_FIELD)
+
+  unless Group.exists?(name: "gift-exchange-2019")
+    Group.create(
+      name: "gift-exchange-2019",
+      full_name: "2019 Holiday Secret Gift Exchange",
+      bio_raw: "Santas :santa: of the 2019 Holiday Secret Gift :gift: Exchange",
+      flair_url: "fa-gifts",
+      public_admission: true,
+    )
+  end
 end

GitHub sha: d63a7315

1 Like