From 8f39516bd6d018ed6fdfa50f2ef6f21cd5e05247 Mon Sep 17 00:00:00 2001 From: Peter Defebvre Date: Mon, 26 Jan 2015 21:34:12 +0000 Subject: [PATCH] KAZOO-3255: Fix hotdesk api --- .../priv/couchdb/account/hotdesks.json | 2 +- .../crossbar/src/modules/cb_hotdesks.erl | 88 +++++++++++-------- 2 files changed, 51 insertions(+), 39 deletions(-) diff --git a/applications/crossbar/priv/couchdb/account/hotdesks.json b/applications/crossbar/priv/couchdb/account/hotdesks.json index 14ce1eaf150..2424a8d60a3 100644 --- a/applications/crossbar/priv/couchdb/account/hotdesks.json +++ b/applications/crossbar/priv/couchdb/account/hotdesks.json @@ -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);}" } } } diff --git a/applications/crossbar/src/modules/cb_hotdesks.erl b/applications/crossbar/src/modules/cb_hotdesks.erl index d0954f5d29c..4c17fc0c3a1 100644 --- a/applications/crossbar/src/modules/cb_hotdesks.erl +++ b/applications/crossbar/src/modules/cb_hotdesks.erl @@ -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 @@ -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}]. + + + + + + + + + + + + + +