From 6c5a334fe4b397aafaff4e52fb67820216eb5067 Mon Sep 17 00:00:00 2001 From: Joy A Date: Wed, 28 Aug 2024 18:46:28 +0530 Subject: [PATCH] avniproject/avni-client#1460 | Catchment not setup for user - error handling --- .../server/service/ScopeAwareService.java | 2 ++ .../org/avni/server/web/MediaController.java | 19 +++++++++++++---- .../org/avni/server/web/SyncController.java | 21 ++++++++++++------- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/avni-server-api/src/main/java/org/avni/server/service/ScopeAwareService.java b/avni-server-api/src/main/java/org/avni/server/service/ScopeAwareService.java index 99a3201ad..787141187 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/ScopeAwareService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/ScopeAwareService.java @@ -7,6 +7,7 @@ import org.avni.server.domain.SubjectType; import org.avni.server.domain.User; import org.avni.server.framework.ApplicationContextProvider; +import org.avni.server.web.validation.ValidationException; import org.joda.time.DateTime; import java.util.List; @@ -20,6 +21,7 @@ default boolean isChangedBySubjectTypeRegistrationLocationType(User user, DateTi } default boolean isChangedByCatchment(User user, DateTime lastModifiedDateTime, SyncEntityName syncEntityName) { + if (user.getCatchment() == null) throw new ValidationException("NoCatchmentFound"); return repository().isEntityChanged(new SyncParameters(lastModifiedDateTime, DateTime.now(), null, null, null, null, null, user.getSyncSettings(), syncEntityName, user.getCatchment())); } diff --git a/avni-server-api/src/main/java/org/avni/server/web/MediaController.java b/avni-server-api/src/main/java/org/avni/server/web/MediaController.java index 0bdc3e668..d1fca591f 100644 --- a/avni-server-api/src/main/java/org/avni/server/web/MediaController.java +++ b/avni-server-api/src/main/java/org/avni/server/web/MediaController.java @@ -27,7 +27,6 @@ import javax.servlet.http.HttpServletResponse; import java.awt.*; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URL; @@ -76,7 +75,11 @@ private ResponseEntity getFileUrlResponse(String fileName, HttpMethod me @PreAuthorize(value = "hasAnyAuthority('user')") public ResponseEntity generateMobileDatabaseBackupUploadUrl() { logger.info("getting mobile database backup upload url"); - return getFileUrlResponse(mobileDatabaseBackupFile(), HttpMethod.PUT); + try { + return getFileUrlResponse(mobileDatabaseBackupFile(), HttpMethod.PUT); + } catch (ValidationException e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); + } } private String mobileDatabaseBackupFile() { @@ -92,14 +95,22 @@ private String mobileDatabaseBackupFile() { @PreAuthorize(value = "hasAnyAuthority('user')") public ResponseEntity generateMobileDatabaseBackupDownloadUrl() { logger.info("getting mobile database backup download url"); - return getFileUrlResponse(mobileDatabaseBackupFile(), HttpMethod.GET); + try { + return getFileUrlResponse(mobileDatabaseBackupFile(), HttpMethod.GET); + } catch (ValidationException e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); + } } @RequestMapping(value = "/media/mobileDatabaseBackupUrl/exists", method = RequestMethod.GET) @PreAuthorize(value = "hasAnyAuthority('user')") public ResponseEntity mobileDatabaseBackupExists() { logger.info("checking whether mobile database backup url exists"); - return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(Boolean.toString(s3Service.fileExists(mobileDatabaseBackupFile()))); + try { + return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(Boolean.toString(s3Service.fileExists(mobileDatabaseBackupFile()))); + } catch (ValidationException e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); + } } @RequestMapping(value = "/media/signedUrl", method = RequestMethod.GET) diff --git a/avni-server-api/src/main/java/org/avni/server/web/SyncController.java b/avni-server-api/src/main/java/org/avni/server/web/SyncController.java index 5a16fb1c6..52535bded 100644 --- a/avni-server-api/src/main/java/org/avni/server/web/SyncController.java +++ b/avni-server-api/src/main/java/org/avni/server/web/SyncController.java @@ -8,11 +8,13 @@ import org.avni.server.service.accessControl.PrivilegeService; import org.avni.server.service.application.MenuItemService; import org.avni.server.web.request.EntitySyncStatusContract; +import org.avni.server.web.validation.ValidationException; import org.joda.time.DateTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.PostMapping; @@ -298,13 +300,18 @@ public ResponseEntity getSyncDetailsWithScopeAwareEAS(@RequestBody List allSyncableItems = syncDetailService.getAllSyncableItems(true, includeUserSubjectType); long afterSyncDetailsService = new DateTime().getMillis(); logger.info(String.format("Time taken for syncDetailsService %d", afterSyncDetailsService - now.getMillis())); - List changedEntities = getChangedEntities(entitySyncStatusContracts, allSyncableItems, true); - logger.info(String.format("Time taken for stuff %d", new DateTime().getMillis() - afterSyncDetailsService)); - return ResponseEntity.ok().body(new JsonObject() - .with("syncDetails", changedEntities) - .with("now", now) - .with("nowMinus10Seconds", nowMinus10Seconds) - ); + try { + List changedEntities = getChangedEntities(entitySyncStatusContracts, allSyncableItems, true); + logger.info(String.format("Time taken for stuff %d", new DateTime().getMillis() - afterSyncDetailsService)); + return ResponseEntity.ok().body(new JsonObject() + .with("syncDetails", changedEntities) + .with("now", now) + .with("nowMinus10Seconds", nowMinus10Seconds) + ); + } catch (ValidationException e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); + } + } /**