Skip to content

Commit

Permalink
added /count method to org invitations. fixes #90. (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
xgp authored Mar 13, 2024
1 parent 8dc399d commit 6d3c33c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public interface OrganizationModel extends WithAttributes {

void revokeMembership(UserModel user);

Long getInvitationsCount();

Stream<InvitationModel> getInvitationsStream();

default Stream<InvitationModel> getInvitationsByEmail(String email) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ public void revokeMembership(UserModel user) {
if (user.getEmail() != null) revokeInvitations(user.getEmail());
}

@Override
public Long getInvitationsCount() {
TypedQuery<Long> query = em.createNamedQuery("getInvitationCount", Long.class);
query.setParameter("organization", org);
return query.getSingleResult();
}

@Override
public Stream<InvitationModel> getInvitationsStream() {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"SELECT i FROM InvitationEntity i WHERE i.organization in (SELECT o FROM OrganizationEntity o WHERE o.realmId = :realmId) AND lower(i.email) = lower(:search) ORDER BY i.createdAt"),
@NamedQuery(
name = "getInvitationCount",
query = "select count(t) from InvitationEntity t where t.organization = :organization")
query = "SELECT COUNT(t) FROM InvitationEntity t WHERE t.organization = :organization")
})
@Entity
@Table(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ public Stream<Invitation> listInvitations(
.map(i -> convertInvitationModelToInvitation(i));
}

@GET
@Path("count")
@Produces(MediaType.APPLICATION_JSON)
public Long countInvitations() {
log.debugf("countInvitations %s %s", realm.getName(), organization.getId());
return organization.getInvitationsCount();
}

@GET
@Path("{invitationId}")
@Produces(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ void testSearchOrganizations() throws IOException {
response = getRequest("count");
assertThat(response.statusCode(), is(Status.OK.getStatusCode()));
Long cnt = objectMapper().readValue(response.getBody().asString(), Long.class);
;
assertThat(orgs, notNullValue());
assertThat(cnt, is(6L));

Expand Down Expand Up @@ -1092,7 +1091,7 @@ void testAddGetDeleteInvitations() throws IOException {
.attribute("foo", "bar")
.attribute("foo", "bar2")
.attribute("humpty", "dumpty");
// xxx

Response response = postRequest(inv, id, "invitations");
assertThat(response.statusCode(), is(Status.CREATED.getStatusCode()));
assertNotNull(response.getHeader("Location"));
Expand All @@ -1118,6 +1117,12 @@ void testAddGetDeleteInvitations() throws IOException {
assertThat(invites.get(0).getAttributes().get("humpty").get(0), is("dumpty"));
String invId = invites.get(0).getId();

// count invitations
response = getRequest(id, "invitations", "count");
assertThat(response.statusCode(), is(Status.OK.getStatusCode()));
Long cnt = objectMapper().readValue(response.getBody().asString(), Long.class);
assertThat(cnt, is(1L));

// get a specific innvitation
response = getRequest(id, "invitations", invId);
Invitation invite =
Expand Down Expand Up @@ -1158,6 +1163,12 @@ void testAddGetDeleteInvitations() throws IOException {
assertThat(invites, notNullValue());
assertThat(invites, empty());

// count invitations, now 0
response = getRequest(id, "invitations", "count");
assertThat(response.statusCode(), is(Status.OK.getStatusCode()));
cnt = objectMapper().readValue(response.getBody().asString(), Long.class);
assertThat(cnt, is(0L));

// delete user
deleteUser(keycloak, REALM, user1.getId());

Expand Down

0 comments on commit 6d3c33c

Please sign in to comment.