From 4796ccb3b79da47dd14add4cabdc6232309b4d67 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Tue, 6 Aug 2024 14:55:03 +0300 Subject: [PATCH] first test version --- internal/cli/event.go | 14 +++++ internal/cli/main.go | 6 +++ internal/data/balances.go | 2 + internal/data/evtypes/models/extra.go | 1 + internal/data/pg/balances.go | 8 +++ internal/service/event/main.go | 54 +++++++++++++++++++ internal/service/workers/leveljustice/main.go | 1 - 7 files changed, 85 insertions(+), 1 deletion(-) diff --git a/internal/cli/event.go b/internal/cli/event.go index 7f1e458..98d921a 100644 --- a/internal/cli/event.go +++ b/internal/cli/event.go @@ -1 +1,15 @@ package cli + +import ( + "log" + + "github.com/rarimo/geo-points-svc/internal/config" + "github.com/rarimo/geo-points-svc/internal/service/event" +) + +func eventStart(cfg config.Config, data int) { + err := event.Run(cfg, data) + if err != nil { + log.Fatalf("Error starting event: %v", err) + } +} diff --git a/internal/cli/main.go b/internal/cli/main.go index 4d7a177..4b09860 100644 --- a/internal/cli/main.go +++ b/internal/cli/main.go @@ -29,6 +29,10 @@ func Run(args []string) bool { migrateCmd = app.Command("migrate", "migrate command") migrateUpCmd = migrateCmd.Command("up", "migrate db up") migrateDownCmd = migrateCmd.Command("down", "migrate db down") + + event = app.Command("event", "claim event command") + eventCmd = event.Command("start", "claim event command") + data = eventCmd.Arg("data", "data after ...").Required().Int() ) cmd, err := app.Parse(args[1:]) @@ -48,6 +52,8 @@ func Run(args []string) bool { err = MigrateUp(cfg) case migrateDownCmd.FullCommand(): err = MigrateDown(cfg) + case eventCmd.FullCommand(): + eventStart(cfg, *data) default: log.Errorf("unknown command %s", cmd) return false diff --git a/internal/data/balances.go b/internal/data/balances.go index 76bf4e4..6556109 100644 --- a/internal/data/balances.go +++ b/internal/data/balances.go @@ -54,6 +54,8 @@ type BalancesQ interface { FilterByInternalAID(aid string) BalancesQ FilterByExternalAID(aid string) BalancesQ FilterBySharedHash(hash string) BalancesQ + FilterByCreatedBefore(time int) BalancesQ + FilterVerified() BalancesQ } type WithoutPassportEventBalance struct { diff --git a/internal/data/evtypes/models/extra.go b/internal/data/evtypes/models/extra.go index 3b794e8..3b7991f 100644 --- a/internal/data/evtypes/models/extra.go +++ b/internal/data/evtypes/models/extra.go @@ -21,6 +21,7 @@ const ( TypeInternalPassportScan = "internal_passport_scan" TypeExternalPassportScan = "external_passport_scan" TypePollParticipation = "poll_participation" + TypeEarlyTest = "early_test" ) const ( diff --git a/internal/data/pg/balances.go b/internal/data/pg/balances.go index 44a0172..c6f4d1a 100644 --- a/internal/data/pg/balances.go +++ b/internal/data/pg/balances.go @@ -222,6 +222,10 @@ func (q *balances) FilterBySharedHash(hash string) data.BalancesQ { return q.applyCondition(squirrel.Eq{"shared_hash": hash}) } +func (q *balances) FilterByCreatedBefore(data int) data.BalancesQ { + return q.applyCondition(squirrel.Lt{"created_at": data}) +} + func (q *balances) applyCondition(cond squirrel.Sqlizer) data.BalancesQ { q.selector = q.selector.Where(cond) q.updater = q.updater.Where(cond) @@ -229,3 +233,7 @@ func (q *balances) applyCondition(cond squirrel.Sqlizer) data.BalancesQ { q.counter = q.counter.Where(cond) return q } + +func (q *balances) FilterVerified() data.BalancesQ { + return q.applyCondition(squirrel.Gt{"verified": true}) +} diff --git a/internal/service/event/main.go b/internal/service/event/main.go index 0e4b82e..e36f922 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -1 +1,55 @@ package event + +import ( + "fmt" + + "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" +) + +func Run(cfg config.Config, date int) error { + log := cfg.Log() + db := cfg.DB() + + balances := pg.NewBalances(db) + events := pg.NewEvents(db) + + users, err := balances.FilterByCreatedBefore(date).FilterVerified().Select() + filtEvents := events.FilterByType(models.TypeEarlyTest) + + if err != nil { + log.WithError(err).Error("failed to filter by updated before") + return err + } + + if users == nil { + log.Infof("no users found") + return nil + } + + for _, user := range users { + userNull := user.Nullifier + eve, err := filtEvents.FilterByNullifier(userNull).Get() + + if err != nil { + log.WithError(err).Error("failed to filter by nullifier") + return err + } + if eve != nil { + break + } + + err = events.Insert(data.Event{ + Nullifier: user.Nullifier, + Type: models.TypeEarlyTest, + Status: data.EventFulfilled, + }) + if err != nil { + return fmt.Errorf("failed to insert `early_test` event: %w", err) + } + } + + return nil +} diff --git a/internal/service/workers/leveljustice/main.go b/internal/service/workers/leveljustice/main.go index e4812fc..ce60041 100644 --- a/internal/service/workers/leveljustice/main.go +++ b/internal/service/workers/leveljustice/main.go @@ -37,5 +37,4 @@ func Run(cfg config.Config, sig chan struct{}) { } sig <- struct{}{} - }