Sha256: 276b1a6a4d556964aafd7138849a56066b3a83b3ec3a8a978c1246ed56a11585

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

module JeraPush::Services
  class SendToDevicesService < JeraPush::Services::BaseService
    def initialize(devices:, title:, body:, data:, android: {}, ios: {})
      super
      @devices = devices
      @title = title
      @body = body
      @data = data
      @android_config = android
      @ios_config = ios
    end

    def call
      validate_device

      return @errors if @errors.present?

      create_message
      send_push_to_devices

      @message
    end

    private

    def validate_device
      @errors.add(:base, message: I18n.t('jera_push.services.errors.not_found.device')) if @devices.empty?
    end

    def create_message
      @message = JeraPush::Message.create(title: @title, body: @body, data: data_params)
    end

    def send_push_to_devices
      @devices.each do |device|
        message_device = @message.message_devices.create(device: device)
        send_push(device, message_device)
      end
    end

    def send_push(device, message_device)
      JeraPush::Services::SendPushService.new(device: device, 
        message: @message, 
        message_device: message_device,
        ios_config: @ios_config,
        android_config: @android_config
      ).call
    end

    def data_params
      @data.merge({ title: @title, body: @body })
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jera_push-2.0.0 lib/jera_push/services/send_to_devices_service.rb