Skip to content

Commit

Permalink
Change CHAR to TEXT in db. Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaptoss committed May 23, 2024
1 parent 0b49dff commit 809ea3d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ post:
404:
$ref: '#/components/responses/notFound'
409:
description: Balance already exists for provide nullifier
description: Balance already exists for provided nullifier
content:
application/vnd.api+json:
schema:
Expand Down
10 changes: 5 additions & 5 deletions internal/assets/migrations/001_initial.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ AS $$ BEGIN NEW.updated_at = EXTRACT('EPOCH' FROM NOW()); RETURN NEW; END; $$;

CREATE TABLE IF NOT EXISTS balances
(
nullifier CHAR(66) PRIMARY KEY CHECK (nullifier ~ '^0x[0-9a-fA-F]{64}$'),
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()),
Expand All @@ -26,19 +26,19 @@ EXECUTE FUNCTION trigger_set_updated_at();
CREATE TABLE IF NOT EXISTS referrals
(
id text PRIMARY KEY,
nullifier CHAR(66) NOT NULL REFERENCES balances (nullifier),
nullifier TEXT NOT NULL REFERENCES balances (nullifier),
is_consumed boolean NOT NULL default false
);

ALTER TABLE balances ADD CONSTRAINT referred_by_fk FOREIGN KEY (referred_by) REFERENCES referrals (id);
CREATE INDEX IF NOT EXISTS referrals_user_did_index ON referrals (nullifier);
CREATE INDEX IF NOT EXISTS referrals_nullifier_index ON referrals (nullifier);

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 CHAR(66) 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 All @@ -60,7 +60,7 @@ EXECUTE FUNCTION trigger_set_updated_at();
CREATE TABLE IF NOT EXISTS withdrawals
(
id uuid PRIMARY KEY default gen_random_uuid(),
nullifier CHAR(66) NOT NULL REFERENCES balances (nullifier),
nullifier TEXT NOT NULL REFERENCES balances (nullifier),
amount integer NOT NULL,
address text NOT NULL,
created_at integer NOT NULL default EXTRACT('EPOCH' FROM NOW())
Expand Down
1 change: 0 additions & 1 deletion internal/data/pg/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

const balancesTable = "balances"
const balancesRankColumns = ", MAX(amount) as amount, created_at, updated_at, referred_by, passport_hash, passport_expires"

type balances struct {
db *pgdb.DB
Expand Down
12 changes: 6 additions & 6 deletions internal/service/handlers/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ import (
)

func Withdraw(w http.ResponseWriter, r *http.Request) {

if PointPrice(r).Disabled {
w.WriteHeader(http.StatusNoContent)
return
}

req, err := requests.NewWithdraw(r)
if err != nil {
ape.RenderErr(w, problems.BadRequest(err)...)
Expand All @@ -34,6 +28,12 @@ func Withdraw(w http.ResponseWriter, r *http.Request) {
"dest_address": req.Data.Attributes.Address,
})

if PointPrice(r).Disabled {
log.Debug("Withdrawal disabled!")
ape.RenderErr(w, problems.Forbidden())
return
}

if !auth.Authenticates(UserClaims(r), auth.UserGrant(req.Data.ID)) {
ape.RenderErr(w, problems.Unauthorized())
return
Expand Down
2 changes: 1 addition & 1 deletion internal/service/workers/reopener/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (c *initCollector) selectReopenable(freq evtypes.Frequency, before int64) (
types := c.types.Names(evtypes.FilterByFrequency(freq), evtypes.FilterInactive)

if len(types) == 0 {
return []data.ReopenableEvent{}, nil
return nil, nil
}

res, err := c.q.New().FilterByType(types...).
Expand Down

0 comments on commit 809ea3d

Please sign in to comment.