Skip to content

Commit

Permalink
Merge pull request #21 from rarimo/debug/activate-balance
Browse files Browse the repository at this point in the history
Debug: activate balance
  • Loading branch information
violog authored Jul 19, 2024
2 parents 47ed105 + 3206075 commit 113f5b7
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 127 deletions.
56 changes: 21 additions & 35 deletions internal/service/handlers/activate_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/rarimo/geo-auth-svc/pkg/auth"
"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/service/requests"
"gitlab.com/distributed_lab/ape"
Expand All @@ -20,8 +19,11 @@ func ActivateBalance(w http.ResponseWriter, r *http.Request) {
return
}

nullifier := req.Data.ID
referralCode := req.Data.Attributes.ReferredBy
var (
nullifier = req.Data.ID
referralCode = req.Data.Attributes.ReferredBy
log = Log(r).WithField("nullifier", nullifier)
)

if !auth.Authenticates(UserClaims(r), auth.UserGrant(nullifier)) {
ape.RenderErr(w, problems.Unauthorized())
Expand All @@ -30,38 +32,38 @@ func ActivateBalance(w http.ResponseWriter, r *http.Request) {

balance, err := BalancesQ(r).FilterByNullifier(nullifier).Get()
if err != nil {
Log(r).WithError(err).Error("Failed to get balance by nullifier")
log.WithError(err).Error("Failed to get balance by nullifier")
ape.RenderErr(w, problems.InternalError())
return
}

if balance == nil {
Log(r).Debugf("Balance %s not exist", nullifier)
log.Debug("Balance not found")
ape.RenderErr(w, problems.NotFound())
return
}

if balance.ReferredBy != nil {
Log(r).Infof("Balance already activated with code '%s'", *balance.ReferredBy)
log.Infof("Balance already activated with code %s", *balance.ReferredBy)
ape.RenderErr(w, problems.Conflict())
return
}

referral, err := ReferralsQ(r).FilterInactive().Get(req.Data.Attributes.ReferredBy)
referral, err := ReferralsQ(r).FilterInactive().Get(referralCode)
if err != nil {
Log(r).WithError(err).Error("Failed to get referral by ID")
log.WithError(err).Error("Failed to get referral by ID")
ape.RenderErr(w, problems.InternalError())
return
}
if referral == nil {
Log(r).Debugf("Referral code '%s' not found", referralCode)
log.Debugf("Referral code %s not found", referralCode)
ape.RenderErr(w, problems.NotFound())
return
}

refBalance, err := BalancesQ(r).FilterByNullifier(referral.Nullifier).Get()
if err != nil || refBalance == nil { // must exist due to FK constraint
Log(r).WithError(err).Error("Failed to get referrer balance by nullifier")
log.WithError(err).Error("Failed to get referrer balance by nullifier")
ape.RenderErr(w, problems.InternalError())
return
}
Expand All @@ -73,14 +75,15 @@ func ActivateBalance(w http.ResponseWriter, r *http.Request) {
}

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

if refBalance.ReferredBy != nil {
log.Debug("Be referred event will be fulfilled for referee")
err = EventsQ(r).Insert(data.Event{
Nullifier: nullifier,
Type: models.TypeBeReferred,
Expand All @@ -95,33 +98,16 @@ func ActivateBalance(w http.ResponseWriter, r *http.Request) {
return fmt.Errorf("failed to consume referral: %w", err)
}

if balance.IsVerified {
// Be referred event is a welcome bonus when you created balance with non-genesis referral code
if err = claimBeReferredEvent(r, *balance); err != nil {
return fmt.Errorf("failed to claim be referred event: %w", err)
}
}

evTypeRef := EventTypes(r).Get(models.TypeReferralSpecific, evtypes.FilterInactive)
if evTypeRef == nil {
Log(r).Debug("Referral specific event type is inactive")
if !balance.IsVerified {
log.Debug("Balance is not verified, events will not be claimed")
return nil
}

if balance.IsVerified {
if err = claimReferralSpecificEvents(r, *evTypeRef, balance.Nullifier); err != nil {
return fmt.Errorf("failed to claim referral specific events: %w", err)
}
}

if err = addEventForReferrer(r, *evTypeRef, *balance); err != nil {
return fmt.Errorf("add event for referrer: %w", err)
}

return nil
balance.ReferredBy = &referral.ID
return doVerificationEventUpdates(r, *balance)
})
if err != nil {
Log(r).WithError(err).Error("Failed to insert events and consume referral for balance")
log.WithError(err).Error("Failed to insert events and consume referral for balance")
ape.RenderErr(w, problems.InternalError())
return
}
Expand All @@ -131,7 +117,7 @@ func ActivateBalance(w http.ResponseWriter, r *http.Request) {
// while with RETURNING we operate a single one.
// Balance will exist cause of previous logic.
if balance, err = BalancesQ(r).GetWithRank(nullifier); err != nil {
Log(r).WithError(err).Error("Failed to get created balance by nullifier")
log.WithError(err).Error("Failed to get created balance by nullifier")
ape.RenderErr(w, problems.InternalError())
return
}
Expand All @@ -141,7 +127,7 @@ func ActivateBalance(w http.ResponseWriter, r *http.Request) {
WithStatus().
Select()
if err != nil {
Log(r).WithError(err).Error("Failed to get referrals by nullifier with rewarding field")
log.WithError(err).Error("Failed to get referrals by nullifier with rewarding field")
ape.RenderErr(w, problems.InternalError())
return
}
Expand Down
15 changes: 9 additions & 6 deletions internal/service/handlers/create_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ func CreateBalance(w http.ResponseWriter, r *http.Request) {
var (
refCode = req.Data.Attributes.ReferredBy
isGenesisRef = false
log = Log(r).WithField("nullifier", nullifier)
)

if refCode != nil {
log.Debug("Balance will be activated with referral code")
referral, err := ReferralsQ(r).FilterInactive().Get(*refCode)
if err != nil {
Log(r).WithError(err).Error("Failed to get referral by ID")
log.WithError(err).Error("Failed to get referral by ID")
ape.RenderErr(w, problems.InternalError())
return
}
Expand All @@ -56,7 +58,7 @@ func CreateBalance(w http.ResponseWriter, r *http.Request) {

refBalance, err := BalancesQ(r).FilterByNullifier(referral.Nullifier).Get()
if err != nil || refBalance == nil { // must exist due to FK constraint
Log(r).WithError(err).Error("Failed to get referrer balance by nullifier")
log.WithError(err).Error("Failed to get referrer balance by nullifier")
ape.RenderErr(w, problems.InternalError())
return
}
Expand All @@ -67,18 +69,19 @@ func CreateBalance(w http.ResponseWriter, r *http.Request) {
if refCode == nil {
balance, err = createBalanceWithEvents(nullifier, events, r)
if err != nil {
Log(r).WithError(err).Error("Failed to create disabled balance with events")
log.WithError(err).Error("Failed to create disabled balance with events")
ape.RenderErr(w, problems.InternalError())
return
}

log.Debug("Created disabled balance with events")
ape.Render(w, newBalanceResponse(*balance, nil, 0, 0))
return
}

err = createBalanceWithEventsAndReferrals(nullifier, *refCode, events, r)
if err != nil {
Log(r).WithError(err).Error("Failed to create balance with events and referrals")
log.WithError(err).Error("Failed to create balance with events and referrals")
ape.RenderErr(w, problems.InternalError())
return
}
Expand All @@ -89,7 +92,7 @@ func CreateBalance(w http.ResponseWriter, r *http.Request) {
// Balance will exist cause of previous logic.
balance, err = BalancesQ(r).GetWithRank(nullifier)
if err != nil {
Log(r).WithError(err).Error("Failed to get created balance by nullifier")
log.WithError(err).Error("Failed to get created balance by nullifier")
ape.RenderErr(w, problems.InternalError())
return
}
Expand All @@ -99,7 +102,7 @@ func CreateBalance(w http.ResponseWriter, r *http.Request) {
WithStatus().
Select()
if err != nil {
Log(r).WithError(err).Error("Failed to get referrals by nullifier with rewarding field")
log.WithError(err).Error("Failed to get referrals by nullifier with rewarding field")
ape.RenderErr(w, problems.InternalError())
return
}
Expand Down
Loading

0 comments on commit 113f5b7

Please sign in to comment.