-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAccountResource.java
More file actions
64 lines (51 loc) · 2.04 KB
/
AccountResource.java
File metadata and controls
64 lines (51 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package org.acme.controller;
import io.quarkus.security.identity.SecurityIdentity;
import jakarta.inject.Inject;
import jakarta.validation.Validator;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.*;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.acme.api.error.ApiError;
import org.acme.auth.AuthUtils;
import org.acme.enums.AccountHookAction;
import org.acme.functions.AccountHooks;
import org.acme.model.dto.Auth.AccountHookRequest;
import org.acme.model.dto.Auth.AccountHookResponse;
@Path("/api")
public class AccountResource {
@Inject
Validator validator;
@Inject
AccountHooks accountHooks;
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("/account-hooks")
public Response accountHooks(@Context SecurityIdentity identity,
AccountHookRequest request) {
Set<AccountHookAction> hooks = request.hooks();
String userId = AuthUtils.getUserId(identity);
if (userId == null) {
return Response.status(Response.Status.UNAUTHORIZED)
.entity(new ApiError(true, "Unauthorized.")).build();
}
// Map of AccountHookAction to a hook side-effect function
// The function returns whether the side-effect was successful
Map<AccountHookAction, Predicate<String>> hooksMap = Map.of(
AccountHookAction.ADD_EXAMPLE_SCREENER,
accountHooks::addExampleScreenerToAccount,
AccountHookAction.UNABLE_TO_DETERMINE,
(String uId) -> true);
// Run each action's function and determine whether successful
Map<String, Boolean> hookResults = hooks.stream()
.collect(Collectors.toMap(s -> s.toString(), s -> {
Predicate<String> fn = hooksMap.get(s);
return fn.test(userId);
}));
AccountHookResponse responseBody = new AccountHookResponse(true,
hookResults);
return Response.ok(responseBody).build();
}
}