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

Add withdraw event #49

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading