Skip to content

Commit 266ee30

Browse files
authored
Include groups in the Account Scaffold API (#924)
1 parent 711096c commit 266ee30

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

app/src/main/java/it/chalmers/gamma/app/accountscaffold/AccountScaffoldFacade.java

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import it.chalmers.gamma.app.apikey.domain.settings.ApiKeyAccountScaffoldSettings;
88
import it.chalmers.gamma.app.apikey.domain.settings.ApiKeySettingsRepository;
99
import it.chalmers.gamma.app.authentication.AccessGuard;
10+
import it.chalmers.gamma.app.group.domain.Group;
1011
import it.chalmers.gamma.app.group.domain.GroupMember;
1112
import it.chalmers.gamma.app.group.domain.GroupRepository;
1213
import it.chalmers.gamma.app.post.domain.Post;
@@ -39,14 +40,13 @@ public AccountScaffoldFacade(
3940
}
4041

4142
/**
42-
* Get all super groups that have the provided types and members that are a part of groups that
43-
* has each supergroup
43+
* Get all super groups that have the provided types and their "sub" groups with their members.
4444
*/
4545
public List<AccountScaffoldSuperGroupDTO> getActiveSuperGroups() {
4646
this.accessGuard.require(isApi(ApiKeyType.ACCOUNT_SCAFFOLD));
4747

4848
List<UserId> gdprTrained = this.gdprTrainedRepository.getAll();
49-
Map<SuperGroupId, SuperGroupWithMembers> superGroupMap = new HashMap<>();
49+
Map<SuperGroupId, SuperGroupWithGroups> superGroupMap = new HashMap<>();
5050

5151
ApiAuthentication apiAuthentication =
5252
(ApiAuthentication) AuthenticationExtractor.getAuthentication();
@@ -71,23 +71,23 @@ public List<AccountScaffoldSuperGroupDTO> getActiveSuperGroups() {
7171
if (!superGroupMap.containsKey(superGroupId)) {
7272
superGroupMap.put(
7373
superGroupId,
74-
new SuperGroupWithMembers(
75-
group.superGroup(), new HashSet<>(activeGroupMember)));
74+
new SuperGroupWithGroups(
75+
group.superGroup(), new ArrayList<>(List.of(new GroupWithMembers(group, new HashSet<>(activeGroupMember))))));
7676
} else {
77-
superGroupMap.get(superGroupId).members.addAll(activeGroupMember);
77+
superGroupMap.get(superGroupId).groups.add(new GroupWithMembers(group, new HashSet<>(activeGroupMember)));
7878
}
7979
});
8080

8181
return superGroupMap.values().stream()
8282
.map(
83-
superGroupWithMembers ->
83+
superGroupWithGroups ->
8484
new AccountScaffoldSuperGroupDTO(
85-
superGroupWithMembers.superGroup,
86-
new ArrayList<>(superGroupWithMembers.members),
85+
superGroupWithGroups.superGroup,
86+
superGroupWithGroups.groups.stream().map(group -> new AccountScaffoldGroupDTO(group.group, new ArrayList<>(group.members))).toList(),
8787
settings.superGroupTypes().stream()
8888
.anyMatch(
8989
row ->
90-
row.type().equals(superGroupWithMembers.superGroup.type())
90+
row.type().equals(superGroupWithGroups.superGroup.type())
9191
&& row.requiresManaged())))
9292
.toList();
9393
}
@@ -156,32 +156,62 @@ public AccountScaffoldUserDTO(GammaUser user) {
156156
}
157157
}
158158

159+
public record AccountScaffoldGroupDTO(
160+
String name,
161+
String prettyName,
162+
List<AccountScaffoldUserPostDTO> members
163+
) {
164+
public AccountScaffoldGroupDTO(
165+
Group group,
166+
List<AccountScaffoldUserPostDTO> members
167+
) {
168+
this(
169+
group.name().value(),
170+
group.prettyName().value(),
171+
members
172+
);
173+
}
174+
}
175+
159176
public record AccountScaffoldSuperGroupDTO(
160177
String name,
161178
String prettyName,
162179
String type,
163-
List<AccountScaffoldUserPostDTO> members,
180+
List<AccountScaffoldGroupDTO> groups,
164181
boolean useManagedAccount) {
165182
public AccountScaffoldSuperGroupDTO(
166183
SuperGroup superGroup,
167-
List<AccountScaffoldUserPostDTO> members,
184+
List<AccountScaffoldGroupDTO> groups,
168185
boolean useManagedAccount) {
169186
this(
170187
superGroup.name().value(),
171188
superGroup.prettyName().value(),
172189
superGroup.type().value(),
173-
members,
190+
groups,
174191
useManagedAccount);
175192
}
176193
}
177194

178-
private static class SuperGroupWithMembers {
179-
private final SuperGroup superGroup;
195+
196+
private static class GroupWithMembers {
197+
private final Group group;
180198
private final Set<AccountScaffoldUserPostDTO> members;
181199

182-
private SuperGroupWithMembers(SuperGroup superGroup, Set<AccountScaffoldUserPostDTO> members) {
183-
this.superGroup = superGroup;
200+
private GroupWithMembers(Group group, Set<AccountScaffoldUserPostDTO> members) {
201+
this.group = group;
184202
this.members = members;
185203
}
186204
}
205+
206+
private static class SuperGroupWithGroups {
207+
private final SuperGroup superGroup;
208+
private final List<GroupWithMembers> groups;
209+
210+
private SuperGroupWithGroups(SuperGroup superGroup, List<GroupWithMembers> groups) {
211+
this.superGroup = superGroup;
212+
this.groups = groups;
213+
}
214+
}
215+
216+
187217
}

0 commit comments

Comments
 (0)