Skip to content

Commit

Permalink
KAZOO-3255: Fix hotdesk api
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Defebvre committed Jan 27, 2015
1 parent 7f8e478 commit 8f39516
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 39 deletions.
2 changes: 1 addition & 1 deletion applications/crossbar/priv/couchdb/account/hotdesks.json
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 || !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});}}"
"map": "function(doc) {if (doc.pvt_deleted || !doc.hotdesk && doc.pvt_type != 'device' || doc.pvt_type != 'user') return;var obj = {'doc_type': doc.pvt_type,'doc_id': doc._id};for(var key in doc.hotdesk) {obj[key] = doc.hotdesk[key];}emit(doc._id, obj);}"
}
}
}
88 changes: 50 additions & 38 deletions applications/crossbar/src/modules/cb_hotdesks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,29 @@ resource_exists() -> 'true'.
%% @end
%%--------------------------------------------------------------------
-spec validate(cb_context:context()) -> cb_context:context().
validate(#cb_context{req_verb = ?HTTP_GET}=Context) ->
Type = wh_json:get_value(<<"pvt_type">>, cb_context:doc(Context)),
route_by_type(Type, Context).
validate(Context) ->
validate_hotdesks(Context, cb_context:req_verb(Context), cb_context:req_nouns(Context)).


%%%===================================================================
%%% Internal functions
%%%===================================================================

%%--------------------------------------------------------------------
%% @private
%% @doc
%% @end
%%--------------------------------------------------------------------
-spec validate_hotdesks(cb_context:context(), http_method(), wh_proplist()) -> cb_context:context().
validate_hotdesks(Context, ?HTTP_GET, [{<<"hotdesks">>, _}, {<<"users">>, [UserId]}|_]) ->
route_by_type({<<"user">>, UserId}, Context);
validate_hotdesks(Context, ?HTTP_GET, [{<<"hotdesks">>, _}, {<<"devices">>, [DeviceId]}|_]) ->
route_by_type({<<"device">>, DeviceId}, Context);
validate_hotdesks(Context, ?HTTP_GET, [{<<"hotdesks">>, _}, {<<"accounts">>, [AccountId]}|_]) ->
route_by_type({<<"account">>, AccountId}, Context);
validate_hotdesks(Context, ?HTTP_GET, _) ->
route_by_type('undefined', Context).

%%--------------------------------------------------------------------
%% @private
%% @doc
Expand All @@ -85,40 +101,36 @@ validate(#cb_context{req_verb = ?HTTP_GET}=Context) ->
normalize_view_results(JObj, Acc) ->
[wh_json:get_value(<<"value">>, JObj)|Acc].

-spec route_by_type(ne_binary(), cb_context:context()) ->
cb_context:context().
%%--------------------------------------------------------------------
%% @private
%% @doc
%% @end
%%--------------------------------------------------------------------
-spec route_by_type('undefined' | {ne_binary(), 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">>, Context) ->
UserIds = wh_json:get_value([<<"hotdesk">>, <<"users">>], cb_context:doc(Context), wh_json:new()),
UserJObjs = wh_json:foldl(
fun(UserId, _, Acc) ->
case get_username(UserId, cb_context:account_db(Context)) of
'undefined' -> Acc;
JObj -> [JObj|Acc]
end
end, [], UserIds),
case UserJObjs of
[] -> cb_context:add_system_error('not_found', Context);
RespData ->
cb_context:set_resp_data(
cb_context:set_resp_status(Context, 'success')
,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(), ne_binary()) -> api_object().
get_username(UserId, AccoundDb) ->
case couch_mgr:open_cache_doc(AccoundDb, UserId) of
{'error', _} -> 'undefined';
{'ok', JObj} ->
FirstName = wh_json:get_value(<<"first_name">>, JObj),
LastName = wh_json:get_value(<<"last_name">>, JObj),
wh_json:from_list([{<<"first_name">>, FirstName}
,{<<"last_name">>, LastName}
,{<<"id">>, UserId}
])
end.
route_by_type({<<"device">>, DeviceId}, Context) ->
crossbar_doc:load_view(?CB_LIST, view_options(DeviceId), Context, fun normalize_view_results/2);
route_by_type({<<"user">>, UserId}, Context) ->
io:format("MARKER:cb_hotdesks.erl:115 ~p~n", [UserId]),
crossbar_doc:load_view(?CB_LIST, view_options(UserId), Context, fun normalize_view_results/2);
route_by_type({<<"account">>, _}, Context) ->
crossbar_doc:load_view(?CB_LIST, [], Context, fun normalize_view_results/2).


view_options(Id) ->
[{<<"key">>, Id}].














0 comments on commit 8f39516

Please sign in to comment.