Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/bugs #4

Merged
merged 5 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 6 additions & 24 deletions internal/data/evtypes/models/event_type.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package models

import (
"net/url"
"time"

"github.com/rarimo/geo-points-svc/resources"
Expand All @@ -19,20 +18,12 @@ type EventType struct {
NoAutoOpen bool `fig:"no_auto_open" db:"no_auto_open"`
AutoClaim bool `fig:"auto_claim" db:"auto_claim"`
Disabled bool `fig:"disabled" db:"disabled"`
ActionURL *url.URL `fig:"action_url" db:"action_url"`
Logo *url.URL `fig:"logo" db:"logo"`
ActionURL *string `fig:"action_url" db:"action_url"`
Logo *string `fig:"logo" db:"logo"`
QRCodeValue *string `fig:"qr_code_value" db:"qr_code_value"`
}

func ResourceToModel(r resources.EventStaticMeta) EventType {
uConv := func(s *string) *url.URL {
if s == nil {
return nil
}
u, _ := url.Parse(*s)
return u
}

// intended that no_auto_open field is not accessible through API due to being
// related only to back-end
return EventType{
Expand All @@ -46,8 +37,8 @@ func ResourceToModel(r resources.EventStaticMeta) EventType {
ExpiresAt: r.ExpiresAt,
AutoClaim: r.AutoClaim,
Disabled: r.Disabled,
ActionURL: uConv(r.ActionUrl),
Logo: uConv(r.Logo),
ActionURL: r.ActionUrl,
Logo: r.Logo,
QRCodeValue: r.QrCodeValue,
}
}
Expand All @@ -66,14 +57,6 @@ func (e EventType) Flag() string {
}

func (e EventType) Resource() resources.EventStaticMeta {
safeConv := func(u *url.URL) *string {
if u == nil {
return nil
}
s := u.String()
return &s
}

return resources.EventStaticMeta{
Name: e.Name,
Description: e.Description,
Expand All @@ -84,10 +67,9 @@ func (e EventType) Resource() resources.EventStaticMeta {
StartsAt: e.StartsAt,
ExpiresAt: e.ExpiresAt,
AutoClaim: e.AutoClaim,
ActionUrl: safeConv(e.ActionURL),
Logo: safeConv(e.Logo),
ActionUrl: e.ActionURL,
Logo: e.Logo,
Flag: e.Flag(),
QrCodeValue: e.QRCodeValue,
}
}

Expand Down
27 changes: 27 additions & 0 deletions internal/service/handlers/create_event_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"net/http"

"github.com/rarimo/geo-auth-svc/pkg/auth"
"github.com/rarimo/geo-points-svc/internal/data"
"github.com/rarimo/geo-points-svc/internal/data/evtypes"
"github.com/rarimo/geo-points-svc/internal/data/evtypes/models"
"github.com/rarimo/geo-points-svc/internal/service/requests"
"gitlab.com/distributed_lab/ape"
Expand Down Expand Up @@ -44,5 +46,30 @@ func CreateEventType(w http.ResponseWriter, r *http.Request) {
}
EventTypes(r).Push(typeModel)

if evtypes.FilterNotOpenable(typeModel) {
violog marked this conversation as resolved.
Show resolved Hide resolved
w.WriteHeader(http.StatusNoContent)
return
}

balances, err := BalancesQ(r).FilterDisabled().Select()
if err != nil {
Log(r).WithError(err).Error("Failed to select balances")
ape.RenderErr(w, problems.InternalError())
return
}

eventsToInsert := make([]data.Event, 0, len(balances))
for _, b := range balances {
eventsToInsert = append(eventsToInsert, data.Event{
Nullifier: b.Nullifier,
Status: data.EventOpen,
Type: typeModel.Name,
})
}
if err = EventsQ(r).Insert(eventsToInsert...); err != nil {
Log(r).WithError(err).Error("Failed to insert qr-code events")
ape.RenderErr(w, problems.InternalError())
return
}
w.WriteHeader(http.StatusNoContent)
}
5 changes: 3 additions & 2 deletions internal/service/handlers/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
balancesQCtxKey
referralsQCtxKey
eventTypesCtxKey
eventTypesQCtxKey
userClaimsCtxKey
levelsCtxKey
verifierCtxKey
Expand Down Expand Up @@ -79,12 +80,12 @@ func EventTypes(r *http.Request) *evtypes.Types {

func CtxEventTypesQ(q data.EventTypesQ) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, eventTypesCtxKey, q)
return context.WithValue(ctx, eventTypesQCtxKey, q)
}
}

func EventTypesQ(r *http.Request) data.EventTypesQ {
return r.Context().Value(eventTypesCtxKey).(data.EventTypesQ).New()
return r.Context().Value(eventTypesQCtxKey).(data.EventTypesQ).New()
}

func CtxUserClaims(claim []resources.Claim) func(context.Context) context.Context {
Expand Down
4 changes: 4 additions & 0 deletions internal/service/handlers/verify_passport.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@ func claimReferralSpecificEvents(r *http.Request, evTypeRef *models.EventType, n
eventsToClaimed[i] = events[i].ID
}

if len(eventsToClaimed) == 0 {
return nil
}

_, err = EventsQ(r).FilterByID(eventsToClaimed...).Update(data.EventClaimed, nil, &evTypeRef.Reward)
if err != nil {
return fmt.Errorf("update event status: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/service/requests/create_event_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewCreateEventType(r *http.Request) (req resources.EventTypeResponse, err e
"data/type": val.Validate(req.Data.Type, val.Required, val.In(resources.EVENT_TYPE)),
"data/attributes/action_url": val.Validate(attr.ActionUrl, is.URL),
"data/attributes/description": val.Validate(attr.Description, val.Required),
"data/attributes/frequency": val.Validate(attr.Frequency, val.Required, val.In(models.Unlimited)),
"data/attributes/frequency": val.Validate(attr.Frequency, val.Required, val.In(string(models.Unlimited))),
"data/attributes/logo": val.Validate(attr.Logo, is.URL),
"data/attributes/name": val.Validate(attr.Name, val.Required, val.In(req.Data.ID)),
"data/attributes/flag": val.Validate(attr.Flag, val.Empty),
Expand Down
Loading