Skip to content

Commit

Permalink
Merge pull request #49 from rarimo/feat/withdraw-event
Browse files Browse the repository at this point in the history
Add withdraw event
  • Loading branch information
Zaptoss authored Oct 15, 2024
2 parents c28d062 + 78de6ab commit 371ec23
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
8 changes: 8 additions & 0 deletions internal/assets/migrations/007_withdraw_event.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- +migrate Up
INSERT INTO events(nullifier, type, status, created_at, updated_at, points_amount)
SELECT w.nullifier AS nullifier, 'withdraw' AS type, 'claimed' AS status,
EXTRACT('EPOCH' FROM w.created_at) AS created_at, EXTRACT('EPOCH' FROM w.created_at) AS updated_at,
(-w.amount) AS points_amount FROM withdrawals w;

-- +migrate Down
DELETE FROM events WHERE type='withdraw';
1 change: 1 addition & 0 deletions internal/data/evtypes/models/extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
TypeEarlyTest = "early_test"
TypeDailyQuestion = "daily_question"
TypeBonusCode = "bonus_code"
TypeWithdraw = "withdraw"
)

const (
Expand Down
33 changes: 28 additions & 5 deletions internal/service/handlers/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/rarimo/geo-auth-svc/pkg/auth"
"github.com/rarimo/geo-points-svc/internal/config"
"github.com/rarimo/geo-points-svc/internal/data"
"github.com/rarimo/geo-points-svc/internal/data/evtypes/models"
"github.com/rarimo/geo-points-svc/internal/data/pg"
"github.com/rarimo/geo-points-svc/internal/service/requests"
"github.com/rarimo/geo-points-svc/resources"
Expand Down Expand Up @@ -95,13 +96,35 @@ func Withdraw(w http.ResponseWriter, r *http.Request) {
return
}

_, err = WithdrawalsQ(r).Insert(data.Withdrawal{
TxHash: txHash[:],
Nullifier: nullifier,
Amount: req.Data.Attributes.Amount,
err = EventsQ(r).Transaction(func() error {
_, err = WithdrawalsQ(r).Insert(data.Withdrawal{
TxHash: txHash[:],
Nullifier: nullifier,
Amount: req.Data.Attributes.Amount,
})
if err != nil {
return fmt.Errorf("failed to insert withdraw entry: %w", err)
}

if evType := EventTypes(r).Get(models.TypeWithdraw); evType == nil {
return fmt.Errorf("event type %s absent", models.TypeWithdraw)
}

pAmount := -req.Data.Attributes.Amount
err = EventsQ(r).Insert(data.Event{
Nullifier: nullifier,
Type: models.TypeWithdraw,
Status: data.EventClaimed,
PointsAmount: &pAmount,
})
if err != nil {
return fmt.Errorf("failed to insert withdraw event: %w", err)
}

return nil
})
if err != nil {
log.WithError(err).Error("Failed to insert withdraw")
log.WithError(err).Error("Failed to insert withdraw and event")
ape.RenderErr(w, problems.InternalError())
return
}
Expand Down

0 comments on commit 371ec23

Please sign in to comment.