This is an automated email from the ASF dual-hosted git repository. yasithdev pushed a commit to branch pi-email-link in repository https://gitbox.apache.org/repos/asf/airavata-custos.git
commit 7008807e5a3b9f7cd302ceed892441e0338711d6 Author: Yasith Jayawardana <[email protected]> AuthorDate: Tue Nov 10 22:47:18 2026 -0500 Link existing PIs by email --- .../handler/request_project_create.go | 32 +++++++++++++------- .../request_project_create_integration_test.go | 34 ++++++++++++++++++++++ 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/connectors/ACCESS/AMIE-Processor/handler/request_project_create.go b/connectors/ACCESS/AMIE-Processor/handler/request_project_create.go index 2a9277d9e..124624279 100644 --- a/connectors/ACCESS/AMIE-Processor/handler/request_project_create.go +++ b/connectors/ACCESS/AMIE-Processor/handler/request_project_create.go @@ -167,16 +167,28 @@ func (h *RequestProjectCreateHandler) ensurePIUser(ctx context.Context, body map if err != nil { 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, - }) - if err != nil { - return nil, fmt.Errorf("create PI user: %w", err) + email, first, last := getString(body, "PiEmail"), getString(body, "PiFirstName"), getString(body, "PiLastName") + 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 PI 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 PI 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_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)
