From 3cafca6cbd9f1508638cc71f03852158194eb13d Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Tue, 19 May 2026 00:33:14 +0000 Subject: [PATCH] Set contact ID when creating a report For now we just set it to the empty contact, which is a bit weird, and wrong until we fix the update logic. --- db/query/comms/contact.go | 35 +++++++++++++++++++++++++++++++++++ platform/contact.go | 16 ++++++++++++++++ platform/publicreport.go | 5 +++++ 3 files changed, 56 insertions(+) diff --git a/db/query/comms/contact.go b/db/query/comms/contact.go index 35c852ff..fec46300 100644 --- a/db/query/comms/contact.go +++ b/db/query/comms/contact.go @@ -2,6 +2,8 @@ package comms import ( "context" + "errors" + "fmt" //"github.com/Gleipnir-Technology/bob" "github.com/Gleipnir-Technology/nidus-sync/db" @@ -18,6 +20,39 @@ func ContactInsert(ctx context.Context, txn db.Ex, m model.Contact) (model.Conta return db.ExecuteOneTx[model.Contact](ctx, txn, statement) } +func ContactEmptyForOrganization(ctx context.Context, txn db.Ex, org_id int64) (*model.Contact, error) { + statement := table.Contact.SELECT( + table.Contact.AllColumns, + ).FROM(table.Contact). + WHERE( + postgres.AND( + table.Contact.OrganizationID.EQ(postgres.Int(org_id)), + table.Contact.Name.EQ(postgres.String("")), + postgres.NOT( + postgres.EXISTS( + postgres.SELECT( + postgres.Int(1), + ).FROM(table.ContactEmail). + WHERE(table.ContactEmail.ContactID.EQ(table.Contact.ID)), + )), + postgres.NOT( + postgres.EXISTS( + postgres.SELECT( + postgres.Int(1), + ).FROM(table.ContactPhone). + WHERE(table.ContactPhone.ContactID.EQ(table.Contact.ID)), + )), + ), + ) + row, err := db.ExecuteOne[model.Contact](ctx, statement) + if err != nil { + if errors.Is(err, db.ErrNoRows) { + return nil, nil + } + return nil, fmt.Errorf("query contact: %w", err) + } + return &row, nil +} func ContactFromID(ctx context.Context, txn db.Ex, id int64) (model.Contact, error) { statement := table.Contact.SELECT( table.Contact.AllColumns, diff --git a/platform/contact.go b/platform/contact.go index 946bbf35..d8775bfe 100644 --- a/platform/contact.go +++ b/platform/contact.go @@ -3,8 +3,10 @@ package platform import ( "context" "fmt" + "time" "github.com/Gleipnir-Technology/nidus-sync/db" + modelcomms "github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/comms/model" querycomms "github.com/Gleipnir-Technology/nidus-sync/db/query/comms" "github.com/Gleipnir-Technology/nidus-sync/platform/types" ) @@ -58,3 +60,17 @@ func ContactsForOrganization(ctx context.Context, org_id int32) (results []types } return results, nil } +func ContactEmptyForOrganization(ctx context.Context, txn db.Ex, org_id int32) (modelcomms.Contact, error) { + contact, err := querycomms.ContactEmptyForOrganization(ctx, txn, int64(org_id)) + if err != nil { + return modelcomms.Contact{}, fmt.Errorf("querycomms: %w", err) + } + if contact != nil { + return *contact, nil + } + return querycomms.ContactInsert(ctx, txn, modelcomms.Contact{ + Created: time.Now(), + Name: "", + OrganizationID: org_id, + }) +} diff --git a/platform/publicreport.go b/platform/publicreport.go index 9e90cbab..0887c56b 100644 --- a/platform/publicreport.go +++ b/platform/publicreport.go @@ -379,6 +379,11 @@ func publicReportCreate(ctx context.Context, setter_report modelpublicreport.Rep } setter_report.OrganizationID = organization_id + contact, err := ContactEmptyForOrganization(ctx, txn, organization_id) + if err != nil { + return result, fmt.Errorf("contact empty: %w", err) + } + setter_report.ReporterContactID = &contact.ID if addr != nil { setter_report.AddressID = addr.ID }