-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from rarimo/feature/passport-scan-verify
Feature: connector and endpoint for passport scan verification
- Loading branch information
Showing
18 changed files
with
312 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ jobs: | |
with: | ||
reporter: github-pr-review | ||
cache: false | ||
golangci_lint_flags: --timeout=2m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package handlers | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/rarimo/rarime-points-svc/internal/data" | ||
"github.com/rarimo/rarime-points-svc/internal/data/evtypes" | ||
"github.com/rarimo/rarime-points-svc/internal/service/requests" | ||
"gitlab.com/distributed_lab/ape" | ||
"gitlab.com/distributed_lab/ape/problems" | ||
) | ||
|
||
func VerifyPassport(w http.ResponseWriter, r *http.Request) { | ||
req, err := requests.NewVerifyPassport(r) | ||
if err != nil { | ||
ape.RenderErr(w, problems.BadRequest(err)...) | ||
return | ||
} | ||
|
||
balance, err := BalancesQ(r).FilterByDID(req.UserDID).Get() | ||
if err != nil { | ||
Log(r).WithError(err).Error("Failed to get balance by DID") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
if balance == nil { | ||
ape.RenderErr(w, problems.NotFound()) | ||
return | ||
} | ||
|
||
err = EventsQ(r).Transaction(func() error { | ||
// If you make this endpoint public, you should check the passport hash for | ||
// uniqueness and provide a better validation. Think about other changes too. | ||
err = BalancesQ(r).FilterByDID(req.UserDID).SetPassport(req.Hash, req.Expiry) | ||
if err != nil { | ||
return fmt.Errorf("set passport for balance by DID: %w", err) | ||
} | ||
|
||
evType := EventTypes(r).Get(evtypes.TypeReferralSpecific, evtypes.FilterInactive) | ||
if evType == nil { | ||
Log(r).Debug("Referral event type is disabled or expired, not accruing points to referrer") | ||
return nil | ||
} | ||
|
||
refDID, err := getReferrerDID(*balance, r) | ||
if err != nil { | ||
return fmt.Errorf("get referrer DID by referred_by: %w", err) | ||
} | ||
if refDID == "" { | ||
return nil | ||
} | ||
|
||
err = EventsQ(r).Insert(data.Event{ | ||
UserDID: refDID, | ||
Type: evType.Name, | ||
Status: data.EventFulfilled, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("add event for referrer: %w", err) | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
if err != nil { | ||
Log(r).WithError(err).Error("Failed to set passport and add event for referrer") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusNoContent) | ||
} | ||
|
||
func getReferrerDID(balance data.Balance, r *http.Request) (string, error) { | ||
if !balance.ReferredBy.Valid { | ||
return "", nil | ||
} | ||
|
||
refBy := balance.ReferredBy.String | ||
referrer, err := BalancesQ(r).FilterByReferralID(refBy).Get() | ||
if err != nil { | ||
return "", fmt.Errorf("failed to get balance by referral ID: %w", err) | ||
} | ||
if referrer == nil { | ||
return "", fmt.Errorf("referrer not found: %s", refBy) | ||
} | ||
|
||
Log(r).Debugf("Found referrer: DID=%s", referrer.DID) | ||
return referrer.DID, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package requests | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"time" | ||
|
||
validation "github.com/go-ozzo/ozzo-validation/v4" | ||
"github.com/rarimo/rarime-points-svc/pkg/connector" | ||
) | ||
|
||
func NewVerifyPassport(r *http.Request) (req connector.VerifyPassportRequest, err error) { | ||
if err = json.NewDecoder(r.Body).Decode(&req); err != nil { | ||
return | ||
} | ||
|
||
return req, validation.Errors{ | ||
"user_did": validation.Validate(req.UserDID, validation.Required), | ||
"hash": validation.Validate(req.Hash, validation.Required), | ||
"expiry": validation.Validate(req.Expiry, validation.Required, validation.By(isNotExpiredRule)), | ||
}.Filter() | ||
} | ||
|
||
func isNotExpiredRule(value interface{}) error { | ||
v, ok := value.(time.Time) | ||
if !ok { | ||
panic("value is not a time.Time") // invalid function usage | ||
} | ||
|
||
if v.Before(time.Now().UTC()) { | ||
return errors.New("expiry is in the past") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.