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

feat(SPV-1341): change delete paymail endpoint to be more restful #854

Merged
merged 4 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 3 additions & 12 deletions actions/admin/paymail_addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func paymailCreateAddress(c *gin.Context, _ *reqctx.AdminContext) {
// @Description Delete paymail
// @Tags Admin
// @Produce json
// @Param PaymailAddress body PaymailAddress false "PaymailAddress model containing paymail address to delete"
// @Param id path string true "id of the paymail"
// @Success 200
// @Failure 400 "Bad request - Error while parsing PaymailAddress from request body or if address is missing"
// @Failure 500 "Internal Server Error - Error while deleting paymail address"
Expand All @@ -169,21 +169,12 @@ func paymailCreateAddress(c *gin.Context, _ *reqctx.AdminContext) {
func paymailDeleteAddress(c *gin.Context, _ *reqctx.AdminContext) {
logger := reqctx.Logger(c)
engine := reqctx.Engine(c)
var requestBody PaymailAddress
if err := c.ShouldBindJSON(&requestBody); err != nil {
spverrors.ErrorResponse(c, spverrors.ErrCannotBindRequest.WithTrace(err), logger)
return
}

if requestBody.Address == "" {
spverrors.ErrorResponse(c, spverrors.ErrMissingAddress, logger)
return
}
id := c.Param("id")

opts := engine.DefaultModelOptions()

// Delete a new paymail address
err := engine.DeletePaymailAddress(c.Request.Context(), requestBody.Address, opts...)
err := engine.DeletePaymailAddressByID(c.Request.Context(), id, opts...)
if err != nil {
spverrors.ErrorResponse(c, spverrors.ErrDeletePaymailAddress.WithTrace(err), logger)
return
Expand Down
13 changes: 6 additions & 7 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,12 +462,11 @@ const docTemplate = `{
"summary": "Delete paymail",
"parameters": [
{
"description": "PaymailAddress model containing paymail address to delete",
"name": "PaymailAddress",
"in": "body",
"schema": {
"$ref": "#/definitions/admin.PaymailAddress"
}
"type": "string",
"description": "id of the paymail",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
Expand Down Expand Up @@ -4676,7 +4675,7 @@ const docTemplate = `{
"key2": "value2"
}
},
"public_name": {
"publicName": {
"description": "The public name of the paymail",
"type": "string",
"example": "Test"
Expand Down
13 changes: 6 additions & 7 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -453,12 +453,11 @@
"summary": "Delete paymail",
"parameters": [
{
"description": "PaymailAddress model containing paymail address to delete",
"name": "PaymailAddress",
"in": "body",
"schema": {
"$ref": "#/definitions/admin.PaymailAddress"
}
"type": "string",
"description": "id of the paymail",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
Expand Down Expand Up @@ -4667,7 +4666,7 @@
"key2": "value2"
}
},
"public_name": {
"publicName": {
"description": "The public name of the paymail",
"type": "string",
"example": "Test"
Expand Down
12 changes: 6 additions & 6 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ definitions:
key: value
key2: value2
type: object
public_name:
publicName:
description: The public name of the paymail
example: Test
type: string
Expand Down Expand Up @@ -3005,11 +3005,11 @@ paths:
delete:
description: Delete paymail
parameters:
- description: PaymailAddress model containing paymail address to delete
in: body
name: PaymailAddress
schema:
$ref: '#/definitions/admin.PaymailAddress'
- description: id of the paymail
in: path
name: id
required: true
type: string
produces:
- application/json
responses:
Expand Down
22 changes: 22 additions & 0 deletions engine/action_paymails.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,28 @@ func (c *Client) NewPaymailAddress(ctx context.Context, xPubKey, address, public
return paymailAddress, nil
}

// DeletePaymailAddressByID will delete a paymail address by its id
func (c *Client) DeletePaymailAddressByID(ctx context.Context, id string, opts ...ModelOps) error {

// Get the paymail address
paymailAddress, err := getPaymailAddressByID(ctx, id, append(opts, c.DefaultModelOptions()...)...)
if err != nil {
return err
} else if paymailAddress == nil {
return spverrors.ErrCouldNotFindPaymail
}

paymailAddress.DeletedAt.Valid = true
paymailAddress.DeletedAt.Time = time.Now()

tx := c.Datastore().DB().Save(&paymailAddress)
if tx.Error != nil {
return spverrors.ErrDeletePaymailAddress.Wrap(tx.Error)
}

return nil
}

// DeletePaymailAddress will delete a paymail address
func (c *Client) DeletePaymailAddress(ctx context.Context, address string, opts ...ModelOps) error {

Expand Down
52 changes: 52 additions & 0 deletions engine/action_paymails_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,58 @@ func (ts *EmbeddedDBTestSuite) TestClient_NewPaymailAddress() {
}
}

// Test_DeletePaymailAddressByID will test the method DeletePaymailAddressByID()
func (ts *EmbeddedDBTestSuite) Test_DeletePaymailAddressByID() {
dzolt-4chain marked this conversation as resolved.
Show resolved Hide resolved
for _, testCase := range dbTestCases {

ts.T().Run(testCase.name+" - empty", func(t *testing.T) {
tc := ts.genericDBClient(t, testCase.database, false)
defer tc.Close(tc.ctx)

id := ""
err := tc.client.DeletePaymailAddressByID(tc.ctx, id, tc.client.DefaultModelOptions()...)
require.ErrorIs(t, err, spverrors.ErrCouldNotFindPaymail)
})

ts.T().Run(testCase.name+" - delete unknown paymail address", func(t *testing.T) {
tc := ts.genericDBClient(t, testCase.database, false)
defer tc.Close(tc.ctx)

err := tc.client.DeletePaymailAddressByID(tc.ctx, "unknown-id", tc.client.DefaultModelOptions()...)
require.ErrorIs(t, err, spverrors.ErrCouldNotFindPaymail)
})

ts.T().Run(testCase.name+" - delete paymail address", func(t *testing.T) {
tc := ts.genericDBClient(t, testCase.database, false)
defer tc.Close(tc.ctx)
opts := tc.client.DefaultModelOptions()

// Create xPub (required to add a paymail address)
xPub, err := tc.client.NewXpub(tc.ctx, testXPub, opts...)
require.NotNil(t, xPub)
require.NoError(t, err)

var paymailAddress *PaymailAddress
paymailAddress, err = tc.client.NewPaymailAddress(tc.ctx, testXPub, testPaymail, testPublicName, testAvatar, opts...)
require.NoError(t, err)
require.NotNil(t, paymailAddress)

err = tc.client.DeletePaymailAddressByID(tc.ctx, paymailAddress.ID, opts...)
require.NoError(t, err)

var p2 *PaymailAddress
p2, err = getPaymailAddress(tc.ctx, testPaymail, opts...)
require.NoError(t, err)
require.Nil(t, p2)

var p3 *PaymailAddress
p3, err = getPaymailAddressByID(tc.ctx, paymailAddress.ID, opts...)
require.NoError(t, err)
require.Nil(t, p3)
})
}
}

// Test_DeletePaymailAddress will test the method DeletePaymailAddress()
func (ts *EmbeddedDBTestSuite) Test_DeletePaymailAddress() {
for _, testCase := range dbTestCases {
Expand Down
1 change: 1 addition & 0 deletions engine/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type ModelService interface {
// PaymailService is the paymail actions & services
type PaymailService interface {
DeletePaymailAddress(ctx context.Context, address string, opts ...ModelOps) error
DeletePaymailAddressByID(ctx context.Context, id string, opts ...ModelOps) error
GetPaymailConfig() *PaymailServerOptions
GetPaymailAddress(ctx context.Context, address string, opts ...ModelOps) (*PaymailAddress, error)
GetPaymailAddressByID(ctx context.Context, id string, opts ...ModelOps) (*PaymailAddress, error)
Expand Down
Loading