Skip to content

Commit

Permalink
Fix issues with balances, referrals, QR code events
Browse files Browse the repository at this point in the history
  • Loading branch information
violog committed Jul 2, 2024
1 parent 122e16b commit 4440ab5
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 45 deletions.
12 changes: 1 addition & 11 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,14 @@ event_types:
frequency: unlimited
description: Prove your participation by scanning QR code
short_description: Short description
no_auto_open: true
auto_claim: true
qr_code_value: "qr_code_base64_string"

levels:
levels:
- lvl: 1
threshold: 0
referrals: 5
withdrawal_allowed: false
- lvl: 2
threshold: 5
referrals: 5
withdrawal_allowed: true
- lvl: 3
threshold: 6
referrals: 5
withdrawal_allowed: true
referrals: 1

countries:
verification_key: "37bc75afc97f8bdcd21cda85ae7b2885b5f1205ae3d79942e56457230f1636a037cc7ebfe42998d66a3dd3446b9d29366271b4f2bd8e0d307db1d320b38fc02f"
Expand Down
2 changes: 1 addition & 1 deletion docs/spec/components/schemas/CreateBalanceKey.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ properties:
pattern: '^0x[0-9a-fA-F]{64}$'
type:
type: string
enum: [ create_balance, update_balance ]
enum: [ create_balance ]
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ patch:
- data
properties:
data:
$ref: '#/components/schemas/ClaimEventKey'
$ref: '#/components/schemas/FulfillQREvent'
responses:
200:
description: Success
Expand Down
24 changes: 12 additions & 12 deletions internal/assets/migrations/001_initial.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ AS $$ BEGIN NEW.updated_at = EXTRACT('EPOCH' FROM NOW()); RETURN NEW; END; $$;

CREATE TABLE IF NOT EXISTS balances
(
nullifier TEXT PRIMARY KEY,
amount bigint NOT NULL default 0,
created_at integer NOT NULL default EXTRACT('EPOCH' FROM NOW()),
updated_at integer NOT NULL default EXTRACT('EPOCH' FROM NOW()),
referred_by text UNIQUE,
level INT NOT NULL,
anonymous_id TEXT UNIQUE,
is_verified BOOLEAN NOT NULL default FALSE,
is_passport_proven BOOLEAN NOT NULL default FALSE
nullifier TEXT PRIMARY KEY,
amount bigint NOT NULL default 0,
created_at integer NOT NULL default EXTRACT('EPOCH' FROM NOW()),
updated_at integer NOT NULL default EXTRACT('EPOCH' FROM NOW()),
referred_by text,
level INT NOT NULL,
anonymous_id TEXT UNIQUE,
is_verified BOOLEAN NOT NULL default FALSE,
is_passport_proven BOOLEAN NOT NULL default FALSE
);

CREATE INDEX IF NOT EXISTS balances_page_index ON balances (amount, updated_at) WHERE referred_by IS NOT NULL;
Expand All @@ -27,8 +27,8 @@ EXECUTE FUNCTION trigger_set_updated_at();

CREATE TABLE IF NOT EXISTS referrals
(
id text PRIMARY KEY,
nullifier TEXT NOT NULL REFERENCES balances (nullifier),
id text PRIMARY KEY,
nullifier TEXT NOT NULL REFERENCES balances (nullifier),
usage_left INTEGER NOT NULL DEFAULT 1
);

Expand All @@ -40,7 +40,7 @@ CREATE TYPE event_status AS ENUM ('open', 'fulfilled', 'claimed');
CREATE TABLE IF NOT EXISTS events
(
id uuid PRIMARY KEY NOT NULL default gen_random_uuid(),
nullifier TEXT NOT NULL REFERENCES balances (nullifier),
nullifier TEXT NOT NULL REFERENCES balances (nullifier),
type text NOT NULL,
status event_status NOT NULL,
created_at integer NOT NULL default EXTRACT('EPOCH' FROM NOW()),
Expand Down
7 changes: 3 additions & 4 deletions internal/config/levels.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
)

type Level struct {
Level int `fig:"lvl,required"`
Threshold int `fig:"threshold,required"`
Referrals int `fig:"referrals,required"`
WithdrawalAllowed bool `fig:"withdrawal_allowed"`
Level int `fig:"lvl,required"`
Threshold int `fig:"threshold,required"`
Referrals int `fig:"referrals,required"`
}

type Levels map[int]Level
Expand Down
11 changes: 8 additions & 3 deletions internal/service/handlers/create_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,19 @@ func CreateBalance(w http.ResponseWriter, r *http.Request) {
return
}

referral, err = ReferralsQ(r).Get(nullifier)
if err != nil || referral == nil {
referrals, err := ReferralsQ(r).FilterByNullifier(nullifier).Select()
if err != nil {
Log(r).WithError(err).Error("Failed to get referral code by nullifier")
ape.RenderErr(w, problems.InternalError())
return
}
if len(referrals) != 1 {
Log(r).WithError(err).Error("There must be only 1 referral code")
ape.RenderErr(w, problems.InternalError())
return
}

ape.Render(w, newBalanceResponse(*balance, referral))
ape.Render(w, newBalanceResponse(*balance, &referrals[0]))
}

func prepareEventsWithRef(nullifier, refBy string, r *http.Request) []data.Event {
Expand Down
10 changes: 1 addition & 9 deletions internal/service/handlers/fulfill_qr_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,5 @@ func FulfillQREvent(w http.ResponseWriter, r *http.Request) {
return
}

// balance should exist cause of previous logic
balance, err = BalancesQ(r).GetWithRank(event.Nullifier)
if err != nil {
Log(r).WithError(err).Error("Failed to get balance by nullifier with rank")
ape.RenderErr(w, problems.InternalError())
return
}

ape.Render(w, newClaimEventResponse(*event, evType.Resource(), *balance))
ape.Render(w, newEventClaimingStateResponse(balance.Nullifier, true))
}
12 changes: 9 additions & 3 deletions internal/service/handlers/get_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,18 @@ func GetBalance(w http.ResponseWriter, r *http.Request) {

var referral *data.Referral
if req.ReferralCodes {
referral, err = ReferralsQ(r).Get(req.Nullifier)
if err != nil || referral == nil {
Log(r).WithError(err).Error("Failed to get referral by code nullifier")
referrals, err := ReferralsQ(r).FilterByNullifier(req.Nullifier).Select()
if err != nil {
Log(r).WithError(err).Error("Failed to get referral code by nullifier")
ape.RenderErr(w, problems.InternalError())
return
}
if len(referrals) != 1 {
Log(r).WithError(err).Error("There must be exactly 1 referral code")
ape.RenderErr(w, problems.InternalError())
return
}
referral = &referrals[0]
}

ape.Render(w, newBalanceResponse(*balance, referral))
Expand Down
2 changes: 1 addition & 1 deletion internal/service/handlers/verify_passport.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func VerifyPassport(w http.ResponseWriter, r *http.Request) {
return
}

ape.Render(w, newEventClaimingStateResponse(req.Data.ID, true))
ape.Render(w, newEventClaimingStateResponse(req.Data.ID, false))
return
}

Expand Down

0 comments on commit 4440ab5

Please sign in to comment.