Skip to content

Commit 0b0369f

Browse files
Merge pull request #202 from CodeForPhilly/publish_check
Added publish check endpoint
2 parents 50ce44d + 0abc692 commit 0b0369f

File tree

6 files changed

+83
-31
lines changed

6 files changed

+83
-31
lines changed

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

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.acme.persistence.EligibilityCheckRepository;
1515
import org.acme.persistence.StorageService;
1616

17-
import java.util.ArrayList;
1817
import java.util.List;
1918
import java.util.Map;
2019
import java.util.Optional;
@@ -216,9 +215,8 @@ public Response createCustomCheck(@Context SecurityIdentity identity,
216215
newCheck.setOwnerId(userId);
217216
newCheck.setPublic(false);
218217
newCheck.setVersion(1);
219-
newCheck.setPublished(false);
220218
try {
221-
eligibilityCheckRepository.saveWorkingCustomCheck(newCheck);
219+
eligibilityCheckRepository.saveNewWorkingCustomCheck(newCheck);
222220
return Response.ok(newCheck, MediaType.APPLICATION_JSON).build();
223221
} catch (Exception e){
224222
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
@@ -231,9 +229,12 @@ public Response createCustomCheck(@Context SecurityIdentity identity,
231229
@Path("/custom-checks")
232230
public Response updateCustomCheck(@Context SecurityIdentity identity,
233231
EligibilityCheck updateCheck){
232+
// Authorization
234233
String userId = AuthUtils.getUserId(identity);
234+
if (!userId.equals(updateCheck.getOwnerId())){
235+
return Response.status(Response.Status.UNAUTHORIZED).build();
236+
}
235237

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

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,15 @@ public class EligibilityCheck {
2020
private String ownerId;
2121
@JsonProperty("isPublic")
2222
private Boolean isPublic;
23-
private Boolean isPublished;
24-
25-
public String getWorkingId() {
26-
return CheckStatus.WORKING.getCode() + "-" + ownerId + "-" + module + "-" + name;
27-
}
28-
29-
public String getPublishedId() {
30-
return CheckStatus.PUBLISHED.getCode() + "-" + ownerId + "-" + module + "-" + name;
31-
}
3223

3324
public String getId() {
3425
return this.id;
3526
}
3627

28+
public void setId(String id) {
29+
this.id = id;
30+
}
31+
3732
public String getName() {
3833
return name;
3934
}
@@ -114,7 +109,5 @@ public void setPublic(Boolean aPublic) {
114109
isPublic = aPublic;
115110
}
116111

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public interface EligibilityCheckRepository {
2222

2323
Optional<EligibilityCheck> getPublishedCustomCheck(String userId, String checkId);
2424

25-
String saveWorkingCustomCheck(EligibilityCheck check) throws Exception;
25+
String saveNewWorkingCustomCheck(EligibilityCheck check) throws Exception;
2626

27-
String savePublishedCustomCheck(EligibilityCheck check) throws Exception;
27+
String saveNewPublishedCustomCheck(EligibilityCheck check) throws Exception;
2828

2929
void updateWorkingCustomCheck(EligibilityCheck check) throws Exception;
3030

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

builder-api/src/main/java/org/acme/persistence/impl/EligibilityCheckRepositoryImpl.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import jakarta.enterprise.context.ApplicationScoped;
77
import jakarta.inject.Inject;
88

9+
import org.acme.constants.CheckStatus;
910
import org.acme.constants.CollectionNames;
1011
import org.acme.constants.FieldNames;
1112
import org.acme.model.domain.Benefit;
@@ -107,39 +108,48 @@ private Optional<EligibilityCheck> getCustomCheck(String userId, String checkId,
107108
return Optional.of(check);
108109
}
109110

110-
public String saveWorkingCustomCheck(EligibilityCheck check) throws Exception{
111+
public String saveNewWorkingCustomCheck(EligibilityCheck check) throws Exception{
112+
String checkId = getWorkingId(check);
113+
check.setId(checkId);
111114
ObjectMapper mapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
112115
Map<String, Object> data = mapper.convertValue(check, Map.class);
113-
String checkDocId = check.getWorkingId();
114-
return FirestoreUtils.persistDocumentWithId(CollectionNames.WORKING_CUSTOM_CHECK_COLLECTION, checkDocId, data);
116+
return FirestoreUtils.persistDocumentWithId(CollectionNames.WORKING_CUSTOM_CHECK_COLLECTION, checkId, data);
115117
}
116118

117119
public void updateWorkingCustomCheck(EligibilityCheck check) throws Exception{
118120
ObjectMapper mapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
119121
Map<String, Object> data = mapper.convertValue(check, Map.class);
120-
String checkDocId = check.getWorkingId();
121-
FirestoreUtils.updateDocument(CollectionNames.WORKING_CUSTOM_CHECK_COLLECTION, data, checkDocId);
122+
FirestoreUtils.updateDocument(CollectionNames.WORKING_CUSTOM_CHECK_COLLECTION, data, check.getId());
122123
}
123124

124-
public String savePublishedCustomCheck(EligibilityCheck check) throws Exception{
125+
public String saveNewPublishedCustomCheck(EligibilityCheck check) throws Exception{
126+
check.setId(getPublishedId(check));
125127
ObjectMapper mapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
126128
Map<String, Object> data = mapper.convertValue(check, Map.class);
127-
String checkDocId = check.getPublishedId();
129+
String checkDocId = getPublishedId(check);
128130
return FirestoreUtils.persistDocumentWithId(CollectionNames.PUBLISHED_CUSTOM_CHECK_COLLECTION, checkDocId, data);
129131
}
130132

131133

132134
public void updatePublishedCustomCheck(EligibilityCheck check) throws Exception{
133135
ObjectMapper mapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
134136
Map<String, Object> data = mapper.convertValue(check, Map.class);
135-
String checkDocId = check.getPublishedId();
136-
FirestoreUtils.updateDocument(CollectionNames.PUBLISHED_CUSTOM_CHECK_COLLECTION, data, checkDocId);
137+
FirestoreUtils.updateDocument(CollectionNames.PUBLISHED_CUSTOM_CHECK_COLLECTION, data, check.getId());
137138
}
138139

139140
public String savePublicCheck(EligibilityCheck check) throws Exception{
141+
String checkId = getPublishedId(check);
142+
check.setId(checkId);
140143
ObjectMapper mapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
141144
Map<String, Object> data = mapper.convertValue(check, Map.class);
142-
String checkDocId = check.getPublishedId();
143-
return FirestoreUtils.persistDocumentWithId(CollectionNames.PUBLIC_CHECK_COLLECTION, checkDocId, data);
145+
return FirestoreUtils.persistDocumentWithId(CollectionNames.PUBLIC_CHECK_COLLECTION, checkId , data);
146+
}
147+
148+
public String getWorkingId(EligibilityCheck check) {
149+
return CheckStatus.WORKING.getCode() + "-" + check.getOwnerId() + "-" + check.getModule() + "-" + check.getName();
150+
}
151+
152+
public String getPublishedId(EligibilityCheck check) {
153+
return CheckStatus.PUBLISHED.getCode() + "-" + check.getOwnerId() + "-" + check.getModule() + "-" + check.getName() + "-" + check.getVersion().toString();
144154
}
145155
}

0 commit comments

Comments
 (0)