@@ -12,6 +12,7 @@ import (
12
12
"github.com/rarimo/geo-auth-svc/pkg/auth"
13
13
"github.com/rarimo/geo-points-svc/internal/config"
14
14
"github.com/rarimo/geo-points-svc/internal/data"
15
+ "github.com/rarimo/geo-points-svc/internal/data/evtypes/models"
15
16
"github.com/rarimo/geo-points-svc/internal/data/pg"
16
17
"github.com/rarimo/geo-points-svc/internal/service/requests"
17
18
"github.com/rarimo/geo-points-svc/resources"
@@ -95,13 +96,35 @@ func Withdraw(w http.ResponseWriter, r *http.Request) {
95
96
return
96
97
}
97
98
98
- _ , err = WithdrawalsQ (r ).Insert (data.Withdrawal {
99
- TxHash : txHash [:],
100
- Nullifier : nullifier ,
101
- Amount : req .Data .Attributes .Amount ,
99
+ err = EventsQ (r ).Transaction (func () error {
100
+ _ , err = WithdrawalsQ (r ).Insert (data.Withdrawal {
101
+ TxHash : txHash [:],
102
+ Nullifier : nullifier ,
103
+ Amount : req .Data .Attributes .Amount ,
104
+ })
105
+ if err != nil {
106
+ return fmt .Errorf ("failed to insert withdraw entry: %w" , err )
107
+ }
108
+
109
+ if evType := EventTypes (r ).Get (models .TypeWithdraw ); evType == nil {
110
+ return fmt .Errorf ("event type %s absent" , models .TypeWithdraw )
111
+ }
112
+
113
+ pAmount := - req .Data .Attributes .Amount
114
+ err = EventsQ (r ).Insert (data.Event {
115
+ Nullifier : nullifier ,
116
+ Type : models .TypeWithdraw ,
117
+ Status : data .EventClaimed ,
118
+ PointsAmount : & pAmount ,
119
+ })
120
+ if err != nil {
121
+ return fmt .Errorf ("failed to insert withdraw event: %w" , err )
122
+ }
123
+
124
+ return nil
102
125
})
103
126
if err != nil {
104
- log .WithError (err ).Error ("Failed to insert withdraw" )
127
+ log .WithError (err ).Error ("Failed to insert withdraw and event " )
105
128
ape .RenderErr (w , problems .InternalError ())
106
129
return
107
130
}
0 commit comments