@@ -260,7 +260,7 @@ public ResponseEntity<Map<String, ConnectionStatusResponse>> connectionStatus(@R
260260 user -> user .getCrmContactId (),
261261 user -> new ConnectionStatusResponse (
262262 user .getName (), user .getEmail (), user .getSchacHomeOrganization (), user .getUid (),
263- " Paired" , "paired"
263+ CRMStatusCode . Paired . getStatus (), CRMStatusCode . Paired . getStatusCode ()
264264 )
265265 ));
266266 return ResponseEntity .ok (responseMap );
@@ -269,19 +269,26 @@ public ResponseEntity<Map<String, ConnectionStatusResponse>> connectionStatus(@R
269269 .stream ()
270270 .collect (Collectors .toMap (
271271 invitation -> invitation .getCrmContactId (),
272- invitation -> userRepository .findByCrmContactIdAndOrganisation (crmOrganisationId , organisation )
273- .map (user -> new ConnectionStatusResponse (
274- user .getName (), user .getEmail (), user .getSchacHomeOrganization (), user .getUid (),
275- "In process" , "in_process"
276- )).orElse (new ConnectionStatusResponse (
277- null , invitation .getEmail (), null , null ,
278- "In process" , "in_process"
279- ))
272+ invitation -> {
273+ CRMStatusCode crmStatusCode = crmStatusCode (invitation );
274+ return userRepository .findByCrmContactIdAndOrganisation (invitation .getCrmContactId (), organisation )
275+ .map (user -> new ConnectionStatusResponse (
276+ user .getName (), user .getEmail (), user .getSchacHomeOrganization (), user .getUid (),
277+ crmStatusCode .getStatus (), crmStatusCode .getStatusCode ()
278+ )).orElse (new ConnectionStatusResponse (
279+ null , invitation .getEmail (), null , null ,
280+ crmStatusCode .getStatus (), crmStatusCode .getStatusCode ()
281+ ));
282+ }
280283 ));
281284 return ResponseEntity .ok (responseMap );
282285 }
283286 }
284287
288+ private CRMStatusCode crmStatusCode (Invitation invitation ) {
289+ return invitation .getExpiryDate ().isBefore (Instant .now ()) ? CRMStatusCode .NotPaired : CRMStatusCode .InProcess ;
290+ }
291+
285292 @ PostMapping (value = "/crm/api/v1/invite/resend" , produces = MediaType .APPLICATION_JSON_VALUE )
286293 @ Operation (summary = "Resend an invitation" ,
287294 description = "Resend an invitation based on the CRM OrganisationID and CRM ContactID" )
@@ -426,18 +433,12 @@ private List<Role> convertCrmRolesToInviteRoles(CRMContact crmContact, List<CRMR
426433 return newCrmRoles .stream ()
427434 .map (crmRole -> roleRepository .findByCrmRoleIdAndOrganisation (
428435 crmRole .getRoleId (), organisation )
429- .or (() -> this .createRole (crmContact .getOrganisation (), crmRole )))
436+ .or (() -> this .createRole (crmContact .getOrganisation (), crmRole , organisation )))
430437 .flatMap (Optional ::stream )
431438 .toList ();
432439 }
433440
434- private Optional <Role > createRole (CRMOrganisation crmOrganisation , CRMRole crmRole ) {
435- Organisation organisation = organisationRepository .findByCrmOrganisationId (crmOrganisation .getOrganisationId ())
436- .orElseGet (() -> organisationRepository .save (new Organisation (
437- crmOrganisation .getOrganisationId (),
438- crmOrganisation .getName (),
439- crmOrganisation .getAbbrev ()
440- )));
441+ private Optional <Role > createRole (CRMOrganisation crmOrganisation , CRMRole crmRole , Organisation organisation ) {
441442 CrmConfigEntry crmConfigEntry = this .crmConfig .get (crmRole .getSabCode ());
442443 if (crmConfigEntry == null ) {
443444 throw new InvalidInputException ("CRM sabCode is not configured: " + crmRole .getSabCode ());
0 commit comments