Skip to content

Commit 0c935dd

Browse files
committed
Return paired users and inprocess invitations
1 parent 09edfb4 commit 0c935dd

3 files changed

Lines changed: 65 additions & 115 deletions

File tree

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

Lines changed: 57 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import java.time.format.DateTimeFormatter;
6161
import java.util.Collections;
6262
import java.util.Comparator;
63+
import java.util.HashMap;
6364
import java.util.List;
6465
import java.util.Map;
6566
import java.util.NoSuchElementException;
@@ -280,109 +281,63 @@ public ResponseEntity<Map<String, ConnectionStatusResponse>> connectionStatus(@R
280281
return ResponseEntity.ok(Map.of());
281282
}
282283
Organisation organisation = optionalOrganisation.get();
283-
if (connectionStatus.connected()) {
284-
Map<String, ConnectionStatusResponse> responseMap = userRepository.findByOrganisation(organisation)
285-
.stream()
286-
.collect(Collectors.toMap(
287-
user -> user.getCrmContactId(),
288-
user -> new ConnectionStatusResponse(
289-
user.getCrmContactId(),
290-
user.getGivenName(),
291-
user.getMiddleName(),
292-
user.getFamilyName(),
293-
user.getName(),
294-
user.getEmail(),
295-
"",
296-
Map.of(
297-
"id", 0,
298-
"abbrev", organisation.getCrmOrganisationAbbrevation(),
299-
"name", organisation.getCrmOrganisationName(),
300-
"oid", 0,
301-
"guid", organisation.getCrmOrganisationId()
302-
),
303-
Map.of(
304-
"uid", user.getUid(),
305-
"idp", user.getSchacHomeOrganization()
306-
),
307-
CRMStatusCode.Paired.getStatus(),
308-
CRMStatusCode.Paired.getStatusCode()
309-
),
310-
(existing, replacement) -> replacement
311-
));
312-
if (LOG.isDebugEnabled()) {
313-
LOG.debug(String.format("/crm/api/v1/profiles connectionStatus.connected is true. Returning %s", responseMap));
314-
}
315-
return ResponseEntity.ok(responseMap);
316-
} else {
317-
Map<String, ConnectionStatusResponse> responseMap = invitationRepository
318-
.findByCrmOrganisationIdAndStatus(crmOrganisationId, Status.OPEN)
319-
.stream()
320-
.collect(Collectors.toMap(
321-
invitation -> invitation.getCrmContactId(),
322-
invitation -> {
323-
CRMStatusCode crmStatusCode = crmStatusCode(invitation);
324-
return userRepository.findByCrmContactIdAndOrganisation(invitation.getCrmContactId(), organisation)
325-
.map(user -> new ConnectionStatusResponse(
326-
invitation.getCrmContactId(),
327-
user.getGivenName(),
328-
user.getMiddleName(),
329-
user.getFamilyName(),
330-
user.getName(),
331-
user.getEmail(),
332-
"",
333-
Map.of(
334-
"id", 0,
335-
"abbrev", organisation.getCrmOrganisationAbbrevation(),
336-
"name", organisation.getCrmOrganisationName(),
337-
"oid", 0,
338-
"guid", organisation.getCrmOrganisationId()
339-
),
340-
Map.of(
341-
"uid", resolveUserUid(user),
342-
"idp", user.getSchacHomeOrganization()
343-
),
344-
crmStatusCode.getStatus(),
345-
crmStatusCode.getStatusCode()
346-
)).orElse(new ConnectionStatusResponse(
347-
invitation.getCrmContactId(),
348-
null,
349-
null,
350-
null,
351-
null,
352-
invitation.getEmail(),
353-
"",
354-
Map.of(
355-
"id", 0,
356-
"abbrev", organisation.getCrmOrganisationAbbrevation(),
357-
"name", organisation.getCrmOrganisationName(),
358-
"oid", 0,
359-
"guid", organisation.getCrmOrganisationId()
360-
),
361-
Map.of(),
362-
crmStatusCode.getStatus(),
363-
crmStatusCode.getStatusCode()
364-
));
365-
},
366-
(existing, replacement) -> replacement
367-
));
368-
if (LOG.isDebugEnabled()) {
369-
LOG.debug(String.format("/crm/api/v1/profiles connectionStatus.connected is false. Returning %s", responseMap));
370-
}
371-
return ResponseEntity.ok(responseMap);
372-
}
373-
}
374-
375-
private String resolveUserUid(User user) {
376-
String uid = user.getUid();
377-
if (StringUtils.hasText(uid)) {
378-
return uid;
284+
Map<String, ConnectionStatusResponse> responseMap = new HashMap<>();
285+
userRepository.findByOrganisation(organisation)
286+
.forEach(user -> responseMap.put(
287+
user.getCrmContactId(),
288+
new ConnectionStatusResponse(
289+
user.getCrmContactId(),
290+
user.getGivenName(),
291+
user.getMiddleName(),
292+
user.getFamilyName(),
293+
user.getName(),
294+
user.getEmail(),
295+
"",
296+
Map.of(
297+
"id", 0,
298+
"abbrev", organisation.getCrmOrganisationAbbrevation(),
299+
"name", organisation.getCrmOrganisationName(),
300+
"oid", 0,
301+
"guid", organisation.getCrmOrganisationId()
302+
),
303+
Map.of(
304+
"uid", user.getUid(),
305+
"idp", user.getSchacHomeOrganization()
306+
),
307+
CRMStatusCode.Paired.getStatus(),
308+
CRMStatusCode.Paired.getStatusCode()
309+
)
310+
)
311+
);
312+
313+
invitationRepository
314+
.findByCrmOrganisationIdAndStatus(crmOrganisationId, Status.OPEN)
315+
.forEach(invitation -> responseMap.put(
316+
invitation.getCrmContactId(),
317+
new ConnectionStatusResponse(
318+
invitation.getCrmContactId(),
319+
null,
320+
null,
321+
null,
322+
null,
323+
invitation.getEmail(),
324+
"",
325+
Map.of(
326+
"id", 0,
327+
"abbrev", organisation.getCrmOrganisationAbbrevation(),
328+
"name", organisation.getCrmOrganisationName(),
329+
"oid", 0,
330+
"guid", organisation.getCrmOrganisationId()
331+
),
332+
Map.of(),
333+
CRMStatusCode.InProcess.getStatus(),
334+
CRMStatusCode.InProcess.getStatusCode()
335+
)
336+
));
337+
if (LOG.isDebugEnabled()) {
338+
LOG.debug(String.format("/crm/api/v1/profiles Returning %s", responseMap));
379339
}
380-
String sub = user.getSub();
381-
return sub.substring(sub.lastIndexOf(":") + 1);
382-
}
383-
384-
private CRMStatusCode crmStatusCode(Invitation invitation) {
385-
return invitation.getExpiryDate().isBefore(Instant.now()) ? CRMStatusCode.NotPaired : CRMStatusCode.InProcess;
340+
return ResponseEntity.ok(responseMap);
386341
}
387342

388343
@PostMapping(value = "/crm/api/v1/invite/resend", produces = MediaType.APPLICATION_JSON_VALUE)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
44

5-
public record ConnectionStatus(@JsonProperty("org_guid") String organisationId, boolean connected) {
5+
public record ConnectionStatus(@JsonProperty("org_guid") String organisationId) {
66
}

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -558,15 +558,14 @@ void connectionStatusWithInvitationExistingUser() {
558558
.accept(ContentType.JSON)
559559
.header(API_KEY_HEADER, "secret")
560560
.contentType(ContentType.JSON)
561-
.body(new ConnectionStatus(CRM_ORGANIZATION_ID, false))
561+
.body(new ConnectionStatus(CRM_ORGANIZATION_ID))
562562
.get("/crm/api/v1/profiles")
563563
.as(new TypeRef<>() {
564564
});
565565
assertEquals(1, response.size());
566566
ConnectionStatusResponse connectionStatusResponse = response.get(CRM_CONTACT_ID);
567-
assertEquals("gb@kb.nl", connectionStatusResponse.email());
568-
assertEquals("George Best", connectionStatusResponse.fullname());
569-
assertEquals(CRMStatusCode.NotPaired.getStatusCode(), connectionStatusResponse.statusCode());
567+
assertEquals("guest@new.com", connectionStatusResponse.email());
568+
assertEquals(CRMStatusCode.InProcess.getStatusCode(), connectionStatusResponse.statusCode());
570569
}
571570

572571
@Test
@@ -583,15 +582,11 @@ void connectionStatusWithInvitationNewUser() {
583582
.accept(ContentType.JSON)
584583
.header(API_KEY_HEADER, "secret")
585584
.contentType(ContentType.JSON)
586-
.body(new ConnectionStatus(CRM_ORGANIZATION_ID, false))
585+
.body(new ConnectionStatus(CRM_ORGANIZATION_ID))
587586
.get("/crm/api/v1/profiles")
588587
.as(new TypeRef<>() {
589588
});
590-
assertEquals(1, response.size());
591-
ConnectionStatusResponse connectionStatusResponse = response.get(crmContactId);
592-
assertEquals(invitation.getEmail(), connectionStatusResponse.email());
593-
assertNull(connectionStatusResponse.fullname());
594-
assertEquals(CRMStatusCode.InProcess.getStatusCode(), connectionStatusResponse.statusCode());
589+
assertEquals(2, response.size());
595590
}
596591

597592
@Test
@@ -601,7 +596,7 @@ void connectionStatusWithUser() {
601596
.accept(ContentType.JSON)
602597
.header(API_KEY_HEADER, "secret")
603598
.contentType(ContentType.JSON)
604-
.body(new ConnectionStatus(CRM_ORGANIZATION_ID, true))
599+
.body(new ConnectionStatus(CRM_ORGANIZATION_ID))
605600
.get("/crm/api/v1/profiles")
606601
.as(new TypeRef<>() {
607602
});
@@ -619,7 +614,7 @@ void connectionStatusWithUnknownOrganisation() {
619614
.accept(ContentType.JSON)
620615
.header(API_KEY_HEADER, "secret")
621616
.contentType(ContentType.JSON)
622-
.body(new ConnectionStatus("nope", true))
617+
.body(new ConnectionStatus("nope"))
623618
.get("/crm/api/v1/profiles")
624619
.as(new TypeRef<>() {
625620
});

0 commit comments

Comments
 (0)