Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification #214

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Notification #214

wants to merge 7 commits into from

Conversation

herdigiorgi
Copy link
Contributor

No description provided.

@@ -24,6 +25,12 @@ send_test(User, Channel, HolidayDate) ->
{ok, Pid} = supervisor:start_child(remind_router_sup, []),
gen_server:cast(Pid, {send_reminder, User, Channel, HolidayDate, Message, true}).

send_message(User, Channel, Message) ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you introduce this generic send_message function then send_test should not exist anymore and use the new one to test the reminder

Copy link
Contributor Author

@herdigiorgi herdigiorgi Jan 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its not possible because send_test accepts HolidayDate, and send_message sets the date internally. Maybe we need something more general like notification_handler, with a function send(User, Channel, MessageType, Message). Where MessageType can be a reminder, test, notification, or something else in the future.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah but what I mean is you should have one send_message function that takes all the parameters. Then you can have a higher level function like send_reminder that builds the reminder specficic message and then callssend_message. send_test could be removed, I think it's enough to just call the generic send_message with the test specific message.

@@ -58,7 +58,8 @@ from_json(Req, State = #{is_new := true,
} when is_list(DaysBefore), length(DaysBefore) =< 5 ->
CleanConfig = maps:filter(fun not_empty_value/2, Config),
{ok, _} = db_channel:create(Email, Name, Type, CleanConfig, DaysBefore, Time, TimeZone),

{ok, Channel} = db_channel:get(Email, Name),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

db_channel:create already returns the channel. You should understand the previous code before introducing a new line.

@@ -99,6 +100,8 @@ from_json(Req, State = #{is_new := false,
end.

delete_resource(Req, State = #{email := Email, name := Name}) ->
{ok, Channel} = db_channel:get(Email, Name),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't necessary, the channel is already part of the State (check the rest of the code).

remind_router:send_message(User, Channel, Msg).

send_creation_notification(Channel=#{name := Name}, #{user := User}) ->
Msg = <<"Crated a remainder '", Name/binary, "'.">>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-Crated
+Created

@herdigiorgi
Copy link
Contributor Author

herdigiorgi commented Jan 30, 2018

I can't figure out why the test of reminders_SUITE.erl on line 257 fails:

{ok, Channel} = db_channel:get(Email, Name),

test output error:

%%% reminders_SUITE ==> {{badmatch,{ok,200,
               [{<<"server">>,<<"Cowboy">>},
                {<<"date">>,<<"Tue, 30 Jan 2018 15:46:01 GMT">>},
                {<<"content-length">>,<<"2">>},
                {<<"content-type">>,<<"application/json">>}],
               []}},
 [{reminders_SUITE,monthly_limit_enforced,1,
                   [{file,"holiday_ping/_build/test/lib/holiday_ping/test/reminders_SUITE.erl"},
                    {line,257}]},
  {test_server,ts_tc,3,[{file,"test_server.erl"},{line,1529}]},
  {test_server,run_test_case_eval1,6,[{file,"test_server.erl"},{line,1045}]},
  {test_server,run_test_case_eval,9,[{file,"test_server.erl"},{line,977}]}]}

Message = <<"This is a holiday reminder: John Doe will be out today.">>,
Reminders = ets_channel:get_reminders(TableId, Email),
true =lists:any(
fun({ReminderEmail, ReminderMessage}) ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's more readable to assign the fun to a variable and the pass that to lists:any

UserName = maps:get(name, User),
FinalMessage = <<UserName/binary, ": ", Message/binary>>,
{ok, Pid} = supervisor:start_child(remind_router_sup, []),
gen_server:cast(Pid, {send_reminder, User, Channel, erlang:date(), FinalMessage, true}).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another issue is that right now, the log of sent reminders in the database has a boolean column test. Now, apart from regular reminders and test reminders, we introduced these notifications. We should probably remove the test column and have a reminder type or something, to properly inform in the log.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, i believe that creating a "message type" or something like that would be the most flexible way of doing it.

@herdigiorgi
Copy link
Contributor Author

Should I try to make remind_router more general, and make a message_router, capable of handle the 3 types of message we currently have? For that i will need to change the db schema.

@facundoolano
Copy link
Contributor

I think you should change the db schema to replace the test boolean for a message type field as discussed. You should also add the send_message function, but don't think you need to rename remind_router or change it's interface other than that (at least don't do it yet, let's see how the code looks after these changes and decide if it needs more refactoring).

Body),
ok = ktn_task:wait_for_success(
fun() ->
[{Email, _}] = ets_channel:get_reminders(TableId, Email),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has to check that the message text is the one expected

"/api/channels/test_ets"),
ok = ktn_task:wait_for_success(
fun() ->
[{Email, _}, {Email, _}] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Message = <<"This is a Holiday Ping test: John Doe will be out on holidays.">>,
Reminders = ets_channel:get_reminders(TableId, Email),
true = lists:any(
fun({ReminderEmail,ReminderMessage}) ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could do

fun({Email, Message}) -> true;
   (_OtherEmail, _OtherMessage) -> false
end

@@ -102,3 +103,37 @@ delete_channel(Config) ->
{ok, 204, _, _} = test_utils:api_request(delete, Token, "/api/channels/my_delete_channel"),
{ok, 404, _, _} = test_utils:api_request(delete, Token, "/api/channels/my_delete_channel"),
ok.

create_delete_channel_notification(Config) ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you also need to test the edit part: if you add a new target to the channel, the notification should go only to the new one; if you remove one, only the removed should receive the notification.

@unbalancedparentheses
Copy link
Member

@hernanex3 add a better title to the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants