Skip to content

Commit

Permalink
KAZOO-6108: check order of users after sort/filter (2600hz#5873)
Browse files Browse the repository at this point in the history
Mostly adding testing to ensure sort/filter is working as expected
  • Loading branch information
jamesaimonetti authored and lazedo committed Jun 26, 2019
1 parent b64ebbc commit 9992053
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 39 deletions.
57 changes: 18 additions & 39 deletions applications/callflow/src/module/cf_directory.erl
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,18 @@

-behaviour(gen_cf_action).

-include_lib("callflow/src/callflow.hrl").

-export([handle/2]).

-ifdef(TEST).
-export([get_directory_user/2
,sort_users/2
,filter_users/3
]).
-endif.

-include_lib("callflow/src/module/cf_directory.hrl").
-include_lib("callflow/src/callflow.hrl").

-define(DIR_DOCS_VIEW, <<"directories/users_listing">>).

-define(FIELDS, [<<"last_name">>, <<"first_name">>]). %% what fields to convert/keep for searching
Expand All @@ -73,8 +81,8 @@
-define(DTMF_RESULT_NEXT, <<"2">>).
-define(DTMF_RESULT_START, <<"3">>).

-define(TIMEOUT_MIN_DTMF, 5000).
-define(TIMEOUT_DTMF, 2000).
-define(TIMEOUT_MIN_DTMF, 5 * ?MILLISECONDS_IN_SECOND).
-define(TIMEOUT_DTMF, 2 * ?MILLISECONDS_IN_SECOND).
-define(TIMEOUT_ENDPOINT, ?DEFAULT_TIMEOUT_S).

-define(PROMPT_ENTER_PERSON_LASTNAME, <<"dir-enter_person_lastname">>). %% Please enter the first few letters of the person's lastname
Expand All @@ -92,35 +100,6 @@
-define(PROMPT_RESULT_NUMBER, <<"dir-result_number">>). %% To call
-define(PROMPT_RESULT_MENU, <<"dir-result_menu">>). %% press one. For the next result press two. To start over press three

%%------------------------------------------------------------------------------
%% Records
%%------------------------------------------------------------------------------
-record(directory_user, {first_name :: kz_term:ne_binary()
,last_name :: kz_term:ne_binary()
,full_name :: kz_term:ne_binary()
,first_last_keys :: kz_term:ne_binary() % DTMF-version of first, last
,last_first_keys :: kz_term:ne_binary() % DTMF-version of last, first
,callflow_id :: kz_term:ne_binary() % what callflow to use on match
,name_audio_id :: kz_term:api_binary() % pre-recorded audio of user's name
}).
-type directory_user() :: #directory_user{}.
-type directory_users() :: [directory_user()].

-type search_field() :: 'first' | 'last' | 'both'.

-record(directory, {sort_by = 'last' :: 'first' | 'last'
,search_fields = 'both' :: search_field()
,min_dtmf :: pos_integer()
,max_dtmf :: non_neg_integer()
,confirm_match = 'false' :: boolean()
,digits_collected = <<>> :: binary()
,users = [] :: directory_users()
,curr_users = [] :: directory_users()
}).
-type directory() :: #directory{}.

-type dtmf_action() :: 'route' | 'next' | 'start_over' | 'invalid' | 'continue'.

%%------------------------------------------------------------------------------
%% @doc Entry point for this module, attempts to call an endpoint as defined
%% in the Data payload. Returns continue if fails to connect or
Expand Down Expand Up @@ -427,24 +406,24 @@ get_directory_listing(Db, DirId) ->
%% play no users in this directory
{'error', 'no_users_in_directory'};
{'ok', Users} ->
{'ok', [get_directory_user(kz_json:get_value(<<"doc">>, U), kz_json:get_value(<<"value">>, U)) || U <- Users]};
{'ok', [get_directory_user(kz_json:get_json_value(<<"doc">>, U), kz_json:get_value(<<"value">>, U)) || U <- Users]};
{'error', _E}=E ->
lager:info("failed to lookup users for directory ~s: ~p", [DirId, _E]),
E
end.

-spec get_directory_user(kz_json:object(), kz_term:ne_binary()) -> directory_user().
get_directory_user(U, CallflowId) ->
First = kz_json:get_value(<<"first_name">>, U),
Last = kz_json:get_value(<<"last_name">>, U),
-spec get_directory_user(kzd_users:doc(), kz_term:ne_binary()) -> directory_user().
get_directory_user(UserDoc, CallflowId) ->
First = kzd_users:first_name(UserDoc),
Last = kzd_users:last_name(UserDoc),

#directory_user{first_name = First
,last_name = Last
,full_name = <<First/binary, " ", Last/binary>>
,first_last_keys = cf_util:alpha_to_dialpad(<<First/binary, Last/binary>>)
,last_first_keys = cf_util:alpha_to_dialpad(<<Last/binary, First/binary>>)
,callflow_id = CallflowId
,name_audio_id = kz_json:get_value(?RECORDED_NAME_KEY, U)
,name_audio_id = kz_json:get_value(?RECORDED_NAME_KEY, UserDoc)
}.

-spec sort_users(directory_users(), 'first' | 'last') -> directory_users().
Expand Down
30 changes: 30 additions & 0 deletions applications/callflow/src/module/cf_directory.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-ifndef(CF_DIRECTORY_HRL).

-record(directory_user, {first_name :: kz_term:ne_binary()
,last_name :: kz_term:ne_binary()
,full_name :: kz_term:ne_binary()
,first_last_keys :: kz_term:ne_binary() % DTMF-version of first, last
,last_first_keys :: kz_term:ne_binary() % DTMF-version of last, first
,callflow_id :: kz_term:ne_binary() % what callflow to use on match
,name_audio_id :: kz_term:api_binary() % pre-recorded audio of user's name
}).
-type directory_user() :: #directory_user{}.
-type directory_users() :: [directory_user()].

-type search_field() :: 'first' | 'last' | 'both'.

-record(directory, {sort_by = 'last' :: 'first' | 'last'
,search_fields = 'both' :: search_field()
,min_dtmf :: pos_integer()
,max_dtmf :: non_neg_integer()
,confirm_match = 'false' :: boolean()
,digits_collected = <<>> :: binary()
,users = [] :: directory_users()
,curr_users = [] :: directory_users()
}).
-type directory() :: #directory{}.

-type dtmf_action() :: 'route' | 'next' | 'start_over' | 'invalid' | 'continue'.

-define(CF_DIRECTORY_HRL, 'true').
-endif.
55 changes: 55 additions & 0 deletions applications/callflow/test/cf_directory_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
-module(cf_directory_tests).

-include_lib("eunit/include/eunit.hrl").
-include_lib("callflow/src/module/cf_directory.hrl").

-define(NAMES, [{<<"Terry">>, <<"White">>}
,{<<"Empty">>, <<"158">>}
,{<<"Alyssa">>, <<"Gruzwalski">>}
,{<<"Lauren">>, <<"Underwood">>}
,{<<"Julie">>, <<"Bloomenstein">>}
,{<<"Laurie">>, <<"Zeidman">>}
,{<<"Empty">>, <<"162">>}
,{<<"NaChelle">>, <<"Webster">>}
,{<<"Robert">>, <<"Sosin">>}
,{<<"Dawna">>, <<"Wilson">>}
,{<<"Greg">>, <<"Shanaberger">>}
,{<<"Mort">>, <<"Noveck">>}
,{<<"Rob">>, <<"Lech">>}
,{<<"Jessica">>, <<"Kalvenas">>}
,{<<"Ralph">>, <<"Sosin">>}
,{<<"Empty">>, <<"160">>}
,{<<"Daniel">>, <<"Noveck">>}
]).

-define(DIR_USERS, [cf_directory:get_directory_user(kz_json:from_list([{<<"first_name">>, First}
,{<<"last_name">>, Last}
])
,'undefined'
)
|| {First, Last} <- ?NAMES
]).

sort_order_test_() ->
[#directory_user{first_name=Alpha} | _] = cf_directory:sort_users(?DIR_USERS, 'first'),
[#directory_user{last_name=Omega} | _] = cf_directory:sort_users(?DIR_USERS, 'last'),
[{"Sort by first name", ?_assertEqual(<<"Alyssa">>, Alpha)}
,{"Sort by last name", ?_assertEqual(<<"158">>, Omega)}
].

filter_users_test_() ->
SortedFirst = cf_directory:sort_users(?DIR_USERS, 'first'),
SortedLast = cf_directory:sort_users(?DIR_USERS, 'last'),

[#directory_user{first_name=AlphaFirst} | _]
= cf_directory:filter_users(SortedFirst, <<"528">>, 'first'),

[#directory_user{last_name=OmegaLast
,first_name=OmegaFirst
} | _]
= cf_directory:filter_users(SortedLast, <<"668">>, 'last'),

[{"Filtered first_name sort", ?_assertEqual(<<"Lauren">>, AlphaFirst)}
,{"Filtered last_name sort - last name", ?_assertEqual(<<"Noveck">>, OmegaLast)}
,{"Filtered last_name sort - first name", ?_assertEqual(<<"Daniel">>, OmegaFirst)}
].

0 comments on commit 9992053

Please sign in to comment.