Skip to content

Commit 564a3f5

Browse files
committed
Add an optional request_id parameter when sending messages. Closes #242
1 parent 22a5beb commit 564a3f5

12 files changed

+45
-14
lines changed

api/pkg/entities/message.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,14 @@ func (s SIM) String() string {
7777

7878
// Message represents a message sent between 2 phone numbers
7979
type Message struct {
80-
ID uuid.UUID `json:"id" gorm:"primaryKey;type:uuid;" example:"32343a19-da5e-4b1b-a767-3298a73703cb"`
81-
Owner string `json:"owner" gorm:"index:idx_messages_user_id__owner__contact" example:"+18005550199"`
82-
UserID UserID `json:"user_id" gorm:"index:idx_messages__user_id" example:"WB7DRDWrJZRGbYrv2CKGkqbzvqdC"`
83-
Contact string `json:"contact" gorm:"index:idx_messages_user_id__owner__contact" example:"+18005550100"`
84-
Content string `json:"content" example:"This is a sample text message"`
85-
Type MessageType `json:"type" example:"mobile-terminated"`
86-
Status MessageStatus `json:"status" gorm:"index:idx_messages_status" example:"pending"`
80+
ID uuid.UUID `json:"id" gorm:"primaryKey;type:uuid;" example:"32343a19-da5e-4b1b-a767-3298a73703cb"`
81+
RequestID *string `json:"request_id" example:"153554b5-ae44-44a0-8f4f-7bbac5657ad4"`
82+
Owner string `json:"owner" gorm:"index:idx_messages_user_id__owner__contact" example:"+18005550199"`
83+
UserID UserID `json:"user_id" gorm:"index:idx_messages__user_id" example:"WB7DRDWrJZRGbYrv2CKGkqbzvqdC"`
84+
Contact string `json:"contact" gorm:"index:idx_messages_user_id__owner__contact" example:"+18005550100"`
85+
Content string `json:"content" example:"This is a sample text message"`
86+
Type MessageType `json:"type" example:"mobile-terminated"`
87+
Status MessageStatus `json:"status" gorm:"index:idx_messages_status" example:"pending"`
8788
// SIM is the SIM card to use to send the message
8889
// * SMS1: use the SIM card in slot 1
8990
// * SMS2: use the SIM card in slot 2

api/pkg/events/message_api_sent_event.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type MessageAPISentPayload struct {
1616
MessageID uuid.UUID `json:"message_id"`
1717
UserID entities.UserID `json:"user_id"`
1818
Owner string `json:"owner"`
19+
RequestID *string `json:"request_id"`
1920
MaxSendAttempts uint `json:"max_send_attempts"`
2021
Contact string `json:"contact"`
2122
RequestReceivedAt time.Time `json:"request_received_at"`

api/pkg/events/message_phone_delivered_event.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type MessagePhoneDeliveredPayload struct {
1616
ID uuid.UUID `json:"id"`
1717
Owner string `json:"owner"`
1818
Contact string `json:"contact"`
19+
RequestID *string `json:"request_id"`
1920
UserID entities.UserID `json:"user_id"`
2021
Timestamp time.Time `json:"timestamp"`
2122
Content string `json:"content"`

api/pkg/events/message_phone_sending_event.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const EventTypeMessagePhoneSending = "message.phone.sending"
1414
type MessagePhoneSendingPayload struct {
1515
ID uuid.UUID `json:"id"`
1616
UserID entities.UserID `json:"user_id"`
17+
RequestID *string `json:"request_id"`
1718
Timestamp time.Time `json:"timestamp"`
1819
Owner string `json:"owner"`
1920
Contact string `json:"contact"`

api/pkg/events/message_phone_sent_event.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const EventTypeMessagePhoneSent = "message.phone.sent"
1515
type MessagePhoneSentPayload struct {
1616
ID uuid.UUID `json:"id"`
1717
UserID entities.UserID `json:"user_id"`
18+
RequestID *string `json:"request_id"`
1819
Owner string `json:"owner"`
1920
Contact string `json:"contact"`
2021
Timestamp time.Time `json:"timestamp"`

api/pkg/events/message_send_expired_event.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const EventTypeMessageSendExpired = "message.send.expired"
1515
type MessageSendExpiredPayload struct {
1616
MessageID uuid.UUID `json:"message_id"`
1717
Owner string `json:"owner"`
18+
RequestID *string `json:"request_id"`
1819
Contact string `json:"contact"`
1920
UserID entities.UserID `json:"user_id"`
2021
Timestamp time.Time `json:"timestamp"`

api/pkg/events/message_send_failed_event.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type MessageSendFailedPayload struct {
1616
ErrorMessage string `json:"error_message"`
1717
UserID entities.UserID `json:"user_id"`
1818
Owner string `json:"owner"`
19+
RequestID *string `json:"request_id"`
1920
Contact string `json:"contact"`
2021
Timestamp time.Time `json:"timestamp"`
2122
Content string `json:"content"`

api/pkg/requests/message_bulk_send_request.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import (
1313
// MessageBulkSend is the payload for sending bulk SMS messages
1414
type MessageBulkSend struct {
1515
request
16-
From string `json:"from" example:"+18005550199"`
17-
To []string `json:"to" example:"+18005550100,+18005550100"`
18-
Content string `json:"content" example:"This is a sample text message"`
16+
From string `json:"from" example:"+18005550199"`
17+
To []string `json:"to" example:"+18005550100,+18005550100"`
18+
RequestID string `json:"request_id" example:"153554b5-ae44-44a0-8f4f-7bbac5657ad4" validate:"optional"`
19+
Content string `json:"content" example:"This is a sample text message"`
1920
}
2021

2122
// Sanitize sets defaults to MessageReceive
@@ -37,6 +38,7 @@ func (input *MessageBulkSend) ToMessageSendParams(userID entities.UserID, source
3738
result = append(result, services.MessageSendParams{
3839
Source: source,
3940
Owner: *from,
41+
RequestID: input.sanitizeStringPointer(input.RequestID),
4042
UserID: userID,
4143
RequestReceivedAt: time.Now().UTC(),
4244
Contact: to,

api/pkg/requests/message_send_request.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package requests
22

33
import (
4+
"strings"
45
"time"
56

67
"github.com/NdoleStudio/httpsms/pkg/entities"
@@ -13,14 +14,16 @@ import (
1314
// MessageSend is the payload for sending and SMS message
1415
type MessageSend struct {
1516
request
16-
From string `json:"from" example:"+18005550199"`
17-
To string `json:"to" example:"+18005550100"`
18-
Content string `json:"content" example:"This is a sample text message"`
17+
From string `json:"from" example:"+18005550199"`
18+
To string `json:"to" example:"+18005550100"`
19+
RequestID string `json:"request_id" example:"153554b5-ae44-44a0-8f4f-7bbac5657ad4" validate:"optional"`
20+
Content string `json:"content" example:"This is a sample text message"`
1921
}
2022

2123
// Sanitize sets defaults to MessageReceive
2224
func (input *MessageSend) Sanitize() MessageSend {
2325
input.To = input.sanitizeAddress(input.To)
26+
input.RequestID = strings.TrimSpace(input.RequestID)
2427
input.From = input.sanitizeAddress(input.From)
2528
return *input
2629
}
@@ -31,6 +34,7 @@ func (input *MessageSend) ToMessageSendParams(userID entities.UserID, source str
3134
return services.MessageSendParams{
3235
Source: source,
3336
Owner: *from,
37+
RequestID: input.sanitizeStringPointer(input.RequestID),
3438
UserID: userID,
3539
RequestReceivedAt: time.Now().UTC(),
3640
Contact: input.sanitizeAddress(input.To),

api/pkg/requests/request.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (input *request) sanitizeAddress(value string) string {
2828
return value
2929
}
3030

31-
// getLimit gets the take as a string
31+
// sanitizeBool sanitizes a boolean string
3232
func (input *request) sanitizeBool(value string) string {
3333
value = strings.TrimSpace(value)
3434
if value == "1" || strings.ToLower(value) == "true" {
@@ -42,6 +42,14 @@ func (input *request) sanitizeBool(value string) string {
4242
return value
4343
}
4444

45+
func (input *request) sanitizeStringPointer(value string) *string {
46+
value = strings.TrimSpace(value)
47+
if value == "" {
48+
return nil
49+
}
50+
return &value
51+
}
52+
4553
func (input *request) removeStringDuplicates(values []string) []string {
4654
cache := map[string]struct{}{}
4755
for _, value := range values {

0 commit comments

Comments
 (0)