Sha256: 568cb429fe351a6d5f0ffdd25952406a4b383c2d1b870e7d909f08228daf48b2

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

module Motor
  module Notes
    module NotifyReminder
      NOTIFICATION_DESCRIPTION_LIMIT = 100

      module_function

      def call(reminder)
        notification = find_or_build_notification(reminder)

        return if notification.persisted?

        notification.save!

        Motor::NotificationsMailer.notify_reminder_email(notification).deliver_later!
        Motor::NotificationsChannel.broadcast_to(reminder.recipient,
                                                 ['notify', notification.as_json(include: %i[record])])
      end

      def find_or_build_notification(reminder)
        notification = reminder.notifications.take

        return notification if notification

        note = reminder.record

        Motor::Notification.new(
          title: build_notification_title(note),
          description: note.body.truncate(NOTIFICATION_DESCRIPTION_LIMIT),
          recipient: reminder.recipient,
          record: reminder
        )
      end

      def build_notification_title(note)
        configs = Motor::BuildSchema.for_model(note.record.class)

        display_value = note.record.attributes[configs['display_column']]

        I18n.t('new_reminder_for',
               resource: ["#{configs['display_name'].singularize} ##{note.record[note.record.class.primary_key]}",
                          display_value].join(' '),
               scope: :motor)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
motor-admin-0.4.3 lib/motor/notes/notify_reminder.rb
motor-admin-0.4.2 lib/motor/notes/notify_reminder.rb
motor-admin-0.4.0 lib/motor/notes/notify_reminder.rb