11package org .acme .controller ;
22
3- import io .quarkus .logging .Log ;
43import io .quarkus .security .identity .SecurityIdentity ;
54import jakarta .inject .Inject ;
65import jakarta .validation .Validator ;
76import jakarta .ws .rs .*;
87import jakarta .ws .rs .core .*;
98
9+ import java .util .Map ;
10+ import java .util .Set ;
11+ import java .util .function .Predicate ;
12+ import java .util .stream .Collectors ;
13+
1014import org .acme .api .error .ApiError ;
1115import org .acme .auth .AuthUtils ;
16+ import org .acme .enums .AccountHookAction ;
17+ import org .acme .functions .AccountHooks ;
18+ import org .acme .model .dto .Auth .AccountHookRequest ;
1219import org .acme .model .dto .Auth .AccountHookResponse ;
1320
1421@ Path ("/api" )
@@ -17,29 +24,40 @@ public class AccountResource {
1724 @ Inject
1825 Validator validator ;
1926
20- @ GET
27+ @ Inject
28+ AccountHooks accountHooks ;
29+
30+ @ POST
31+ @ Consumes (MediaType .APPLICATION_JSON )
2132 @ Path ("/account-hooks" )
22- public Response getScreeners (@ Context SecurityIdentity identity ,
23- @ QueryParam ("action" ) String action ) {
33+ public Response accountHooks (@ Context SecurityIdentity identity ,
34+ AccountHookRequest request ) {
35+
36+ Set <AccountHookAction > hooks = request .hooks ();
2437 String userId = AuthUtils .getUserId (identity );
38+
2539 if (userId == null ) {
2640 return Response .status (Response .Status .UNAUTHORIZED )
2741 .entity (new ApiError (true , "Unauthorized." )).build ();
2842 }
2943
30- if (action == null || action .isBlank ()) {
31- return Response .status (Response .Status .BAD_REQUEST ).entity (
32- new ApiError (true , "Query parameter 'action' is required." ))
33- .build ();
34- }
35- Log .info ("Running account hooks for: " + userId );
44+ // Map of AccountHookAction to a hook side-effect function
45+ // The function returns whether the side-effect was successful
46+ Map <AccountHookAction , Predicate <String >> hooksMap = Map .of (
47+ AccountHookAction .ADD_EXAMPLE_SCREENER ,
48+ accountHooks ::addExampleScreenerToAccount ,
49+ AccountHookAction .UNABLE_TO_DETERMINE ,
50+ (String uId ) -> true );
3651
37- if (action .equals ("add example screener" )) {
38- Log .info ("***** Adding an exaample screener to the account *****" );
39- }
52+ // Run each action's function and determine whether successful
53+ Map <String , Boolean > hookResults = hooks .stream ()
54+ .collect (Collectors .toMap (s -> s .toString (), s -> {
55+ Predicate <String > fn = hooksMap .get (s );
56+ return fn .test (userId );
57+ }));
4058
41- AccountHookResponse responseBody = new AccountHookResponse (
42- "add example screener" , true );
59+ AccountHookResponse responseBody = new AccountHookResponse (true ,
60+ hookResults );
4361
4462 return Response .ok (responseBody ).build ();
4563 }
0 commit comments