Skip to content

Commit

Permalink
Merge pull request #16 from gobicycle/withdrawal_meta
Browse files Browse the repository at this point in the history
Withdrawal meta
  • Loading branch information
gobicycle authored May 21, 2024
2 parents 356ac1e + 0263618 commit 33ef176
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 147 deletions.
15 changes: 11 additions & 4 deletions api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/gobicycle/bicycle/config"
"github.com/gobicycle/bicycle/core"
Expand Down Expand Up @@ -57,7 +58,9 @@ type WithdrawalResponse struct {
}

type WithdrawalStatusResponse struct {
Status core.WithdrawalStatus `json:"status"`
UserID string `json:"user_id"`
QueryID string `json:"query_id"`
Status core.WithdrawalStatus `json:"status"`
}

type GetIncomeResponse struct {
Expand Down Expand Up @@ -208,7 +211,7 @@ func (h *Handler) getWithdrawalStatus(resp http.ResponseWriter, req *http.Reques
return
}
status, err := h.storage.GetExternalWithdrawalStatus(req.Context(), id)
if err == core.ErrNotFound {
if errors.Is(err, core.ErrNotFound) {
writeHttpError(resp, http.StatusBadRequest, "request ID not found")
return
}
Expand All @@ -218,7 +221,11 @@ func (h *Handler) getWithdrawalStatus(resp http.ResponseWriter, req *http.Reques
}
resp.Header().Add("Content-Type", "application/json")
resp.WriteHeader(http.StatusOK)
err = json.NewEncoder(resp).Encode(WithdrawalStatusResponse{Status: status})
err = json.NewEncoder(resp).Encode(WithdrawalStatusResponse{
UserID: status.UserID,
QueryID: status.QueryID,
Status: status.Status,
})
if err != nil {
log.Errorf("json encode error: %v", err)
}
Expand Down Expand Up @@ -612,7 +619,7 @@ type storage interface {
SaveWithdrawalRequest(ctx context.Context, w core.WithdrawalRequest) (int64, error)
IsWithdrawalRequestUnique(ctx context.Context, w core.WithdrawalRequest) (bool, error)
IsActualBlockData(ctx context.Context) (bool, error)
GetExternalWithdrawalStatus(ctx context.Context, id int64) (core.WithdrawalStatus, error)
GetExternalWithdrawalStatus(ctx context.Context, id int64) (core.WithdrawalData, error)
GetWalletType(address core.Address) (core.WalletType, bool)
GetIncome(ctx context.Context, userID string, isDepositSide bool) ([]core.TotalIncome, error)
SaveServiceWithdrawalRequest(ctx context.Context, w core.ServiceWithdrawalRequest) (uuid.UUID, error)
Expand Down
6 changes: 6 additions & 0 deletions core/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ type WithdrawalRequest struct {
Comment string
}

type WithdrawalData struct {
QueryID string
UserID string
Status WithdrawalStatus
}

type ServiceWithdrawalRequest struct {
From Address
JettonMaster *Address
Expand Down
26 changes: 16 additions & 10 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1047,28 +1047,34 @@ func (c *Connection) IsInProgressInternalWithdrawalRequest(
return true, nil
}

func (c *Connection) GetExternalWithdrawalStatus(ctx context.Context, id int64) (core.WithdrawalStatus, error) {
var processing, processed bool
func (c *Connection) GetExternalWithdrawalStatus(ctx context.Context, id int64) (core.WithdrawalData, error) {
var (
processing, processed bool
data core.WithdrawalData
)
err := c.client.QueryRow(ctx, `
SELECT processing, processed
SELECT processing, processed, user_id, user_query_id
FROM payments.withdrawal_requests
WHERE query_id = $1 AND is_internal = false
LIMIT 1
`, id).Scan(&processing, &processed)
`, id).Scan(&processing, &processed, &data.UserID, &data.QueryID)
if errors.Is(err, pgx.ErrNoRows) {
return "", core.ErrNotFound
return core.WithdrawalData{}, core.ErrNotFound
}
if err != nil {
return "", err
return core.WithdrawalData{}, err
}
if processing && processed {
return core.ProcessedStatus, nil
data.Status = core.ProcessedStatus
return data, nil
} else if processing && !processed {
return core.ProcessingStatus, nil
data.Status = core.ProcessingStatus
return data, nil
} else if !processing && !processed {
return core.PendingStatus, nil
data.Status = core.PendingStatus
return data, nil
}
return "", fmt.Errorf("bad status")
return core.WithdrawalData{}, fmt.Errorf("bad status")
}

// GetIncome returns list of incomes by user_id
Expand Down
4 changes: 3 additions & 1 deletion docs/api.apib
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ Returns `memo` as comment for transfer message.

## Get withdrawal status [GET /v1/withdrawal/status{?id}]

Get withdrawal status. Returns `pending`, `processing`, `processed`
Get withdrawal status. Returns `pending`, `processing`, `processed` and request meta (user_id and query_id).

+ Parameters

Expand All @@ -356,6 +356,8 @@ Get withdrawal status. Returns `pending`, `processing`, `processed`
+ Body

{
"user_id": "123",
"query_id": "321",
"status": "pending"
}

Expand Down
6 changes: 4 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ <h4>Example URI</h4><div class="definition"><span class="method post">POST</span
"<span class="hljs-attribute">error</span>": <span class="hljs-value"><span class="hljs-string">"error text"</span>
</span>}</code></pre><div style="height: 1px;"></div></div></div><div class="title"><strong>Response&nbsp;&nbsp;<code>401</code></strong></div><div class="title"><strong>Response&nbsp;&nbsp;<code>500</code></strong><div class="collapse-button"><span class="close">Hide</span><span class="open">Show</span></div></div><div class="collapse-content"><div class="inner"><h5>Headers</h5><pre><code><span class="hljs-attribute">Content-Type</span>: <span class="hljs-string">application/json</span></code></pre><div style="height: 1px;"></div><h5>Body</h5><pre><code>{
"<span class="hljs-attribute">error</span>": <span class="hljs-value"><span class="hljs-string">"error text"</span>
</span>}</code></pre><div style="height: 1px;"></div></div></div></div></div><div id="get-withdrawal-status" class="resource"><h3 class="resource-heading">Get withdrawal status <a href="#get-withdrawal-status" class="permalink">&nbsp;&para;</a></h3><div id="get-withdrawal-status-get" class="action get"><h4 class="action-heading"><div class="name">Get withdrawal status</div><a href="#get-withdrawal-status-get" class="method get">GET</a><code class="uri">/v1/withdrawal/status{?id}</code></h4><p>Get withdrawal status. Returns <code>pending</code>, <code>processing</code>, <code>processed</code></p>
</span>}</code></pre><div style="height: 1px;"></div></div></div></div></div><div id="get-withdrawal-status" class="resource"><h3 class="resource-heading">Get withdrawal status <a href="#get-withdrawal-status" class="permalink">&nbsp;&para;</a></h3><div id="get-withdrawal-status-get" class="action get"><h4 class="action-heading"><div class="name">Get withdrawal status</div><a href="#get-withdrawal-status-get" class="method get">GET</a><code class="uri">/v1/withdrawal/status{?id}</code></h4><p>Get withdrawal status. Returns <code>pending</code>, <code>processing</code>, <code>processed</code> and request meta (user_id and query_id).</p>
<h4>Example URI</h4><div class="definition"><span class="method get">GET</span>&nbsp;<span class="uri"><span class="hostname"></span>/v1/withdrawal/status?<span class="hljs-attribute">id=</span><span class="hljs-literal"></span></span></div><div class="title"><strong>URI Parameters</strong><div class="collapse-button show"><span class="close">Hide</span><span class="open">Show</span></div></div><div class="collapse-content"><dl class="inner"><dt>id</dt><dd><code>number</code>&nbsp;<span class="required">(required)</span>&nbsp;<p>An unique identifier of the withdrawal.</p>
</dd></dl></div><div class="title"><strong>Request</strong><div class="collapse-button"><span class="close">Hide</span><span class="open">Show</span></div></div><div class="collapse-content"><div class="inner"><h5>Headers</h5><pre><code><span class="hljs-attribute">Content-Type</span>: <span class="hljs-string">application/json</span><br><span class="hljs-attribute">Authorization</span>: <span class="hljs-string">Bearer</span></code></pre><div style="height: 1px;"></div></div></div><div class="title"><strong>Response&nbsp;&nbsp;<code>200</code></strong><div class="collapse-button"><span class="close">Hide</span><span class="open">Show</span></div></div><div class="collapse-content"><div class="inner"><h5>Headers</h5><pre><code><span class="hljs-attribute">Content-Type</span>: <span class="hljs-string">application/json</span></code></pre><div style="height: 1px;"></div><h5>Body</h5><pre><code>{
"<span class="hljs-attribute">user_id</span>": <span class="hljs-value"><span class="hljs-string">"123"</span></span>,
"<span class="hljs-attribute">query_id</span>": <span class="hljs-value"><span class="hljs-string">"321"</span></span>,
"<span class="hljs-attribute">status</span>": <span class="hljs-value"><span class="hljs-string">"pending"</span>
</span>}</code></pre><div style="height: 1px;"></div></div></div><div class="title"><strong>Response&nbsp;&nbsp;<code>400</code></strong><div class="collapse-button"><span class="close">Hide</span><span class="open">Show</span></div></div><div class="collapse-content"><div class="inner"><h5>Headers</h5><pre><code><span class="hljs-attribute">Content-Type</span>: <span class="hljs-string">application/json</span></code></pre><div style="height: 1px;"></div><h5>Body</h5><pre><code>{
"<span class="hljs-attribute">error</span>": <span class="hljs-value"><span class="hljs-string">"error text"</span>
Expand All @@ -125,7 +127,7 @@ <h4>Example URI</h4><div class="definition"><span class="method get">GET</span>&
"<span class="hljs-attribute">is_synced</span>": <span class="hljs-value"><span class="hljs-literal">false</span>
</span>}</code></pre><div style="height: 1px;"></div></div></div><div class="title"><strong>Response&nbsp;&nbsp;<code>500</code></strong><div class="collapse-button"><span class="close">Hide</span><span class="open">Show</span></div></div><div class="collapse-content"><div class="inner"><h5>Headers</h5><pre><code><span class="hljs-attribute">Content-Type</span>: <span class="hljs-string">application/json</span></code></pre><div style="height: 1px;"></div><h5>Body</h5><pre><code>{
"<span class="hljs-attribute">error</span>": <span class="hljs-value"><span class="hljs-string">"error text"</span>
</span>}</code></pre><div style="height: 1px;"></div></div></div></div></div></section></div></div></div><p style="text-align: center;" class="text-muted">Generated by&nbsp;<a href="https://github.com/danielgtaylor/aglio" class="aglio">aglio</a>&nbsp;on 26 Sep 2023</p><script>/* eslint-env browser */
</span>}</code></pre><div style="height: 1px;"></div></div></div></div></div></section></div></div></div><p style="text-align: center;" class="text-muted">Generated by&nbsp;<a href="https://github.com/danielgtaylor/aglio" class="aglio">aglio</a>&nbsp;on 21 May 2024</p><script>/* eslint-env browser */
/* eslint quotes: [2, "single"] */
'use strict';

Expand Down
20 changes: 10 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.19
require (
github.com/caarlos0/env/v6 v6.9.3
github.com/gofrs/uuid v4.0.0+incompatible
github.com/jackc/pgx/v4 v4.17.0
github.com/jackc/pgx/v4 v4.18.2
github.com/prometheus/client_golang v1.13.0
github.com/shopspring/decimal v1.2.0
github.com/sirupsen/logrus v1.8.1
Expand All @@ -19,23 +19,23 @@ require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect
github.com/jackc/pgconn v1.14.3 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.12.0 // indirect
github.com/jackc/puddle v1.2.1 // indirect
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/puddle v1.3.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/oasisprotocol/curve25519-voi v0.0.0-20220328075252-7dd334e3daae // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3 // indirect
github.com/snksoft/crc v1.1.0 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20230116083435-1de6713980de // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/text v0.3.8 // indirect
google.golang.org/protobuf v1.28.1 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
Loading

0 comments on commit 33ef176

Please sign in to comment.