Skip to content

Commit

Permalink
Merge branch 'master' into ia-case-documents-api-dlrm-fc
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/uk/gov/hmcts/reform/iacasedocumentsapi/domain/entities/AsylumCaseDefinition.java
#	src/main/resources/application.yaml
#	src/test/java/uk/gov/hmcts/reform/iacasedocumentsapi/domain/entities/ccd/EventTest.java
  • Loading branch information
ikirsanov committed Oct 17, 2024
2 parents 3928790 + 1c2de41 commit 020a821
Show file tree
Hide file tree
Showing 29 changed files with 1,034 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr-title-validation.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Does PR title match RIA-nnnn or SNI-nnnn?
name: Does PR title match DIAC-nnn or RIA-nnnn or SNI-nnnn?
on:
pull_request:
types:
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ dependencies {
compileOnly 'org.projectlombok:lombok:1.18.32'

testImplementation group: 'org.mockito', name: 'mockito-junit-jupiter', version: '3.12.4'
testImplementation group: 'org.mockito', name: 'mockito-inline', version: '3.4.0'
testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
testImplementation group: 'org.springframework.security', name: 'spring-security-test', version: versions.springSecurity

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,9 @@ public enum AsylumCaseDefinition {

FEE_UPDATE_REASON(
"feeUpdateReason", new TypeReference<FeeUpdateReason>(){}),

NOTIFICATIONS(
"notifications", new TypeReference<List<IdValue<StoredNotification>>>(){})
;

private final String value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.field.IdValue;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.field.InterpreterLanguage;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.field.NationalityFieldValue;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.field.PreviousDecisionDetails;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.field.YesOrNo;
import uk.gov.hmcts.reform.iacasedocumentsapi.infrastructure.clients.model.refdata.CourtVenue;

public enum BailCaseFieldDefinition {
SENT_BY_CHECKLIST(
Expand Down Expand Up @@ -310,6 +310,8 @@ public enum BailCaseFieldDefinition {
"unsgnDecisionDocumentWithMetadata", new TypeReference<List<IdValue<DocumentWithMetadata>>>(){}),
UPLOAD_SIGNED_DECISION_NOTICE_DOCUMENT(
"uploadSignedDecisionNoticeDocument", new TypeReference<Document>(){}),
PREVIOUS_DECISION_DETAILS(
"previousDecisionDetails", new TypeReference<List<IdValue<PreviousDecisionDetails>>>() {}),
SIGNED_DECISION_DOCUMENT_WITH_METADATA(
"signDecisionDocumentWithMetadata", new TypeReference<List<IdValue<DocumentWithMetadata>>>(){}),
APPLICANT_INTERPRETER_SPOKEN_LANGUAGE(
Expand Down Expand Up @@ -357,7 +359,9 @@ public enum BailCaseFieldDefinition {
REF_DATA_LISTING_LOCATION_DETAIL(
"refDataListingLocationDetail", new TypeReference<CourtVenue>() {}),
IS_REMOTE_HEARING(
"isRemoteHearing", new TypeReference<YesOrNo>() {});
"isRemoteHearing", new TypeReference<YesOrNo>() {}),
CURRENT_CASE_STATE_VISIBLE_TO_ALL_USERS(
"currentCaseStateVisibleToAllUsers", new TypeReference<String>() {});


private final String value;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities;

import lombok.*;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.field.Document;

@EqualsAndHashCode
@ToString
@Getter
@Builder
@AllArgsConstructor
public class StoredNotification {
@NonNull private String notificationId;
@NonNull private String notificationDateSent;
@NonNull private String notificationSentTo;
@NonNull private String notificationBody;
@Setter private Document notificationDocument;
@NonNull private String notificationMethod;
@NonNull private String notificationStatus;
@NonNull private String notificationReference;
@NonNull private String notificationSubject;
private String notificationErrorMessage;
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public enum Event {

UPDATE_HEARING_REQUIREMENTS("updateHearingRequirements", CaseType.ASYLUM),
UPDATE_HEARING_ADJUSTMENTS("updateHearingAdjustments", CaseType.ASYLUM),
SAVE_NOTIFICATIONS_TO_DATA("saveNotificationsToData", CaseType.ASYLUM),

@JsonEnumDefaultValue
UNKNOWN("unknown", CaseType.UNKNOWN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public enum State {
PENDING_PAYMENT("pendingPayment"),
ADJOURNED("adjourned"),
FTPA_SUBMITTED("ftpaSubmitted"),



DECISION_CONDITIONAL_BAIL("decisionConditionalBail"),

@JsonEnumDefaultValue
UNKNOWN("unknown");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.field;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PreviousDecisionDetails {

private String decisionDetailsDate;
private String recordDecisionType;
private Document uploadSignedDecisionNoticeDocument;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package uk.gov.hmcts.reform.iacasedocumentsapi.domain.handlers.presubmit;

import org.springframework.stereotype.Component;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.AsylumCase;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.StoredNotification;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.CaseDetails;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.Event;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.callback.Callback;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.callback.PreSubmitCallbackResponse;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.callback.PreSubmitCallbackStage;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.field.Document;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.field.IdValue;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.handlers.PreSubmitCallbackHandler;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.service.SaveNotificationsToDataPdfService;

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

import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;
import static uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.AsylumCaseDefinition.NOTIFICATIONS;

@Component
public class SaveNotificationsToDataHandler implements PreSubmitCallbackHandler<AsylumCase> {

private final SaveNotificationsToDataPdfService saveNotificationsToDataPdfService;

public SaveNotificationsToDataHandler(
SaveNotificationsToDataPdfService saveNotificationsToDataPdfService
) {
this.saveNotificationsToDataPdfService = saveNotificationsToDataPdfService;
}

public boolean canHandle(
PreSubmitCallbackStage callbackStage,
Callback<AsylumCase> callback
) {
requireNonNull(callbackStage, "callbackStage must not be null");
requireNonNull(callback, "callback must not be null");

return callbackStage == PreSubmitCallbackStage.ABOUT_TO_SUBMIT
&& callback.getEvent() == Event.SAVE_NOTIFICATIONS_TO_DATA;
}

public PreSubmitCallbackResponse<AsylumCase> handle(
PreSubmitCallbackStage callbackStage,
Callback<AsylumCase> callback
) {
if (!canHandle(callbackStage, callback)) {
throw new IllegalStateException("Cannot handle callback");
}

final CaseDetails<AsylumCase> caseDetails = callback.getCaseDetails();
final AsylumCase asylumCase = caseDetails.getCaseData();

Optional<List<IdValue<StoredNotification>>> maybeExistingNotifications =
asylumCase.read(NOTIFICATIONS);

ArrayList<IdValue<StoredNotification>> newNotifications = new ArrayList<>();
List<String> invalidNotificationStatuses = List.of("Cancelled", "Failed", "Technical-failure",
"Temporary-failure", "Permanent-failure", "Validation-failed", "Virus-scan-failed");
for (IdValue<StoredNotification> notification : maybeExistingNotifications.orElse(emptyList())) {
StoredNotification storedNotification = notification.getValue();
if (storedNotification.getNotificationDocument() == null
&& !invalidNotificationStatuses.contains(storedNotification.getNotificationStatus())) {
String notificationBody = storedNotification.getNotificationBody();
String notificationReference = storedNotification.getNotificationReference();
Document notificationPdf =
saveNotificationsToDataPdfService.createPdf(notificationBody, notificationReference);
storedNotification.setNotificationDocument(notificationPdf);
notification = new IdValue<>(notification.getId(), storedNotification);
}
newNotifications.add(notification);
}
asylumCase.write(NOTIFICATIONS, newNotifications);

return new PreSubmitCallbackResponse<>(asylumCase);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package uk.gov.hmcts.reform.iacasedocumentsapi.domain.handlers.presubmit.bail;

import static java.util.Objects.requireNonNull;
import static uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.BailCaseFieldDefinition.HEARING_DOCUMENTS;
import static uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.BailCaseFieldDefinition.LISTING_EVENT;
import static uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.BailCaseFieldDefinition.*;
import static uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ListingEvent.INITIAL_LISTING;

import org.springframework.beans.factory.annotation.Qualifier;
Expand All @@ -12,6 +11,7 @@
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ListingEvent;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.CaseDetails;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.Event;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.State;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.callback.Callback;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.callback.PreSubmitCallbackResponse;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.callback.PreSubmitCallbackStage;
Expand All @@ -25,15 +25,18 @@ public class BailNoticeOfHearingCreator implements PreSubmitCallbackHandler<Bail

private final DocumentCreator<BailCase> bailInitialListingNoticeOfHearingCreator;
private final DocumentCreator<BailCase> bailRelistingNoticeOfHearingCreator;
private final DocumentCreator<BailCase> bailConditionalBailRelistingNoticeOfHearingCreator;
private final BailDocumentHandler bailDocumentHandler;

public BailNoticeOfHearingCreator(
@Qualifier("bailNoticeOfHearingInitialListing") DocumentCreator<BailCase> bailInitialListingNoticeOfHearingCreator,
@Qualifier("bailNoticeOfHearingRelisting") DocumentCreator<BailCase> bailRelistingNoticeOfHearingCreator,
@Qualifier("bailNoticeOfHearingConditionalBailRelisting") DocumentCreator<BailCase> bailConditionalBailRelistingNoticeOfHearingCreator,
BailDocumentHandler bailDocumentHandler
) {
this.bailInitialListingNoticeOfHearingCreator = bailInitialListingNoticeOfHearingCreator;
this.bailRelistingNoticeOfHearingCreator = bailRelistingNoticeOfHearingCreator;
this.bailConditionalBailRelistingNoticeOfHearingCreator = bailConditionalBailRelistingNoticeOfHearingCreator;
this.bailDocumentHandler = bailDocumentHandler;
}

Expand All @@ -60,10 +63,16 @@ public PreSubmitCallbackResponse<BailCase> handle(
final BailCase bailCase = caseDetails.getCaseData();
boolean isInitialListing = bailCase.read(LISTING_EVENT, ListingEvent.class)
.map(listingEvent -> INITIAL_LISTING == listingEvent).orElse(false);

Document bailDocument = isInitialListing
? bailInitialListingNoticeOfHearingCreator.create(caseDetails)
: bailRelistingNoticeOfHearingCreator.create(caseDetails);
boolean isConditionalBail = bailCase.read(CURRENT_CASE_STATE_VISIBLE_TO_ALL_USERS, String.class)
.orElse("null").equals(State.DECISION_CONDITIONAL_BAIL.toString());
Document bailDocument;
if (isInitialListing) {
bailDocument = bailInitialListingNoticeOfHearingCreator.create(caseDetails);
} else if (isConditionalBail) {
bailDocument = bailConditionalBailRelistingNoticeOfHearingCreator.create(caseDetails);
} else {
bailDocument = bailRelistingNoticeOfHearingCreator.create(caseDetails);
}

bailDocumentHandler.appendWithMetadata(
bailCase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,31 @@
import uk.gov.hmcts.reform.iacasedocumentsapi.infrastructure.clients.DocmosisDocumentConversionClient;

@Service
public class WordDocumentToPdfConverter {
public class DocumentToPdfConverter {

private final DocmosisDocumentConversionClient docmosisDocumentConversionClient;

public WordDocumentToPdfConverter(DocmosisDocumentConversionClient docmosisDocumentConversionClient) {
public DocumentToPdfConverter(DocmosisDocumentConversionClient docmosisDocumentConversionClient) {
this.docmosisDocumentConversionClient = docmosisDocumentConversionClient;
}

public File convertResourceToPdf(Resource resource) {
public File convertWordDocResourceToPdf(Resource resource) {
return convertResourceToPdf(resource, ".docx");
}

public File convertHtmlDocResourceToPdf(Resource resource) {
return convertResourceToPdf(resource, ".html");
}

private File convertResourceToPdf(Resource resource, String suffix) {

File tempPdfFile;

try (InputStream wordDocumentInputStream = resource.getInputStream()) {

File tempWordDocumentFile = createTempFile(
"tmp_",
".docx");
suffix);

Files.copy(
wordDocumentInputStream,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package uk.gov.hmcts.reform.iacasedocumentsapi.domain.service;

import org.apache.commons.io.FileUtils;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.field.Document;

import java.io.File;
import java.io.IOException;

@Service
public class SaveNotificationsToDataPdfService {

private static final String PDF_CONTENT_TYPE = "application/pdf";

private final DocumentToPdfConverter documentToPdfConverter;
private final DocumentUploader documentUploader;

public SaveNotificationsToDataPdfService(
DocumentUploader documentUploader,
DocumentToPdfConverter documentToPdfConverter
) {
this.documentUploader = documentUploader;
this.documentToPdfConverter = documentToPdfConverter;
}

public Document createPdf(String notificationBody, String notificationReference) {

byte[] byteArray = notificationBody.getBytes();
Resource resource = new ByteArrayResource(byteArray);

File notificationPdf =
documentToPdfConverter.convertHtmlDocResourceToPdf(resource);

ByteArrayResource byteArrayResource = getByteArrayResource(
notificationPdf,
notificationReference + ".PDF"
);

return documentUploader.upload(byteArrayResource, PDF_CONTENT_TYPE);
}

private ByteArrayResource getByteArrayResource(File notificationPdf, String filename) {

byte[] byteArray;

try {
byteArray = FileUtils.readFileToByteArray(notificationPdf);

} catch (IOException e) {
throw new IllegalStateException("Error reading converted pdf");
}

return new ByteArrayResource(byteArray) {
@Override
public String getFilename() {
return filename;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package uk.gov.hmcts.reform.iacasedocumentsapi.domain.service.bail;

import static java.util.Objects.requireNonNull;
import static uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.BailCaseFieldDefinition.DECISION_UNSIGNED_DOCUMENT;
import static uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.BailCaseFieldDefinition.SIGNED_DECISION_DOCUMENT_WITH_METADATA;
import static uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.BailCaseFieldDefinition.UNSIGNED_DECISION_DOCUMENTS_WITH_METADATA;
import static uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.BailCaseFieldDefinition.UPLOAD_SIGNED_DECISION_NOTICE_DOCUMENT;
import static uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.BailCaseFieldDefinition.*;

import org.springframework.stereotype.Service;
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.BailCase;
Expand Down Expand Up @@ -36,13 +33,21 @@ public void uploadSignedDecision(CaseDetails<BailCase> caseDetails) {
uploadSignedDecisionPdfService.generatePdf(caseDetails);

requireNonNull(finalSignedDecisionPdf, "Document to pdf conversion failed");

documentHandler.addWithMetadata(
if (bailCase.read(PREVIOUS_DECISION_DETAILS).isPresent()) {
documentHandler.appendWithMetadata(
bailCase,
finalSignedDecisionPdf,
SIGNED_DECISION_DOCUMENT_WITH_METADATA,
DocumentTag.SIGNED_DECISION_NOTICE
);
} else {
documentHandler.addWithMetadata(
bailCase,
finalSignedDecisionPdf,
SIGNED_DECISION_DOCUMENT_WITH_METADATA,
DocumentTag.SIGNED_DECISION_NOTICE
);
);
}
bailCase.clear(DECISION_UNSIGNED_DOCUMENT);
bailCase.clear(UNSIGNED_DECISION_DOCUMENTS_WITH_METADATA);
} catch (RuntimeException e) {
Expand Down
Loading

0 comments on commit 020a821

Please sign in to comment.