@Override
  @Transactional
  public void sendNotificationToAddedDelegate(
      OrcidProfile orcidUserGrantingPermission, List<DelegationDetails> delegatesGrantedByUser) {
    // Create map of template params
    Map<String, Object> templateParams = new HashMap<String, Object>();
    String subject = getSubject("email.subject.added_as_delegate", orcidUserGrantingPermission);

    for (DelegationDetails newDelegation : delegatesGrantedByUser) {
      ProfileEntity delegateProfileEntity =
          profileDao.find(newDelegation.getDelegateSummary().getOrcidIdentifier().getPath());
      Boolean sendAdministrativeChangeNotifications =
          delegateProfileEntity.getSendAdministrativeChangeNotifications();
      if (sendAdministrativeChangeNotifications == null || !sendAdministrativeChangeNotifications) {
        LOGGER.debug(
            "Not sending added delegate email, because option to send administrative change notifications not set to true for delegate: {}",
            delegateProfileEntity.getId());
        return;
      }

      String grantingOrcidEmail =
          orcidUserGrantingPermission
              .getOrcidBio()
              .getContactDetails()
              .retrievePrimaryEmail()
              .getValue();
      String emailNameForDelegate = deriveEmailFriendlyName(delegateProfileEntity);
      String email = delegateProfileEntity.getPrimaryEmail().getId();

      templateParams.put("emailNameForDelegate", emailNameForDelegate);
      templateParams.put(
          "grantingOrcidValue", orcidUserGrantingPermission.getOrcidIdentifier().getPath());
      templateParams.put("grantingOrcidName", deriveEmailFriendlyName(orcidUserGrantingPermission));
      templateParams.put("baseUri", orcidUrlManager.getBaseUrl());
      templateParams.put("baseUriHttp", orcidUrlManager.getBaseUriHttp());
      templateParams.put("grantingOrcidEmail", grantingOrcidEmail);
      templateParams.put("subject", subject);

      addMessageParams(templateParams, orcidUserGrantingPermission);

      // Generate body from template
      String body = templateManager.processTemplate("added_as_delegate_email.ftl", templateParams);
      // Generate html from template
      String html =
          templateManager.processTemplate("added_as_delegate_email_html.ftl", templateParams);

      boolean notificationsEnabled = delegateProfileEntity.getEnableNotifications();
      if (notificationsEnabled) {
        NotificationCustom notification = new NotificationCustom();
        notification.setNotificationType(NotificationType.CUSTOM);
        notification.setSubject(subject);
        notification.setBodyHtml(html);
        createNotification(
            newDelegation.getDelegateSummary().getOrcidIdentifier().getPath(), notification);
      } else {
        mailGunManager.sendEmail(DELEGATE_NOTIFY_ORCID_ORG, email, subject, body, html);
      }
    }
  }
  @Override
  public void sendDelegationRequestEmail(OrcidProfile managed, OrcidProfile trusted, String link) {
    // Create map of template params
    String orcid = managed.getOrcidIdentifier().getPath();
    Map<String, Object> templateParams = new HashMap<String, Object>();
    templateParams.put("baseUri", orcidUrlManager.getBaseUrl());
    templateParams.put("baseUriHttp", orcidUrlManager.getBaseUriHttp());
    templateParams.put("link", link);

    String trustedOrcidValue = trusted.retrieveOrcidPath();
    String managedOrcidValue = managed.retrieveOrcidPath();
    String emailNameForDelegate = deriveEmailFriendlyName(managed);
    String trustedOrcidName = deriveEmailFriendlyName(trusted);
    templateParams.put("emailNameForDelegate", emailNameForDelegate);
    templateParams.put("trustedOrcidName", trustedOrcidName);
    templateParams.put("trustedOrcidValue", trustedOrcidValue);
    templateParams.put("managedOrcidValue", managedOrcidValue);

    Email primaryEmail = managed.getOrcidBio().getContactDetails().retrievePrimaryEmail();
    if (primaryEmail == null) {
      LOGGER.info("Cant send admin delegate email if primary email is null: {}", orcid);
      return;
    }

    addMessageParams(templateParams, managed);

    String htmlBody =
        templateManager.processTemplate("admin_delegate_request_html.ftl", templateParams);

    // Send message
    if (apiRecordCreationEmailEnabled) {
      String subject = getSubject("email.subject.admin_as_delegate", managed, trustedOrcidName);
      ProfileEntity trustedProfileEntity = profileDao.find(trusted.getOrcidIdentifier().getPath());
      boolean notificationsEnabled =
          trustedProfileEntity != null ? trustedProfileEntity.getEnableNotifications() : false;
      if (notificationsEnabled) {
        NotificationCustom notification = new NotificationCustom();
        notification.setNotificationType(NotificationType.CUSTOM);
        notification.setSubject(subject);
        notification.setBodyHtml(htmlBody);
        createNotification(managed.getOrcidIdentifier().getPath(), notification);
      } else {
        mailGunManager.sendEmail(
            DELEGATE_NOTIFY_ORCID_ORG, primaryEmail.getValue(), subject, null, htmlBody);
      }
      profileEventDao.persist(
          new ProfileEventEntity(orcid, ProfileEventType.ADMIN_PROFILE_DELEGATION_REQUEST));
    } else {
      LOGGER.debug(
          "Not sending admin delegate email, because API record creation email option is disabled. Message would have been: {}",
          htmlBody);
    }
  }