Skip to content

Commit

Permalink
More migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Portals committed Jun 8, 2024
1 parent e8f2c81 commit 9b49343
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 8 deletions.
1 change: 0 additions & 1 deletion app/src/main/java/it/chalmers/gamma/GammaApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

@SpringBootApplication
@ConfigurationPropertiesScan
@EnableAsync
public class GammaApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package it.chalmers.gamma.adapter.primary.web;

import it.chalmers.gamma.app.client.domain.ClientUid;
import it.chalmers.gamma.app.migration.ClientApproving;
import it.chalmers.gamma.app.user.domain.UserId;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;

/**
* This is only used as a part of migration from old gamma to new gamma.
* We want to transfer the accepting of a client for certain project,
* such as HubbIT and BookIT.
*/
@Controller
public class ClientApprovingController {

private final ClientApproving clientApproving;

public ClientApprovingController(ClientApproving clientApproving) {
this.clientApproving = clientApproving;
}

public record ClientApprovingForm(String clientUid, String userIds) { }

@GetMapping("/client-approving")
public ModelAndView getClientApproving(@RequestHeader(value = "HX-Request", required = false) boolean htmxRequest) {
ModelAndView mv = new ModelAndView();
if (htmxRequest) {
mv.setViewName("pages/client-approving");
} else {
mv.setViewName("index");
mv.addObject("page", "pages/client-approving");
}

mv.addObject("form", new ClientApprovingForm("", ""));

return mv;
}

@PostMapping("/client-approving")
public ModelAndView massApproveClient(ClientApprovingForm form) {
List<UserId> userIds = Arrays.stream(form.userIds.split(",")).map(UserId::valueOf).toList();

clientApproving.approve(ClientUid.valueOf(form.clientUid), userIds);

return new ModelAndView("redirect:/clients/" + form.clientUid);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package it.chalmers.gamma.app.migration;

import it.chalmers.gamma.app.authentication.AccessGuard;
import it.chalmers.gamma.app.client.domain.ClientRepository;
import it.chalmers.gamma.app.client.domain.ClientUid;
import it.chalmers.gamma.app.user.domain.UserId;
import org.springframework.stereotype.Service;

import java.util.List;

import static it.chalmers.gamma.app.authentication.AccessGuard.isAdmin;

@Service
public class ClientApproving {

private final AccessGuard accessGuard;
private final ClientRepository clientRepository;

public ClientApproving(AccessGuard accessGuard, ClientRepository clientRepository) {
this.accessGuard = accessGuard;
this.clientRepository = clientRepository;
}

public void approve(ClientUid clientUid, List<UserId> userIds) {
accessGuard.require(isAdmin());

for (UserId userId : userIds) {
try {
clientRepository.addClientApproval(userId, clientUid);
} catch(Exception e) {
//ignore...
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package it.chalmers.gamma.app.migration;

import it.chalmers.gamma.app.authentication.AccessGuard;
import it.chalmers.gamma.app.common.Email;
import it.chalmers.gamma.app.common.PrettyName;
import it.chalmers.gamma.app.common.Text;
Expand All @@ -13,9 +14,10 @@
import java.util.Optional;
import java.util.UUID;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import static it.chalmers.gamma.app.authentication.AccessGuard.isAdmin;

@Service
public class GammaMigration {

Expand All @@ -24,18 +26,20 @@ public class GammaMigration {
private final GroupRepository groupRepository;
private final UserRepository userRepository;
private final PostRepository postRepository;
private final AccessGuard accessGuard;

public GammaMigration(
SuperGroupTypeRepository superGroupTypeRepository,
SuperGroupRepository superGroupRepository,
GroupRepository groupRepository,
UserRepository userRepository,
PostRepository postRepository) {
SuperGroupTypeRepository superGroupTypeRepository,
SuperGroupRepository superGroupRepository,
GroupRepository groupRepository,
UserRepository userRepository,
PostRepository postRepository, AccessGuard accessGuard) {
this.superGroupTypeRepository = superGroupTypeRepository;
this.superGroupRepository = superGroupRepository;
this.groupRepository = groupRepository;
this.userRepository = userRepository;
this.postRepository = postRepository;
this.accessGuard = accessGuard;
}

public enum OldLanguage {
Expand Down Expand Up @@ -108,8 +112,9 @@ public record OldData(
List<OldSuperGroup> superGroups,
List<OldPost> posts) {}

@Async
public void migrate(String apiKey) {
accessGuard.require(isAdmin());

var gammaData = getDataFromGamma(apiKey);

// We got everything we need, let's delete E V E R Y T H I N G
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/resources/templates/pages/client-approving.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<header th:replace="~{common/header-without-nav}"></header>
<main>
<article>
<header>
Used to mass approve users to a client
</header>
<p>
Use with care, just use this when you know that the user already has accepted the client before.
</p>
<p>
Used for migration purposes.
</p>
<form id="client-approving" th:object="${form}" th:method="post" th:action="@{/client-approving}">
<div th:replace="~{common/input :: textInput2(field='clientUid', label='Client UID')}"></div>
<div th:replace="~{common/input :: textInput2(field='userIds', label='User Ids (comma seperated)')}"></div>
</form>
<footer>
<button form="client-approving">
Mass approve
</button>
</footer>
</article>
</main>

0 comments on commit 9b49343

Please sign in to comment.