12274f9ed0
Prefs in user.preferences; push_subscription table; VAPID send on reminder/waitlist. Refs EventHub/EventHubBack#75
80 lines
2.6 KiB
Erlang
80 lines
2.6 KiB
Erlang
%%%-------------------------------------------------------------------
|
|
%%% @doc GET/PUT /v1/notifications/prefs (Back#75).
|
|
%%% @end
|
|
%%%-------------------------------------------------------------------
|
|
-module(handler_notification_prefs).
|
|
-behaviour(cowboy_handler).
|
|
|
|
-export([init/2, trails/0]).
|
|
|
|
init(Req, Opts) ->
|
|
Method = cowboy_req:method(Req),
|
|
case handler_utils:auth_user(Req) of
|
|
{ok, UserId, Req1} ->
|
|
case Method of
|
|
<<"GET">> ->
|
|
case logic_notification_prefs:get_prefs(UserId) of
|
|
{ok, Prefs} ->
|
|
handler_utils:send_json(Req1, 200, Prefs);
|
|
{error, not_found} ->
|
|
handler_utils:send_error(Req1, 404, <<"User not found">>)
|
|
end;
|
|
<<"PUT">> ->
|
|
put_prefs(Req1, UserId);
|
|
_ ->
|
|
cowboy_req:reply(405, #{}, <<>>, Req1)
|
|
end;
|
|
{error, Code, Message, Req1} ->
|
|
handler_utils:send_error(Req1, Code, Message)
|
|
end,
|
|
{ok, Req, Opts}.
|
|
|
|
trails() ->
|
|
[
|
|
#{
|
|
path => <<"/v1/notifications/prefs">>,
|
|
method => <<"GET">>,
|
|
description => <<"Get notification channel prefs (email/push)">>,
|
|
tags => [<<"Notifications">>],
|
|
responses => #{
|
|
200 => #{description => <<"Prefs">>},
|
|
401 => #{description => <<"Unauthorized">>}
|
|
}
|
|
},
|
|
#{
|
|
path => <<"/v1/notifications/prefs">>,
|
|
method => <<"PUT">>,
|
|
description => <<"Update notification channel prefs">>,
|
|
tags => [<<"Notifications">>],
|
|
responses => #{
|
|
200 => #{description => <<"Updated prefs">>},
|
|
400 => #{description => <<"Invalid body">>},
|
|
401 => #{description => <<"Unauthorized">>}
|
|
}
|
|
}
|
|
].
|
|
|
|
put_prefs(Req0, UserId) ->
|
|
{ok, Body, Req1} = cowboy_req:read_body(Req0),
|
|
try jsx:decode(Body, [return_maps]) of
|
|
Map when is_map(Map) ->
|
|
case {maps:get(<<"email">>, Map, undefined), maps:get(<<"push">>, Map, undefined)} of
|
|
{Email, Push} when is_boolean(Email), is_boolean(Push) ->
|
|
case logic_notification_prefs:put_prefs(UserId, #{email => Email, push => Push}) of
|
|
{ok, Prefs} ->
|
|
handler_utils:send_json(Req1, 200, Prefs);
|
|
{error, not_found} ->
|
|
handler_utils:send_error(Req1, 404, <<"User not found">>);
|
|
{error, _} ->
|
|
handler_utils:send_error(Req1, 500, <<"Update failed">>)
|
|
end;
|
|
_ ->
|
|
handler_utils:send_error(Req1, 400, <<"email and push booleans required">>)
|
|
end;
|
|
_ ->
|
|
handler_utils:send_error(Req1, 400, <<"Invalid JSON">>)
|
|
catch
|
|
_:_ ->
|
|
handler_utils:send_error(Req1, 400, <<"Invalid JSON format">>)
|
|
end.
|