-
Notifications
You must be signed in to change notification settings - Fork 2
Api trigger impl in data gateway #264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Gmasek
wants to merge
3
commits into
develop
Choose a base branch
from
develop_api_trigger
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/io/redlink/more/data/controller/TriggerProxyController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Copyright LBI-DHP and/or licensed to LBI-DHP under one or more | ||
| * contributor license agreements (LBI-DHP: Ludwig Boltzmann Institute | ||
| * for Digital Health and Prevention -- A research institute of the | ||
| * Ludwig Boltzmann Gesellschaft, Oesterreichische Vereinigung zur | ||
| * Foerderung der wissenschaftlichen Forschung). | ||
| * Licensed under the Elastic License 2.0. | ||
| */ | ||
| package io.redlink.more.data.controller; | ||
|
|
||
| import io.redlink.more.data.model.ResolvedInterventionToken; | ||
| import io.redlink.more.data.repository.StudyRepository; | ||
| import io.redlink.more.data.service.ExternalService; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.access.AccessDeniedException; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| @RestController | ||
| public class TriggerProxyController { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(TriggerProxyController.class); | ||
| static final String PENDING_PARTICIPANTS_KEY = "pendingParticipants"; | ||
|
|
||
| private final StudyRepository studyRepository; | ||
| private final ExternalService externalService; | ||
|
|
||
| public TriggerProxyController(StudyRepository studyRepository, ExternalService externalService) { | ||
| this.studyRepository = studyRepository; | ||
| this.externalService = externalService; | ||
| } | ||
|
|
||
| @PostMapping(value = "/api/v1/trigger/external", produces = MediaType.APPLICATION_JSON_VALUE) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add it to the OpenAPI Spec |
||
| public ResponseEntity<?> triggerExternal( | ||
| @RequestHeader("More-Api-Token") String moreApiToken, | ||
| @RequestBody TriggerRequest request | ||
| ) { | ||
| LOG.info("Validating intervention token and writing pending participants to DB"); | ||
|
|
||
| try { | ||
| ResolvedInterventionToken resolved = externalService.validateInterventionToken(moreApiToken); | ||
|
|
||
| if (!resolved.studyActive()) { | ||
| LOG.warn("Trigger rejected: study {} is not active", resolved.studyId()); | ||
| return ResponseEntity.status(HttpStatus.FORBIDDEN) | ||
| .body(Map.of("error", "Study not active")); | ||
| } | ||
|
|
||
| studyRepository.addPendingTriggerParticipants( | ||
| resolved.studyId(), | ||
| resolved.interventionId(), | ||
| PENDING_PARTICIPANTS_KEY, | ||
| request.participantIds() | ||
| ); | ||
|
|
||
| return ResponseEntity.accepted().build(); | ||
| } catch (AccessDeniedException e) { | ||
| return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); | ||
| } catch (Exception e) { | ||
| LOG.error("Error processing external trigger request", e); | ||
| return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); | ||
| } | ||
| } | ||
|
|
||
| public record TriggerRequest(List<Integer> participantIds) {} | ||
| } | ||
8 changes: 8 additions & 0 deletions
8
src/main/java/io/redlink/more/data/model/ResolvedInterventionToken.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package io.redlink.more.data.model; | ||
|
|
||
|
|
||
| public record ResolvedInterventionToken( | ||
| Long studyId, | ||
| Integer interventionId, | ||
| boolean studyActive | ||
| ) {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the
moreApiTokensimilar as in theExternalDataApiV1Controller