Skip to content

Commit e2ef520

Browse files
Added publish check endpoint
1 parent 5736ff1 commit e2ef520

File tree

4 files changed

+62
-9
lines changed

4 files changed

+62
-9
lines changed

builder-api/src/main/java/org/acme/controller/EligibilityCheckResource.java

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ public Response createCustomCheck(@Context SecurityIdentity identity,
216216
newCheck.setOwnerId(userId);
217217
newCheck.setPublic(false);
218218
newCheck.setVersion(1);
219-
newCheck.setPublished(false);
220219
try {
221220
eligibilityCheckRepository.saveWorkingCustomCheck(newCheck);
222221
return Response.ok(newCheck, MediaType.APPLICATION_JSON).build();
@@ -231,9 +230,12 @@ public Response createCustomCheck(@Context SecurityIdentity identity,
231230
@Path("/custom-checks")
232231
public Response updateCustomCheck(@Context SecurityIdentity identity,
233232
EligibilityCheck updateCheck){
233+
// Authorization
234234
String userId = AuthUtils.getUserId(identity);
235+
if (!userId.equals(updateCheck.getOwnerId())){
236+
return Response.status(Response.Status.UNAUTHORIZED).build();
237+
}
235238

236-
// TODO: Add authorization to update check
237239
try {
238240
eligibilityCheckRepository.updateWorkingCustomCheck(updateCheck);
239241
return Response.ok().entity(updateCheck).build();
@@ -243,4 +245,53 @@ public Response updateCustomCheck(@Context SecurityIdentity identity,
243245
.build();
244246
}
245247
}
248+
249+
@POST
250+
@Path("/publish-check/{checkId}")
251+
public Response publishCustomCheck(@Context SecurityIdentity identity, @PathParam("checkId") String checkId){
252+
253+
String userId = AuthUtils.getUserId(identity);
254+
Optional<EligibilityCheck> checkOpt = eligibilityCheckRepository.getWorkingCustomCheck(userId, checkId);
255+
if (checkOpt.isEmpty()){
256+
return Response.status(Response.Status.NOT_FOUND).build();
257+
}
258+
259+
EligibilityCheck check = checkOpt.get();
260+
261+
// Authorization
262+
if (!userId.equals(check.getOwnerId())){
263+
return Response.status(Response.Status.UNAUTHORIZED).build();
264+
}
265+
266+
// Update workingCheck so that the incremented version number is saved
267+
check.setVersion(check.getVersion() + 1);
268+
try {
269+
eligibilityCheckRepository.updateWorkingCustomCheck(check);
270+
} catch (Exception e){
271+
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
272+
.entity(Map.of("error", "could not update working Check, published check version was not created"))
273+
.build();
274+
}
275+
276+
// Create new published custom check
277+
check.setId(check.getPublishedId());
278+
try {
279+
// save published check meta data document
280+
eligibilityCheckRepository.savePublishedCustomCheck(check);
281+
282+
// save published check DMN to storage
283+
Optional<String> workingDmnOpt = storageService.getStringFromStorage(storageService.getCheckDmnModelPath(userId, check.getWorkingId()));
284+
if (workingDmnOpt.isPresent()){
285+
String workingDmn = workingDmnOpt.get();
286+
storageService.writeStringToStorage(storageService.getCheckDmnModelPath(userId, check.getPublishedId()), workingDmn, "application/xml");
287+
288+
}
289+
} catch (Exception e){
290+
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
291+
.entity(Map.of("error", "could not create new published custom check version"))
292+
.build();
293+
}
294+
295+
return Response.ok(check, MediaType.APPLICATION_JSON).build();
296+
}
246297
}

builder-api/src/main/java/org/acme/model/domain/EligibilityCheck.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,23 @@ public class EligibilityCheck {
2020
private String ownerId;
2121
@JsonProperty("isPublic")
2222
private Boolean isPublic;
23-
private Boolean isPublished;
2423

2524
public String getWorkingId() {
2625
return CheckStatus.WORKING.getCode() + "-" + ownerId + "-" + module + "-" + name;
2726
}
2827

2928
public String getPublishedId() {
30-
return CheckStatus.PUBLISHED.getCode() + "-" + ownerId + "-" + module + "-" + name;
29+
return CheckStatus.PUBLISHED.getCode() + "-" + ownerId + "-" + module + "-" + name + "-" + version.toString();
3130
}
3231

3332
public String getId() {
3433
return this.id;
3534
}
3635

36+
public void setId(String id) {
37+
this.id = id;
38+
}
39+
3740
public String getName() {
3841
return name;
3942
}
@@ -114,7 +117,5 @@ public void setPublic(Boolean aPublic) {
114117
isPublic = aPublic;
115118
}
116119

117-
public void setPublished(Boolean published) {
118-
isPublished = published;
119-
}
120+
120121
}

builder-api/src/main/java/org/acme/persistence/GoogleStorageService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class GoogleStorageService implements StorageService {
2626
String bucketName;
2727

2828
@Override
29-
public void writeStringToStorage(String filePath, String content, String contentType){
29+
public void writeStringToStorage(String filePath, String content, String contentType) throws Exception {
3030
try {
3131
BlobId blobId = BlobId.of(bucketName, filePath);
3232
BlobInfo blobInfo = BlobInfo.newBuilder(blobId)
@@ -37,6 +37,7 @@ public void writeStringToStorage(String filePath, String content, String content
3737
Log.info("Uploaded to GCS: " + filePath);
3838
} catch (Exception e){
3939
Log.error("Error writing string to GCS: " + e.getMessage());
40+
throw new Exception(e);
4041
}
4142
}
4243

builder-api/src/main/java/org/acme/persistence/StorageService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.util.Optional;
88

99
public interface StorageService {
10-
void writeStringToStorage(String filePath, String content, String contentType);
10+
void writeStringToStorage(String filePath, String content, String contentType) throws Exception;
1111

1212
void writeBytesToStorage(String filePath, byte[] content, String contentType);
1313

0 commit comments

Comments
 (0)