@@ -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}
0 commit comments