Skip to content

Commit

Permalink
[master] flush registrar and fs_xml when enable/disable an account or…
Browse files Browse the repository at this point in the history
… a device (2600hz#4100)

* flush registrar and fs_xml when enable/disable an account or a device

* fix fun name
  • Loading branch information
icehess authored and lazedo committed Aug 25, 2017
1 parent 6a90792 commit 82282b9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
19 changes: 17 additions & 2 deletions applications/crossbar/src/crossbar_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ format_emergency_caller_id_number(Context, Emergency) ->
end.

%% @public
-type refresh_type() :: 'user' | 'device' | 'sys_info'.
-type refresh_type() :: 'user' | 'device' | 'sys_info' | 'account'.

-spec maybe_refresh_fs_xml(refresh_type(), cb_context:context()) -> 'ok'.
maybe_refresh_fs_xml(Kind, Context) ->
Expand Down Expand Up @@ -1032,6 +1032,10 @@ maybe_refresh_fs_xml('device', Context, Precondition) ->
,DbDoc
),
'ok';
maybe_refresh_fs_xml('account', Context, _) ->
Devices = get_account_devices(cb_context:account_id(Context)),
Realm = kz_account:fetch_realm(cb_context:account_db(Context)),
lists:foreach(fun(DevDoc) -> refresh_fs_xml(Realm, DevDoc) end, Devices);
maybe_refresh_fs_xml('sys_info', Context, Precondition) ->
Doc = cb_context:doc(Context),
Servers = kz_json:get_value(<<"servers">>, Doc, []),
Expand Down Expand Up @@ -1097,7 +1101,7 @@ refresh_fs_xml(Context) ->

-spec refresh_fs_xml(ne_binary(), kz_json:object()) -> 'ok'.
refresh_fs_xml(Realm, Doc) ->
case kz_device:sip_username(Doc) of
case kz_device:sip_username(Doc, kz_json:get_value(<<"username">>, Doc)) of
'undefined' -> 'ok';
Username ->
lager:debug("flushing fs xml for user '~s' at '~s'", [Username,Realm]),
Expand All @@ -1122,6 +1126,17 @@ get_devices_by_owner(AccountDb, OwnerId) ->
[]
end.

-spec get_account_devices(api_binary()) -> ne_binaries().
get_account_devices('undefined') -> [];
get_account_devices(Account) ->
AccountDb = kz_util:format_account_db(Account),
case kz_datamgr:get_results(AccountDb, <<"devices/crossbar_listing">>, []) of
{'ok', JObjs} -> [kz_json:get_value(<<"value">>, JObj) || JObj <- JObjs];
{'error', _R} ->
lager:warning("unable to find device documents owned by ~s: ~p", [AccountDb, _R]),
[]
end.

%%--------------------------------------------------------------------
%% @private
%% @doc
Expand Down
2 changes: 1 addition & 1 deletion applications/crossbar/src/modules/cb_accounts.erl
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ post(Context, AccountId) ->

case cb_context:resp_status(Context1) of
'success' ->
_ = kz_util:spawn(fun notification_util:maybe_notify_account_change/2, [Existing, cb_context:doc(Context1)]),
_ = kz_util:spawn(fun notification_util:maybe_notify_account_change/2, [Existing, Context]),
_ = kz_util:spawn(fun provisioner_util:maybe_update_account/1, [Context1]),

JObj = cb_context:doc(Context1),
Expand Down
24 changes: 15 additions & 9 deletions applications/crossbar/src/modules/notification_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,35 @@
maybe_notify_account_change/2
]).

-spec maybe_notify_account_change(kz_json:object(), kz_json:object()) -> 'ok'.
maybe_notify_account_change(Old, New) ->
-spec maybe_notify_account_change(kz_json:object(), cb_context:context()) -> 'ok'.
maybe_notify_account_change(Old, Context) ->
New = cb_context:doc(Context),
Filter = fun({K, V}) ->
case kz_json:get_value(K, New) of
V -> 'false';
_ -> 'true'
end
end,

AccountId = kz_json:get_value(<<"id">>, New),
AccountId = kz_doc:id(New),
Changed = kz_json:filter(Filter, Old),
Notify = fun(V) -> maybe_notify(AccountId, V) end,
Notify = fun({K, _}) -> notify_account_change(AccountId, {K, kz_json:get_value(K, New)}, Context) end,

kz_json:foreach(Notify, Changed).

-spec maybe_notify(api_binary(), {ne_binary(), kz_json:object()}) -> 'ok'.
maybe_notify(Account, {<<"zones">>, Zones}) ->
lager:info("publishing zone change notification for ~p, zones: ~p", [Account, Zones]),
Props = [{<<"Account-ID">>, Account}
-spec notify_account_change(api_binary(), {ne_binary(), kz_json:object()}, cb_context:context()) -> 'ok'.
notify_account_change(AccountId, {<<"zones">>, Zones}, _Context) ->
lager:info("publishing zone change notification for ~p, zones: ~p", [AccountId, Zones]),
Props = [{<<"Account-ID">>, AccountId}
,{<<"Zones">>, Zones}
| kz_api:default_headers(?APP_VERSION, ?APP_NAME)
],
kapps_notify_publisher:cast(Props, fun kapi_notifications:publish_account_zone_change/1);

maybe_notify(_Account, {_Key, _Value}) ->
notify_account_change(AccountId, {<<"pvt_enabled">>, IsEnabled}, Context) ->
lager:info("account ~s enabled has changed, sending registrations flush: pvt_enable: ~p", [AccountId, IsEnabled]),
crossbar_util:flush_registrations(Context),
crossbar_util:maybe_refresh_fs_xml('account', Context);

notify_account_change(_Account, {_Key, _Value}, _Context) ->
'ok'.

0 comments on commit 82282b9

Please sign in to comment.