-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from rarimo/feature/withdrawal-history
Feature: withdrawal history
- Loading branch information
Showing
23 changed files
with
436 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
allOf: | ||
- $ref: '#/components/schemas/PointPriceKey' | ||
- type: object | ||
required: | ||
- attributes | ||
properties: | ||
type: | ||
type: string | ||
enum: [point_price] | ||
attributes: | ||
type: object | ||
required: | ||
- urmo | ||
properties: | ||
urmo: | ||
type: integer | ||
description: Amount of `urmo` tokens for one point | ||
example: 1000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
type: object | ||
required: | ||
- type | ||
properties: | ||
type: | ||
type: string | ||
enum: [point_price] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
docs/spec/paths/integrations@rarime-points-svc@v1@point_price.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
get: | ||
tags: | ||
- Points balance | ||
summary: Get point price | ||
description: How many `urmo` tokens cost one point. | ||
operationId: getPointPrice | ||
responses: | ||
200: | ||
description: Success | ||
content: | ||
application/vnd.api+json: | ||
schema: | ||
type: object | ||
required: | ||
- data | ||
properties: | ||
data: | ||
$ref: '#/components/schemas/PointPrice' | ||
500: | ||
$ref: '#/components/responses/internalError' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package data | ||
|
||
import ( | ||
"gitlab.com/distributed_lab/kit/pgdb" | ||
) | ||
|
||
type Balance struct { | ||
DID string `db:"did"` | ||
Amount int32 `db:"amount"` | ||
CreatedAt int32 `db:"created_at"` | ||
UpdatedAt int32 `db:"updated_at"` | ||
Rank *int `db:"rank"` | ||
} | ||
|
||
type BalancesQ interface { | ||
New() BalancesQ | ||
Insert(did string) error | ||
UpdateAmountBy(points int32) error | ||
|
||
Page(*pgdb.OffsetPageParams) BalancesQ | ||
Select() ([]Balance, error) | ||
Get() (*Balance, error) | ||
WithRank() BalancesQ | ||
|
||
FilterByDID(string) BalancesQ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package pg | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Masterminds/squirrel" | ||
"github.com/rarimo/rarime-points-svc/internal/data" | ||
"gitlab.com/distributed_lab/kit/pgdb" | ||
) | ||
|
||
const withdrawalsTable = "withdrawals" | ||
|
||
type withdrawals struct { | ||
db *pgdb.DB | ||
selector squirrel.SelectBuilder | ||
} | ||
|
||
func NewWithdrawals(db *pgdb.DB) data.WithdrawalsQ { | ||
return &withdrawals{ | ||
db: db, | ||
selector: squirrel.Select("*").From(withdrawalsTable), | ||
} | ||
} | ||
|
||
func (q *withdrawals) New() data.WithdrawalsQ { | ||
return NewWithdrawals(q.db.Clone()) | ||
} | ||
|
||
func (q *withdrawals) Insert(w data.Withdrawal) (*data.Withdrawal, error) { | ||
var res data.Withdrawal | ||
stmt := squirrel.Insert(withdrawalsTable).SetMap(map[string]interface{}{ | ||
"user_did": w.UserDID, | ||
"amount": w.Amount, | ||
"address": w.Address, | ||
}).Suffix("RETURNING *") | ||
|
||
if err := q.db.Get(&res, stmt); err != nil { | ||
return nil, fmt.Errorf("insert withdrawal [%+v]: %w", w, err) | ||
} | ||
|
||
return &res, nil | ||
} | ||
|
||
func (q *withdrawals) Page(page *pgdb.CursorPageParams) data.WithdrawalsQ { | ||
q.selector = page.ApplyTo(q.selector, "created_at") | ||
return q | ||
} | ||
|
||
func (q *withdrawals) Select() ([]data.Withdrawal, error) { | ||
var res []data.Withdrawal | ||
|
||
if err := q.db.Select(&res, q.selector); err != nil { | ||
return nil, fmt.Errorf("select withdrawals: %w", err) | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
func (q *withdrawals) FilterByUserDID(did string) data.WithdrawalsQ { | ||
q.selector = q.selector.Where(squirrel.Eq{"user_did": did}) | ||
return q | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package data | ||
|
||
import ( | ||
"gitlab.com/distributed_lab/kit/pgdb" | ||
) | ||
|
||
type Withdrawal struct { | ||
ID string `db:"id"` | ||
UserDID string `db:"user_did"` | ||
Amount int32 `db:"amount"` | ||
Address string `db:"address"` | ||
CreatedAt int32 `db:"created_at"` | ||
} | ||
|
||
type WithdrawalsQ interface { | ||
New() WithdrawalsQ | ||
Insert(Withdrawal) (*Withdrawal, error) | ||
|
||
Page(*pgdb.CursorPageParams) WithdrawalsQ | ||
Select() ([]Withdrawal, error) | ||
|
||
FilterByUserDID(string) WithdrawalsQ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package handlers | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/rarimo/rarime-auth-svc/pkg/auth" | ||
"github.com/rarimo/rarime-points-svc/internal/data" | ||
"github.com/rarimo/rarime-points-svc/internal/service/requests" | ||
"github.com/rarimo/rarime-points-svc/resources" | ||
"gitlab.com/distributed_lab/ape" | ||
"gitlab.com/distributed_lab/ape/problems" | ||
) | ||
|
||
func ListWithdrawals(w http.ResponseWriter, r *http.Request) { | ||
req, err := requests.NewListWithdrawals(r) | ||
if err != nil { | ||
ape.RenderErr(w, problems.BadRequest(err)...) | ||
return | ||
} | ||
|
||
if !auth.Authenticates(UserClaims(r), auth.UserGrant(req.DID)) { | ||
ape.RenderErr(w, problems.Unauthorized()) | ||
return | ||
} | ||
|
||
withdrawals, err := WithdrawalsQ(r).FilterByUserDID(req.DID).Page(&req.CursorPageParams).Select() | ||
if err != nil { | ||
Log(r).WithError(err).Error("Failed to get withdrawal list") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
var last string | ||
if len(withdrawals) > 0 { | ||
last = withdrawals[len(withdrawals)-1].ID | ||
} | ||
|
||
resp := newWithdrawalsResponse(withdrawals) | ||
resp.Links = req.CursorParams.GetLinks(r, last) | ||
} | ||
|
||
func newWithdrawalsResponse(withdrawals []data.Withdrawal) resources.WithdrawalListResponse { | ||
list := make([]resources.Withdrawal, len(withdrawals)) | ||
for i, w := range withdrawals { | ||
list[i] = newWithdrawalModel(w) | ||
} | ||
return resources.WithdrawalListResponse{Data: list} | ||
} | ||
|
||
func newWithdrawalModel(w data.Withdrawal) resources.Withdrawal { | ||
return resources.Withdrawal{ | ||
Key: resources.Key{ | ||
ID: w.ID, | ||
Type: resources.WITHDRAWAL, | ||
}, | ||
Attributes: resources.WithdrawalAttributes{ | ||
Amount: w.Amount, | ||
Address: w.Address, | ||
CreatedAt: w.CreatedAt, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package handlers | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/rarimo/rarime-points-svc/resources" | ||
"gitlab.com/distributed_lab/ape" | ||
) | ||
|
||
func GetPointPrice(w http.ResponseWriter, r *http.Request) { | ||
ape.Render(w, resources.PointPriceResponse{ | ||
Data: resources.PointPrice{ | ||
Key: resources.Key{ | ||
Type: resources.POINT_PRICE, | ||
}, | ||
Attributes: resources.PointPriceAttributes{ | ||
Urmo: PointPrice(r), | ||
}, | ||
}, | ||
}) | ||
} |
Oops, something went wrong.