From 784775fdf9240ca66c9c2f0ac7a09855e6582ff5 Mon Sep 17 00:00:00 2001 From: gearnode Date: Thu, 6 Feb 2025 10:49:56 -0800 Subject: [PATCH] Add create people Signed-off-by: gearnode --- pkg/probo/coredata/people.go | 39 ++++++++++++++++++ pkg/probo/create_people.go | 77 ++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 pkg/probo/create_people.go diff --git a/pkg/probo/coredata/people.go b/pkg/probo/coredata/people.go index 6d6220cad..0172ea175 100644 --- a/pkg/probo/coredata/people.go +++ b/pkg/probo/coredata/people.go @@ -97,6 +97,45 @@ LIMIT 1; return nil } +func (p People) Insert( + ctx context.Context, + conn pg.Conn, +) error { + q := ` +INSERT INTO + peoples ( + id, + organization_id, + full_name, + primary_email_address, + additional_email_addresses, + created_at, + updated_at + ) +VALUES ( + @people_id, + @organization_id, + @full_name, + @primary_email_address, + @additional_email_addresses, + @created_at, + @updated_at +) +` + + args := pgx.NamedArgs{ + "people_id": p.ID, + "organization_id": p.OrganizationID, + "full_name": p.FullName, + "primary_email_address": p.PrimaryEmailAddress, + "additional_email_addresses": p.AdditionalEmailAddresses, + "created_at": p.CreatedAt, + "updated_at": p.UpdatedAt, + } + _, err := conn.Exec(ctx, q, args) + return err +} + func (p *Peoples) LoadByOrganizationID( ctx context.Context, conn pg.Conn, diff --git a/pkg/probo/create_people.go b/pkg/probo/create_people.go new file mode 100644 index 000000000..d3d87a769 --- /dev/null +++ b/pkg/probo/create_people.go @@ -0,0 +1,77 @@ +// Copyright (c) 2025 Probo Inc . +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +// PERFORMANCE OF THIS SOFTWARE. + +package probo + +import ( + "context" + "fmt" + "time" + + "github.com/getprobo/probo/pkg/gid" + "github.com/getprobo/probo/pkg/probo/coredata" + "go.gearno.de/kit/pg" +) + +type ( + CreatePeopleRequest struct { + OrganizationID gid.GID + FullName string + PrimaryEmailAddress string + AdditionalEmailAddresses []string + } +) + +func (s Service) CreatePeople( + ctx context.Context, + req CreatePeopleRequest, +) (*coredata.People, error) { + now := time.Now() + peopleID, err := gid.NewGID(coredata.PeopleEntityType) + if err != nil { + return nil, fmt.Errorf("cannot create people global id: %w", err) + } + + organization := &coredata.Organization{} + people := &coredata.People{ + ID: peopleID, + OrganizationID: req.OrganizationID, + FullName: req.FullName, + PrimaryEmailAddress: req.PrimaryEmailAddress, + AdditionalEmailAddresses: req.AdditionalEmailAddresses, + CreatedAt: now, + UpdatedAt: now, + } + + err = s.pg.WithTx( + ctx, + func(conn pg.Conn) error { + if err := organization.LoadByID(ctx, conn, s.scope, req.OrganizationID); err != nil { + return fmt.Errorf("cannot load organization %q: %w", req.OrganizationID, err) + } + + if err := people.Insert(ctx, conn); err != nil { + return fmt.Errorf("cannot insert people: %w", err) + } + + return nil + }, + ) + + if err != nil { + return nil, err + } + + return people, nil +}