diff --git a/applications/crossbar/src/modules/provisioner_v5.erl b/applications/crossbar/src/modules/provisioner_v5.erl index 810a53190cb..c5f30909475 100644 --- a/applications/crossbar/src/modules/provisioner_v5.erl +++ b/applications/crossbar/src/modules/provisioner_v5.erl @@ -109,21 +109,37 @@ update_account(AccountId, JObj, AuthToken) -> ,AuthToken ,AccountId ,'none'). + %%-------------------------------------------------------------------- %% @private %% @doc %% %% @end %%-------------------------------------------------------------------- --spec set_account(wh_json:object()) -> wh_json:object(). -set_account(JObj) -> - AccountId = wh_json:get_value(<<"pvt_account_id">>, JObj), - AccountDb = wh_json:get_value(<<"pvt_account_db">>, JObj), - case couch_mgr:open_cache_doc(AccountDb, AccountId) of +-spec provision_data(wh_json:object()) -> wh_json:object(). +provision_data(JObj) -> + Routines = [ + fun set_realm/1 + ,fun set_owner/1 + ,fun maybe_set_timezone/1 + ,fun create_provision_settings/1 + ], + lists:foldl(fun(F, J) -> F(J) end, JObj, Routines). + +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% +%% @end +%%-------------------------------------------------------------------- +-spec set_realm(wh_json:object()) -> wh_json:object(). +set_realm(JObj) -> + case get_account(JObj) of {'ok', Doc} -> Realm = wh_json:get_value(<<"realm">>, Doc), wh_json:set_value(<<"realm">>, Realm, JObj); {'error', _R} -> + AccountId = wh_json:get_value(<<"pvt_account_id">>, JObj), lager:warning("failed to get account definition for ~s: ~p", [AccountId, _R]), JObj end. @@ -138,20 +154,76 @@ set_account(JObj) -> set_owner(JObj) -> AccountId = wh_json:get_value(<<"pvt_account_id">>, JObj), OwnerId = wh_json:get_ne_value(<<"owner_id">>, JObj), - Owner = get_owner(OwnerId, AccountId), - wh_json:merge_recursive(JObj, Owner). + case get_owner(OwnerId, AccountId) of + {'ok', Doc} -> wh_json:merge_recursive(Doc, JObj); + {'error', _R} -> JObj + end. +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% +%% @end +%%-------------------------------------------------------------------- +-spec maybe_set_timezone(wh_json:object()) -> wh_json:object(). +-spec maybe_set_timezone(wh_json:object(), wh_json:object()) -> wh_json:object(). +maybe_set_timezone(JObj) -> + case wh_json:get_value(<<"timezone">>, JObj) of + 'undefined' -> maybe_set_account_timezone(JObj); + _TZ -> JObj + end. + +maybe_set_timezone(JObj, AccountDoc) -> + case wh_json:get_value(<<"timezone">>, AccountDoc) of + 'undefined' -> JObj; + TZ -> set_timezone(JObj, TZ) + end. + +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% +%% @end +%%-------------------------------------------------------------------- +-spec maybe_set_account_timezone(wh_json:object()) -> wh_json:object(). +maybe_set_account_timezone(JObj) -> + case get_account(JObj) of + {'ok', Doc} -> maybe_set_timezone(JObj, Doc); + {'error', _R} -> JObj + end. --spec get_owner(api_binary(), ne_binary()) -> wh_json:object(). -get_owner('undefined', _) -> wh_json:new(); +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% +%% @end +%%-------------------------------------------------------------------- +-spec set_timezone(wh_json:object(), ne_binary()) -> wh_json:object(). +set_timezone(JObj, TZ) -> + wh_json:set_value(<<"timezone">>, TZ, JObj). +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% +%% @end +%%-------------------------------------------------------------------- +-spec get_owner(api_binary(), ne_binary()) -> {'ok', wh_json:object()} | {'error', any()}. +get_owner('undefined', _) -> {'error', 'undefined'}; get_owner(OwnerId, AccountId) -> AccountDb = wh_util:format_account_id(AccountId, 'encoded'), - case couch_mgr:open_cache_doc(AccountDb, OwnerId) of - {'ok', Owner} -> Owner; - {'error', _R} -> - lager:debug("unable to open user definition ~s/~s: ~p", [AccountDb, OwnerId, _R]), - wh_json:new() - end. + couch_mgr:open_cache_doc(AccountDb, OwnerId). + +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% +%% @end +%%-------------------------------------------------------------------- +-spec get_account(wh_json:object()) -> {'ok', wh_json:object()} | {'error', any()}. +get_account(JObj) -> + AccountId = wh_json:get_value(<<"pvt_account_id">>, JObj), + AccountDb = wh_json:get_value(<<"pvt_account_db">>, JObj), + couch_mgr:open_cache_doc(AccountDb, AccountId). %%-------------------------------------------------------------------- %% @private @@ -161,17 +233,17 @@ get_owner(OwnerId, AccountId) -> %%-------------------------------------------------------------------- -spec account_settings(ne_binary(), wh_json:object()) -> wh_json:object(). account_settings(AccountId, JObj) -> - Settings = wh_json:set_values([ - {<<"lines">>, [set_realm(JObj)]} - ], wh_json:new()), + Settings = wh_json:from_list([ + {<<"lines">>, [set_line_realm(JObj)]} + ]), - wh_json:set_values([ + wh_json:from_list([ {<<"provider_id">>, wh_services:find_reseller_id(AccountId)} ,{<<"name">>, wh_json:get_value(<<"name">>, JObj)} ,{<<"settings">>, Settings} - ], wh_json:new()). + ]). -set_realm(JObj) -> +set_line_realm(JObj) -> wh_json:set_value( <<"sip">> ,wh_json:set_value( @@ -186,9 +258,9 @@ set_realm(JObj) -> %% %% @end %%-------------------------------------------------------------------- --spec device_settings(wh_json:object()) -> wh_json:object(). -device_settings(JObj) -> - Settings = wh_json:set_values([ +-spec create_provision_settings(wh_json:object()) -> wh_json:object(). +create_provision_settings(JObj) -> + Settings = wh_json:from_list([ {<<"lines">>, [set_line(JObj)]} ,{<<"codecs">>, [set_codecs(JObj)]} ,{<<"timezone">>, wh_json:get_value(<<"timezone">>, JObj)} @@ -199,7 +271,7 @@ device_settings(JObj) -> ,{<<"model">>, wh_json:get_value([<<"provision">>, <<"endpoint_model">>], JObj, <<>>)} ,{<<"name">>, wh_json:get_value(<<"name">>, JObj)} ,{<<"settings">>, Settings} - ], wh_json:new()). + ]). %%-------------------------------------------------------------------- %% @private