./ct_report/coverage/mongoose_graphql_account_admin_mutation.COVER.html

1 -module(mongoose_graphql_account_admin_mutation).
2 -behaviour(mongoose_graphql).
3
4 -export([execute/4]).
5 -export([await_execution/4]).
6
7 -ignore_xref([execute/4]).
8
9 -include("../mongoose_graphql_types.hrl").
10
11 -import(mongoose_graphql_helper, [format_result/2, make_error/2]).
12
13 execute(_Ctx, account, <<"registerUser">>, Args) ->
14 23 register_user(Args);
15 execute(_Ctx, account, <<"removeUser">>, Args) ->
16 17 remove_user(Args);
17 execute(_Ctx, account, <<"banUser">>, Args) ->
18 7 ban_user(Args);
19 execute(_Ctx, account, <<"changeUserPassword">>, Args) ->
20 10 change_user_password(Args);
21 execute(#{method := cli}, account, <<"importUsers">>, Args) ->
22 2 import_users(Args);
23 execute(#{method := http}, account, <<"importUsers">>, #{<<"filename">> := Filename}) ->
24 1 spawn(?MODULE, await_execution, [1000, mongoose_account_api, import_users, [Filename]]),
25 1 {ok, #{<<"status">> => <<"ImportUsers scheduled">>}}.
26
27 % Internal
28
29 -spec register_user(map()) -> {ok, map()} | {error, resolver_error()}.
30 register_user(#{<<"domain">> := Domain, <<"username">> := null,
31 <<"password">> := Password}) ->
32 3 case mongoose_account_api:register_generated_user(Domain, Password) of
33 {{ok, Msg}, JID} ->
34 3 {ok, make_user_payload(Msg, JID)};
35 {Result, JID} ->
36
:-(
format_result(Result, #{jid => JID})
37 end;
38 register_user(#{<<"domain">> := Domain, <<"username">> := Username,
39 <<"password">> := Password}) ->
40 20 Result = mongoose_account_api:register_user(Username, Domain, Password),
41 20 format_user_payload(Result, jid:make_bare(Username, Domain)).
42
43 -spec remove_user(map()) -> {ok, map()} | {error, resolver_error()}.
44 remove_user(#{<<"user">> := JID}) ->
45 17 Result = mongoose_account_api:unregister_user(JID),
46 17 format_user_payload(Result, JID).
47
48 -spec ban_user(map()) -> {ok, map()} | {error, resolver_error()}.
49 ban_user(#{<<"user">> := JID, <<"reason">> := Reason}) ->
50 7 Result = mongoose_account_api:ban_account(JID, Reason),
51 7 format_user_payload(Result, JID).
52
53 -spec change_user_password(map()) -> {ok, map()} | {error, resolver_error()}.
54 change_user_password(#{<<"user">> := JID, <<"newPassword">> := Password}) ->
55 10 Result = mongoose_account_api:change_password(JID, Password),
56 10 format_user_payload(Result, JID).
57
58 -spec import_users(map()) -> {ok, map()} | {error, resolver_error()}.
59 import_users(#{<<"filename">> := Filename}) ->
60 2 case mongoose_account_api:import_users(Filename) of
61 1 {ok, _} = Result -> Result;
62 1 Error -> make_error(Error, #{filename => Filename})
63 end.
64
65 -spec format_user_payload({atom(), string()}, jid:jid()) -> {ok, map()} | {error, resolver_error()}.
66 format_user_payload(InResult, JID) ->
67 54 case InResult of
68 {ok, Msg} ->
69 28 {ok, make_user_payload(Msg, jid:to_binary(JID))};
70 Result ->
71 26 make_error(Result, #{jid => jid:to_binary(JID)})
72 end.
73
74 -spec make_user_payload(string(), jid:literal_jid()) -> map().
75 make_user_payload(Msg, JID) ->
76 31 #{<<"message">> => iolist_to_binary(Msg), <<"jid">> => JID}.
77
78 await_execution(Timeout, Module, Fun, Args) ->
79 1 timer:sleep(Timeout),
80 1 apply(Module, Fun, Args).
Line Hits Source