@@ -5,6 +5,11 @@ import (
55 "fmt"
66 "net/url"
77
8+ "github.com/NdoleStudio/httpsms/pkg/entities"
9+ "github.com/NdoleStudio/httpsms/pkg/repositories"
10+ "github.com/NdoleStudio/httpsms/pkg/services"
11+ "github.com/palantir/stacktrace"
12+
813 "github.com/NdoleStudio/httpsms/pkg/requests"
914
1015 "github.com/NdoleStudio/httpsms/pkg/telemetry"
@@ -14,18 +19,21 @@ import (
1419// WebhookHandlerValidator validates models used in handlers.WebhookHandler
1520type WebhookHandlerValidator struct {
1621 validator
17- logger telemetry.Logger
18- tracer telemetry.Tracer
22+ logger telemetry.Logger
23+ tracer telemetry.Tracer
24+ phoneService * services.PhoneService
1925}
2026
2127// NewWebhookHandlerValidator creates a new handlers.WebhookHandler validator
2228func NewWebhookHandlerValidator (
2329 logger telemetry.Logger ,
2430 tracer telemetry.Tracer ,
31+ phoneService * services.PhoneService ,
2532) (v * WebhookHandlerValidator ) {
2633 return & WebhookHandlerValidator {
27- logger : logger .WithService (fmt .Sprintf ("%T" , v )),
28- tracer : tracer ,
34+ logger : logger .WithService (fmt .Sprintf ("%T" , v )),
35+ tracer : tracer ,
36+ phoneService : phoneService ,
2937 }
3038}
3139
@@ -54,7 +62,10 @@ func (validator *WebhookHandlerValidator) ValidateIndex(_ context.Context, reque
5462}
5563
5664// ValidateStore validates the requests.WebhookStore request
57- func (validator * WebhookHandlerValidator ) ValidateStore (_ context.Context , request requests.WebhookStore ) url.Values {
65+ func (validator * WebhookHandlerValidator ) ValidateStore (ctx context.Context , userID entities.UserID , request requests.WebhookStore ) url.Values {
66+ ctx , span := validator .tracer .Start (ctx )
67+ defer span .End ()
68+
5869 v := govalidator .New (govalidator.Options {
5970 Data : & request ,
6071 Rules : govalidator.MapData {
@@ -72,13 +83,32 @@ func (validator *WebhookHandlerValidator) ValidateStore(_ context.Context, reque
7283 "required" ,
7384 webhookEventsRule ,
7485 },
86+ "phone_numbers" : []string {
87+ "required" ,
88+ multipleContactPhoneNumberRule ,
89+ },
7590 },
7691 })
77- return v .ValidateStruct ()
92+
93+ result := v .ValidateStruct ()
94+ if len (result ) > 0 {
95+ return result
96+ }
97+
98+ for _ , address := range request .PhoneNumbers {
99+ _ , err := validator .phoneService .Load (ctx , userID , address )
100+ if stacktrace .GetCode (err ) == repositories .ErrCodeNotFound {
101+ 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 ))
102+ }
103+ }
104+ return result
78105}
79106
80107// ValidateUpdate validates the requests.WebhookUpdate request
81- func (validator * WebhookHandlerValidator ) ValidateUpdate (_ context.Context , request requests.WebhookUpdate ) url.Values {
108+ func (validator * WebhookHandlerValidator ) ValidateUpdate (ctx context.Context , userID entities.UserID , request requests.WebhookUpdate ) url.Values {
109+ ctx , span := validator .tracer .Start (ctx )
110+ defer span .End ()
111+
82112 v := govalidator .New (govalidator.Options {
83113 Data : & request ,
84114 Rules : govalidator.MapData {
@@ -100,7 +130,23 @@ func (validator *WebhookHandlerValidator) ValidateUpdate(_ context.Context, requ
100130 "required" ,
101131 webhookEventsRule ,
102132 },
133+ "phone_numbers" : []string {
134+ "required" ,
135+ multipleContactPhoneNumberRule ,
136+ },
103137 },
104138 })
105- return v .ValidateStruct ()
139+
140+ result := v .ValidateStruct ()
141+ if len (result ) > 0 {
142+ return result
143+ }
144+
145+ for _ , address := range request .PhoneNumbers {
146+ _ , err := validator .phoneService .Load (ctx , userID , address )
147+ if stacktrace .GetCode (err ) == repositories .ErrCodeNotFound {
148+ 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 ))
149+ }
150+ }
151+ return result
106152}
0 commit comments