Spaces:
Paused
Paused
File size: 3,783 Bytes
83a59ca | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | package validators
import (
"context"
"fmt"
"net/url"
"github.com/NdoleStudio/httpsms/pkg/entities"
"github.com/NdoleStudio/httpsms/pkg/repositories"
"github.com/NdoleStudio/httpsms/pkg/services"
"github.com/NdoleStudio/stacktrace"
"github.com/NdoleStudio/httpsms/pkg/requests"
"github.com/NdoleStudio/httpsms/pkg/telemetry"
"github.com/thedevsaddam/govalidator"
)
// WebhookHandlerValidator validates models used in handlers.WebhookHandler
type WebhookHandlerValidator struct {
validator
logger telemetry.Logger
tracer telemetry.Tracer
phoneService *services.PhoneService
}
// NewWebhookHandlerValidator creates a new handlers.WebhookHandler validator
func NewWebhookHandlerValidator(
logger telemetry.Logger,
tracer telemetry.Tracer,
phoneService *services.PhoneService,
) (v *WebhookHandlerValidator) {
return &WebhookHandlerValidator{
logger: logger.WithService(fmt.Sprintf("%T", v)),
tracer: tracer,
phoneService: phoneService,
}
}
// ValidateIndex validates the requests.HeartbeatIndex request
func (validator *WebhookHandlerValidator) ValidateIndex(_ context.Context, request requests.WebhookIndex) url.Values {
v := govalidator.New(govalidator.Options{
Data: &request,
Rules: govalidator.MapData{
"limit": []string{
"required",
"numeric",
"min:1",
"max:100",
},
"skip": []string{
"required",
"numeric",
"min:0",
},
"query": []string{
"max:100",
},
},
})
return v.ValidateStruct()
}
// ValidateStore validates the requests.WebhookStore request
func (validator *WebhookHandlerValidator) ValidateStore(ctx context.Context, userID entities.UserID, request requests.WebhookStore) url.Values {
ctx, span := validator.tracer.Start(ctx)
defer span.End()
v := govalidator.New(govalidator.Options{
Data: &request,
Rules: govalidator.MapData{
"signing_key": []string{
"min:1",
"max:255",
},
"url": []string{
"required",
"url",
"max:255",
},
"events": []string{
"required",
webhookEventsRule,
},
"phone_numbers": []string{
"required",
multipleContactPhoneNumberRule,
},
},
})
result := v.ValidateStruct()
if len(result) > 0 {
return result
}
for _, address := range request.PhoneNumbers {
_, err := validator.phoneService.Load(ctx, userID, address)
if stacktrace.GetCode(err) == repositories.ErrCodeNotFound {
result.Add("from", fmt.Sprintf("The phone number [%s] is not available in your account. Install the android app on your phone to store a webhook with this phone number", address))
}
}
return result
}
// ValidateUpdate validates the requests.WebhookUpdate request
func (validator *WebhookHandlerValidator) ValidateUpdate(ctx context.Context, userID entities.UserID, request requests.WebhookUpdate) url.Values {
ctx, span := validator.tracer.Start(ctx)
defer span.End()
v := govalidator.New(govalidator.Options{
Data: &request,
Rules: govalidator.MapData{
"signing_key": []string{
"min:1",
"max:255",
},
"webhookID": []string{
"required",
"uuid",
},
"url": []string{
"required",
"url",
"max:255",
},
"events": []string{
"required",
webhookEventsRule,
},
"phone_numbers": []string{
"required",
multipleContactPhoneNumberRule,
},
},
})
result := v.ValidateStruct()
if len(result) > 0 {
return result
}
for _, address := range request.PhoneNumbers {
_, err := validator.phoneService.Load(ctx, userID, address)
if stacktrace.GetCode(err) == repositories.ErrCodeNotFound {
result.Add("from", fmt.Sprintf("The phone number [%s] is not available in your account. Install the android app on your phone to store a webhook with this phone number", address))
}
}
return result
}
|