Skip to content

Commit

Permalink
Handle conflicts issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmendrak committed Nov 7, 2024
1 parent 8087638 commit 857a510
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface PocApiClient {

@PostMapping(value = "/ccd/cases", consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<CaseDetails> createCase(@RequestBody POCCaseDetails caseDetails);
CaseDetails createCase(@RequestBody POCCaseDetails caseDetails);

@GetMapping(value = "/ccd/cases/{case-ref}/history")
List<AuditEvent> getEvents(@PathVariable("case-ref") String caseReference);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package uk.gov.hmcts.ccd.domain.service.createcase;

import javax.persistence.OptimisticLockException;
import javax.persistence.PersistenceException;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.StaleObjectStateException;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import uk.gov.hmcts.ccd.clients.PocApiClient;
import uk.gov.hmcts.ccd.data.casedetails.CaseDetailsEntity;
import uk.gov.hmcts.ccd.domain.model.aggregated.IdamUser;
import uk.gov.hmcts.ccd.domain.model.aggregated.POCCaseDetails;
import uk.gov.hmcts.ccd.domain.model.aggregated.POCEventDetails;
Expand All @@ -20,8 +16,6 @@
import uk.gov.hmcts.ccd.domain.service.common.CaseTypeService;
import uk.gov.hmcts.ccd.domain.service.stdapi.AboutToSubmitCallbackResponse;
import uk.gov.hmcts.ccd.endpoint.exceptions.CaseConcurrencyException;
import uk.gov.hmcts.ccd.endpoint.exceptions.CasePersistenceException;
import uk.gov.hmcts.ccd.endpoint.exceptions.ReferenceKeyUniqueConstraintException;

@Slf4j
@Service
Expand Down Expand Up @@ -64,20 +58,22 @@ public CaseDetails saveAuditEventForCaseDetails(AboutToSubmitCallbackResponse re
.caseDetails(newCaseDetails).eventDetails(eventDetails.build()).build();


var createdCaseResponse = pocApiClient.createCase(pocCaseDetails);
if (createdCaseResponse.getStatusCode().equals(HttpStatus.CONFLICT)) {
try {
CaseDetails caseDetails = pocApiClient.createCase(pocCaseDetails);
log.info("pocCaseDetails: {}", caseDetails);
log.info("pocCaseDetails id: {}", caseDetails.getId());
log.info("pocCaseDetails reference before: {}", caseDetails.getReference());
caseDetails.setId(caseDetails.getReference().toString());
caseDetails.setReference(caseDetails.getReference());
log.info("pocCaseDetails reference: {}", caseDetails.getReference());

return caseDetails;
} catch (FeignException.Conflict conflict) {
throw new CaseConcurrencyException("""
Unfortunately we were unable to save your work to the case as \
another action happened at the same time.
Please review the case and try again.""");

}
CaseDetails caseDetails = createdCaseResponse.getBody();
log.info("pocCaseDetails: {}", createdCaseResponse);
log.info("pocCaseDetails id: {}", caseDetails.getId());
log.info("pocCaseDetails reference before: {}", caseDetails.getReference());
caseDetails.setId(caseDetails.getReference().toString());
caseDetails.setReference(caseDetails.getReference());
log.info("pocCaseDetails reference: {}", caseDetails.getReference());
return caseDetails;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package uk.gov.hmcts.ccd.domain.service.createevent;

import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import uk.gov.hmcts.ccd.clients.PocApiClient;
import uk.gov.hmcts.ccd.domain.model.aggregated.POCCaseDetails;
Expand All @@ -12,8 +14,8 @@
import uk.gov.hmcts.ccd.domain.model.definition.CaseTypeDefinition;
import uk.gov.hmcts.ccd.domain.model.std.Event;
import uk.gov.hmcts.ccd.domain.service.common.CaseTypeService;
import uk.gov.hmcts.ccd.domain.service.message.MessageContext;
import uk.gov.hmcts.ccd.domain.service.message.MessageService;
import uk.gov.hmcts.ccd.endpoint.exceptions.CaseConcurrencyException;

@Slf4j
@Service
Expand All @@ -32,10 +34,10 @@ public POCCreateCaseEventService(final CaseTypeService caseTypeService,
}

public CaseDetails saveAuditEventForCaseDetails(final Event event,
final CaseEventDefinition caseEventDefinition,
final CaseDetails caseDetails,
final CaseTypeDefinition caseTypeDefinition,
final CaseDetails caseDetailsBefore
final CaseEventDefinition caseEventDefinition,
final CaseDetails caseDetails,
final CaseTypeDefinition caseTypeDefinition,
final CaseDetails caseDetailsBefore
) {

CaseStateDefinition caseStateDefinition =
Expand All @@ -57,7 +59,15 @@ public CaseDetails saveAuditEventForCaseDetails(final Event event,
.eventDetails(eventDetails.build())
.build();

final CaseDetails savedPocCaseDetails = pocApiClient.createCase(pocCaseDetails);
try {
return pocApiClient.createCase(pocCaseDetails);
} catch (FeignException.Conflict conflict) {
throw new CaseConcurrencyException("""
Unfortunately we were unable to save your work to the case as \
another action happened at the same time.
Please review the case and try again.""");

}

//TODO need to enable this feature
// messageService.handleMessage(MessageContext.builder()
Expand All @@ -67,7 +77,5 @@ public CaseDetails saveAuditEventForCaseDetails(final Event event,
// .oldState(caseDetailsBefore.getState())
// .build());

log.info("pocCaseDetails: {}", savedPocCaseDetails);
return savedPocCaseDetails;
}
}

0 comments on commit 857a510

Please sign in to comment.