Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creates Initiative Owner when creating an Initiative #161

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/server/pactasrv/pactasrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type DB interface {
PortfolioSnapshots(tx db.Tx, ids []pacta.PortfolioSnapshotID) (map[pacta.PortfolioSnapshotID]*pacta.PortfolioSnapshot, error)

GetOwnerForUser(tx db.Tx, uID pacta.UserID) (pacta.OwnerID, error)
GetOwnerForInitiative(tx db.Tx, iID pacta.InitiativeID) (pacta.OwnerID, error)

PortfolioGroup(tx db.Tx, id pacta.PortfolioGroupID) (*pacta.PortfolioGroup, error)
PortfolioGroupsByOwner(tx db.Tx, owner pacta.OwnerID) ([]*pacta.PortfolioGroup, error)
Expand Down
28 changes: 17 additions & 11 deletions db/sqldb/initiative.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,22 @@ func (d *DB) CreateInitiative(tx db.Tx, i *pacta.Initiative) error {
if err := validateInitiativeForCreation(i); err != nil {
return fmt.Errorf("validating initiative for creation: %w", err)
}
err := d.exec(tx, `
INSERT INTO initiative
(id, name, affiliation, public_description, internal_description, requires_invitation_to_join, is_accepting_new_members, is_accepting_new_portfolios, pacta_version_id, language)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);`,
i.ID, i.Name, i.Affiliation, i.PublicDescription, i.InternalDescription, i.RequiresInvitationToJoin, i.IsAcceptingNewMembers, i.IsAcceptingNewPortfolios, i.PACTAVersion.ID, i.Language)
if err != nil {
return fmt.Errorf("creating initiative: %w", err)
}
return nil
return d.RunOrContinueTransaction(tx, func(tx db.Tx) error {
err := d.exec(tx, `
INSERT INTO initiative
(id, name, affiliation, public_description, internal_description, requires_invitation_to_join, is_accepting_new_members, is_accepting_new_portfolios, pacta_version_id, language)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);`,
i.ID, i.Name, i.Affiliation, i.PublicDescription, i.InternalDescription, i.RequiresInvitationToJoin, i.IsAcceptingNewMembers, i.IsAcceptingNewPortfolios, i.PACTAVersion.ID, i.Language)
if err != nil {
return fmt.Errorf("creating initiative: %w", err)
}
_, err = d.createOwner(tx, &pacta.Owner{Initiative: &pacta.Initiative{ID: i.ID}})
if err != nil {
return fmt.Errorf("creating owner: %w", err)
}
return nil
})
}

func (d *DB) UpdateInitiative(tx db.Tx, id pacta.InitiativeID, mutations ...db.UpdateInitiativeFn) error {
Expand Down Expand Up @@ -117,7 +123,7 @@ func (d *DB) UpdateInitiative(tx db.Tx, id pacta.InitiativeID, mutations ...db.U
func (d *DB) DeleteInitiative(tx db.Tx, id pacta.InitiativeID) ([]pacta.BlobURI, error) {
buris := []pacta.BlobURI{}
err := d.RunOrContinueTransaction(tx, func(db.Tx) error {
owner, err := d.GetOrCreateOwnerForInitiative(tx, id)
owner, err := d.GetOwnerForInitiative(tx, id)
if err != nil {
return fmt.Errorf("getting owner for initiative: %w", err)
}
Expand Down
46 changes: 13 additions & 33 deletions db/sqldb/owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,45 +71,25 @@ func (d *DB) ownerByInitiative(tx db.Tx, id pacta.InitiativeID) (*pacta.Owner, e
}

func (d *DB) GetOwnerForUser(tx db.Tx, uID pacta.UserID) (pacta.OwnerID, error) {
var ownerID pacta.OwnerID
err := d.RunOrContinueTransaction(tx, func(tx db.Tx) error {
owner, err := d.ownerByUser(tx, uID)
if err == nil {
ownerID = owner.ID
return nil
}
if !db.IsNotFound(err) {
return fmt.Errorf("user owner not found: %w", err)
}
return fmt.Errorf("looking up owner: %w", err)
})
owner, err := d.ownerByUser(tx, uID)
if err != nil {
return "", fmt.Errorf("getting or creating owner for initiative: %w", err)
if db.IsNotFound(err) {
return "", db.NotFound(uID, "ownerByUserId")
}
return "", fmt.Errorf("error retrieving user owner: %w", err)
}
return ownerID, nil
return owner.ID, nil
}

func (d *DB) GetOrCreateOwnerForInitiative(tx db.Tx, iID pacta.InitiativeID) (pacta.OwnerID, error) {
var ownerID pacta.OwnerID
err := d.RunOrContinueTransaction(tx, func(tx db.Tx) error {
owner, err := d.ownerByInitiative(tx, iID)
if err == nil {
ownerID = owner.ID
return nil
}
if !db.IsNotFound(err) {
return fmt.Errorf("querying owner by user: %w", err)
}
ownerID, err = d.createOwner(tx, &pacta.Owner{Initiative: &pacta.Initiative{ID: iID}})
if err != nil {
return fmt.Errorf("creating owner: %w", err)
}
return nil
})
func (d *DB) GetOwnerForInitiative(tx db.Tx, iID pacta.InitiativeID) (pacta.OwnerID, error) {
owner, err := d.ownerByInitiative(tx, iID)
if err != nil {
return "", fmt.Errorf("getting or creating owner for initiative: %w", err)
if db.IsNotFound(err) {
return "", db.NotFound(iID, "ownerByInitiativeId")
}
return "", fmt.Errorf("error retrieving initiative owner: %w", err)
}
return ownerID, nil
return owner.ID, nil
}

func (d *DB) DeleteOwner(tx db.Tx, oID pacta.OwnerID) ([]pacta.BlobURI, error) {
Expand Down
6 changes: 3 additions & 3 deletions db/sqldb/owner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ func TestGetOrCreateOwners(t *testing.T) {
t.Fatalf("expected owner id %q, got %q", uo, uo2)
}

io, err := tdb.GetOrCreateOwnerForInitiative(tx, i.ID)
io, err := tdb.GetOwnerForInitiative(tx, i.ID)
if err != nil {
t.Fatalf("creating owner for initiative: %v", err)
}
io2, err := tdb.GetOrCreateOwnerForInitiative(tx, i.ID)
io2, err := tdb.GetOwnerForInitiative(tx, i.ID)
if err != nil {
t.Fatalf("creating owner for initiative: %v", err)
}
Expand Down Expand Up @@ -96,7 +96,7 @@ func ownerUserForTesting(t *testing.T, tdb *DB, u *pacta.User) *pacta.Owner {
func ownerInitiativeForTesting(t *testing.T, tdb *DB, i *pacta.Initiative) *pacta.Owner {
t.Helper()
tx := tdb.NoTxn(context.Background())
oid, err := tdb.GetOrCreateOwnerForInitiative(tx, i.ID)
oid, err := tdb.GetOwnerForInitiative(tx, i.ID)
if err != nil {
t.Fatalf("creating owner for user: %v", err)
}
Expand Down
Loading