1 |
|
-module(mongoose_graphql_last_admin_mutation). |
2 |
|
-behaviour(mongoose_graphql). |
3 |
|
|
4 |
|
-export([execute/4]). |
5 |
|
|
6 |
|
-ignore_xref([execute/4]). |
7 |
|
|
8 |
|
-include("../mongoose_graphql_types.hrl"). |
9 |
|
|
10 |
|
-import(mongoose_graphql_helper, [make_error/2]). |
11 |
|
-import(mongoose_graphql_last_helper, [format_old_users/1]). |
12 |
|
|
13 |
|
-type old_users() :: mongoose_graphql_last_helper:old_users(). |
14 |
|
-type last_info() :: mongoose_graphql_last_helper:last_info(). |
15 |
|
-type args() :: mongoose_graphql:args(). |
16 |
|
|
17 |
|
execute(_Ctx, last, <<"setLast">>, Args) -> |
18 |
:-( |
set_last(Args); |
19 |
|
execute(_Ctx, last, <<"removeOldUsers">>, Args) -> |
20 |
:-( |
remove_old_users(Args). |
21 |
|
|
22 |
|
-spec set_last(args()) -> {ok, last_info()} | {error, resolver_error()}. |
23 |
|
set_last(#{<<"user">> := JID, <<"timestamp">> := Timestamp, <<"status">> := Status}) -> |
24 |
:-( |
mongoose_graphql_last_helper:set_last(JID, Timestamp, Status). |
25 |
|
|
26 |
|
-spec remove_old_users(args()) -> {ok, old_users()} | {error, resolver_error()}. |
27 |
|
remove_old_users(#{<<"domain">> := null, <<"timestamp">> := Timestamp}) -> |
28 |
:-( |
Timestamp2 = mongoose_graphql_last_helper:microseconds_to_seconds(Timestamp), |
29 |
:-( |
{ok, OldUsers} = mod_last_api:remove_old_users(Timestamp2), |
30 |
:-( |
{ok, format_old_users(OldUsers)}; |
31 |
|
remove_old_users(#{<<"domain">> := Domain, <<"timestamp">> := Timestamp}) -> |
32 |
:-( |
Timestamp2 = mongoose_graphql_last_helper:microseconds_to_seconds(Timestamp), |
33 |
:-( |
case mod_last_api:remove_old_users(Domain, Timestamp2) of |
34 |
|
{ok, OldUsers} -> |
35 |
:-( |
{ok, format_old_users(OldUsers)}; |
36 |
|
Error -> |
37 |
:-( |
make_error(Error, #{domain => Domain}) |
38 |
|
end. |