Class: Credentials::Builder::ExternalSources::Child

Inherits:
Base
  • Object
show all
Defined in:
app/services/credentials/builder/external_sources/child.rb

Instance Method Summary collapse

Instance Method Details

#changeable_for(type) ⇒ Object (private)

Checks if specific credential changed by user or by external_source

Parameters:

  • type (Symbol)

    possible options: :email or :phone



14
15
16
17
# File 'app/services/credentials/builder/external_sources/child.rb', line 14

def changeable_for(type)
  previous_credential = send("external_source_#{type}_previous")
  previous_credential.blank? || child_credentials.public_send(type).first&.credential == previous_credential
end

#child_user_updatable_for?(credential) ⇒ Boolean (private)

Checks if specific student credential could be updated. Student should exists Student should be invitee or suspended or connected to SIS with active users update enabled Credential should not be taken by another User

Parameters:

  • credential (String)

    credential value, could be email or phone number

Returns:

  • (Boolean)


65
66
67
68
69
# File 'app/services/credentials/builder/external_sources/child.rb', line 65

def child_user_updatable_for?(credential)
  child_user.present? &&
    (child_user.invitee? || child_user.suspended? || sis_connector&.update_active_users?) &&
    !exists_by_credential?(credential)
end

#performable_credential?(credential, type) ⇒ Boolean (private)

Checks if specific credential could be updated.

Parameters:

  • credential (String)

    credential value, could be email or phone number

  • type (Symbol)

    possible options: :email or :phone

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'app/services/credentials/builder/external_sources/child.rb', line 42

def performable_credential?(credential, type)
  child_performable = credential.present? && changeable_for(type)
  student_performable = child_performable && child_user_updatable_for?(credential)

  child_performable && (child_user.blank? || student_performable)
end

#update_or_create_credential(object, type, value) ⇒ Object (private)

Updates or creates credential for specific object

Parameters:

  • object (Object)

    possible object: child or child_user

  • type (Symbol)

    possible options: :email or :phone

  • value (String)

    credential value, could be email or phone number



76
77
78
79
80
81
82
83
# File 'app/services/credentials/builder/external_sources/child.rb', line 76

def update_or_create_credential(object, type, value)
  scope = object.is_a?(Child) ? :child_credentials : :user_credentials
  credential = object.public_send(scope).public_send(type).take

  return process_credential_update(credential, value) if credential.present?

  object.update("#{scope}_attributes": [{ credential: value }])
end