diff --git a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/event/DashboardView.java b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/event/DashboardView.java new file mode 100644 index 0000000000..0b6997adca --- /dev/null +++ b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/event/DashboardView.java @@ -0,0 +1,37 @@ +package uk.gov.hmcts.reform.pcs.ccd.event; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; +import uk.gov.hmcts.ccd.sdk.api.CCDConfig; +import uk.gov.hmcts.ccd.sdk.api.DecentralisedConfigBuilder; +import uk.gov.hmcts.ccd.sdk.api.Permission; +import uk.gov.hmcts.reform.pcs.ccd.ShowConditions; +import uk.gov.hmcts.reform.pcs.ccd.accesscontrol.UserRole; +import uk.gov.hmcts.reform.pcs.ccd.domain.PCSCase; +import uk.gov.hmcts.reform.pcs.ccd.domain.State; +import uk.gov.hmcts.reform.pcs.ccd.event.dashboard.StartDashboardViewHandler; +import uk.gov.hmcts.reform.pcs.ccd.event.dashboard.SubmitDashboardViewHandler; + +import static uk.gov.hmcts.reform.pcs.ccd.event.EventId.dashboardView; + +@Component +@Slf4j +@RequiredArgsConstructor +public class DashboardView implements CCDConfig { + + private final StartDashboardViewHandler startDashboardViewHandler; + private final SubmitDashboardViewHandler submitDashboardViewHandler; + + @Override + public void configureDecentralised(final DecentralisedConfigBuilder configBuilder) { + configBuilder + .decentralisedEvent(dashboardView.name(), submitDashboardViewHandler, startDashboardViewHandler) + .forState(State.CASE_ISSUED) + .showCondition(ShowConditions.NEVER_SHOW) + .name("Dashboard view") + .description("Compute dashboard notifications for case journey") + .grant(Permission.CRU, UserRole.DEFENDANT); + } +} + diff --git a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/event/EventId.java b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/event/EventId.java index 3a84e15878..903d63e465 100644 --- a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/event/EventId.java +++ b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/event/EventId.java @@ -7,5 +7,6 @@ public enum EventId { enforceTheOrder, respondPossessionClaim, submitDefendantResponse, - createTestCase + createTestCase, + dashboardView } diff --git a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/event/dashboard/StartDashboardViewHandler.java b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/event/dashboard/StartDashboardViewHandler.java new file mode 100644 index 0000000000..01eb9d96a1 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/event/dashboard/StartDashboardViewHandler.java @@ -0,0 +1,75 @@ +package uk.gov.hmcts.reform.pcs.ccd.event.dashboard; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; +import uk.gov.hmcts.ccd.sdk.api.EventPayload; +import uk.gov.hmcts.ccd.sdk.api.callback.Start; +import uk.gov.hmcts.reform.pcs.ccd.domain.PCSCase; +import uk.gov.hmcts.reform.pcs.ccd.domain.State; +import uk.gov.hmcts.reform.pcs.ccd.event.EventId; +import uk.gov.hmcts.reform.pcs.ccd.service.DraftCaseDataService; +import uk.gov.hmcts.reform.pcs.ccd.service.dashboard.DashboardJourneyService; +import uk.gov.hmcts.reform.pcs.dashboard.model.DashboardNotification; + +import java.util.List; +import java.util.Optional; + +import static uk.gov.hmcts.reform.pcs.ccd.event.EventId.respondPossessionClaim; + +@Component +@Slf4j +@RequiredArgsConstructor +public class StartDashboardViewHandler implements Start { + + private final DraftCaseDataService draftCaseDataService; + private final DashboardJourneyService dashboardJourneyService; + private final ObjectMapper objectMapper; + + @Override + public PCSCase start(EventPayload eventPayload) { + long caseReference = eventPayload.caseReference(); + State state = State.CASE_ISSUED; + + PCSCase submittedCaseData = eventPayload.caseData(); + + Optional draftForRespondPossession = getDraft(caseReference, respondPossessionClaim); + + List notifications = dashboardJourneyService.computeNotifications( + caseReference, + state, + submittedCaseData, + draftForRespondPossession + ); + + String payloadJson = toPayloadJson(caseReference, notifications); + + submittedCaseData.setCaseTitleMarkdown(payloadJson); + return submittedCaseData; + } + + private Optional getDraft(long caseReference, EventId eventId) { + try { + return draftCaseDataService.getUnsubmittedCaseData(caseReference, eventId); + } catch (Exception e) { + log.warn("Failed to load draft case data for caseReference={} eventId={}", caseReference, eventId, e); + return Optional.empty(); + } + } + + private String toPayloadJson(long caseReference, List notifications) { + DashboardPayload payload = new DashboardPayload(notifications); + try { + return objectMapper.writeValueAsString(payload); + } catch (JsonProcessingException e) { + log.error("Failed to serialise dashboard payload for caseReference={}", caseReference, e); + return "{\"notifications\":[]}"; + } + } + + private record DashboardPayload(List notifications) { + } +} + diff --git a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/event/dashboard/SubmitDashboardViewHandler.java b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/event/dashboard/SubmitDashboardViewHandler.java new file mode 100644 index 0000000000..9d8f643eb8 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/event/dashboard/SubmitDashboardViewHandler.java @@ -0,0 +1,23 @@ +package uk.gov.hmcts.reform.pcs.ccd.event.dashboard; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; +import uk.gov.hmcts.ccd.sdk.api.EventPayload; +import uk.gov.hmcts.ccd.sdk.api.callback.Submit; +import uk.gov.hmcts.ccd.sdk.api.callback.SubmitResponse; +import uk.gov.hmcts.reform.pcs.ccd.domain.PCSCase; +import uk.gov.hmcts.reform.pcs.ccd.domain.State; + +@Component +@Slf4j +@RequiredArgsConstructor +public class SubmitDashboardViewHandler implements Submit { + + @Override + public SubmitResponse submit(EventPayload eventPayload) { + // READ-ONLY: the dashboard view event should not persist changes. + return SubmitResponse.defaultResponse(); + } +} + diff --git a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/dashboard/DashboardJourneyService.java b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/dashboard/DashboardJourneyService.java new file mode 100644 index 0000000000..4c8de29018 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/dashboard/DashboardJourneyService.java @@ -0,0 +1,56 @@ +package uk.gov.hmcts.reform.pcs.ccd.service.dashboard; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import uk.gov.hmcts.reform.pcs.ccd.domain.PCSCase; +import uk.gov.hmcts.reform.pcs.ccd.domain.State; +import uk.gov.hmcts.reform.pcs.dashboard.model.DashboardNotification; + +import java.util.List; +import java.util.Map; +import java.util.Optional; + +/** + * Computes dashboard notifications (template IDs + values) from submitted and draft case data. + * + *

READ-ONLY: callers are responsible for loading any data needed and passing it in.

+ */ +@Service +@Slf4j +@RequiredArgsConstructor +public class DashboardJourneyService { + + public List computeNotifications(long caseReference, + State state, + PCSCase submittedCaseData, + Optional draftCaseData) { + + boolean hasDraft = draftCaseData.isPresent(); + + if (state == State.CASE_ISSUED) { + DashboardNotification journeyNotification = DashboardNotification.builder() + .templateId("Notice.PCS.Dashboard.CaseIssued") + .templateValues(Map.of( + "caseReference", caseReference, + "hasDraft", hasDraft + )) + .build(); + + return List.of(journeyNotification); + } + + if (state == State.AWAITING_SUBMISSION_TO_HMCTS && hasDraft) { + DashboardNotification resumeNotification = DashboardNotification.builder() + .templateId("Notice.PCS.Dashboard.DraftInProgress") + .templateValues(Map.of( + "caseReference", caseReference + )) + .build(); + return List.of(resumeNotification); + } + + return List.of(); + } +} +