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

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

commit f249518873912b4aba8fecd409c8c9ab2ee3593b
Author: lahiruj <[email protected]>
AuthorDate: Sat Nov 7 02:18:16 2026 -0500

    Link existing users by email during ACCESS account create
---
 .../handler/request_account_create.go              | 33 ++++++++++++------
 .../request_account_create_integration_test.go     | 40 ++++++++++++++++++++++
 2 files changed, 63 insertions(+), 10 deletions(-)

diff --git a/connectors/ACCESS/AMIE-Processor/handler/request_account_create.go 
b/connectors/ACCESS/AMIE-Processor/handler/request_account_create.go
index 84f060605..d40a7fd20 100644
--- a/connectors/ACCESS/AMIE-Processor/handler/request_account_create.go
+++ b/connectors/ACCESS/AMIE-Processor/handler/request_account_create.go
@@ -161,16 +161,29 @@ func (h *RequestAccountCreateHandler) ensureUser(ctx 
context.Context, body map[s
        if err != nil {
                return nil, fmt.Errorf("ensure user organization: %w", err)
        }
-       email := getString(body, "UserEmail")
-       user, err := h.svc.CreateUser(ctx, &models.User{
-               OrganizationID: org.ID,
-               FirstName:      getString(body, "UserFirstName"),
-               LastName:       getString(body, "UserLastName"),
-               Email:          email,
-               Status:         models.UserPending,
-       })
-       if err != nil {
-               return nil, fmt.Errorf("create user: %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:
+               return nil, err
        }
        if _, err := h.svc.CreateUserIdentity(ctx, &models.UserIdentity{
                UserID:     user.ID,
diff --git 
a/connectors/ACCESS/AMIE-Processor/handler/request_account_create_integration_test.go
 
b/connectors/ACCESS/AMIE-Processor/handler/request_account_create_integration_test.go
index 50dd550d6..55b868b70 100644
--- 
a/connectors/ACCESS/AMIE-Processor/handler/request_account_create_integration_test.go
+++ 
b/connectors/ACCESS/AMIE-Processor/handler/request_account_create_integration_test.go
@@ -30,6 +30,8 @@ import (
        "github.com/jmoiron/sqlx"
 
        
"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"
 )
 
 // baseRACBody returns a fully populated request_account_create body with
@@ -439,6 +441,44 @@ func TestRequestAccountCreate_ForThePI(t *testing.T) {
        }
 }
 
+// Make sure an account create links a user who already exists with the same 
email,
+// taking the ACCESS organization and name rather than failing with the 
duplicate entry error.
+func TestRequestAccountCreate_LinksExistingUserByEmail(t *testing.T) {
+       database := setupTestDB(t)
+       projectID := seedProjectForRAC(t, database)
+       svc := newTestCoreService(database)
+       ctx := tracingtest.Context()
+       body := baseRACBody()
+       body["ProjectID"] = projectID
+       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: "Sammy", Email: body["UserEmail"].(string), Status: 
models.UserActive})
+       if err != nil {
+               t.Fatalf("create user: %v", err)
+       }
+
+       pkt := insertPacket(t, database, "request_account_create", body)
+       h := NewRequestAccountCreateHandler(svc, testClusterID, 
&fakeAmieClient{}, newTestAuditService(database))
+       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-user-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 != "Sam" || linked.LastName != "Userman" || 
linked.OrganizationID == org.ID {
+               t.Errorf("user: got %+v, want the ACCESS name and 
organization", linked)
+       }
+       if got := countRows(t, database, "compute_allocation_memberships"); got 
!= 2 {
+               t.Errorf("memberships: got %d, want 2 (PI and this user)", got)
+       }
+}
+
 // TestRequestAccountCreate_MissingRequiredField asserts the handler rejects a
 // packet missing a required field, without leaving partial state behind.
 // GrantNumber is one of the required fields.

Reply via email to