Skip to content

Commit

Permalink
review: align context reasons split and convert
Browse files Browse the repository at this point in the history
  • Loading branch information
Varsius committed Feb 26, 2025
1 parent a9684f9 commit 614d361
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
29 changes: 25 additions & 4 deletions internal/api/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,9 @@ func (p *v1Provider) DeleteProjectCommitment(w http.ResponseWriter, r *http.Requ
func (p *v1Provider) canDeleteCommitment(token *gopherpolicy.Token, commitment db.ProjectCommitment) bool {
// up to 24 hours after creation of fresh commitments, future commitments can still be deleted by their creators
if commitment.State == db.CommitmentStatePlanned || commitment.State == db.CommitmentStatePending || commitment.State == db.CommitmentStateActive {
if p.timeNow().Before(commitment.CreatedAt.Add(24 * time.Hour)) {
var creationContext db.CommitmentWorkflowContext
err := json.Unmarshal(commitment.CreationContextJSON, &creationContext)
if err == nil && creationContext.Reason == db.CommitmentReasonCreate && p.timeNow().Before(commitment.CreatedAt.Add(24*time.Hour)) {
if token.Check("project:edit") {
return true
}
Expand Down Expand Up @@ -861,9 +863,28 @@ func (p *v1Provider) buildSplitCommitment(dbCommitment db.ProjectCommitment, amo
}

func (p *v1Provider) buildConvertedCommitment(dbCommitment db.ProjectCommitment, azResourceID db.ProjectAZResourceID, amount uint64) (db.ProjectCommitment, error) {
commitment, err := p.buildSplitCommitment(dbCommitment, amount)
commitment.AZResourceID = azResourceID
return commitment, err
now := p.timeNow()
creationContext := db.CommitmentWorkflowContext{
Reason: db.CommitmentReasonConvert,
RelatedCommitmentIDs: []db.ProjectCommitmentID{dbCommitment.ID},
}
buf, err := json.Marshal(creationContext)
if err != nil {
return db.ProjectCommitment{}, err
}
return db.ProjectCommitment{
AZResourceID: azResourceID,
Amount: amount,
Duration: dbCommitment.Duration,
CreatedAt: now,
CreatorUUID: dbCommitment.CreatorUUID,
CreatorName: dbCommitment.CreatorName,
ConfirmBy: dbCommitment.ConfirmBy,
ConfirmedAt: dbCommitment.ConfirmedAt,
ExpiresAt: dbCommitment.ExpiresAt,
CreationContextJSON: json.RawMessage(buf),
State: dbCommitment.State,
}, nil
}

// GetCommitmentByTransferToken handles GET /v1/commitments/{token}
Expand Down
7 changes: 5 additions & 2 deletions internal/collector/commitment_cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ func TestCleanupOldCommitmentsJob(t *testing.T) {
tr.DBChanges().AssertEqualf(`DELETE FROM project_commitments WHERE id = 2 AND transfer_token = NULL;`)

// test 2: simulate a commitment that was created yesterday,
// and then moved to a different project five minutes later
// and then converted five minutes later
creationContext = db.CommitmentWorkflowContext{Reason: db.CommitmentReasonCreate}
buf, err = json.Marshal(creationContext)
mustT(t, err)
supersedeContext := db.CommitmentWorkflowContext{Reason: db.CommitmentReasonSplit, RelatedCommitmentIDs: []db.ProjectCommitmentID{4}}
supersedeContext := db.CommitmentWorkflowContext{Reason: db.CommitmentReasonConvert, RelatedCommitmentIDs: []db.ProjectCommitmentID{4}}
supersedeBuf, err := json.Marshal(supersedeContext)
mustT(t, err)
mustT(t, c.DB.Insert(&db.ProjectCommitment{
Expand All @@ -136,6 +136,9 @@ func TestCleanupOldCommitmentsJob(t *testing.T) {
CreationContextJSON: json.RawMessage(buf),
SupersedeContextJSON: pointerTo(json.RawMessage(supersedeBuf)),
}))
creationContext = db.CommitmentWorkflowContext{Reason: db.CommitmentReasonConvert, RelatedCommitmentIDs: []db.ProjectCommitmentID{3}}
buf, err = json.Marshal(creationContext)
mustT(t, err)
mustT(t, c.DB.Insert(&db.ProjectCommitment{
ID: 4,
AZResourceID: 2,
Expand Down

0 comments on commit 614d361

Please sign in to comment.