REFACTOR: makes reminders to be in raw

REFACTOR: makes reminders to be in raw

diff --git a/app/controllers/discourse_post_event/events_controller.rb b/app/controllers/discourse_post_event/events_controller.rb
index a55727a..21d8c5f 100644
--- a/app/controllers/discourse_post_event/events_controller.rb
+++ b/app/controllers/discourse_post_event/events_controller.rb
@@ -36,7 +36,7 @@ module DiscoursePostEvent
     end
 
     def show
-      event = Event.includes(:reminders).find(params[:id])
+      event = Event.find(params[:id])
       guardian.ensure_can_see!(event.post)
       serializer = EventSerializer.new(event, scope: guardian)
       render_json_dump(serializer)
@@ -56,7 +56,6 @@ module DiscoursePostEvent
         guardian.ensure_can_edit!(event.post)
         guardian.ensure_can_act_on_discourse_post_event!(event)
         event.update_with_params!(event_params)
-        event.update_reminders!(event_reminders_params)
         serializer = EventSerializer.new(event, scope: guardian)
         render_json_dump(serializer)
       end
@@ -132,10 +131,6 @@ module DiscoursePostEvent
 
     private
 
-    def event_reminders_params
-      Array(params.require(:event).permit(reminders: [:id, :value, :unit])[:reminders])
-    end
-
     def event_params
       allowed_custom_fields = SiteSetting.discourse_post_event_allowed_custom_fields.split('|')
 
diff --git a/app/controllers/discourse_post_event/reminders_controller.rb b/app/controllers/discourse_post_event/reminders_controller.rb
deleted file mode 100644
index 2577c0d..0000000
--- a/app/controllers/discourse_post_event/reminders_controller.rb
+++ /dev/null
@@ -1,14 +0,0 @@
-# frozen_string_literal: true
-
-module DiscoursePostEvent
-  class RemindersController < DiscoursePostEventController
-    def destroy
-      event = Event.find_by(id: params[:post_id])
-      reminder = event.reminders.find_by(id: params[:id])
-      guardian.ensure_can_act_on_discourse_post_event!(event)
-      Jobs.cancel_scheduled_job(:discourse_post_event_send_reminder, reminder_id: reminder.id)
-      reminder.destroy!
-      render json: success_json
-    end
-  end
-end
diff --git a/app/models/discourse_post_event/event.rb b/app/models/discourse_post_event/event.rb
index b9b9518..4072ed9 100644
--- a/app/models/discourse_post_event/event.rb
+++ b/app/models/discourse_post_event/event.rb
@@ -51,7 +51,9 @@ module DiscoursePostEvent
             Jobs.enqueue_at(will_start_at, :discourse_post_event_event_will_start, event_id: self.id)
           end
         end
+      end
 
+      if saved_change_to_starts_at || saved_change_to_reminders
         self.refresh_reminders!
       end
 
@@ -68,7 +70,6 @@ module DiscoursePostEvent
     end
 
     has_many :invitees, foreign_key: :post_id, dependent: :delete_all
-    has_many :reminders, foreign_key: :post_id, dependent: :delete_all
     belongs_to :post, foreign_key: :id
 
     scope :visible, -> { where(deleted_at: nil) }
@@ -233,7 +234,8 @@ module DiscoursePostEvent
           ends_at: event_params[:end],
           url: event_params[:url],
           status: event_params[:status].present? ? Event.statuses[event_params[:status].to_sym] : event.status,
-          raw_invitees: event_params[:"allowed-groups"] ? event_params[:"allowed-groups"].split(',') : nil
+          raw_invitees: event_params[:"allowed-groups"] ? event_params[:"allowed-groups"].split(',') : nil,
+          reminders: event_params[:reminders],
         }
 
         event.update_with_params!(params)
@@ -261,16 +263,16 @@ module DiscoursePostEvent
     end
 
     def refresh_reminders!
-      self.reminders.each(&:refresh!)
-    end
+      (self.reminders || '').split(',').map do |reminder|
+        value, unit = reminder.split('.')
+
+        if transaction_include_any_action?([:update])
+          Jobs.cancel_scheduled_job(:discourse_post_event_send_reminder, event_id: self.id, reminder: reminder)
+        end
 
-    def update_reminders!(reminders)
-      reminders.each do |reminder|
-        if reminder[:id]
-          model = Reminder.find(reminder[:id])
-          model.update!(value: reminder[:value], unit: reminder[:unit])
-        else
-          model = Reminder.create!(value: reminder[:value], unit: reminder[:unit], post_id: self.id)
+        enqueue_at = self.starts_at - value.to_i.send(unit)
+        if enqueue_at > Time.now
+          Jobs.enqueue_at(enqueue_at, :discourse_post_event_send_reminder, event_id: self.id, reminder: reminder)
         end
       end
     end
diff --git a/app/serializers/discourse_post_event/event_serializer.rb b/app/serializers/discourse_post_event/event_serializer.rb
index b055a5b..e17c718 100644
--- a/app/serializers/discourse_post_event/event_serializer.rb
+++ b/app/serializers/discourse_post_event/event_serializer.rb
@@ -28,6 +28,13 @@ module DiscoursePostEvent
       scope.can_act_on_discourse_post_event?(object)
     end
 
+    def reminders
+      (object.reminders || '').split(',').map do |reminder|
+        value, unit = reminder.split('.')
+        { value: value.to_i, unit: unit }
+      end
+    end
+
     def is_expired
       object.is_expired?
     end
diff --git a/assets/javascripts/discourse/controllers/discourse-post-event-builder.js.es6 b/assets/javascripts/discourse/controllers/discourse-post-event-builder.js.es6
index 9b65636..7991a40 100644
--- a/assets/javascripts/discourse/controllers/discourse-post-event-builder.js.es6
+++ b/assets/javascripts/discourse/controllers/discourse-post-event-builder.js.es6
@@ -9,7 +9,7 @@ import { Promise } from "rsvp";
 
 import { buildParams, replaceRaw } from "../../lib/raw-event-helper";
 
-const DEFAULT_REMINDER = { value: 15, unit: "minutes", type: "notification" };
+const DEFAULT_REMINDER = { value: 15, unit: "minutes" };
 
 export default Controller.extend(ModalFunctionality, {
   reminders: null,
@@ -44,7 +44,7 @@ export default Controller.extend(ModalFunctionality, {
 
   allowsInvitees: equal("model.eventModel.status", "private"),
 
-  addReminderDisabled: gte("reminders.length", 5),
+  addReminderDisabled: gte("model.eventModel.reminders.length", 5),
 
   @action
   onChangeCustomField(field, event) {
@@ -60,18 +60,6 @@ export default Controller.extend(ModalFunctionality, {
   @action
   removeReminder(reminder) {
     this.model.eventModel.reminders.removeObject(reminder);
-
-    if (reminder.id) {
-      this.set("isLoadingReminders", true);
-
-      this.store
-        .createRecord("discourse-post-event-reminder", {
-          id: reminder.id,
-          post_id: this.model.eventModel.id
-        })
-        .destroyRecord()
-        .finally(() => this.set("isLoadingReminders", false));
-    }
   },
 
   @action
@@ -169,10 +157,7 @@ export default Controller.extend(ModalFunctionality, {
       const promises = [];
 
       // custom_fields are not stored on the raw and are updated separately
-      const data = this.model.eventModel.getProperties(
-        "custom_fields",
-        "reminders"
-      );
+      const data = this.model.eventModel.getProperties("custom_fields");
       promises.push(this.model.eventModel.update(data));
 
       const updateRawPromise = new Promise(resolve => {
diff --git a/assets/javascripts/discourse/templates/modal/discourse-post-event-builder.hbs b/assets/javascripts/discourse/templates/modal/discourse-post-event-builder.hbs
index 176d6f6..371177b 100644
--- a/assets/javascripts/discourse/templates/modal/discourse-post-event-builder.hbs
+++ b/assets/javascripts/discourse/templates/modal/discourse-post-event-builder.hbs
@@ -77,46 +77,44 @@
         }}
       {{/event-field}}
 
-      {{#if model.eventModel.reminders}}
-        {{#event-field class="reminders" label="discourse_post_event.builder_modal.reminders.label"}}
-          <div class="reminders-list">
-            {{#each model.eventModel.reminders as |reminder|}}
-              <div class="reminder-item">
-                {{input
-                  class="reminder-value"
-                  value=(readonly reminder.value)

[... diff too long, it was truncated ...]

GitHub sha: ad57efb6

I wonder if reminders would be better stored in a separate table with a one to many relationship? Something to keep in mind.

I did this first, and went back on this design… He wasn’t giving me any value for now.