lhs
lhs copied to clipboard
Make maps available for nested data that is linked with an existing service
When traversing data, href should be evaluated if matching an existing service to make maps available that are defined in the specific Service (via endpoints comparison).
Scenario:
We have
module Services
class Claims < LHS::Service
endpoint "#{LCL[:datastore_service]}/v2/places/:entry_id/claims"
endpoint "#{LCL[:datastore_service]}/v2/localch-accounts/:user_id/claims/:entry_id"
end
end
and
module Services
class User < LHS::Service
endpoint "#{LCL[:datastore_service]}/v2/localch-accounts"
# duplicate code in rails-auth/User#from_hash
map :employee?, ->(user) { user.try(:[], :authorities)._raw_.try(:include?, 'ROLE_LDAP') }
# need to know MBA status, so we can show the MBA notice
map :mba_active?, ->(user) { # rubocop:disable Lambda
url = "#{LCL[:datastore_service]}/customer_center/v1/customers/#{user.id}.json"
mba_info = JSON.parse(LHC.get(url).body)
mba_info['mba_data'].try(:[], 'status') == 'active'
}
end
end
It would be epic if this would work:
claims = Services::Claims.includes(:localch_account).where(entry_id: params[:entry_id])
claims.first.localch_account.mba_active?
or even:
claims = Services::Claims.includes(localch_account: {as: :user}).where(entry_id: params[:entry_id])
claims.first.user.mba_active?
Here's the data hash from the claims service call
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/places/RQeteaOwM8NRmWkdg3oynQ/claims",
"items"=>
[{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/localch-accounts/RlXxAWV033pL5swfVZds5w/claims/RQeteaOwM8NRmWkdg3oynQ",
"localch_account"=>
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/localch-accounts/RlXxAWV033pL5swfVZds5w",
"id"=>"RlXxAWV033pL5swfVZds5w",
"name"=>"Priska Bischof",
"phone"=>"+41797070944",
"phone_display"=>"079 707 09 44",
"email"=>"[email protected]",
"language"=>"de",
"created_date"=>"2014-08-12T14:37:36.463+02:00",
"claims"=>
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/localch-accounts/RlXxAWV033pL5swfVZds5w/claims",
"items"=>
[{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/localch-accounts/RlXxAWV033pL5swfVZds5w/claims/RQeteaOwM8NRmWkdg3oynQ",
"localch_account"=>
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/localch-accounts/RlXxAWV033pL5swfVZds5w"},
"place"=>
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/places/RQeteaOwM8NRmWkdg3oynQ"},
"created_date"=>"2014-08-12T14:37:36.691+02:00",
"method"=>"MBA",
"claim_notification"=>
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/localch-accounts/RlXxAWV033pL5swfVZds5w/notifications"}}],
"total"=>1},
"accepted_agbs"=>
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/localch-accounts/RlXxAWV033pL5swfVZds5w/accepted-agbs",
"items"=>[],
"total"=>0},
"customer_center_place_history_items"=>
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/localch-accounts/RlXxAWV033pL5swfVZds5w/customer-center-place-history-items"},
"enabled"=>true,
"non_locked"=>true,
"authorities"=>["ROLE_USER", "ROLE_LOCALCH_ACCOUNT"],
"activate_account"=>
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/localch-accounts/RlXxAWV033pL5swfVZds5w/activation-codes?code=956c9348-4944-4a81-917e-0edcb5b298b1&mac=0454d2750b425746ab447f240b3e09f7"},
"username"=>"[email protected]"},
"place"=>
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/places/RQeteaOwM8NRmWkdg3oynQ"},
"created_date"=>"2014-08-12T14:37:36.691+02:00",
"method"=>"MBA",
"claim_notification"=>
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/localch-accounts/RlXxAWV033pL5swfVZds5w/notifications"}},
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/localch-accounts/mMlV1R2Fo_J94qKZ3bynag/claims/RQeteaOwM8NRmWkdg3oynQ",
"localch_account"=>
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/localch-accounts/mMlV1R2Fo_J94qKZ3bynag",
"id"=>"mMlV1R2Fo_J94qKZ3bynag",
"name"=>"Priska Bischof",
"phone"=>"+41797070944",
"phone_display"=>"079 707 09 44",
"email"=>"[email protected]",
"language"=>"de",
"created_date"=>"2014-04-16T17:20:06.970+02:00",
"claims"=>
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/localch-accounts/mMlV1R2Fo_J94qKZ3bynag/claims",
"items"=>
[{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/localch-accounts/mMlV1R2Fo_J94qKZ3bynag/claims/RQeteaOwM8NRmWkdg3oynQ",
"localch_account"=>
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/localch-accounts/mMlV1R2Fo_J94qKZ3bynag"},
"place"=>
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/places/RQeteaOwM8NRmWkdg3oynQ"},
"created_date"=>"2014-04-16T17:20:07.281+02:00",
"method"=>"MBA",
"claim_notification"=>
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/localch-accounts/mMlV1R2Fo_J94qKZ3bynag/notifications"}}],
"total"=>1},
"password_hash"=>
"$2a$10$jN1qc9Nh1hS6Mhg6Vj1vNe8TQUdFjhNCzXIoOAPsahxbIHhviBIQq",
"accepted_agbs"=>
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/localch-accounts/mMlV1R2Fo_J94qKZ3bynag/accepted-agbs",
"items"=>[],
"total"=>0},,
"customer_center_place_history_items"=>
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/localch-accounts/mMlV1R2Fo_J94qKZ3bynag/customer-center-place-history-items"},
"enabled"=>true,
"non_locked"=>true,
"authorities"=>["ROLE_USER", "ROLE_LOCALCH_ACCOUNT"],
"username"=>"[email protected]"},
"place"=>
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/places/RQeteaOwM8NRmWkdg3oynQ"},
"created_date"=>"2014-04-16T17:20:07.281+02:00",
"method"=>"MBA",
"claim_notification"=>
{"href"=>
"http://datastore-stg.lb-service.sunrise.intra.local.ch/v2/localch-accounts/mMlV1R2Fo_J94qKZ3bynag/notifications"}}
],
"total"=>2}
For the explicit casting, would suggest something like:
Services::Claims.where().first.local_ch_account.cast!(Serivces::User).mba_active?