Skip to content

Commit

Permalink
GAP-2097: Download Submission (#89)
Browse files Browse the repository at this point in the history
* initial

* handle no submitted date, rm gap id row

* Added file download to endpoint

* Fixed missing headers

* Add change to outputstream close

* Add basis for controller test

* Try with resources implementation
Remove filename and local save

* Exception test

* Restore functionality to odtservice

* add authorization, fetch email from user-service, misc changes

* controller tests

* service tests

* rm imports

* push pom changes

* add applicant service tests

* add odts service unit tests

* copy latest logic from lambda

* fix warnings

* fix switch case warning

* rm

* extract lambda fns

* Update src/main/java/gov/cabinetoffice/gap/applybackend/service/SubmissionService.java

Co-authored-by: jgunnCO <[email protected]>

* Update src/main/java/gov/cabinetoffice/gap/applybackend/service/SubmissionService.java

Co-authored-by: jgunnCO <[email protected]>

---------

Co-authored-by: john-tco <[email protected]>
Co-authored-by: john-tco <[email protected]>
Co-authored-by: jgunnCO <[email protected]>
  • Loading branch information
4 people authored Jan 18, 2024
1 parent 45fd111 commit 7a415c9
Show file tree
Hide file tree
Showing 12 changed files with 1,268 additions and 12 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,21 @@
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.odftoolkit</groupId>
<artifactId>odfdom-java</artifactId>
<version>0.10.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
Expand Down Expand Up @@ -140,6 +151,7 @@
<artifactId>notifications-java-client</artifactId>
<version>3.17.3-RELEASE</version>
</dependency>

<dependency>
<groupId>com.contentful.java</groupId>
<artifactId>java-sdk</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.UUID;

@EntityListeners(AuditingEntityListener.class)
Expand Down Expand Up @@ -98,6 +99,10 @@ public class Submission extends BaseEntity {
@Builder.Default
private Boolean mandatorySectionsCompleted = false;

public List<SubmissionSection> getSections() {
return this.getDefinition().getSections();
}

public SubmissionSection getSection(String sectionId) {
return this.getDefinition()
.getSections()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Data
@NoArgsConstructor
Expand All @@ -21,6 +23,21 @@ public class SubmissionSection {
@Builder.Default
private List<SubmissionQuestion> questions = new ArrayList<>();

public Optional<SubmissionQuestion> optionalGetQuestionById(String questionId) {
return this.questions
.stream()
.filter(question -> Objects.equals(question.getQuestionId(), questionId))
.findFirst();
}

public SubmissionQuestion getQuestionById(String questionId) {
return this.questions
.stream()
.filter(question -> Objects.equals(question.getQuestionId(), questionId))
.findFirst()
.orElseThrow(() -> new RuntimeException("Question with id " + questionId + " does not exist"));
}

public void addQuestion(final SubmissionQuestion question) {
if (this.getQuestions() == null) {
this.questions = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
package gov.cabinetoffice.gap.applybackend.service;

import gov.cabinetoffice.gap.applybackend.config.UserServiceConfig;
import gov.cabinetoffice.gap.applybackend.dto.api.JwtPayload;
import gov.cabinetoffice.gap.applybackend.exception.NotFoundException;
import gov.cabinetoffice.gap.applybackend.model.GrantApplicant;
import gov.cabinetoffice.gap.applybackend.repository.GrantApplicantRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

@RequiredArgsConstructor
@Service
public class GrantApplicantService {

private final UserServiceConfig userServiceConfig;
private final RestTemplate restTemplate;

@Value("${user-service.domain}")
private String userServiceDomain;


private final GrantApplicantRepository grantApplicantRepository;

public GrantApplicant getApplicantById(final String applicantId) {
Expand All @@ -20,6 +35,17 @@ public GrantApplicant getApplicantById(final String applicantId) {
.orElseThrow(() -> new NotFoundException(String.format("No Grant Applicant with ID %s was found", applicantId)));
}

public String getEmailById(final String applicantId, HttpServletRequest request) {
final String header = request.getHeader(HttpHeaders.AUTHORIZATION);
String jwt = header.split(" ")[1];
String url = userServiceDomain + "/user/" + applicantId + "/email";
final HttpHeaders headers = new HttpHeaders();
headers.add("Cookie", userServiceConfig.getCookieName() + "=" + jwt);
final HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);

return restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class).getBody();
}

public GrantApplicant getApplicantFromPrincipal() {
final JwtPayload jwtPayload = (JwtPayload) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
return this.getApplicantById(jwtPayload.getSub());
Expand Down
Loading

0 comments on commit 7a415c9

Please sign in to comment.