Skip to content

Commit

Permalink
Merge pull request #19 from rarimo/fix/upsert-verify-user
Browse files Browse the repository at this point in the history
* update: auth version to have config disabled by default in any case

* feature: implement upsert to update params for users with the same user id

* add: helper function to check default ZK date

* update: use Upsert instead of Get/Insert approach, use helper default zk date for clear code
  • Loading branch information
mhrynenko authored Dec 11, 2024
2 parents 754ffb5 + 4991bce commit 3e4401a
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 55 deletions.
30 changes: 18 additions & 12 deletions internal/data/pg/verify_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import (
"fmt"
"time"

"github.com/pkg/errors"
"gitlab.com/distributed_lab/kit/pgdb"

sq "github.com/Masterminds/squirrel"

"github.com/pkg/errors"
"github.com/rarimo/verificator-svc/internal/data"
"gitlab.com/distributed_lab/kit/pgdb"
)

type VerifyUsersQ struct {
Expand Down Expand Up @@ -62,14 +60,14 @@ func (q *VerifyUsersQ) Get() (*data.VerifyUsers, error) {
return &result, nil
}

func (q *VerifyUsersQ) Insert(VerifyUsers *data.VerifyUsers) error {
func (q *VerifyUsersQ) Upsert(VerifyUsers *data.VerifyUsers) (data.VerifyUsers, error) {
var response data.VerifyUsers
proofJSON, err := json.Marshal(VerifyUsers.Proof)
if err != nil {
return fmt.Errorf("failed to marshal proof for user %s: %w", VerifyUsers.UserID, err)
return response, fmt.Errorf("failed to marshal proof for user %s: %w", VerifyUsers.UserID, err)
}

stmt := sq.Insert(verifyUsersTableName).SetMap(map[string]interface{}{
"user_id": VerifyUsers.UserID,
newData := map[string]interface{}{
"user_id_hash": VerifyUsers.UserIDHash,
"age_lower_bound": VerifyUsers.AgeLowerBound,
"nationality": VerifyUsers.Nationality,
Expand All @@ -83,13 +81,21 @@ func (q *VerifyUsersQ) Insert(VerifyUsers *data.VerifyUsers) error {
"anonymous_id": VerifyUsers.AnonymousID,
"nullifier": VerifyUsers.Nullifier,
"expiration_lower_bound": VerifyUsers.ExpirationLowerBound,
})
}

updateStmt, args, _ := sq.Update(" ").SetMap(newData).ToSql()

newData["user_id"] = VerifyUsers.UserID

if err = q.db.Exec(stmt); err != nil {
return fmt.Errorf("insert user %+v: %w", VerifyUsers, err)
query := sq.Insert(verifyUsersTableName).SetMap(newData).
Suffix("ON CONFLICT (user_id) DO "+updateStmt, args...).
Suffix("RETURNING *")

if err = q.db.Get(&response, query); err != nil {
return response, errors.Wrap(err, "failed to upsert new row")
}

return nil
return response, nil
}

func (q *VerifyUsersQ) Update(VerifyUsers *data.VerifyUsers) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/data/verify_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type VerifyUsersQ interface {
Get() (*VerifyUsers, error)
Select() ([]VerifyUsers, error)
Update(*VerifyUsers) error
Insert(*VerifyUsers) error
Upsert(*VerifyUsers) (VerifyUsers, error)
Delete() error

DeleteByID(*VerifyUsers) error
Expand Down
16 changes: 3 additions & 13 deletions internal/service/handlers/get_proof_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,12 @@ func GetProofParameters(w http.ResponseWriter, r *http.Request) {
TimestampUpperBound: TimestampUpperBound,
}

existingUser, err := VerifyUsersQ(r).WhereHashID(user.UserIDHash).Get()
dbUser, err := VerifyUsersQ(r).Upsert(user)
if err != nil {
Log(r).WithError(err).Errorf("failed to query user with userID [%s]", user.UserIDHash)
ape.RenderErr(w, problems.InternalError())
return
}
if existingUser != nil {
ape.Render(w, responses.NewProofParametersResponse(*existingUser, proofParameters))
return
}

if err = VerifyUsersQ(r).Insert(user); err != nil {
Log(r).WithError(err).Errorf("failed to insert user with userID [%s]", user.UserIDHash)
Log(r).WithError(err).WithField("user", user).Errorf("failed to upsert user with userID [%s]", user.UserIDHash)
ape.RenderErr(w, problems.InternalError())
return
}

ape.Render(w, responses.NewProofParametersResponse(*user, proofParameters))
ape.Render(w, responses.NewProofParametersResponse(dbUser, proofParameters))
}
4 changes: 4 additions & 0 deletions internal/service/handlers/helpers/proof_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,7 @@ func CheckUniqueness(selectorInt int, serviceStartTimestamp, identityTimestampUp

return status, nil
}

func IsDefaultZKDate(date string) bool {
return date == DefaultDateHex
}
2 changes: 1 addition & 1 deletion internal/service/handlers/proof_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func GetProofParamsById(w http.ResponseWriter, r *http.Request) {
Nationality: existingUser.Nationality,
SexEnable: existingUser.SexEnable,
NationalityEnable: existingUser.NationalityEnable,
ExpirationLowerBound: existingUser.ExpirationLowerBound != helpers.DefaultDateHex, // If there is non-default value, selector should be enabled
ExpirationLowerBound: !helpers.IsDefaultZKDate(existingUser.ExpirationLowerBound), // If there is non-default value, selector should be enabled
})
callbackURL = fmt.Sprintf("%s/integrations/verificator-svc/public/callback/%s", Callback(r).URL, userIDHash)
)
Expand Down
2 changes: 1 addition & 1 deletion internal/service/handlers/proof_params_light.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func GetProofParamsLightById(w http.ResponseWriter, r *http.Request) {
Nationality: existingUser.Nationality,
SexEnable: existingUser.SexEnable,
NationalityEnable: existingUser.NationalityEnable,
ExpirationLowerBound: existingUser.ExpirationLowerBound != helpers.DefaultDateHex, // If there is non-default value, selector should be enabled
ExpirationLowerBound: !helpers.IsDefaultZKDate(existingUser.ExpirationLowerBound), // If there is non-default value, selector should be enabled
})
callbackURL = fmt.Sprintf("%s/integrations/verificator-svc/light/public/callback-sign/%s", Callback(r).URL, userIDHash)
)
Expand Down
17 changes: 3 additions & 14 deletions internal/service/handlers/verification_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,12 @@ func VerificationLink(w http.ResponseWriter, r *http.Request) {
user.ExpirationLowerBound = helpers.GetExpirationLowerBound(*req.Data.Attributes.ExpirationLowerBound)
}

existingUser, err := VerifyUsersQ(r).WhereHashID(user.UserIDHash).Get()
dbUser, err := VerifyUsersQ(r).Upsert(user)
if err != nil {
Log(r).WithError(err).Errorf("failed to query user with userID [%s]", user.UserIDHash)
Log(r).WithError(err).WithField("user", user).Errorf("failed to upsert user with userID [%s]", user.UserIDHash)
ape.RenderErr(w, problems.InternalError())
return
}
if existingUser != nil {
ape.Render(w, responses.NewVerificationLinkResponse(*existingUser, Callback(r).URL))
return
}

if err = VerifyUsersQ(r).Insert(user); err != nil {
Log(r).WithError(err).Errorf("failed to insert user with userID [%s]", user.UserIDHash)
ape.RenderErr(w, problems.InternalError())
return
}

ape.Render(w, responses.NewVerificationLinkResponse(*user, Callback(r).URL))

ape.Render(w, responses.NewVerificationLinkResponse(dbUser, Callback(r).URL))
}
16 changes: 3 additions & 13 deletions internal/service/handlers/verification_link_light.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,13 @@ func VerificationLinkLight(w http.ResponseWriter, r *http.Request) {
user.ExpirationLowerBound = helpers.GetExpirationLowerBound(*req.Data.Attributes.ExpirationLowerBound)
}

existingUser, err := VerifyUsersQ(r).WhereHashID(user.UserIDHash).Get()
dbUser, err := VerifyUsersQ(r).Upsert(user)
if err != nil {
Log(r).WithError(err).Errorf("failed to query user with userID [%s]", user.UserIDHash)
ape.RenderErr(w, problems.InternalError())
return
}
if existingUser != nil {
ape.Render(w, responses.NewVerificationLinkLightResponse(*existingUser, Callback(r).URL))
return
}

if err = VerifyUsersQ(r).Insert(user); err != nil {
Log(r).WithError(err).Errorf("failed to insert user with userID [%s]", user.UserIDHash)
Log(r).WithError(err).WithField("user", user).Errorf("failed to upsert user with userID [%s]", user.UserIDHash)
ape.RenderErr(w, problems.InternalError())
return
}

ape.Render(w, responses.NewVerificationLinkLightResponse(*user, Callback(r).URL))
ape.Render(w, responses.NewVerificationLinkLightResponse(dbUser, Callback(r).URL))

}

0 comments on commit 3e4401a

Please sign in to comment.