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

test version early test event #30

Merged
merged 25 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
13 changes: 13 additions & 0 deletions internal/cli/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cli

import (
"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 {
cfg.Log().Errorf("Error starting event: %s", err)
}
}
6 changes: 6 additions & 0 deletions internal/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("create-early-test", "claim event command")
data = eventCmd.Arg("before", "data after ...").Required().Int()
kish1n marked this conversation as resolved.
Show resolved Hide resolved
)

cmd, err := app.Parse(args[1:])
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions internal/data/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type BalancesQ interface {
FilterByInternalAID(aid string) BalancesQ
FilterByExternalAID(aid string) BalancesQ
FilterBySharedHash(hash string) BalancesQ
FilterByCreatedBefore(date int) BalancesQ
FilterVerified() BalancesQ
}

type WithoutPassportEventBalance struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/data/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type EventsQ interface {
SelectAbsentTypes(allTypes ...string) ([]ReopenableEvent, error)

FilterByID(...string) EventsQ
FilterByNullifier(string) EventsQ
FilterByNullifier(...string) EventsQ
FilterByStatus(...EventStatus) EventsQ
FilterByType(...string) EventsQ
FilterByNotType(types ...string) EventsQ
Expand Down
1 change: 1 addition & 0 deletions internal/data/evtypes/models/extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
TypeInternalPassportScan = "internal_passport_scan"
TypeExternalPassportScan = "external_passport_scan"
TypePollParticipation = "poll_participation"
TypeEarlyTest = "early_test"
)

const (
Expand Down
11 changes: 11 additions & 0 deletions internal/data/pg/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,21 @@ 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)
q.rank = q.rank.Where(cond)
q.counter = q.counter.Where(cond)
return q
}

func (q *balances) FilterVerified() data.BalancesQ {
return q.applyCondition(squirrel.Or{
squirrel.NotEq{"internal_aid": nil},
squirrel.NotEq{"external_aid": nil},
})
}
2 changes: 1 addition & 1 deletion internal/data/pg/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (q *events) FilterByID(ids ...string) data.EventsQ {
return q.applyCondition(squirrel.Eq{"id": ids})
}

func (q *events) FilterByNullifier(nullifier string) data.EventsQ {
func (q *events) FilterByNullifier(nullifier ...string) data.EventsQ {
return q.applyCondition(squirrel.Eq{"nullifier": nullifier})
}

Expand Down
101 changes: 101 additions & 0 deletions internal/service/event/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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"
"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/handlers"
)

func Run(cfg config.Config, date int) error {
log := cfg.Log()
db := cfg.DB()
lvls := cfg.Levels()
evTypes := cfg.EventTypes()

balancesQ := pg.NewBalances(db)
eventsQ := pg.NewEvents(db)
referralsQ := pg.NewReferrals(db)

evType := evTypes.Get(models.TypeEarlyTest, evtypes.FilterInactive, evtypes.FilterByAutoClaim(true))
kish1n marked this conversation as resolved.
Show resolved Hide resolved
if evType == nil {
log.Infof("Event type %s is inactive", models.TypeEarlyTest)
return nil
}

balances, err := balancesQ.FilterByCreatedBefore(date).FilterVerified().Select()

if err != nil {
log.WithError(err).Error("failed to filter by updated before")
return err
}
if len(balances) == 0 {
log.Infof("no balances found")
return nil
}

nullifiers := make([]string, 0, len(balances))

for _, balance := range balances {
nullifiers = append(nullifiers, balance.Nullifier)
}

filteredEvents, err := eventsQ.
FilterByStatus(data.EventFulfilled).
FilterByNullifier(nullifiers...).
Select()
artemskriabin marked this conversation as resolved.
Show resolved Hide resolved

if err != nil {
log.WithError(err).Error("Failed to select %s events")
return err
}

EventsMap := make(map[string]data.Event)
kish1n marked this conversation as resolved.
Show resolved Hide resolved
for _, event := range filteredEvents {
EventsMap[event.Nullifier] = event
}
kish1n marked this conversation as resolved.
Show resolved Hide resolved

for _, balance := range balances {
if _, exists := EventsMap[balance.Nullifier]; exists {
continue
}

err = eventsQ.Insert(data.Event{
Nullifier: balance.Nullifier,
Type: models.TypeEarlyTest,
Status: data.EventFulfilled,
})
if err != nil {
return fmt.Errorf("failed to insert `early_test` event: %w", err)
}

var totalPoints int64

_, err = eventsQ.FilterByNullifier(nullifiers...).Update(data.EventClaimed, nil, &evType.Reward)
kish1n marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("failedt to update %s events for user=%s: %w", models.TypeEarlyTest, balance.Nullifier, err)
}

totalPoints += evType.Reward

level, err := handlers.DoLevelRefUpgrade(lvls, referralsQ, &balance, totalPoints)
if err != nil {
return fmt.Errorf("failed to do lvlup and referrals updates: %w", err)
}

err = balancesQ.FilterByNullifier(balance.Nullifier).Update(map[string]any{
data.ColAmount: pg.AddToValue(data.ColAmount, totalPoints),
data.ColLevel: level,
})

if err != nil {
return fmt.Errorf("error update balance amount and level: %w", err)
}
kish1n marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}
1 change: 0 additions & 1 deletion internal/service/workers/leveljustice/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@ func Run(cfg config.Config, sig chan struct{}) {
}

sig <- struct{}{}

}
Loading