Sha256: 39017c18b8760b239bd088798ab6826850c2ea91cc68f6e85babe964f1de57b8

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 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', 'role', 'faculty_status',
        'school_type', 'salesforce_contact_id', 'uuid', 'support_identifier', 'is_test'
      ]

      lev_routine transaction: :no_transaction

      protected

      def exec(options={})

        return if OpenStax::Accounts.configuration.enable_stubbing?

        response = OpenStax::Accounts::Api.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.find_by(
            openstax_uid: app_account.account.openstax_uid
          ) || app_account.account
          account.syncing = true

          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::Api.mark_account_updates_as_read(updated_app_accounts)

      end

    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
openstax_accounts-8.0.0 app/routines/openstax/accounts/sync_accounts.rb
openstax_accounts-7.13.1 app/routines/openstax/accounts/sync_accounts.rb
openstax_accounts-7.13.0 app/routines/openstax/accounts/sync_accounts.rb