From 78de6ab7189c6eca8c353cb1d120fd099d27a555 Mon Sep 17 00:00:00 2001 From: Zaptoss Date: Mon, 14 Oct 2024 18:44:40 +0300 Subject: [PATCH] Add withdraw event --- .../assets/migrations/007_withdraw_event.sql | 8 +++++ internal/data/evtypes/models/extra.go | 1 + internal/service/handlers/withdraw.go | 33 ++++++++++++++++--- 3 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 internal/assets/migrations/007_withdraw_event.sql diff --git a/internal/assets/migrations/007_withdraw_event.sql b/internal/assets/migrations/007_withdraw_event.sql new file mode 100644 index 0000000..934387a --- /dev/null +++ b/internal/assets/migrations/007_withdraw_event.sql @@ -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'; diff --git a/internal/data/evtypes/models/extra.go b/internal/data/evtypes/models/extra.go index 69afe6e..6c283bc 100644 --- a/internal/data/evtypes/models/extra.go +++ b/internal/data/evtypes/models/extra.go @@ -24,6 +24,7 @@ const ( TypeEarlyTest = "early_test" TypeDailyQuestion = "daily_question" TypeBonusCode = "bonus_code" + TypeWithdraw = "withdraw" ) const ( diff --git a/internal/service/handlers/withdraw.go b/internal/service/handlers/withdraw.go index 0c0ced7..d867056 100644 --- a/internal/service/handlers/withdraw.go +++ b/internal/service/handlers/withdraw.go @@ -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" @@ -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 }