Skip to content

Commit

Permalink
Autoclaim fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaptoss committed Jun 13, 2024
1 parent 948b071 commit 1d5ecfd
Show file tree
Hide file tree
Showing 5 changed files with 353 additions and 277 deletions.
5 changes: 3 additions & 2 deletions internal/data/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type BalancesQ interface {
GetWithRank(nullifier string) (*Balance, error)
SelectWithRank() ([]Balance, error)

// Filters not applied. Return balances which already have scanned passport, but there no fulfilled or claimed events for this
// Filters not applied. Return balances which already have scanned passport, but there no claimed events for this
WithoutPassportEvent() ([]WithoutPassportEventBalance, error)
WithoutReferralEvent() ([]ReferredReferrer, error)

Expand All @@ -46,7 +46,8 @@ type BalancesQ interface {

type WithoutPassportEventBalance struct {
Balance
EventID *string `db:"event_id"`
EventID string `db:"event_id"`
EventStatus EventStatus `db:"event_status"`
}

type ReferredReferrer struct {
Expand Down
6 changes: 3 additions & 3 deletions internal/data/pg/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ func (q *balances) GetWithRank(nullifier string) (*data.Balance, error) {
func (q *balances) WithoutPassportEvent() ([]data.WithoutPassportEventBalance, error) {
var res []data.WithoutPassportEventBalance
stmt := fmt.Sprintf(`
SELECT b.*, e.id AS event_id
FROM %s AS b LEFT JOIN %s AS e
SELECT b.*, e.id AS event_id, e.status AS event_status
FROM %s AS b INNER JOIN %s AS e
ON b.nullifier = e.nullifier AND e.type='passport_scan'
WHERE (e.status NOT IN ('fulfilled', 'claimed') OR e.nullifier IS NULL)
WHERE e.status NOT IN ('claimed')
AND b.referred_by IS NOT NULL
AND b.country IS NOT NULL
`, balancesTable, eventsTable)
Expand Down
87 changes: 61 additions & 26 deletions internal/service/handlers/claim_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"net/http"

"github.com/rarimo/decentralized-auth-svc/pkg/auth"
"github.com/rarimo/rarime-points-svc/internal/config"
"github.com/rarimo/rarime-points-svc/internal/data"
"github.com/rarimo/rarime-points-svc/internal/data/evtypes"
"github.com/rarimo/rarime-points-svc/internal/data/pg"
"github.com/rarimo/rarime-points-svc/internal/service/requests"
"github.com/rarimo/rarime-points-svc/resources"
Expand Down Expand Up @@ -83,7 +85,7 @@ func ClaimEvent(w http.ResponseWriter, r *http.Request) {
}

err = EventsQ(r).Transaction(func() error {
event, err = claimEventWithPoints(r, *event, evType.Reward, balance)
event, err = claimEvent(r, event, balance)
if err != nil {
return err
}
Expand All @@ -107,49 +109,82 @@ func ClaimEvent(w http.ResponseWriter, r *http.Request) {
ape.Render(w, newClaimEventResponse(*event, evType.Resource(), *balance))
}

// claimEventWithPoints requires event to exist
func claimEventWithPoints(r *http.Request, event data.Event, reward int64, balance *data.Balance) (claimed *data.Event, err error) {
// Upgrade level logic when threshold is reached
refsCount, level := Levels(r).LvlUp(balance.Level, reward+balance.Amount)
if level != balance.Level {
count, err := ReferralsQ(r).FilterByNullifier(balance.Nullifier).Count()
// claimEvent requires event to exist
// call in transaction to prevent unexpected changes
func claimEvent(r *http.Request, event *data.Event, balance *data.Balance) (claimed *data.Event, err error) {
if event == nil {
return nil, nil
}

evType := EventTypes(r).Get(event.Type, evtypes.FilterInactive)
if evType == nil {
return event, nil
}

claimed, err = EventsQ(r).FilterByID(event.ID).Update(data.EventClaimed, nil, &evType.Reward)
if err != nil {
return nil, fmt.Errorf("update event status: %w", err)
}

err = DoClaimEventUpdates(
Levels(r),
ReferralsQ(r),
BalancesQ(r),
CountriesQ(r),
*balance,
evType.Reward)
if err != nil {
return nil, fmt.Errorf("failed to do claim event updates: %w", err)
}

return claimed, nil
}

// do updates which link to claim event:
// update reserved amount in country;
// lvlup and update referrals count;
// accruing points;
//
// Balance must be active and with verified passport
func DoClaimEventUpdates(
levels config.Levels,
referralsQ data.ReferralsQ,
balancesQ data.BalancesQ,
countriesQ data.CountriesQ,
balance data.Balance,
reward int64) (err error) {

refsCount, level := levels.LvlUp(balance.Level, reward+balance.Amount)
if refsCount > 0 {
count, err := referralsQ.New().FilterByNullifier(balance.Nullifier).Count()
if err != nil {
return nil, fmt.Errorf("failed to get referral count: %w", err)
return fmt.Errorf("failed to get referral count: %w", err)
}

refToAdd := prepareReferralsToAdd(balance.Nullifier, uint64(refsCount), count)
if err = ReferralsQ(r).Insert(refToAdd...); err != nil {
return nil, fmt.Errorf("failed to insert referrals: %w", err)
if err = referralsQ.New().Insert(refToAdd...); err != nil {
return fmt.Errorf("failed to insert referrals: %w", err)
}

err = BalancesQ(r).FilterByNullifier(balance.Nullifier).Update(map[string]any{
data.ColLevel: level,
})
if err != nil {
return nil, fmt.Errorf("failed to update level: %w", err)
}
}

claimed, err = EventsQ(r).FilterByID(event.ID).Update(data.EventClaimed, nil, &reward)
if err != nil {
return nil, fmt.Errorf("update event status: %w", err)
return nil
}

err = BalancesQ(r).FilterByNullifier(balance.Nullifier).Update(map[string]any{
err = balancesQ.FilterByNullifier(balance.Nullifier).Update(map[string]any{
data.ColAmount: pg.AddToValue(data.ColAmount, reward),
data.ColLevel: level,
})
if err != nil {
return nil, fmt.Errorf("update balance amount: %w", err)
return fmt.Errorf("update balance amount and level: %w", err)
}

err = CountriesQ(r).FilterByCodes(*balance.Country).Update(map[string]any{
err = countriesQ.FilterByCodes(*balance.Country).Update(map[string]any{
data.ColReserved: pg.AddToValue(data.ColReserved, reward),
})
if err != nil {
return nil, fmt.Errorf("increase country reserve: %w", err)
return fmt.Errorf("increase country reserve: %w", err)
}

return claimed, nil
return nil
}

func newClaimEventResponse(
Expand Down
Loading

0 comments on commit 1d5ecfd

Please sign in to comment.