-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
305 lines (258 loc) · 8.29 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package main
import (
"database/sql"
"errors"
"math/rand"
"net/http"
"time"
"github.com/gamezop/interview-assignment-sc-rewards/repo"
"github.com/gamezop/jinbe/pkg/db_psql"
"github.com/gamezop/jinbe/pkg/errr"
gh "github.com/gamezop/jinbe/pkg/http/ginhelpers"
"github.com/gamezop/jinbe/pkg/http/httpclient"
"github.com/gamezop/jinbe/pkg/random"
"github.com/gin-contrib/requestid"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/kelseyhightower/envconfig"
"github.com/rs/zerolog/log"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/lib/pq"
)
var Version = "unknown-updated-during-build-time"
var serviceName = "reward-service-mock"
var maxOrderStatusInPendingStateSeconds = 10
func runMigrate(db *sql.DB) {
driver, err := postgres.WithInstance(db, &postgres.Config{})
errr.PanicIfNotNil(err, "failed to get driver migrate")
m, err := migrate.NewWithDatabaseInstance(
"file://migrations",
"postgres", driver)
errr.PanicIfNotNil(err, "failed to get migration instance")
err = m.Up()
if err != nil && !errors.Is(err, migrate.ErrNoChange) {
errr.PanicIfNotNil(err, "failed to run migration")
}
log.Info().Msg("completed migration")
}
//////// config
type config struct {
Flags struct {
OnlySuccessfullyOrders bool `split_words:"true"`
}
DB struct {
RewardPayoutsURI string `required:"true" split_words:"true"`
}
}
var Env config
func Init() {
rand.Seed(time.Now().Unix())
err := envconfig.Process("", &Env)
errr.PanicIfNotNil(err, "failed to load env")
}
//////// random logic
func NewOrderId() uuid.UUID {
return uuid.New()
}
func getRandomOrderStatus(arr []repo.OrderStatus) repo.OrderStatus {
return arr[rand.Intn(len(arr))]
}
func getTerminalOrderStatus() repo.OrderStatus {
var status repo.OrderStatus = getRandomOrderStatus([]repo.OrderStatus{
repo.OrderStatusFailed,
repo.OrderStatusSuccess,
})
if Env.Flags.OnlySuccessfullyOrders {
return repo.OrderStatusSuccess
}
return status
}
//////// handlers
type RequestBodyRewardPayout struct {
ScId uuid.UUID `json:"scId" binding:"required"`
}
type ResponseRewardPayout struct {
Status repo.OrderStatus `json:"status"`
OrderId uuid.UUID `json:"orderId"`
}
func handlerR1Payout(
gM *gh.GinResponseHelper,
r *repo.Queries,
) gin.HandlerFunc {
return func(c *gin.Context) {
ctx := gh.GetDDCtxFromGinOrBackground(c)
l := gh.RequestLogger(c)
var req RequestBodyRewardPayout
if err := c.ShouldBindJSON(&req); errr.IfErrAndLog(err, &l, "validation") {
gM.RespondWithValidationError(c, err)
return
}
rewardPayoutDetails, err := r.CreateRewardPayout(ctx, repo.CreateRewardPayoutParams{
Status: getTerminalOrderStatus(),
ScID: req.ScId,
})
if errr.IfErrAndLog(err, &l, "failed to create reward payout") {
gM.RespondWithError(c, "DB", err)
return
}
gM.RespondWithSuccess(c, ResponseRewardPayout{
Status: rewardPayoutDetails.Status,
OrderId: rewardPayoutDetails.OrderID,
})
}
}
func handlerR2Payout(
gM *gh.GinResponseHelper,
r *repo.Queries,
) gin.HandlerFunc {
return func(c *gin.Context) {
ctx := gh.GetDDCtxFromGinOrBackground(c)
l := gh.RequestLogger(c)
var req RequestBodyRewardPayout
if err := c.ShouldBindJSON(&req); errr.IfErrAndLog(err, &l, "validation") {
gM.RespondWithValidationError(c, err)
return
}
rewardPayoutDetails, err := r.CreateRewardPayout(ctx, repo.CreateRewardPayoutParams{
Status: repo.OrderStatusPending,
ScID: req.ScId,
})
if errr.IfErrAndLog(err, &l, "failed to create reward payout") {
gM.RespondWithError(c, "DB", err)
return
}
go func() {
time.Sleep(time.Duration(random.RandInt(2, maxOrderStatusInPendingStateSeconds)) * time.Second)
terminalStatus := getTerminalOrderStatus()
l = l.With().
Str("terminalStatus", string(terminalStatus)).
Str("orderId", rewardPayoutDetails.OrderID.String()).
Logger()
err := r.UpdateRewardPayoutStatus(ctx, repo.UpdateRewardPayoutStatusParams{Status: terminalStatus, OrderID: rewardPayoutDetails.OrderID})
errr.LogIfErr(err, &l, "failed to update reward payout status")
l.Trace().Msg("updated order status")
}()
gM.RespondWithSuccess(c, ResponseRewardPayout{
Status: rewardPayoutDetails.Status,
OrderId: rewardPayoutDetails.OrderID,
})
}
}
type RequestQueryParamsRewardPayout2Status struct {
OrderId string `form:"order-id"`
}
type ResponseRewardPayouts2Status struct {
Status repo.OrderStatus `json:"status"`
}
func handlerR2Status(
gM *gh.GinResponseHelper,
r *repo.Queries,
) gin.HandlerFunc {
return func(c *gin.Context) {
ctx := gh.GetDDCtxFromGinOrBackground(c)
l := gh.RequestLogger(c)
var req RequestQueryParamsRewardPayout2Status
if err := c.ShouldBindQuery(&req); errr.IfErrAndLog(err, &l, "validation") {
gM.RespondWithValidationError(c, err)
return
}
rewardPayoutDetails, err := r.GetRewardPayoutByOrderId(ctx, uuid.MustParse(req.OrderId))
if errr.IfErrAndLog(err, &l, "failed to get reward payout") {
gM.RespondWithError(c, "DB", err)
return
}
gM.RespondWithSuccess(c, ResponseRewardPayouts2Status{
Status: rewardPayoutDetails.Status,
})
}
}
type RequestHeadersRewardPayout3 struct {
CallbackURL string `header:"x-callback-url" binding:"required,url"`
}
func handlerR3Payout(
gM *gh.GinResponseHelper,
r *repo.Queries,
webhookHttpClient *http.Client,
) gin.HandlerFunc {
return func(c *gin.Context) {
ctx := gh.GetDDCtxFromGinOrBackground(c)
l := gh.RequestLogger(c)
var req RequestBodyRewardPayout
if err := c.ShouldBindJSON(&req); errr.IfErrAndLog(err, &l, "validation") {
gM.RespondWithValidationError(c, err)
return
}
var reqHeaders RequestHeadersRewardPayout3
if err := c.ShouldBindHeader(&reqHeaders); errr.IfErrAndLog(err, &l, "validation") {
gM.RespondWithValidationError(c, err)
return
}
rewardPayoutDetails, err := r.CreateRewardPayout(ctx, repo.CreateRewardPayoutParams{
Status: repo.OrderStatusPending,
ScID: req.ScId,
})
if errr.IfErrAndLog(err, &l, "failed to create reward payout") {
gM.RespondWithError(c, "DB", err)
return
}
go func() {
time.Sleep(time.Duration(random.RandInt(2, maxOrderStatusInPendingStateSeconds)) * time.Second)
terminalStatus := getTerminalOrderStatus()
l = l.With().
Str("terminalStatus", string(terminalStatus)).
Str("orderId", rewardPayoutDetails.OrderID.String()).
Logger()
err := r.UpdateRewardPayoutStatus(ctx, repo.UpdateRewardPayoutStatusParams{Status: terminalStatus, OrderID: rewardPayoutDetails.OrderID})
errr.LogIfErr(err, &l, "failed to update reward payout status")
newRewardPayoutDetails, err := r.GetRewardPayoutByOrderId(ctx, rewardPayoutDetails.OrderID)
if errr.IfErrAndLog(err, &l, "failed to get reward payout after updating") {
return
}
var response interface{}
// ensure that a 2xx response is received
_, err = httpclient.SendHttpRequest(ctx, webhookHttpClient, "PUT", reqHeaders.CallbackURL, nil, nil, newRewardPayoutDetails, &response)
if errr.IfErrAndLog(err, &l, "failed to call webhook") {
return
}
l.Trace().Msg("updated order status")
}()
gM.RespondWithSuccess(c, ResponseRewardPayout{
Status: rewardPayoutDetails.Status,
OrderId: rewardPayoutDetails.OrderID,
})
}
}
func Router(repository *repo.Queries, httpClient *http.Client) *gin.Engine {
r := gin.New()
ginModule := gh.NewGinResponseHelper(Version, serviceName, r, nil)
r.Use(
requestid.New(),
gin.Logger(),
gin.Recovery(),
)
r.POST("r1/payout", handlerR1Payout(ginModule, repository))
r.POST("r2/payout", handlerR2Payout(ginModule, repository))
// this api can be used to pull data for any reward type
r.GET("r2/payout/status", handlerR2Status(ginModule, repository))
r.POST("r3/payout", handlerR3Payout(ginModule, repository, httpClient))
r.PUT("credit", func(c *gin.Context) {
l := gh.RequestLogger(c)
body, err := c.GetRawData()
errr.LogIfErr(err, &l, "failed to get body")
l.Info().Str("body", string(body)).Msg("credit api called")
})
return r
}
func main() {
Init()
DBRewards := db_psql.DBConnectUrl("reward", Env.DB.RewardPayoutsURI, serviceName, false)
runMigrate(DBRewards)
repository := repo.New(DBRewards)
r := Router(repository, &http.Client{
Timeout: 5 * time.Second,
})
err := r.Run(":3010")
errr.PanicIfNotNil(err, "server stopped")
}