@@ -86,7 +86,7 @@ public class CRMController {
8686 private final ProvisioningService provisioningService ;
8787 private final MailBox mailBox ;
8888 private final Manage manage ;
89- private final Map <String , CrmConfigEntry > crmConfig ;
89+ private final Map <String , CRMConfigEntry > crmConfig ;
9090 private final UserRoleAuditService userRoleAuditService ;
9191 private final Provisionable provisionable = () -> "SURF CRM" ;
9292 private final InvitationRepository invitationRepository ;
@@ -122,10 +122,10 @@ public CRMController(@Value("${crm.collab-person-prefix}") String collabPersonPr
122122 Map <String , Map <String , Object >> crmConfigRaw = objectMapper .readValue (crmConfigResource .getInputStream (), new TypeReference <>() {
123123 });
124124 this .crmConfig = crmConfigRaw .entrySet ().stream ()
125- .map (entry -> new CrmConfigEntry (entry .getKey (), (String ) entry .getValue ().get ("name" ),
125+ .map (entry -> new CRMConfigEntry (entry .getKey (), (String ) entry .getValue ().get ("name" ),
126126 ((List <Map <String , String >>) entry .getValue ().getOrDefault ("applications" , List .of ()))
127127 .stream ()
128- .map (application -> new CrmManageIdentifier (
128+ .map (application -> new CRMManageIdentifier (
129129 EntityType .valueOf (application .get ("manageType" ).toUpperCase ()),
130130 application .get ("manageEntityID" ))).toList ()))
131131 .collect (Collectors .toMap (
@@ -395,6 +395,30 @@ public ResponseEntity<Void> deleteOrganisation(@RequestBody CRMOrganisation crmO
395395 return ResponseEntity .status (204 ).build ();
396396 }
397397
398+ @ PostMapping (value = "/crm/api/v1/invite/send" , produces = MediaType .APPLICATION_JSON_VALUE )
399+ @ Operation (summary = "Send invite for CRM role memberships" ,
400+ description = "Send invite for CRM role memberships" )
401+ @ SecurityRequirement (name = API_HEADER_SCHEME_NAME )
402+ @ PreAuthorize ("hasRole('CRM')" )
403+ public ResponseEntity <String > send (@ RequestBody SendInvitation sendInvitation ) {
404+ LOG .debug ("POST /crm/api/v1/invite/send: " + sendInvitation );
405+
406+ if (CollectionUtils .isEmpty (sendInvitation .roles ())) {
407+ throw new InvalidInputException ("Roles are required for /crm/api/v1/invite/send" );
408+ }
409+
410+ Organisation organisation = organisationRepository .findByCrmOrganisationId (sendInvitation .crmOrganisationId ())
411+ .orElseThrow (() -> new NotFoundException ("Organisation not found: " +sendInvitation .crmOrganisationId ()));
412+ CRMContact crmContact = new CRMContact ();
413+ crmContact .setEmail (sendInvitation .email ());
414+ crmContact .setRoles (sendInvitation .roles ());
415+ crmContact .setContactId (sendInvitation .crmContatcId ());
416+
417+ sendInvitation (crmContact , organisation );
418+
419+ return ResponseEntity .ok ().body ("send" );
420+ }
421+
398422 private ProfileResponse crmUserNotFoundOrNoRoles () {
399423 return new ProfileResponse ("Could not find any profiles with the given search parameters" , 50 , List .of ());
400424 }
@@ -522,7 +546,7 @@ private boolean sendInvitation(CRMContact crmContact, Organisation organisation)
522546 Set <InvitationRole > invitationRoles = roles .stream ()
523547 .map (role -> new InvitationRole (role ))
524548 .collect (Collectors .toSet ());
525- Invitation invitation = createInvitation (crmContact , invitationRoles );
549+ Invitation invitation = createInvitation (crmContact , invitationRoles , organisation );
526550
527551 Optional <String > idpName = identityProviderName (manage , invitation );
528552 if (crmContact .isSuppressInvitation ()) {
@@ -547,7 +571,7 @@ private List<Role> convertCrmRolesToInviteRoles(CRMContact crmContact, List<CRMR
547571 }
548572
549573 private Optional <Role > createRole (CRMOrganisation crmOrganisation , CRMRole crmRole , Organisation organisation ) {
550- CrmConfigEntry crmConfigEntry = this .crmConfig .get (crmRole .getSabCode ());
574+ CRMConfigEntry crmConfigEntry = this .crmConfig .get (crmRole .getSabCode ());
551575 if (crmConfigEntry == null ) {
552576 throw new InvalidInputException ("CRM sabCode is not configured: " + crmRole .getSabCode ());
553577 }
@@ -567,9 +591,10 @@ private Optional<Role> createRole(CRMOrganisation crmOrganisation, CRMRole crmRo
567591 })
568592 .map (application -> new ApplicationUsage (application , application .getLandingPage ()))
569593 .collect (Collectors .toSet ());
594+ String organisationName = crmOrganisation != null ? crmOrganisation .getName () : organisation .getCrmOrganisationName ();
570595 Role unsavedRole = new Role (
571- String .format ("%s for %s" , crmConfigEntry .name (), crmOrganisation . getName () ),
572- String .format ("CRM role %s for organisation %s" , crmConfigEntry .name (), crmOrganisation . getName () ),
596+ String .format ("%s for %s" , crmConfigEntry .name (), organisationName ),
597+ String .format ("CRM role %s for organisation %s" , crmConfigEntry .name (), organisationName ),
573598 applicationUsages ,
574599 365 * 25 ,
575600 true ,
@@ -579,14 +604,14 @@ private Optional<Role> createRole(CRMOrganisation crmOrganisation, CRMRole crmRo
579604 unsavedRole .setCrmRoleName (crmConfigEntry .name ());
580605 unsavedRole .setCrmRoleAbbrevation (crmRole .getSabCode ());
581606 unsavedRole .setOrganisation (organisation );
582- unsavedRole .setOrganizationGUID (crmOrganisation . getOrganisationId ());
607+ unsavedRole .setOrganizationGUID (organisation . getCrmOrganisationId ());
583608
584609 Role role = roleRepository .save (unsavedRole );
585610 this .provisioningService .newGroupRequest (role );
586611 return Optional .of (role );
587612 }
588613
589- private Invitation createInvitation (CRMContact crmContact , Set <InvitationRole > invitationRoles ) {
614+ private Invitation createInvitation (CRMContact crmContact , Set <InvitationRole > invitationRoles , Organisation organisation ) {
590615 Invitation invitation = new Invitation (
591616 Authority .GUEST ,
592617 HashGenerator .generateRandomHash (),
@@ -603,7 +628,8 @@ private Invitation createInvitation(CRMContact crmContact, Set<InvitationRole> i
603628 invitationRoles ,
604629 null
605630 );
606- String crmOrganisationId = crmContact .getOrganisation ().getOrganisationId ();
631+ CRMOrganisation crmOrganisation = crmContact .getOrganisation ();
632+ String crmOrganisationId = crmOrganisation != null ? crmOrganisation .getOrganisationId () : organisation .getCrmOrganisationId ();
607633 invitation .setOrganizationGUID (crmOrganisationId );
608634 invitation .setCrmOrganisationId (crmOrganisationId );
609635 invitation .setCrmContactId (crmContact .getContactId ());
0 commit comments