Skip to content

Commit e813f16

Browse files
committed
branch
1 parent 2701ee3 commit e813f16

34 files changed

Lines changed: 461 additions & 141 deletions

api/pkg/entities/message.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ type MessageEventName string
4848

4949
const (
5050
// MessageEventNameSent is emitted when a message is sent by the mobile phone
51-
MessageEventNameSent = "SENT"
51+
MessageEventNameSent = MessageEventName("SENT")
5252

5353
// MessageEventNameDelivered is emitted when a message is delivered by the mobile phone
54-
MessageEventNameDelivered = "DELIVERED"
54+
MessageEventNameDelivered = MessageEventName("DELIVERED")
5555

5656
// MessageEventNameFailed is emitted when a message is failed by the mobile phone
57-
MessageEventNameFailed = "FAILED"
57+
MessageEventNameFailed = MessageEventName("FAILED")
5858
)
5959

6060
// Message represents a message sent between 2 phone numbers
@@ -85,6 +85,16 @@ func (message *Message) IsSending() bool {
8585
return message.Status == MessageStatusSending
8686
}
8787

88+
// IsDelivered checks if a message is delivered
89+
func (message *Message) IsDelivered() bool {
90+
return message.Status == MessageStatusDelivered
91+
}
92+
93+
// IsPending checks if a message is pending
94+
func (message *Message) IsPending() bool {
95+
return message.Status == MessageStatusPending
96+
}
97+
8898
// IsExpired checks if a message is expired
8999
func (message *Message) IsExpired() bool {
90100
return message.Status == MessageStatusExpired
@@ -106,9 +116,10 @@ func (message *Message) Sent(timestamp time.Time) *Message {
106116
}
107117

108118
// Failed registers a message as failed
109-
func (message *Message) Failed(timestamp time.Time) *Message {
119+
func (message *Message) Failed(timestamp time.Time, errorMessage string) *Message {
110120
message.SentAt = &timestamp
111121
message.Status = MessageStatusFailed
122+
message.FailureReason = &errorMessage
112123
message.updateOrderTimestamp(timestamp)
113124
return message
114125
}
@@ -121,6 +132,14 @@ func (message *Message) Delivered(timestamp time.Time) *Message {
121132
return message
122133
}
123134

135+
// Expired registers a message as expired
136+
func (message *Message) Expired(timestamp time.Time) *Message {
137+
message.SentAt = &timestamp
138+
message.Status = MessageStatusExpired
139+
message.updateOrderTimestamp(timestamp)
140+
return message
141+
}
142+
124143
// AddSendAttempt configures a Message for sending
125144
func (message *Message) AddSendAttempt(timestamp time.Time) *Message {
126145
message.Status = MessageStatusSending

api/pkg/entities/phone.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ type Phone struct {
1313
FcmToken *string `json:"fcm_token" example:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzd....."`
1414
PhoneNumber string `json:"phone_number" example:"+18005550199"`
1515
MessagesPerMinute uint `json:"messages_per_minute" example:"1"`
16-
CreatedAt time.Time `json:"created_at" example:"2022-06-05T14:26:02.302718+03:00"`
17-
UpdatedAt time.Time `json:"updated_at" example:"2022-06-05T14:26:10.303278+03:00"`
16+
17+
// MessageExpirationTimeout is the time after sending a message when it is considered to be expired
18+
MessageExpirationTimeout time.Duration `json:"message_expiration_timeout"`
19+
20+
CreatedAt time.Time `json:"created_at" example:"2022-06-05T14:26:02.302718+03:00"`
21+
UpdatedAt time.Time `json:"updated_at" example:"2022-06-05T14:26:10.303278+03:00"`
1822
}

api/pkg/entities/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/google/uuid"
77
)
88

9-
// UserID is the ID of a user
9+
// UserID is the MessageID of a user
1010
type UserID string
1111

1212
// User stores information about a user

api/pkg/events/message_notification_sent_event.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ const EventTypeMessageNotificationSent = "message.notification.sent"
1313

1414
// MessageNotificationSentPayload is the payload of the EventTypeMessageNotificationSent event
1515
type MessageNotificationSentPayload struct {
16-
MessageID uuid.UUID `json:"message_id"`
17-
UserID entities.UserID `json:"user_id"`
18-
PhoneID uuid.UUID `json:"phone_id"`
19-
ScheduledAt time.Time `json:"scheduled_at"`
20-
FcmMessageID string `json:"fcm_message_id"`
21-
NotificationSentAt time.Time `json:"notification_sent_at"`
22-
NotificationID uuid.UUID `json:"notification_id"`
16+
MessageID uuid.UUID `json:"message_id"`
17+
UserID entities.UserID `json:"user_id"`
18+
PhoneID uuid.UUID `json:"phone_id"`
19+
ScheduledAt time.Time `json:"scheduled_at"`
20+
FcmMessageID string `json:"fcm_message_id"`
21+
MessageExpirationTimeout time.Duration `json:"message_expiration_timeout"`
22+
NotificationSentAt time.Time `json:"notification_sent_at"`
23+
NotificationID uuid.UUID `json:"notification_id"`
2324
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package events
2+
3+
import (
4+
"time"
5+
6+
"github.com/NdoleStudio/httpsms/pkg/entities"
7+
8+
"github.com/google/uuid"
9+
)
10+
11+
// EventTypeMessageSendExpiredCheck is emitted to trigger checking if a message is expired
12+
const EventTypeMessageSendExpiredCheck = "message.send.expired.check"
13+
14+
// MessageSendExpiredCheckPayload is the payload of the EventTypeMessageSendExpiredCheck event
15+
type MessageSendExpiredCheckPayload struct {
16+
MessageID uuid.UUID `json:"message_id"`
17+
ScheduledAt time.Time `json:"scheduled_at"`
18+
UserID entities.UserID `json:"user_id"`
19+
}

api/pkg/events/message_phone_failed_event.go renamed to api/pkg/events/message_send_expired_event.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ import (
44
"time"
55

66
"github.com/NdoleStudio/httpsms/pkg/entities"
7+
78
"github.com/google/uuid"
89
)
910

10-
// EventTypeMessagePhoneFailed is emitted when the phone could not send
11-
const EventTypeMessagePhoneFailed = "message.phone.failed"
11+
// EventTypeMessageSendExpired is emitted when the phone a message expires
12+
const EventTypeMessageSendExpired = "message.send.expired"
1213

13-
// MessagePhoneFailedPayload is the payload of the EventTypeMessagePhoneFailed event
14-
type MessagePhoneFailedPayload struct {
15-
ID uuid.UUID `json:"id"`
16-
UserID entities.UserID `json:"user_id"`
14+
// MessageSendExpiredPayload is the payload of the EventTypeMessageSendExpired event
15+
type MessageSendExpiredPayload struct {
16+
MessageID uuid.UUID `json:"message_id"`
1717
Owner string `json:"owner"`
1818
Contact string `json:"contact"`
19+
UserID entities.UserID `json:"user_id"`
1920
Timestamp time.Time `json:"timestamp"`
2021
Content string `json:"content"`
2122
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package events
2+
3+
import (
4+
"time"
5+
6+
"github.com/NdoleStudio/httpsms/pkg/entities"
7+
"github.com/google/uuid"
8+
)
9+
10+
// EventTypeMessageSendFailed is emitted when the phone could not send
11+
const EventTypeMessageSendFailed = "message.send.failed"
12+
13+
// MessageSendFailedPayload is the payload of the EventTypeMessageSendFailed event
14+
type MessageSendFailedPayload struct {
15+
ID uuid.UUID `json:"id"`
16+
ErrorMessage string `json:"errorMessage"`
17+
UserID entities.UserID `json:"user_id"`
18+
Owner string `json:"owner"`
19+
Contact string `json:"contact"`
20+
Timestamp time.Time `json:"timestamp"`
21+
Content string `json:"content"`
22+
}

api/pkg/handlers/events_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ func (h *EventsHandler) Dispatch(c *fiber.Ctx) error {
6363
}
6464

6565
if h.userIDFomContext(c) != h.queueConfig.UserID {
66-
msg := fmt.Sprintf("user with ID [%s], cannot dispatch event [%+#v]", h.userIDFomContext(c), request)
66+
msg := fmt.Sprintf("user with MessageID [%s], cannot dispatch event [%+#v]", h.userIDFomContext(c), request)
6767
ctxLogger.Error(stacktrace.NewError(msg))
6868
return h.responseForbidden(c)
6969
}
7070

7171
err := h.service.Dispatch(ctx, request)
7272
if err != nil {
73-
msg := fmt.Sprintf("cannot dispatch event with ID [%s]", request.ID())
73+
msg := fmt.Sprintf("cannot dispatch event with MessageID [%s]", request.ID())
7474
ctxLogger.Error(stacktrace.Propagate(err, msg))
7575
return h.responseInternalServerError(c)
7676
}

api/pkg/handlers/message_handler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (h *MessageHandler) PostSend(c *fiber.Ctx) error {
100100
// @Tags Messages
101101
// @Accept json
102102
// @Produce json
103-
// @Param message_id query string true "The ID of the message" default(32343a19-da5e-4b1b-a767-3298a73703cb)
103+
// @Param message_id query string true "The MessageID of the message" default(32343a19-da5e-4b1b-a767-3298a73703cb)
104104
// @Success 200 {object} responses.MessageResponse
105105
// @Failure 400 {object} responses.BadRequest
106106
// @Failure 401 {object} responses.Unauthorized
@@ -129,7 +129,7 @@ func (h *MessageHandler) GetOutstanding(c *fiber.Ctx) error {
129129

130130
message, err := h.service.GetOutstanding(ctx, request.ToGetOutstandingParams(c.OriginalURL(), h.userIDFomContext(c), timestamp))
131131
if err != nil {
132-
msg := fmt.Sprintf("cannot get outstnading messgage with ID [%s]", request.MessageID)
132+
msg := fmt.Sprintf("cannot get outstnading messgage with MessageID [%s]", request.MessageID)
133133
ctxLogger.Error(stacktrace.Propagate(err, msg))
134134
return h.responseInternalServerError(c)
135135
}
@@ -191,7 +191,7 @@ func (h *MessageHandler) Index(c *fiber.Ctx) error {
191191
// @Tags Messages
192192
// @Accept json
193193
// @Produce json
194-
// @Param messageID path string true "ID of the message" default(32343a19-da5e-4b1b-a767-3298a73703ca)
194+
// @Param messageID path string true "MessageID of the message" default(32343a19-da5e-4b1b-a767-3298a73703ca)
195195
// @Param payload body requests.MessageEvent true "Payload of the event emitted."
196196
// @Success 200 {object} responses.MessageResponse
197197
// @Failure 400 {object} responses.BadRequest
@@ -223,7 +223,7 @@ func (h *MessageHandler) PostEvent(c *fiber.Ctx) error {
223223

224224
message, err := h.service.GetMessage(ctx, h.userIDFomContext(c), uuid.MustParse(request.MessageID))
225225
if err != nil && stacktrace.GetCode(err) == repositories.ErrCodeNotFound {
226-
return h.responseNotFound(c, fmt.Sprintf("cannot find message with ID [%s]", request.MessageID))
226+
return h.responseNotFound(c, fmt.Sprintf("cannot find message with MessageID [%s]", request.MessageID))
227227
}
228228

229229
if err != nil {

api/pkg/handlers/phone_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func (h *PhoneHandler) Upsert(c *fiber.Ctx) error {
139139
// @Tags Phones
140140
// @Accept json
141141
// @Produce json
142-
// @Param phoneID path string true "ID of the phone" default(32343a19-da5e-4b1b-a767-3298a73703ca)
142+
// @Param phoneID path string true "MessageID of the phone" default(32343a19-da5e-4b1b-a767-3298a73703ca)
143143
// @Success 204 {object} responses.NoContent
144144
// @Failure 400 {object} responses.BadRequest
145145
// @Failure 401 {object} responses.Unauthorized

0 commit comments

Comments
 (0)