diff --git a/CHANGELOG.md b/CHANGELOG.md index ee49cb1..8f1ef05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ Given a version number MAJOR.MINOR.PATCH, increment: ## [Unreleased] +### Added +- update function to Deposit resource ## [0.4.2] - 2023-12-15 ### Changed diff --git a/README.md b/README.md index c8899f0..87604b6 100644 --- a/README.md +++ b/README.md @@ -1445,6 +1445,39 @@ func main() { ``` +## Update a Deposit + +Update a deposit by passing its id to be partially or fully reversed. + +```golang +package main + +import ( + "fmt" + "github.com/starkbank/sdk-go/starkbank" + Deposit "github.com/starkbank/sdk-go/starkbank/deposit" + "github.com/starkbank/sdk-go/tests/utils" +) + +func main() { + + starkbank.User = utils.ExampleProject + + var patchData = map[string]interface{}{} + patchData["amount"] = 0 + + deposit, err := Deposit.Update("5155165527080960", patchData, nil) + if err.Errors != nil { + for _, e := range err.Errors { + panic(fmt.Sprintf("code: %s, message: %s", e.Code, e.Message)) + } + } + + fmt.Println(deposit) +} + +``` + ## Query deposit logs Logs are pretty important to understand the life cycle of a deposit. diff --git a/starkbank/deposit/deposit.go b/starkbank/deposit/deposit.go index 5b93290..6c0e533 100644 --- a/starkbank/deposit/deposit.go +++ b/starkbank/deposit/deposit.go @@ -135,3 +135,27 @@ func Page(params map[string]interface{}, user user.User) ([]Deposit, string, Err } return deposit, cursor, err } + +func Update(id string, patchData map[string]interface{}, user user.User) (Deposit, Error.StarkErrors) { + // Update Deposit entity + // + // Update the Deposit by passing its id to be partially or fully reversed. + // + // Parameters (required): + // - patchData [map[string]interface{}]: map containing the attributes to be updated. ex: map[string]interface{}{"amount": 9090} + // Parameters (optional): + // - amount [string]: The new amount of the Deposit. If the amount = 0 the Deposit will be fully reversed + // + // Parameters (optional): + // - user [Organization/Project struct, default nil]: Organization or Project struct. Not necessary if starkbank.User was set before function call + // + // Return: + // - Target Deposit with updated attributes + update, err := utils.Patch(resource, id, patchData, user) + unmarshalError := json.Unmarshal(update, &object) + if unmarshalError != nil { + return object, err + } + return object, err +} + diff --git a/tests/sdk/deposit_test.go b/tests/sdk/deposit_test.go index ca11ed2..fd97829 100644 --- a/tests/sdk/deposit_test.go +++ b/tests/sdk/deposit_test.go @@ -50,6 +50,33 @@ func TestDepositQuery(t *testing.T) { assert.Equal(t, 201, i) } +func TestDepositUpdate(t *testing.T) { + + starkbank.User = Utils.ExampleProject + + var depositList []Deposit.Deposit + var params = map[string]interface{}{} + params["limit"] = 1 + params["status"] = "created" + + deposits := Deposit.Query(params, nil) + for deposit := range deposits { + depositList = append(depositList, deposit) + } + + var patchData = map[string]interface{}{} + patchData["amount"] = 0 + + updated, err := Deposit.Update(depositList[rand.Intn(len(depositList))].Id, patchData, nil) + if err.Errors != nil { + for _, e := range err.Errors { + panic(fmt.Sprintf("code: %s, message: %s", e.Code, e.Message)) + } + } + + assert.Equal(t, updated.Amount, patchData["amount"]) +} + func TestDepositPage(t *testing.T) { starkbank.User = Utils.ExampleProject