Skip to content

Commit

Permalink
Add query parameter referred_users_count to get_balance endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaptoss committed Jul 15, 2024
1 parent 2787fed commit 0abe7ee
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ get:
required: false
schema:
type: boolean
- in: query
name: 'referred_users_count'
description: 'Specifies whether to return the referred users count'
required: false
schema:
type: boolean
responses:
200:
description: Success
Expand Down
2 changes: 1 addition & 1 deletion internal/service/handlers/create_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func CreateBalance(w http.ResponseWriter, r *http.Request) {
return
}

ape.Render(w, newBalanceResponse(*balance, referrals, 0, 0))
ape.Render(w, newBalanceResponse(*balance, referrals, nil, nil))
}

func prepareEventsWithRef(nullifier, refBy string, isGenesisRef bool, r *http.Request) []data.Event {
Expand Down
59 changes: 34 additions & 25 deletions internal/service/handlers/get_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func GetBalance(w http.ResponseWriter, r *http.Request) {
}

var referrals []data.Referral
var referredUsers int
var rewardedReferredUsers int
if req.ReferralCodes {
var referredUsers *int
var rewardedReferredUsers *int
if req.ReferralCodes || req.ReferredUsersCount {
referrals, err = ReferralsQ(r).
FilterByNullifier(req.Nullifier).
WithStatus().
Expand All @@ -56,31 +56,40 @@ func GetBalance(w http.ResponseWriter, r *http.Request) {
return
}

// Infinite referral codes initially have 0 uses and,
// accordingly, after use, this value will decrease,
// i.e. the number of invited users for this code will
// be an absolute value
//
// A one-time code is considered used if it has 0 uses,
// because the initial value is 1
for _, ref := range referrals {
if ref.Infinity {
referredUsers += -int(ref.UsageLeft)
continue
if req.ReferredUsersCount {
referredUsers = new(int)
rewardedReferredUsers = new(int)
// Infinite referral codes initially have 0 uses and,
// accordingly, after use, this value will decrease,
// i.e. the number of invited users for this code will
// be an absolute value
//
// A one-time code is considered used if it has 0 uses,
// because the initial value is 1
for _, ref := range referrals {
if ref.Infinity {
*referredUsers += -int(ref.UsageLeft)
continue
}
if ref.UsageLeft == 0 {
*referredUsers++
}
}
if ref.UsageLeft == 0 {
referredUsers++

*rewardedReferredUsers, err = EventsQ(r).FilterByNullifier(req.Nullifier).FilterByType(models.TypeReferralSpecific).FilterByStatus(data.EventClaimed).Count()
if err != nil {
Log(r).WithError(err).Error("Failed to get count rewarded referral_specific events for user")
ape.RenderErr(w, problems.InternalError())
return
}
}

rewardedReferredUsers, err = EventsQ(r).FilterByNullifier(req.Nullifier).FilterByType(models.TypeReferralSpecific).FilterByStatus(data.EventClaimed).Count()
if err != nil {
Log(r).WithError(err).Error("Failed to get count rewarded referral_specific events for user")
ape.RenderErr(w, problems.InternalError())
return
}
}

if !req.ReferralCodes {
referrals = nil
}

ape.Render(w, newBalanceResponse(*balance, referrals, referredUsers, rewardedReferredUsers))
}

Expand All @@ -102,14 +111,14 @@ func newBalanceModel(balance data.Balance) resources.Balance {
}
}

func newBalanceResponse(balance data.Balance, referrals []data.Referral, referredUsers, rewardedReferredUsers int) resources.BalanceResponse {
func newBalanceResponse(balance data.Balance, referrals []data.Referral, referredUsers, rewardedReferredUsers *int) resources.BalanceResponse {
resp := resources.BalanceResponse{Data: newBalanceModel(balance)}
boolP := func(b bool) *bool { return &b }

resp.Data.Attributes.IsDisabled = boolP(balance.ReferredBy == nil)
resp.Data.Attributes.IsVerified = boolP(balance.IsVerified)
resp.Data.Attributes.ReferredUsersCount = &referredUsers
resp.Data.Attributes.RewardedReferredUsersCount = &rewardedReferredUsers
resp.Data.Attributes.ReferredUsersCount = referredUsers
resp.Data.Attributes.RewardedReferredUsersCount = rewardedReferredUsers

if len(referrals) == 0 {
return resp
Expand Down
7 changes: 4 additions & 3 deletions internal/service/requests/get_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
)

type GetBalance struct {
Nullifier string
Rank bool `url:"rank"`
ReferralCodes bool `url:"referral_codes"`
Nullifier string
Rank bool `url:"rank"`
ReferralCodes bool `url:"referral_codes"`
ReferredUsersCount bool `url:"referred_users_count"`
}

func NewGetBalance(r *http.Request) (getBalance GetBalance, err error) {
Expand Down

0 comments on commit 0abe7ee

Please sign in to comment.