From e07961a20117577eaad2f15059a809bb33939446 Mon Sep 17 00:00:00 2001
From: andrej romanov <50377758+auumgn@users.noreply.github.com>
Date: Wed, 30 Oct 2024 21:04:38 +0200
Subject: [PATCH] add source name and or id to emails missing both

---
 .../web/controllers/ManageProfileController.java     | 12 +++++++++++-
 .../web/controllers/PublicRecordController.java      | 12 ++++++++++--
 2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/orcid-web/src/main/java/org/orcid/frontend/web/controllers/ManageProfileController.java b/orcid-web/src/main/java/org/orcid/frontend/web/controllers/ManageProfileController.java
index e3e99fb1b3f..f743785b496 100644
--- a/orcid-web/src/main/java/org/orcid/frontend/web/controllers/ManageProfileController.java
+++ b/orcid-web/src/main/java/org/orcid/frontend/web/controllers/ManageProfileController.java
@@ -536,7 +536,17 @@ public ModelAndView confirmDeactivateOrcidAccount(HttpServletRequest request, Ht
         if (Features.EMAIL_DOMAINS.isActive()) {
             emailDomains = profileEmailDomainManagerReadOnly.getEmailDomains(getCurrentUserOrcid());
         }
-        return org.orcid.pojo.ajaxForm.Emails.valueOf(v2Emails, emailDomains);
+        org.orcid.pojo.ajaxForm.Emails emails = org.orcid.pojo.ajaxForm.Emails.valueOf(v2Emails, emailDomains);
+        // Old emails are missing the source name and id -- assign the user as the source
+        for (org.orcid.pojo.ajaxForm.Email email: emails.getEmails()) {
+            if (email.getSource() == null && email.getSourceName() == null) {
+                String orcid = getCurrentUserOrcid();
+                String displayName = getPersonDetails(orcid, true).getDisplayName();
+                email.setSource(orcid);
+                email.setSourceName(displayName);
+            }
+        }
+        return emails;
     }
 
     @RequestMapping(value = "/emails.json", method = RequestMethod.POST)
diff --git a/orcid-web/src/main/java/org/orcid/frontend/web/controllers/PublicRecordController.java b/orcid-web/src/main/java/org/orcid/frontend/web/controllers/PublicRecordController.java
index feb81265199..b0787513446 100644
--- a/orcid-web/src/main/java/org/orcid/frontend/web/controllers/PublicRecordController.java
+++ b/orcid-web/src/main/java/org/orcid/frontend/web/controllers/PublicRecordController.java
@@ -202,8 +202,16 @@ PublicRecord getRecord(String orcid) {
         if (Features.EMAIL_DOMAINS.isActive()) {
             emailDomains = profileEmailDomainManagerReadOnly.getPublicEmailDomains(orcid);
         }
-        
-        publicRecord.setEmails(org.orcid.pojo.ajaxForm.Emails.valueOf(filteredEmails, emailDomains));
+
+        org.orcid.pojo.ajaxForm.Emails emails = org.orcid.pojo.ajaxForm.Emails.valueOf(filteredEmails, emailDomains);
+        // Old emails are missing the source name and id -- assign the user as the source
+        for (org.orcid.pojo.ajaxForm.Email email: emails.getEmails()) {
+            if (email.getSource() == null && email.getSourceName() == null) {
+                email.setSource(orcid);
+                email.setSourceName(publicRecord.getDisplayName());
+            }
+        }
+        publicRecord.setEmails(emails);
 
         // Fill external identifiers
         PersonExternalIdentifiers publicPersonExternalIdentifiers;