Skip to content

Commit 20ed491

Browse files
committed
Fixes #687
1 parent ddcbd74 commit 20ed491

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

server/src/main/java/invite/crm/CRMController.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import invite.model.Organisation;
2222
import invite.model.Provisionable;
2323
import invite.model.Role;
24+
import invite.model.Status;
2425
import invite.model.User;
2526
import invite.model.UserRole;
2627
import invite.model.UserRoleAudit;
@@ -448,6 +449,20 @@ private boolean sendInvitation(CRMContact crmContact, Organisation organisation)
448449
Optional<User> optionalUser =
449450
userRepository.findByCrmContactIdAndOrganisation(
450451
crmContact.getContactId(), organisation);
452+
//Idempotency - if there is already an open invitation with the same roles for this contact / organisation then do nothing
453+
List<Invitation> invitations = invitationRepository
454+
.findByCrmContactIdAndCrmOrganisationId(crmContact.getContactId(), organisation.getCrmOrganisationId());
455+
boolean duplicateInvitation = invitations.stream().anyMatch(invitation -> invitation.getStatus().equals(Status.OPEN) &&
456+
invitation.getRoles().stream()
457+
.allMatch(invitationRole -> crmContact.getRoles().stream()
458+
.anyMatch(crmRole -> crmRole.getRoleId().equals(invitationRole.getRole().getCrmRoleId()))));
459+
if (duplicateInvitation) {
460+
LOG.info(String.format("Not sending invitation to %s as there is already an outstanding invitation with roles %s",
461+
crmContact.getEmail(),
462+
crmContact.getRoles().stream().map(crmRole -> crmRole.getName()).collect(Collectors.joining(", "))));
463+
return false;
464+
}
465+
451466
List<CRMRole> newCrmRoles = syncCrmRoles(crmContact, optionalUser.orElse(new User()));
452467
//Only save the user when the user already existed
453468
optionalUser.ifPresent(user -> userRepository.save(user));

server/src/test/java/invite/crm/CRMControllerTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,23 @@ void contactInviteNewUser() throws Exception {
180180
Invitation invitation = invitations.getFirst();
181181
assertEquals("SURF CRM", invitation.getRemoteApiUser());
182182

183+
//Now we send the same POST again, and because of idenpotentcy no new invitation should be created
184+
String newResponse = given()
185+
.when()
186+
.accept(ContentType.JSON)
187+
.header(API_KEY_HEADER, "secret")
188+
.contentType(ContentType.JSON)
189+
.body(crmContact)
190+
.post("/crm/profile")
191+
.then()
192+
.extract()
193+
.asString();
194+
assertEquals("updated", newResponse);
195+
List<Invitation> invitationsAfterSyncs = invitationRepository.findByCrmContactIdAndCrmOrganisationId(
196+
crmContactID, crmOrganisationID);
197+
assertEquals(1, invitationsAfterSyncs.size());
198+
assertEquals(invitations.getFirst().getId(), invitationsAfterSyncs.getFirst().getId());
199+
183200
response = given()
184201
.when()
185202
.accept(ContentType.JSON)

0 commit comments

Comments
 (0)