Skip to content

Commit

Permalink
update: use Upsert instead of Get/Insert approach, use helper default…
Browse files Browse the repository at this point in the history
… zk date for clear code
  • Loading branch information
mhrynenko committed Dec 13, 2024
1 parent d6ae94d commit f117000
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 42 deletions.
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))
}
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 f117000

Please sign in to comment.