Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KAZOO-791: Fix hot desking (now allow /accounts/xxx/devices/xxx/hostdesk... #15

Merged
merged 2 commits into from
Apr 6, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
,"language": "javascript"
,"views": {
"crossbar_listing" : {
"map": "function(doc) { if (doc.pvt_deleted || !doc.hotdesk ) return; emit(doc._id, {'owner_id':doc._id, 'first_name':doc.first_name, 'last_name':doc.last_name, 'hotdesk': doc.hotdesk })}"
"map": "function (doc) {if (doc.pvt_deleted || !doc.hotdesk || !doc.hotdesk.users || doc.pvt_type != 'device') return;for (var idx in doc.hotdesk.users) {emit(idx, {'device_name': doc.name,'device_id': doc._id});}}"
}
}
}
49 changes: 47 additions & 2 deletions whistle_apps/apps/crossbar/src/modules/cb_hotdesks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ resource_exists() ->
%% @end
%%--------------------------------------------------------------------
-spec validate(cb_context:context()) -> cb_context:context().
validate(#cb_context{req_verb = <<"get">>}=Context) ->
crossbar_doc:load_view(?CB_LIST, [], Context, fun normalize_view_results/2).
validate(#cb_context{req_verb = <<"get">>, doc=Doc}=Context) ->
Type = wh_json:get_value(<<"pvt_type">>, Doc, <<"undefined">>),
route_by_type(Type, Context).

%%%===================================================================
%%% Internal functions
Expand All @@ -84,3 +85,47 @@ validate(#cb_context{req_verb = <<"get">>}=Context) ->
-spec normalize_view_results(wh_json:json_object(), wh_json:json_objects()) -> wh_json:json_objects().
normalize_view_results(JObj, Acc) ->
[wh_json:get_value(<<"value">>, JObj)|Acc].

-spec route_by_type(ne_binary(), cb_context:context()) ->cb_context:context().
route_by_type(<<"undefined">>, Context) ->
crossbar_doc:load_view(?CB_LIST, [], Context, fun normalize_view_results/2);
route_by_type(<<"device">>, #cb_context{doc=Doc, db_name=AccoundDb}=Context) ->
UserIds = wh_json:to_proplist(wh_json:get_value([<<"hotdesk">>, <<"users">>], Doc, wh_json:new())),
JObjs = lists:foldl(
fun(UserId, Acc) ->
case get_username(UserId, AccoundDb) of
'undefined' ->
Acc;
JObj ->
[JObj|Acc]
end
end, [], UserIds),
case JObjs of
[] ->
cb_context:add_system_error('not_found', Context);
RespData ->
Context#cb_context{resp_status=success
,resp_data=RespData
}
end;
route_by_type(<<"user">>, #cb_context{doc=Doc}=Context) ->
UserId = wh_json:get_value(<<"_id">>, Doc),
crossbar_doc:load_view(?CB_LIST, [{<<"key">>, UserId}], Context, fun normalize_view_results/2).

-spec get_username({ne_binary(), any()}, ne_binary()) -> wh_json:object().
get_username({Id, _}, AccoundDb) ->
case couch_mgr:open_cache_doc(AccoundDb, Id) of
{'ok', JObj} ->
FirstName = wh_json:get_value(<<"first_name">>, JObj),
LastName = wh_json:get_value(<<"last_name">>, JObj),
wh_json:set_values([{<<"first_name">>, FirstName}
,{<<"last_name">>, LastName}
,{<<"id">>, Id}]
,wh_json:new());
_ ->
'undefined'
end;
get_username(_, _) ->
'undefined'.