This is an automated email from the ASF dual-hosted git repository.

lahirujayathilake pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airavata-custos.git


The following commit(s) were added to refs/heads/master by this push:
     new a0000c9c3 Link existing PIs by email (#738)
a0000c9c3 is described below

commit a0000c9c3eb5f1b5095830aee47be27274f8cf05
Author: Yasith Jayawardana <[email protected]>
AuthorDate: Thu Nov 12 23:58:06 2026 -0500

    Link existing PIs by email (#738)
---
 .../ACCESS/AMIE-Processor/handler/handler.go       | 28 ++++++++++++++++++
 .../handler/request_account_create.go              | 25 ++--------------
 .../handler/request_project_create.go              | 10 ++-----
 .../request_project_create_integration_test.go     | 34 ++++++++++++++++++++++
 4 files changed, 67 insertions(+), 30 deletions(-)

diff --git a/connectors/ACCESS/AMIE-Processor/handler/handler.go 
b/connectors/ACCESS/AMIE-Processor/handler/handler.go
index 212c6c2a1..b1fa98dc9 100644
--- a/connectors/ACCESS/AMIE-Processor/handler/handler.go
+++ b/connectors/ACCESS/AMIE-Processor/handler/handler.go
@@ -217,6 +217,34 @@ func ensureClusterUser(ctx context.Context, svc 
*service.Service, clusterID, use
        return allocateAndCreateClusterUser(ctx, svc, clusterID, userID)
 }
 
+// ensureUserByEmail returns the user with email, creating a PENDING one if 
none exists.
+// An existing user, added internally, takes the organization and name from 
ACCESS.
+func ensureUserByEmail(ctx context.Context, svc *service.Service, orgID, 
email, first, last string) (*models.User, error) {
+       user, err := svc.GetUserByEmail(ctx, email)
+       switch {
+       case err == nil:
+               user.OrganizationID, user.FirstName, user.LastName = orgID, 
first, last
+               if err := svc.UpdateUser(ctx, user); err != nil {
+                       return nil, fmt.Errorf("update user: %w", err)
+               }
+               return user, nil
+       case errors.Is(err, service.ErrNotFound):
+               user, err = svc.CreateUser(ctx, &models.User{
+                       OrganizationID: orgID,
+                       FirstName:      first,
+                       LastName:       last,
+                       Email:          email,
+                       Status:         models.UserPending,
+               })
+               if err != nil {
+                       return nil, fmt.Errorf("create user: %w", err)
+               }
+               return user, nil
+       default:
+               return nil, err
+       }
+}
+
 // ensureOrganization looks up an Organization by its originated_id (the
 // AMIE-side org code such as "TEST123"); creates one if missing, using the
 // human-readable organization name from the packet.
diff --git a/connectors/ACCESS/AMIE-Processor/handler/request_account_create.go 
b/connectors/ACCESS/AMIE-Processor/handler/request_account_create.go
index d40a7fd20..ac5845f38 100644
--- a/connectors/ACCESS/AMIE-Processor/handler/request_account_create.go
+++ b/connectors/ACCESS/AMIE-Processor/handler/request_account_create.go
@@ -161,28 +161,9 @@ func (h *RequestAccountCreateHandler) ensureUser(ctx 
context.Context, body map[s
        if err != nil {
                return nil, fmt.Errorf("ensure user organization: %w", err)
        }
-       email, first, last := getString(body, "UserEmail"), getString(body, 
"UserFirstName"), getString(body, "UserLastName")
-       user, err := h.svc.GetUserByEmail(ctx, email)
-       switch {
-       case err == nil:
-               // A user added internally takes the organization and name from 
ACCESS.
-               user.OrganizationID, user.FirstName, user.LastName = org.ID, 
first, last
-               if err := h.svc.UpdateUser(ctx, user); err != nil {
-                       return nil, fmt.Errorf("update user: %w", err)
-               }
-
-       case errors.Is(err, service.ErrNotFound):
-               user, err = h.svc.CreateUser(ctx, &models.User{
-                       OrganizationID: org.ID,
-                       FirstName:      first,
-                       LastName:       last,
-                       Email:          email,
-                       Status:         models.UserPending,
-               })
-               if err != nil {
-                       return nil, fmt.Errorf("create user: %w", err)
-               }
-       default:
+       email := getString(body, "UserEmail")
+       user, err := ensureUserByEmail(ctx, h.svc, org.ID, email, 
getString(body, "UserFirstName"), getString(body, "UserLastName"))
+       if err != nil {
                return nil, err
        }
        if _, err := h.svc.CreateUserIdentity(ctx, &models.UserIdentity{
diff --git a/connectors/ACCESS/AMIE-Processor/handler/request_project_create.go 
b/connectors/ACCESS/AMIE-Processor/handler/request_project_create.go
index 2a9277d9e..5829b87cb 100644
--- a/connectors/ACCESS/AMIE-Processor/handler/request_project_create.go
+++ b/connectors/ACCESS/AMIE-Processor/handler/request_project_create.go
@@ -168,15 +168,9 @@ func (h *RequestProjectCreateHandler) ensurePIUser(ctx 
context.Context, body map
                return nil, fmt.Errorf("ensure PI organization: %w", err)
        }
        email := getString(body, "PiEmail")
-       user, err := h.svc.CreateUser(ctx, &models.User{
-               OrganizationID: org.ID,
-               FirstName:      getString(body, "PiFirstName"),
-               LastName:       getString(body, "PiLastName"),
-               Email:          email,
-               Status:         models.UserPending,
-       })
+       user, err := ensureUserByEmail(ctx, h.svc, org.ID, email, 
getString(body, "PiFirstName"), getString(body, "PiLastName"))
        if err != nil {
-               return nil, fmt.Errorf("create PI user: %w", err)
+               return nil, err
        }
        if _, err := h.svc.CreateUserIdentity(ctx, &models.UserIdentity{
                UserID:     user.ID,
diff --git 
a/connectors/ACCESS/AMIE-Processor/handler/request_project_create_integration_test.go
 
b/connectors/ACCESS/AMIE-Processor/handler/request_project_create_integration_test.go
index a35739134..1c36fc242 100644
--- 
a/connectors/ACCESS/AMIE-Processor/handler/request_project_create_integration_test.go
+++ 
b/connectors/ACCESS/AMIE-Processor/handler/request_project_create_integration_test.go
@@ -27,6 +27,7 @@ import (
        "testing"
 
        
"github.com/apache/airavata-custos/connectors/ACCESS/AMIE-Processor/model"
+       "github.com/apache/airavata-custos/internal/tracing/tracingtest"
        "github.com/apache/airavata-custos/pkg/models"
        coreservice "github.com/apache/airavata-custos/pkg/service"
        "github.com/jmoiron/sqlx"
@@ -442,6 +443,39 @@ func TestRequestProjectCreate_NegativeTransferDeducts(t 
*testing.T) {
        }
 }
 
+// Make sure a project create links a PI who already exists with the same 
email,
+// taking the ACCESS organization and name rather than failing with the 
duplicate entry error.
+func TestRequestProjectCreate_LinksExistingPIByEmail(t *testing.T) {
+       database := setupTestDB(t)
+       svc := newTestCoreService(database)
+       ctx := tracingtest.Context()
+       body := baseRPCBody()
+       org, err := svc.CreateOrganization(ctx, &models.Organization{Name: 
"Placeholder Org"})
+       if err != nil {
+               t.Fatalf("create org: %v", err)
+       }
+       existing, err := svc.CreateUser(ctx, &models.User{OrganizationID: 
org.ID, FirstName: "Patty", Email: body["PiEmail"].(string), Status: 
models.UserActive})
+       if err != nil {
+               t.Fatalf("create user: %v", err)
+       }
+
+       pkt := insertPacket(t, database, "request_project_create", body)
+       h := NewRequestProjectCreateHandler(svc, testClusterID, 
&fakeAmieClient{}, newTestAuditService(database), nil)
+       if err := runHandlerInTx(t, database, func(ctx context.Context, tx 
*sql.Tx) error {
+               return h.Handle(ctx, tx, map[string]any{"type": pkt.Type, 
"body": body}, pkt, "")
+       }); err != nil {
+               t.Fatalf("Handle returned error: %v", err)
+       }
+
+       linked, err := svc.GetUserByUserIdentity(ctx, "access", "bl-pi-001")
+       if err != nil || linked.ID != existing.ID {
+               t.Fatalf("access identity: got %+v, %v, want it on user %s", 
linked, err, existing.ID)
+       }
+       if linked.FirstName != "Pat" || linked.LastName != "First" || 
linked.OrganizationID == org.ID {
+               t.Errorf("user: got %+v, want the ACCESS name and 
organization", linked)
+       }
+}
+
 // Make sure a project packet whose PI has no email is rejected as invalid and 
creates nothing.
 func TestRequestProjectCreate_MissingPIEmailIsInvalid(t *testing.T) {
        database := setupTestDB(t)

Reply via email to