From 752c474cc4020139a161a3d1f460726370caf9b7 Mon Sep 17 00:00:00 2001 From: epuzanov Date: Tue, 21 May 2024 15:47:43 +0200 Subject: [PATCH] Add the @isApproved RestApiMapping --- .../substance/approval/ApprovalService.java | 2 ++ .../approval/DefaultApprovalService.java | 20 +++++++++++++++++++ .../controllers/SubstanceController.java | 11 ++++++++++ 3 files changed, 33 insertions(+) diff --git a/gsrs-module-substances-core/src/main/java/gsrs/module/substance/approval/ApprovalService.java b/gsrs-module-substances-core/src/main/java/gsrs/module/substance/approval/ApprovalService.java index 8dc6df367..8b78056a4 100644 --- a/gsrs-module-substances-core/src/main/java/gsrs/module/substance/approval/ApprovalService.java +++ b/gsrs-module-substances-core/src/main/java/gsrs/module/substance/approval/ApprovalService.java @@ -9,6 +9,8 @@ public interface ApprovalService { + boolean isApprovable(Substance s); + ApprovalResult approve(Substance s) throws ApprovalException; @Data diff --git a/gsrs-module-substances-core/src/main/java/gsrs/module/substance/approval/DefaultApprovalService.java b/gsrs-module-substances-core/src/main/java/gsrs/module/substance/approval/DefaultApprovalService.java index 9ecfdaa83..08a0cbdc9 100644 --- a/gsrs-module-substances-core/src/main/java/gsrs/module/substance/approval/DefaultApprovalService.java +++ b/gsrs-module-substances-core/src/main/java/gsrs/module/substance/approval/DefaultApprovalService.java @@ -124,6 +124,26 @@ protected void defaultApprovalValidation(Substance s, String usernameOfApprover) } } + /** + * Check if the given Substance is approvable by the user invoking this method. + * @param s the Substance to approve. + * @return a true if the user invoking this method is able to approve given Substance. + */ + @Override + public boolean isApprovable(Substance s) { + String userName = GsrsSecurityUtils.getCurrentUsername().orElse(null); + if (userName == null) { + return false; + } + try { + defaultApprovalValidation(s, userName); + extraApprovalValidation(s, userName); + return true; + } catch (ApprovalException ex) { + return false; + } + } + /** * Try to approve the given Substance. The user invoking this method * must have Approver Role. diff --git a/gsrs-module-substances-core/src/main/java/gsrs/module/substance/controllers/SubstanceController.java b/gsrs-module-substances-core/src/main/java/gsrs/module/substance/controllers/SubstanceController.java index 1df712628..282cf9f68 100644 --- a/gsrs-module-substances-core/src/main/java/gsrs/module/substance/controllers/SubstanceController.java +++ b/gsrs-module-substances-core/src/main/java/gsrs/module/substance/controllers/SubstanceController.java @@ -1036,6 +1036,17 @@ public void saveTempStructure(Structure s) { } + @GetGsrsRestApiMapping(value={"({id})/@isApprovable", "/{id}/@isApprovable" }) + public ResponseEntity isApprovableGetMethod(@PathVariable("id") String substanceUUIDOrName, @RequestParam Map queryParameters) throws Exception { + Optional substance = getEntityService().getEntityBySomeIdentifier(substanceUUIDOrName); + + if(!substance.isPresent()){ + return getGsrsControllerConfiguration().handleNotFound(queryParameters); + } + boolean approvable = approvalService.isApprovable(substance.get()); + return new ResponseEntity<>(String.valueOf(approvable), HttpStatus.OK); + } + @Transactional @GetGsrsRestApiMapping(value={"({id})/@approve", "/{id}/@approve" }) @hasApproverRole