-
Notifications
You must be signed in to change notification settings - Fork 6
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
feat: add MarkDelegationAsTransitioned method #163
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package v2dbclient | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/babylonlabs-io/staking-api-service/internal/shared/db" | ||
dbmodel "github.com/babylonlabs-io/staking-api-service/internal/shared/db/model" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
func (v2dbclient *V2Database) MarkV1DelegationAsTransitioned(ctx context.Context, stakingTxHashHex string) error { | ||
session, err := v2dbclient.Client.StartSession() | ||
if err != nil { | ||
return err | ||
} | ||
defer session.EndSession(ctx) | ||
|
||
transactionWork := func(sessCtx mongo.SessionContext) (interface{}, error) { | ||
client := v2dbclient.Client.Database(v2dbclient.DbName).Collection(dbmodel.V1DelegationCollection) | ||
filter := bson.M{"_id": stakingTxHashHex} | ||
|
||
var delegation interface{} | ||
err := client.FindOne(sessCtx, filter).Decode(&delegation) | ||
if err != nil { | ||
if errors.Is(err, mongo.ErrNoDocuments) { | ||
return nil, &db.NotFoundError{ | ||
Key: stakingTxHashHex, | ||
Message: "Delegation not found", | ||
} | ||
} | ||
return nil, err | ||
} | ||
|
||
update := bson.M{"$set": bson.M{"transitioned": true}} | ||
result, err := client.UpdateOne(sessCtx, filter, update) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if result.MatchedCount == 0 { | ||
return nil, &db.NotFoundError{ | ||
Key: stakingTxHashHex, | ||
Message: "Delegation not found", | ||
} | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
_, err = session.WithTransaction(ctx, transactionWork) | ||
return err | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wont' this be
is_transitioned
as per the model?