diff --git a/builder-api/src/main/java/org/acme/controller/EligibilityCheckResource.java b/builder-api/src/main/java/org/acme/controller/EligibilityCheckResource.java index 732f1824..4adaf2ca 100644 --- a/builder-api/src/main/java/org/acme/controller/EligibilityCheckResource.java +++ b/builder-api/src/main/java/org/acme/controller/EligibilityCheckResource.java @@ -14,7 +14,6 @@ import org.acme.persistence.EligibilityCheckRepository; import org.acme.persistence.StorageService; -import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Optional; @@ -216,9 +215,8 @@ public Response createCustomCheck(@Context SecurityIdentity identity, newCheck.setOwnerId(userId); newCheck.setPublic(false); newCheck.setVersion(1); - newCheck.setPublished(false); try { - eligibilityCheckRepository.saveWorkingCustomCheck(newCheck); + eligibilityCheckRepository.saveNewWorkingCustomCheck(newCheck); return Response.ok(newCheck, MediaType.APPLICATION_JSON).build(); } catch (Exception e){ return Response.status(Response.Status.INTERNAL_SERVER_ERROR) @@ -231,9 +229,12 @@ public Response createCustomCheck(@Context SecurityIdentity identity, @Path("/custom-checks") public Response updateCustomCheck(@Context SecurityIdentity identity, EligibilityCheck updateCheck){ + // Authorization String userId = AuthUtils.getUserId(identity); + if (!userId.equals(updateCheck.getOwnerId())){ + return Response.status(Response.Status.UNAUTHORIZED).build(); + } - // TODO: Add authorization to update check try { eligibilityCheckRepository.updateWorkingCustomCheck(updateCheck); return Response.ok().entity(updateCheck).build(); @@ -243,4 +244,51 @@ public Response updateCustomCheck(@Context SecurityIdentity identity, .build(); } } + + @POST + @Path("/publish-check/{checkId}") + public Response publishCustomCheck(@Context SecurityIdentity identity, @PathParam("checkId") String checkId){ + + String userId = AuthUtils.getUserId(identity); + Optional checkOpt = eligibilityCheckRepository.getWorkingCustomCheck(userId, checkId); + if (checkOpt.isEmpty()){ + return Response.status(Response.Status.NOT_FOUND).build(); + } + + EligibilityCheck check = checkOpt.get(); + + // Authorization + if (!userId.equals(check.getOwnerId())){ + return Response.status(Response.Status.UNAUTHORIZED).build(); + } + + // Update workingCheck so that the incremented version number is saved + check.setVersion(check.getVersion() + 1); + try { + eligibilityCheckRepository.updateWorkingCustomCheck(check); + } catch (Exception e){ + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(Map.of("error", "could not update working Check, published check version was not created")) + .build(); + } + + // Create new published custom check + try { + // save published check meta data document + String publishedCheckId = eligibilityCheckRepository.saveNewPublishedCustomCheck(check); + + // save published check DMN to storage + Optional workingDmnOpt = storageService.getStringFromStorage(storageService.getCheckDmnModelPath(userId, check.getId())); + if (workingDmnOpt.isPresent()){ + String workingDmn = workingDmnOpt.get(); + storageService.writeStringToStorage(storageService.getCheckDmnModelPath(userId, publishedCheckId), workingDmn, "application/xml"); + } + } catch (Exception e){ + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(Map.of("error", "could not create new published custom check version")) + .build(); + } + + return Response.ok(check, MediaType.APPLICATION_JSON).build(); + } } diff --git a/builder-api/src/main/java/org/acme/model/domain/EligibilityCheck.java b/builder-api/src/main/java/org/acme/model/domain/EligibilityCheck.java index 97ba0fa2..f4ef0625 100644 --- a/builder-api/src/main/java/org/acme/model/domain/EligibilityCheck.java +++ b/builder-api/src/main/java/org/acme/model/domain/EligibilityCheck.java @@ -20,20 +20,15 @@ public class EligibilityCheck { private String ownerId; @JsonProperty("isPublic") private Boolean isPublic; - private Boolean isPublished; - - public String getWorkingId() { - return CheckStatus.WORKING.getCode() + "-" + ownerId + "-" + module + "-" + name; - } - - public String getPublishedId() { - return CheckStatus.PUBLISHED.getCode() + "-" + ownerId + "-" + module + "-" + name; - } public String getId() { return this.id; } + public void setId(String id) { + this.id = id; + } + public String getName() { return name; } @@ -114,7 +109,5 @@ public void setPublic(Boolean aPublic) { isPublic = aPublic; } - public void setPublished(Boolean published) { - isPublished = published; - } + } diff --git a/builder-api/src/main/java/org/acme/persistence/EligibilityCheckRepository.java b/builder-api/src/main/java/org/acme/persistence/EligibilityCheckRepository.java index 381995df..bf322e5b 100644 --- a/builder-api/src/main/java/org/acme/persistence/EligibilityCheckRepository.java +++ b/builder-api/src/main/java/org/acme/persistence/EligibilityCheckRepository.java @@ -22,9 +22,9 @@ public interface EligibilityCheckRepository { Optional getPublishedCustomCheck(String userId, String checkId); - String saveWorkingCustomCheck(EligibilityCheck check) throws Exception; + String saveNewWorkingCustomCheck(EligibilityCheck check) throws Exception; - String savePublishedCustomCheck(EligibilityCheck check) throws Exception; + String saveNewPublishedCustomCheck(EligibilityCheck check) throws Exception; void updateWorkingCustomCheck(EligibilityCheck check) throws Exception; diff --git a/builder-api/src/main/java/org/acme/persistence/GoogleStorageService.java b/builder-api/src/main/java/org/acme/persistence/GoogleStorageService.java index dbe93ee1..f0046019 100644 --- a/builder-api/src/main/java/org/acme/persistence/GoogleStorageService.java +++ b/builder-api/src/main/java/org/acme/persistence/GoogleStorageService.java @@ -26,7 +26,7 @@ public class GoogleStorageService implements StorageService { String bucketName; @Override - public void writeStringToStorage(String filePath, String content, String contentType){ + public void writeStringToStorage(String filePath, String content, String contentType) throws Exception { try { BlobId blobId = BlobId.of(bucketName, filePath); BlobInfo blobInfo = BlobInfo.newBuilder(blobId) @@ -37,6 +37,7 @@ public void writeStringToStorage(String filePath, String content, String content Log.info("Uploaded to GCS: " + filePath); } catch (Exception e){ Log.error("Error writing string to GCS: " + e.getMessage()); + throw new Exception(e); } } diff --git a/builder-api/src/main/java/org/acme/persistence/StorageService.java b/builder-api/src/main/java/org/acme/persistence/StorageService.java index 7019b3d7..2f839d42 100644 --- a/builder-api/src/main/java/org/acme/persistence/StorageService.java +++ b/builder-api/src/main/java/org/acme/persistence/StorageService.java @@ -7,7 +7,7 @@ import java.util.Optional; public interface StorageService { - void writeStringToStorage(String filePath, String content, String contentType); + void writeStringToStorage(String filePath, String content, String contentType) throws Exception; void writeBytesToStorage(String filePath, byte[] content, String contentType); diff --git a/builder-api/src/main/java/org/acme/persistence/impl/EligibilityCheckRepositoryImpl.java b/builder-api/src/main/java/org/acme/persistence/impl/EligibilityCheckRepositoryImpl.java index c413c5cd..cfadf023 100644 --- a/builder-api/src/main/java/org/acme/persistence/impl/EligibilityCheckRepositoryImpl.java +++ b/builder-api/src/main/java/org/acme/persistence/impl/EligibilityCheckRepositoryImpl.java @@ -6,6 +6,7 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; +import org.acme.constants.CheckStatus; import org.acme.constants.CollectionNames; import org.acme.constants.FieldNames; import org.acme.model.domain.Benefit; @@ -107,24 +108,25 @@ private Optional getCustomCheck(String userId, String checkId, return Optional.of(check); } - public String saveWorkingCustomCheck(EligibilityCheck check) throws Exception{ + public String saveNewWorkingCustomCheck(EligibilityCheck check) throws Exception{ + String checkId = getWorkingId(check); + check.setId(checkId); ObjectMapper mapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL); Map data = mapper.convertValue(check, Map.class); - String checkDocId = check.getWorkingId(); - return FirestoreUtils.persistDocumentWithId(CollectionNames.WORKING_CUSTOM_CHECK_COLLECTION, checkDocId, data); + return FirestoreUtils.persistDocumentWithId(CollectionNames.WORKING_CUSTOM_CHECK_COLLECTION, checkId, data); } public void updateWorkingCustomCheck(EligibilityCheck check) throws Exception{ ObjectMapper mapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL); Map data = mapper.convertValue(check, Map.class); - String checkDocId = check.getWorkingId(); - FirestoreUtils.updateDocument(CollectionNames.WORKING_CUSTOM_CHECK_COLLECTION, data, checkDocId); + FirestoreUtils.updateDocument(CollectionNames.WORKING_CUSTOM_CHECK_COLLECTION, data, check.getId()); } - public String savePublishedCustomCheck(EligibilityCheck check) throws Exception{ + public String saveNewPublishedCustomCheck(EligibilityCheck check) throws Exception{ + check.setId(getPublishedId(check)); ObjectMapper mapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL); Map data = mapper.convertValue(check, Map.class); - String checkDocId = check.getPublishedId(); + String checkDocId = getPublishedId(check); return FirestoreUtils.persistDocumentWithId(CollectionNames.PUBLISHED_CUSTOM_CHECK_COLLECTION, checkDocId, data); } @@ -132,14 +134,22 @@ public String savePublishedCustomCheck(EligibilityCheck check) throws Exception{ public void updatePublishedCustomCheck(EligibilityCheck check) throws Exception{ ObjectMapper mapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL); Map data = mapper.convertValue(check, Map.class); - String checkDocId = check.getPublishedId(); - FirestoreUtils.updateDocument(CollectionNames.PUBLISHED_CUSTOM_CHECK_COLLECTION, data, checkDocId); + FirestoreUtils.updateDocument(CollectionNames.PUBLISHED_CUSTOM_CHECK_COLLECTION, data, check.getId()); } public String savePublicCheck(EligibilityCheck check) throws Exception{ + String checkId = getPublishedId(check); + check.setId(checkId); ObjectMapper mapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL); Map data = mapper.convertValue(check, Map.class); - String checkDocId = check.getPublishedId(); - return FirestoreUtils.persistDocumentWithId(CollectionNames.PUBLIC_CHECK_COLLECTION, checkDocId, data); + return FirestoreUtils.persistDocumentWithId(CollectionNames.PUBLIC_CHECK_COLLECTION, checkId , data); + } + + public String getWorkingId(EligibilityCheck check) { + return CheckStatus.WORKING.getCode() + "-" + check.getOwnerId() + "-" + check.getModule() + "-" + check.getName(); + } + + public String getPublishedId(EligibilityCheck check) { + return CheckStatus.PUBLISHED.getCode() + "-" + check.getOwnerId() + "-" + check.getModule() + "-" + check.getName() + "-" + check.getVersion().toString(); } }