Skip to content

Commit

Permalink
avniproject#788 | Added metabaseSetupEnable for settings in Organisat…
Browse files Browse the repository at this point in the history
…ion config table and configured the code to work with frontend
  • Loading branch information
ombhardwajj committed Sep 15, 2024
1 parent ec064aa commit 81cc9f5
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,14 @@ public static String getExtensionFilePath(String fileName) {
return String.format("%s/%s", EXTENSION_DIR, fileName);
}
}

public boolean isMetabaseSetupEnabled() {
Boolean setupEnabled = (Boolean) settings.get("metabaseSetupEnabled");
return setupEnabled != null && setupEnabled;
}

public void setMetabaseSetupEnabled(boolean setupEnabled) {
settings.put("metabaseSetupEnabled", setupEnabled);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,16 @@ private JsonObject getMetadataConfigSettingsAfterReset() {
settings.put(OrganisationConfig.Extension.EXTENSION_DIR, Collections.emptyList());
return settings;
}

public void setMetabaseSetupEnabled(Organisation organisation, boolean isEnabled) {
OrganisationConfig config = organisationConfigRepository.findByOrganisationId(organisation.getId());
config.setMetabaseSetupEnabled(isEnabled);
organisationConfigRepository.save(config);
}

public boolean isMetabaseSetupEnabled(Organisation organisation) {
OrganisationConfig config = organisationConfigRepository.findByOrganisationId(organisation.getId());
return config.isMetabaseSetupEnabled();
}

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
package org.avni.server.web;
import org.avni.server.domain.Organisation;
import org.avni.server.domain.metabase.SyncStatus;
import org.avni.server.framework.security.UserContextHolder;
import org.avni.server.service.metabase.DatabaseService;
import org.avni.server.domain.accessControl.PrivilegeType;
import org.avni.server.dao.metabase.MetabaseConnector;
import org.avni.server.dao.metabase.DatabaseRepository;
import org.avni.server.service.metabase.MetabaseService;
import org.avni.server.service.UserService;
import org.avni.server.service.metabase.MetabaseUserService;
import org.avni.server.service.OrganisationConfigService;
import org.avni.server.service.accessControl.AccessControlService;
import org.avni.server.web.response.metabase.CreateQuestionsResponse;
import org.avni.server.web.response.metabase.SetupStatusResponse;
import org.avni.server.web.response.metabase.SetupToggleResponse;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/metabase")
public class MetabaseController {
private final DatabaseService databaseService;
private final MetabaseService metabaseService;
private final MetabaseUserService userService;
private final AccessControlService accessControlService;
private final OrganisationConfigService organisationConfigService;

public MetabaseController(DatabaseService databaseService,MetabaseService metabaseService, MetabaseConnector metabaseConnector,DatabaseRepository databaseRepository, UserService userService,AccessControlService accessControlService) {
public MetabaseController(DatabaseService databaseService,MetabaseService metabaseService, MetabaseUserService userService , MetabaseConnector metabaseConnector,DatabaseRepository databaseRepository,AccessControlService accessControlService ,OrganisationConfigService organisationConfigService) {
this.databaseService = databaseService;
this.metabaseService = metabaseService;
this.userService = userService;
this.accessControlService= accessControlService;
this.organisationConfigService = organisationConfigService;
}

@PostMapping("/setup")
Expand All @@ -28,14 +38,47 @@ public void setupMetabase() {
metabaseService.setupMetabase();
}

@PostMapping("/create-questions")
public void createQuestions() throws Exception{
databaseService.createQuestions();
}

@GetMapping("/sync-status")
public SyncStatus getSyncStatus() {
return databaseService.getInitialSyncStatus();
}

@PostMapping("/setup-toggle")
public SetupToggleResponse toggleSetupMetabase(@RequestParam boolean enabled) {
Organisation organisation = UserContextHolder.getUserContext().getOrganisation();

organisationConfigService.setMetabaseSetupEnabled(organisation, enabled);

if (enabled) {
metabaseService.setupMetabase();
try {
databaseService.createQuestions();
return new SetupToggleResponse(true, "Metabase setup enabled and questions created successfully.");
} catch (RuntimeException e) {
return new SetupToggleResponse(true, "Metabase setup enabled, but questions could not be created. Database sync is incomplete. Please refresh tables after sync is complete.");
}
} else {
return new SetupToggleResponse(false, "Metabase setup disabled.");
}
}


@GetMapping("/setup-status")
public SetupStatusResponse getSetupStatus() {
Organisation organisation = UserContextHolder.getUserContext().getOrganisation();
boolean isEnabled = organisationConfigService.isMetabaseSetupEnabled(organisation);
return new SetupStatusResponse(isEnabled);
}

@PostMapping("/create-questions")
public CreateQuestionsResponse createQuestions() {
try {
databaseService.createQuestions();
return new CreateQuestionsResponse(true, "Questions created successfully.");
} catch (RuntimeException e) {
return new CreateQuestionsResponse(false, "Database sync is not complete. Cannot create questions.");
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.avni.server.web.response.metabase;

public class CreateQuestionsResponse {
private boolean success;
private String message;

public CreateQuestionsResponse(boolean success, String message) {
this.success = success;
this.message = message;
}

public boolean isSuccess() {
return success;
}

public void setSuccess(boolean success) {
this.success = success;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.avni.server.web.response.metabase;

public class SetupStatusResponse {
private boolean setupEnabled;

public SetupStatusResponse(boolean setupEnabled) {
this.setupEnabled = setupEnabled;
}

public boolean isSetupEnabled() {
return setupEnabled;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.avni.server.web.response.metabase;

public class SetupToggleResponse {
private boolean success;
private String message;

public SetupToggleResponse(boolean success, String message) {
this.success = success;
this.message = message;
}

public boolean isSuccess() {
return success;
}

public String getMessage() {
return message;
}
}

0 comments on commit 81cc9f5

Please sign in to comment.