Skip to content

Commit

Permalink
Deprecate ensure saved (2600hz#5103)
Browse files Browse the repository at this point in the history
* Remove ensure_saved in favor of using update_doc

* kill the check process

* add some logging for why tasks aren't finishing in CI

* move trace logs to CI artifact path

* log the status if not successful

* dialyzer updates

* log the before/after

* ensure its saved

* require update in Options be a list

* let script take arbitrary list of .erl files to dialyze

* remove logging

* missed wrapping updates into update options

* needs to be a proplist

* catch ets exceptions when shutting down

* fix spec
  • Loading branch information
jamesaimonetti authored and lazedo committed Sep 17, 2018
1 parent 7acee57 commit ca58af7
Show file tree
Hide file tree
Showing 69 changed files with 1,068 additions and 731 deletions.
1 change: 1 addition & 0 deletions .aspell.en.prepl
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ ourself ourselves
kined kind of
assiging assigning
fouth fourth
indexs indexes
multipy multiplied
likelyhood likelihood
maxium maximum
Expand Down
1 change: 1 addition & 0 deletions .aspell.en.pws
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ freetdm
IPs
ips
chargebacks
Dunton
IPv
Belmullet
noop
Expand Down
63 changes: 41 additions & 22 deletions applications/callflow/src/callflow_maintenance.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

-include("callflow.hrl").

-define(DOLLAR_SIGN, 36). % = $\$ but makes fmt wonky atm

%%------------------------------------------------------------------------------
%% @doc
%% @end
Expand Down Expand Up @@ -290,21 +292,24 @@ set_account_classifier_action(Action, Classifier, AccountDb) ->
io:format("found account: ~p", [kzd_accounts:fetch_name(AccountDb)]),
AccountId = kz_util:format_account_id(AccountDb, 'raw'),

_ = kz_datamgr:update_doc(AccountDb, AccountId, [{[<<"call_restriction">>, Classifier, <<"action">>], Action}]),
_ = kz_datamgr:update_doc(?KZ_ACCOUNTS_DB, AccountId, [{[<<"call_restriction">>, Classifier, <<"action">>], Action}]),
Update = [{[<<"call_restriction">>, Classifier, <<"action">>], Action}],
{'ok', _} = kzd_accounts:update(AccountId, Update),

kz_endpoint:flush_account(AccountDb),

io:format(" ... classifier '~s' switched to action '~s'\n", [Classifier, Action]).

-spec all_accounts_set_classifier(kz_term:ne_binary(), kz_term:ne_binary()) -> 'ok'.
all_accounts_set_classifier(Action, Classifier) ->
'true' = is_classifier(Classifier),
lists:foreach(fun(AccountDb) ->
timer:sleep(2000),
%% Not sure if this interruption is really needed.
%% Keeping it as it was taken as an example from kapps_util:update_all_accounts/1
set_account_classifier_action(Action, Classifier, AccountDb)
end, kapps_util:get_all_accounts()).
end
,kapps_util:get_all_accounts()
).

%%------------------------------------------------------------------------------
%% @doc Set `call_restriction' flag on device level.
Expand Down Expand Up @@ -334,10 +339,16 @@ set_device_classifier_action(Action, Classifier, Uri) ->
'true' = is_classifier(Classifier),
[User, Realm] = binary:split(Uri, <<"@">>),
{'ok', AccountDb} = kapps_util:get_account_by_realm(Realm),

Options = [{'key', User}],
{'ok', [DeviceDoc]} = kz_datamgr:get_results(AccountDb, <<"devices/sip_credentials">>, Options),
DeviceId = kz_doc:id(DeviceDoc),
_ = kz_datamgr:update_doc(AccountDb, DeviceId, [{[<<"call_restriction">>, Classifier, <<"action">>], Action}]),

Update = [{[<<"call_restriction">>, Classifier, <<"action">>], Action}],
UpdateOptions = [{'update', Update}],

{'ok', _} = kz_datamgr:update_doc(AccountDb, DeviceId, UpdateOptions),

kz_endpoint:flush(AccountDb, DeviceId).

%%------------------------------------------------------------------------------
Expand Down Expand Up @@ -452,22 +463,30 @@ update_feature_codes(Account) ->
maybe_update_feature_codes(AccountDb, Patterns)
end.

maybe_update_feature_codes(Db, []) ->
io:format("~s : feature codes up to date\n", [kz_util:format_account_id(Db, 'raw')]);
maybe_update_feature_codes(Db, [Pattern|Patterns]) ->
-spec maybe_update_feature_codes(kz_term:ne_binary(), kz_json:objects()) -> 'ok'.
maybe_update_feature_codes(Db, Patterns) ->
lists:foreach(fun(Pattern) -> maybe_update_feature_code(Db, Pattern) end
,Patterns
),
io:format("~s : feature codes up to date\n", [kz_util:format_account_id(Db, 'raw')]).

-spec maybe_update_feature_code(kz_term:ne_binary(), kz_json:object()) -> 'ok'.
maybe_update_feature_code(Db, Pattern) ->
maybe_update_feature_code(Db, Pattern, kz_json:get_ne_binary_value(<<"key">>, Pattern)).

maybe_update_feature_code(Db, Pattern, <<"^\\*5([0-9]*)", ?DOLLAR_SIGN>>=_Regex) ->
DocId = kz_doc:id(Pattern),
Regex = kz_json:get_value(<<"key">>, Pattern),
case Regex of
<<"^\\*5([0-9]*)$">> ->
NewRegex = <<"^\\*5(|[0-9]{2,})$">>,
case kz_datamgr:update_doc(Db, DocId, [{<<"patterns">>, [NewRegex]}]) of
{'error', _Reason} ->
io:format("failed to update doc ~s with new patterns\n", [DocId]);
{'ok', _} ->
io:format("successfully updated patterns for doc ~s (~p -> ~p)\n",
[DocId, Regex, NewRegex])
end;
_OtherRegex ->
io:format("skipping pattern ~p\n", [_OtherRegex])
end,
maybe_update_feature_codes(Db, Patterns).
NewRegex = <<"^\\*5(|[0-9]{2,})", ?DOLLAR_SIGN>>,
Update = [{<<"patterns">>, [NewRegex]}],
UpdateOptions = [{'update', Update}],

case kz_datamgr:update_doc(Db, DocId, UpdateOptions) of
{'error', _Reason} ->
io:format("failed to update doc ~s with new patterns\n", [DocId]);
{'ok', _} ->
io:format("successfully updated patterns for doc ~s (~p -> ~p)\n"
,[DocId, _Regex, NewRegex]
)
end;
maybe_update_feature_code(_Db, _Pattern, _Regex) ->
io:format("skipping pattern ~p\n", [_Regex]).
20 changes: 14 additions & 6 deletions applications/callflow/src/module/cf_manual_presence.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,19 @@ handle(Data, Call) ->

-spec update_presence(kz_term:ne_binary(), kz_term:ne_binary(), kapps_call:call()) -> 'ok'.
update_presence(<<"idle">>, PresenceId, Call) ->
_ = kz_datamgr:update_doc(kapps_call:account_db(Call), ?MANUAL_PRESENCE_DOC, [{PresenceId, <<"terminated">>}]),
kapps_call_command:presence(<<"terminated">>, PresenceId, kz_term:to_hex_binary(crypto:hash(md5, PresenceId)));
_ = update_doc(Call, PresenceId, <<"terminated">>),
kapps_call_command:presence(<<"terminated">>, PresenceId, kz_term:to_hex_binary(crypto:hash('md5', PresenceId)));
update_presence(<<"ringing">>, PresenceId, Call) ->
_ = kz_datamgr:update_doc(kapps_call:account_db(Call), ?MANUAL_PRESENCE_DOC, [{PresenceId, <<"early">>}]),
kapps_call_command:presence(<<"early">>, PresenceId, kz_term:to_hex_binary(crypto:hash(md5, PresenceId)));
_ = update_doc(Call, PresenceId, <<"early">>),
kapps_call_command:presence(<<"early">>, PresenceId, kz_term:to_hex_binary(crypto:hash('md5', PresenceId)));
update_presence(<<"busy">>, PresenceId, Call) ->
_ = kz_datamgr:update_doc(kapps_call:account_db(Call), ?MANUAL_PRESENCE_DOC, [{PresenceId, <<"confirmed">>}]),
kapps_call_command:presence(<<"confirmed">>, PresenceId, kz_term:to_hex_binary(crypto:hash(md5, PresenceId))).
_ = update_doc(Call, PresenceId, <<"confirmed">>),
kapps_call_command:presence(<<"confirmed">>, PresenceId, kz_term:to_hex_binary(crypto:hash('md5', PresenceId))).

-spec update_doc(kapps_call:call(), kz_term:ne_binary(), kz_term:ne_binary()) ->
{'ok', kz_json:object()} |
kz_datamgr:data_error().
update_doc(Call, PresenceId, State) ->
Update = [{PresenceId, State}],
UpdateOptions = [{'update', Update}],
kz_datamgr:update_doc(kapps_call:account_db(Call), ?MANUAL_PRESENCE_DOC, UpdateOptions).
26 changes: 15 additions & 11 deletions applications/cccp/src/cccp_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ handle_disconnect_cause(JObj, Call) ->
case kz_json:get_value(<<"Disposition">>, JObj) of
'undefined' -> 'ok';
<<"UNALLOCATED_NUMBER">> ->
kapps_call_command:prompt(<<"hotdesk-invalid_entry">>, Call), %%% don't feel if it is needed
_ = kapps_call_command:prompt(<<"hotdesk-invalid_entry">>, Call), %%% don't feel if it is needed
kapps_call_command:queued_hangup(Call); %%% we can setup different prompt
<<"INVALID_NUMBER_FORMAT">> -> %%% for different hangup cause
kapps_call_command:prompt(<<"hotdesk-invalid_entry">>, Call),
_ = kapps_call_command:prompt(<<"hotdesk-invalid_entry">>, Call),
kapps_call_command:queued_hangup(Call);
<<"CALL_REJECTED">> ->
kapps_call_command:prompt(<<"hotdesk-invalid_entry">>, Call),
_ = kapps_call_command:prompt(<<"hotdesk-invalid_entry">>, Call),
kapps_call_command:queued_hangup(Call);
<<"USER_BUSY">> ->
kapps_call_command:prompt(<<"hotdesk-invalid_entry">>, Call),
_ = kapps_call_command:prompt(<<"hotdesk-invalid_entry">>, Call),
kapps_call_command:queued_hangup(Call);
UnhandledCause ->
lager:debug("unhandled disconnect cause: ~p", [UnhandledCause]),
Expand Down Expand Up @@ -85,7 +85,7 @@ get_number(Call) ->
'ok'.
get_number(Call, 0) ->
lager:info("run out of attempts amount... hanging up"),
kapps_call_command:prompt(<<"hotdesk-invalid_entry">>, Call),
_ = kapps_call_command:prompt(<<"hotdesk-invalid_entry">>, Call),
kapps_call_command:queued_hangup(Call);
get_number(Call, Retries) ->
RedialCode = kapps_config:get_ne_binary(?CCCP_CONFIG_CAT, <<"last_number_redial_code">>, <<"*0">>),
Expand All @@ -96,7 +96,7 @@ get_number(Call, Retries) ->
verify_entered_number(EnteredNumber, Call, Retries);
_Err ->
lager:info("no phone number obtained: ~p", [_Err]),
kapps_call_command:prompt(<<"hotdesk-invalid_entry">>, Call),
_ = kapps_call_command:prompt(<<"hotdesk-invalid_entry">>, Call),
get_number(Call, Retries - 1)
end.

Expand All @@ -108,7 +108,7 @@ verify_entered_number(EnteredNumber, Call, Retries) ->
check_restrictions(Number, Call);
_ ->
lager:debug("wrong number entered: ~p", [EnteredNumber]),
kapps_call_command:prompt(<<"hotdesk-invalid_entry">>, Call),
_ = kapps_call_command:prompt(<<"hotdesk-invalid_entry">>, Call),
get_number(Call, Retries - 1)
end.

Expand All @@ -121,16 +121,20 @@ get_last_dialed_number(Call) ->
LastDialed = kz_json:get_value(<<"pvt_last_dialed">>, Doc),
case cccp_allowed_callee(LastDialed) of
'false' ->
kapps_call_command:prompt(<<"hotdesk-invalid_entry">>, Call),
_ = kapps_call_command:prompt(<<"hotdesk-invalid_entry">>, Call),
kapps_call_command:queued_hangup(Call);
'true' ->
check_restrictions(LastDialed, Call)
end.

-spec store_last_dialed(kz_term:ne_binary(), kz_term:ne_binary()) -> 'ok'.
store_last_dialed(Number, DocId) ->
{'ok', Doc} = kz_datamgr:update_doc(?KZ_CCCPS_DB, DocId, [{<<"pvt_last_dialed">>, Number}]),
_ = kz_datamgr:update_doc(kz_doc:account_db(Doc), DocId, [{<<"pvt_last_dialed">>, Number}]),
Updates = [{<<"pvt_last_dialed">>, Number}],
UpdateOptions = [{'update', Updates}],

{'ok', Doc} = kz_datamgr:update_doc(?KZ_CCCPS_DB, DocId, UpdateOptions),
_ = kz_datamgr:update_doc(kz_doc:account_db(Doc), DocId, UpdateOptions),

'ok'.

-spec check_restrictions(kz_term:ne_binary(), kapps_call:call()) ->
Expand Down Expand Up @@ -172,7 +176,7 @@ is_user_restricted(Number, UserId, AccountDb, Call) ->

-spec hangup_unauthorized_call(kapps_call:call()) -> 'ok'.
hangup_unauthorized_call(Call) ->
kapps_call_command:prompt(<<"cf-unauthorized_call">>, Call),
_ = kapps_call_command:prompt(<<"cf-unauthorized_call">>, Call),
kapps_call_command:queued_hangup(Call).

-spec cccp_allowed_callee(kz_term:ne_binary()) -> boolean().
Expand Down
37 changes: 16 additions & 21 deletions applications/crossbar/src/cb_apps_maintenance.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
migrate(Account) when is_binary(Account) ->
case kzd_accounts:fetch(Account) of
{'error', _R}=Error -> Error;
{'ok', JObj} ->
CurrentApps = kzd_apps_store:apps(JObj),
{'ok', AccountJObj} ->
CurrentApps = kzd_apps_store:apps(AccountJObj),
case kz_term:is_empty(CurrentApps) of
'true' -> {'error', 'migrated'};
'false' ->
Doc = kzd_apps_store:new(Account),
save(Account, kzd_apps_store:set_apps(Doc, CurrentApps), JObj)
AppsStoreDoc = kzd_apps_store:new(Account),
save(Account, kzd_apps_store:set_apps(AppsStoreDoc, CurrentApps))
end
end.

Expand All @@ -36,28 +36,23 @@ migrate(Account) when is_binary(Account) ->
%% @doc
%% @end
%%------------------------------------------------------------------------------
-spec save(kz_term:ne_binary(), kz_json:object(), kz_json:object()) -> {'ok', kz_json:object()} | {'error', any()}.
save(Account, Doc, AccountDoc) ->
-spec save(kz_term:ne_binary(), kzd_apps_store:doc()) ->
{'ok', kzd_accounts:doc()} |
kz_datamgr:data_error().
save(Account, AppsStoreDoc) ->
AccountDb = kz_util:format_account_id(Account, 'encoded'),
case kz_datamgr:save_doc(AccountDb, Doc) of
case kz_datamgr:save_doc(AccountDb, AppsStoreDoc) of
{'error', _R}=Error -> Error;
{'ok', _}=Ok ->
_ = save_account(Account, AccountDoc),
{'ok', _SavedAppsStoreDoc}=Ok ->
_ = save_account(Account),
Ok

end.


-spec save_account(kz_term:ne_binary(), kz_json:object()) -> 'ok'.
save_account(Account, AccountDoc) ->
AccountDb = kz_util:format_account_id(Account, 'encoded'),
case kz_datamgr:ensure_saved(AccountDb, kz_json:delete_key(<<"apps">>, AccountDoc)) of
-spec save_account(kz_term:ne_binary()) -> 'ok'.
save_account(Account) ->
case kzd_accounts:update(Account, [{<<"apps">>, 'null'}]) of
{'error', _R} ->
lager:error("failed to save ~s : ~p", [AccountDb, _R]);
{'ok', JObj} ->
case kz_datamgr:ensure_saved(?KZ_ACCOUNTS_DB, JObj) of
{'error', _R} ->
lager:error("failed to save ~s in accounts db: ~p", [Account, _R]);
{'ok', _} -> 'ok'
end
lager:error("failed to save ~s : ~p", [Account, _R]);
{'ok', _AccountDoc} -> 'ok'
end.
7 changes: 3 additions & 4 deletions applications/crossbar/src/cb_apps_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,10 @@ is_authorized(AccountId, UserId, AppId, AppStoreJObj) ->
'false'
end.

-spec get_specific_ids(kz_json:objects()) -> kz_term:ne_binaries().
-spec get_specific_ids(kz_json:ne_binaries()) -> kz_term:ne_binaries().
get_specific_ids(Users) ->
[Id || User <- Users,
Id <- [kz_doc:id(User)],
'undefined' =/= Id
[UserId || UserId <- Users,
'undefined' =/= UserId
].

-spec add_permissions(kz_json:object(), kz_json:object()) -> kz_json:object().
Expand Down
Loading

0 comments on commit ca58af7

Please sign in to comment.