From c3657e7dae644d2a7270799326bcdeeaebc1da1c Mon Sep 17 00:00:00 2001 From: roman_harazha Date: Wed, 10 Jan 2024 17:33:41 +0200 Subject: [PATCH] add org id to proofs --- docs/spec/components/schemas/Proof.yaml | 8 +++++-- docs/spec/components/schemas/ProofCreate.yaml | 8 +++++-- internal/assets/migrations/001_initial.sql | 1 + internal/data/main.go | 1 + internal/data/pg/link_schemas.go | 1 + internal/data/pg/links_to_proofs_schemas.go | 1 + internal/data/pg/proof_schemas.go | 1 + internal/data/pg/schema.xo.go | 24 +++++++++---------- internal/data/schema.xo.go | 1 + .../services/api/handlers/create_proof.go | 14 ++++++++--- internal/services/api/handlers/get_proof.go | 3 ++- .../api/handlers/get_proofs_by_link.go | 11 +++++---- .../api/handlers/get_proofs_by_user.go | 3 ++- resources/model_proof_attributes.go | 4 +++- resources/model_proof_create.go | 4 +++- 15 files changed, 57 insertions(+), 28 deletions(-) diff --git a/docs/spec/components/schemas/Proof.yaml b/docs/spec/components/schemas/Proof.yaml index 6939e77..b09e99e 100644 --- a/docs/spec/components/schemas/Proof.yaml +++ b/docs/spec/components/schemas/Proof.yaml @@ -10,7 +10,8 @@ allOf: - creator - created_at - proof - - type + - proof_type + - org_id properties: creator: type: string @@ -23,6 +24,9 @@ allOf: type: string description: The proof object in JSON string format example: "{\"pub_signals\":[...],\"proof\":{\"pi_a\":[...],\"pi_b\":[],\"pi_c\":[...]}}" - type: + proof_type: type: string description: The type of the proof + org_id: + type: string + description: The ID of the organization that issued the proof's claim diff --git a/docs/spec/components/schemas/ProofCreate.yaml b/docs/spec/components/schemas/ProofCreate.yaml index 982ed13..5afb079 100644 --- a/docs/spec/components/schemas/ProofCreate.yaml +++ b/docs/spec/components/schemas/ProofCreate.yaml @@ -1,12 +1,16 @@ type: object required: - proof - - type + - proof_type + - org_id properties: proof: type: string description: The proof object in JSON string format example: "{\"pub_signals\":[...],\"proof\":{\"pi_a\":[...],\"pi_b\":[],\"pi_c\":[...]}}" - type: + proof_type: type: string description: The type of the proof + org_id: + type: string + description: The ID of the organization that issued the proof's claim diff --git a/internal/assets/migrations/001_initial.sql b/internal/assets/migrations/001_initial.sql index a24da3f..754e6ab 100644 --- a/internal/assets/migrations/001_initial.sql +++ b/internal/assets/migrations/001_initial.sql @@ -4,6 +4,7 @@ create table if not exists proofs( creator text not null, created_at timestamp without time zone not null default now(), proof jsonb not null, + org_id uuid not null, type text not null ); diff --git a/internal/data/main.go b/internal/data/main.go index b6e805d..e47f87c 100644 --- a/internal/data/main.go +++ b/internal/data/main.go @@ -2,6 +2,7 @@ package data import ( "context" + "github.com/google/uuid" ) diff --git a/internal/data/pg/link_schemas.go b/internal/data/pg/link_schemas.go index 11c2d94..33c4859 100644 --- a/internal/data/pg/link_schemas.go +++ b/internal/data/pg/link_schemas.go @@ -2,6 +2,7 @@ package pg import ( "context" + "github.com/Masterminds/squirrel" "github.com/rarimo/rarime-link-svc/internal/data" ) diff --git a/internal/data/pg/links_to_proofs_schemas.go b/internal/data/pg/links_to_proofs_schemas.go index dd69c0b..f7a126f 100644 --- a/internal/data/pg/links_to_proofs_schemas.go +++ b/internal/data/pg/links_to_proofs_schemas.go @@ -2,6 +2,7 @@ package pg import ( "context" + "github.com/Masterminds/squirrel" "github.com/google/uuid" "github.com/rarimo/rarime-link-svc/internal/data" diff --git a/internal/data/pg/proof_schemas.go b/internal/data/pg/proof_schemas.go index c2c2b59..50fc57a 100644 --- a/internal/data/pg/proof_schemas.go +++ b/internal/data/pg/proof_schemas.go @@ -2,6 +2,7 @@ package pg import ( "context" + "github.com/Masterminds/squirrel" "github.com/rarimo/rarime-link-svc/internal/data" ) diff --git a/internal/data/pg/schema.xo.go b/internal/data/pg/schema.xo.go index 27f8c4d..96f7968 100644 --- a/internal/data/pg/schema.xo.go +++ b/internal/data/pg/schema.xo.go @@ -292,18 +292,18 @@ func (s Storage) ProofQ() data.ProofQ { return NewProofQ(s.DB()) } -var colsProof = `id, creator, created_at, proof, type` +var colsProof = `id, creator, created_at, proof, org_id, type` // InsertCtx inserts a Proof to the database. func (q ProofQ) InsertCtx(ctx context.Context, p *data.Proof) error { // sql insert query, primary key must be provided sqlstr := `INSERT INTO public.proofs (` + - `id, creator, created_at, proof, type` + + `id, creator, created_at, proof, org_id, type` + `) VALUES (` + - `$1, $2, $3, $4, $5` + + `$1, $2, $3, $4, $5, $6` + `)` // run - err := q.db.ExecRawContext(ctx, sqlstr, p.ID, p.Creator, p.CreatedAt, p.Proof, p.Type) + err := q.db.ExecRawContext(ctx, sqlstr, p.ID, p.Creator, p.CreatedAt, p.Proof, p.OrgID, p.Type) return errors.Wrap(err, "failed to execute insert query") } @@ -316,10 +316,10 @@ func (q ProofQ) Insert(p *data.Proof) error { func (q ProofQ) UpdateCtx(ctx context.Context, p *data.Proof) error { // update with composite primary key sqlstr := `UPDATE public.proofs SET ` + - `creator = $1, proof = $2, type = $3 ` + - `WHERE id = $4` + `creator = $1, proof = $2, org_id = $3, type = $4 ` + + `WHERE id = $5` // run - err := q.db.ExecRawContext(ctx, sqlstr, p.Creator, p.Proof, p.Type, p.ID) + err := q.db.ExecRawContext(ctx, sqlstr, p.Creator, p.Proof, p.OrgID, p.Type, p.ID) return errors.Wrap(err, "failed to execute update") } @@ -332,15 +332,15 @@ func (q ProofQ) Update(p *data.Proof) error { func (q ProofQ) UpsertCtx(ctx context.Context, p *data.Proof) error { // upsert sqlstr := `INSERT INTO public.proofs (` + - `id, creator, created_at, proof, type` + + `id, creator, created_at, proof, org_id, type` + `) VALUES (` + - `$1, $2, $3, $4, $5` + + `$1, $2, $3, $4, $5, $6` + `)` + ` ON CONFLICT (id) DO ` + `UPDATE SET ` + - `creator = EXCLUDED.creator, proof = EXCLUDED.proof, type = EXCLUDED.type ` + `creator = EXCLUDED.creator, proof = EXCLUDED.proof, org_id = EXCLUDED.org_id, type = EXCLUDED.type ` // run - if err := q.db.ExecRawContext(ctx, sqlstr, p.ID, p.Creator, p.CreatedAt, p.Proof, p.Type); err != nil { + if err := q.db.ExecRawContext(ctx, sqlstr, p.ID, p.Creator, p.CreatedAt, p.Proof, p.OrgID, p.Type); err != nil { return errors.Wrap(err, "failed to execute upsert stmt") } return nil @@ -473,7 +473,7 @@ func (q LinksToProofQ) LinksToProofByLinkIDProofID(linkID, proofID uuid.UUID, is func (q ProofQ) ProofByIDCtx(ctx context.Context, id uuid.UUID, isForUpdate bool) (*data.Proof, error) { // query sqlstr := `SELECT ` + - `id, creator, created_at, proof, type ` + + `id, creator, created_at, proof, org_id, type ` + `FROM public.proofs ` + `WHERE id = $1` // run diff --git a/internal/data/schema.xo.go b/internal/data/schema.xo.go index 133d6a3..e7c81ca 100644 --- a/internal/data/schema.xo.go +++ b/internal/data/schema.xo.go @@ -92,6 +92,7 @@ type Proof struct { Creator string `db:"creator" json:"creator" structs:"creator"` // creator CreatedAt time.Time `db:"created_at" json:"created_at" structs:"created_at"` // created_at Proof xo.Jsonb `db:"proof" json:"proof" structs:"proof"` // proof + OrgID uuid.UUID `db:"org_id" json:"org_id" structs:"org_id"` // org_id Type string `db:"type" json:"type" structs:"type"` // type } diff --git a/internal/services/api/handlers/create_proof.go b/internal/services/api/handlers/create_proof.go index 8097962..6a3b64b 100644 --- a/internal/services/api/handlers/create_proof.go +++ b/internal/services/api/handlers/create_proof.go @@ -31,7 +31,7 @@ func newProofCreateRequest(r *http.Request) (*proofCreateRequest, error) { } } - if req.Data.Type == "" { + if req.Data.ProofType == "" { return nil, validation.Errors{ "type": errors.New("type is required"), } @@ -47,12 +47,19 @@ func CreateProof(w http.ResponseWriter, r *http.Request) { return } + orgID, err := uuid.Parse(req.Data.OrgId) + if err != nil { + ape.RenderErr(w, problems.BadRequest(err)...) + return + } + proof := data.Proof{ ID: uuid.New(), Creator: UserID(r), CreatedAt: time.Now().UTC(), Proof: []byte(req.Data.Proof), - Type: req.Data.Type, + Type: req.Data.ProofType, + OrgID: orgID, } err = Storage(r).ProofQ().Insert(&proof) @@ -72,7 +79,8 @@ func CreateProof(w http.ResponseWriter, r *http.Request) { CreatedAt: strconv.FormatInt(proof.CreatedAt.Unix(), 10), Creator: proof.Creator, Proof: string(proof.Proof), - Type: proof.Type, + ProofType: proof.Type, + OrgId: proof.OrgID.String(), }, }, Included: resources.Included{}, diff --git a/internal/services/api/handlers/get_proof.go b/internal/services/api/handlers/get_proof.go index 95e4d3f..be518fe 100644 --- a/internal/services/api/handlers/get_proof.go +++ b/internal/services/api/handlers/get_proof.go @@ -57,7 +57,8 @@ func ProofByID(w http.ResponseWriter, r *http.Request) { CreatedAt: strconv.FormatInt(proof.CreatedAt.Unix(), 10), Creator: proof.Creator, Proof: string(proof.Proof), - Type: proof.Type, + ProofType: proof.Type, + OrgId: proof.OrgID.String(), }, }, Included: resources.Included{}, diff --git a/internal/services/api/handlers/get_proofs_by_link.go b/internal/services/api/handlers/get_proofs_by_link.go index a791cd6..3e898f1 100644 --- a/internal/services/api/handlers/get_proofs_by_link.go +++ b/internal/services/api/handlers/get_proofs_by_link.go @@ -35,22 +35,22 @@ func GetProofsByLinkID(w http.ResponseWriter, r *http.Request) { return } - proofs, err := Storage(r).LinksToProofQ().GetLinksToProofsByLinkID(r.Context(), req.LinkID) + links, err := Storage(r).LinksToProofQ().GetLinksToProofsByLinkID(r.Context(), req.LinkID) if err != nil { Log(r).WithError(err).Error("failed to get link to proofs") ape.RenderErr(w, problems.InternalError()) return } - if proofs == nil { + if links == nil { Log(r).WithField("link_id", req.LinkID).Warn("link not found") ape.RenderErr(w, problems.NotFound()) return } var response resources.ProofListResponse - for _, proof := range proofs { - proof, err := Storage(r).ProofQ().ProofByID(proof.ProofID, false) + for _, link := range links { + proof, err := Storage(r).ProofQ().ProofByID(link.ProofID, false) if err != nil { Log(r).WithError(err).Error("failed to get proof") ape.RenderErr(w, problems.InternalError()) @@ -66,7 +66,8 @@ func GetProofsByLinkID(w http.ResponseWriter, r *http.Request) { CreatedAt: proof.CreatedAt.String(), Creator: proof.Creator, Proof: string(proof.Proof), - Type: proof.Type, + ProofType: proof.Type, + OrgId: proof.OrgID.String(), }, }, } diff --git a/internal/services/api/handlers/get_proofs_by_user.go b/internal/services/api/handlers/get_proofs_by_user.go index be93acc..974d15f 100644 --- a/internal/services/api/handlers/get_proofs_by_user.go +++ b/internal/services/api/handlers/get_proofs_by_user.go @@ -59,7 +59,8 @@ func GetProofsByUserDID(w http.ResponseWriter, r *http.Request) { CreatedAt: strconv.FormatInt(proof.CreatedAt.Unix(), 10), Creator: proof.Creator, Proof: string(proof.Proof), - Type: proof.Type, + ProofType: proof.Type, + OrgId: proof.OrgID.String(), }, }, } diff --git a/resources/model_proof_attributes.go b/resources/model_proof_attributes.go index 75a2ecd..39aa8dd 100644 --- a/resources/model_proof_attributes.go +++ b/resources/model_proof_attributes.go @@ -9,8 +9,10 @@ type ProofAttributes struct { CreatedAt string `json:"created_at"` // The ID of the user who created the proof Creator string `json:"creator"` + // The ID of the organization that issued the proof's claim + OrgId string `json:"org_id"` // The proof object in JSON string format Proof string `json:"proof"` // The type of the proof - Type string `json:"type"` + ProofType string `json:"proof_type"` } diff --git a/resources/model_proof_create.go b/resources/model_proof_create.go index e5382fb..7dfa995 100644 --- a/resources/model_proof_create.go +++ b/resources/model_proof_create.go @@ -5,8 +5,10 @@ package resources type ProofCreate struct { + // The ID of the organization that issued the proof's claim + OrgId string `json:"org_id"` // The proof object in JSON string format Proof string `json:"proof"` // The type of the proof - Type string `json:"type"` + ProofType string `json:"proof_type"` }