Sha256: 281b625e640a421065756f58b457cd287cb633e5304bae0894de9f8e2d4a61d5

Contents?: true

Size: 1.67 KB

Versions: 6

Compression:

Stored size: 1.67 KB

Contents

# Routine for getting account updates from the Accounts server
#
# Should be scheduled to run regularly

module OpenStax
  module Accounts

    class SyncAccounts

      SYNC_ATTRIBUTES = ['username', 'first_name', 'last_name',
                         'full_name', 'title']

      lev_routine transaction: :no_transaction
      
      protected
      
      def exec(options={})

        begin
          OpenStax::Accounts.syncing = true

          return if OpenStax::Accounts.configuration.enable_stubbing?

          response = OpenStax::Accounts.get_application_account_updates

          app_accounts = []
          app_accounts_rep = OpenStax::Accounts::Api::V1::ApplicationAccountsRepresenter
                             .new(app_accounts)
          app_accounts_rep.from_json(response.body)

          return if app_accounts.empty?

          updated_app_accounts = []
          app_accounts.each do |app_account|
            account = OpenStax::Accounts::Account.where(
                        :openstax_uid => app_account.account.openstax_uid).first ||\
                        app_account.account

            if account != app_account.account
              SYNC_ATTRIBUTES.each do |attribute|
                account.send("#{attribute}=", app_account.account.send(attribute))
              end
            end

            next unless account.save

            updated_app_accounts << {user_id: account.openstax_uid,
                                    read_updates: app_account.unread_updates}
          end

          OpenStax::Accounts.mark_account_updates_as_read(updated_app_accounts)
        ensure
          OpenStax::Accounts.syncing = false
        end

      end

    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
openstax_accounts-4.1.1 app/routines/openstax/accounts/sync_accounts.rb
openstax_accounts-4.1.0 app/routines/openstax/accounts/sync_accounts.rb
openstax_accounts-4.0.0 app/routines/openstax/accounts/sync_accounts.rb
openstax_accounts-3.1.1 app/routines/openstax/accounts/sync_accounts.rb
openstax_accounts-3.1.0 app/routines/openstax/accounts/sync_accounts.rb
openstax_accounts-3.0.0 app/routines/openstax/accounts/sync_accounts.rb