Skip to content

Commit

Permalink
avniproject/avni-client#1460 | Catchment not setup for user - error h…
Browse files Browse the repository at this point in the history
…andling
  • Loading branch information
1t5j0y committed Aug 28, 2024
1 parent 0256593 commit 6c5a334
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,7 +75,11 @@ private ResponseEntity<String> getFileUrlResponse(String fileName, HttpMethod me
@PreAuthorize(value = "hasAnyAuthority('user')")
public ResponseEntity<String> 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() {
Expand All @@ -92,14 +95,22 @@ private String mobileDatabaseBackupFile() {
@PreAuthorize(value = "hasAnyAuthority('user')")
public ResponseEntity<String> 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<String> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -298,13 +300,18 @@ public ResponseEntity<?> getSyncDetailsWithScopeAwareEAS(@RequestBody List<Entit
Set<SyncableItem> allSyncableItems = syncDetailService.getAllSyncableItems(true, includeUserSubjectType);
long afterSyncDetailsService = new DateTime().getMillis();
logger.info(String.format("Time taken for syncDetailsService %d", afterSyncDetailsService - now.getMillis()));
List<EntitySyncStatusContract> 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<EntitySyncStatusContract> 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());
}

}

/**
Expand Down

0 comments on commit 6c5a334

Please sign in to comment.