Skip to content

Commit

Permalink
Merge pull request #679 from nyaruka/msg_created_by
Browse files Browse the repository at this point in the history
Add created_by_id to send payload and send with tembachat messages
  • Loading branch information
rowanseymour authored Jan 19, 2024
2 parents 6aeb9fa + 02f854b commit c8a4f66
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 11 deletions.
2 changes: 2 additions & 0 deletions backends/rapidpro/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type Msg struct {
ModifiedOn_ time.Time ` db:"modified_on"`
QueuedOn_ time.Time ` db:"queued_on"`
SentOn_ *time.Time ` db:"sent_on"`
CreatedByID_ courier.UserID `json:"created_by_id" db:"created_by_id"`
LogUUIDs pq.StringArray ` db:"log_uuids"`

// extra non-model fields that mailroom will include in queued payload
Expand Down Expand Up @@ -163,6 +164,7 @@ func (m *Msg) SentOn() *time.Time { return m.SentOn_ }
func (m *Msg) IsResend() bool { return m.IsResend_ }
func (m *Msg) Flow() *courier.FlowReference { return m.Flow_ }
func (m *Msg) OptIn() *courier.OptInReference { return m.OptIn_ }
func (m *Msg) CreatedByID() courier.UserID { return m.CreatedByID_ }
func (m *Msg) SessionStatus() string { return m.SessionStatus_ }
func (m *Msg) HighPriority() bool { return m.HighPriority_ }

Expand Down
8 changes: 5 additions & 3 deletions handlers/tembachat/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ func (h *handler) receiveMessage(ctx context.Context, c courier.Channel, w http.
}

type sendPayload struct {
Identifier string `json:"identifier"`
Text string `json:"text"`
Origin string `json:"origin"`
Identifier string `json:"identifier"`
Text string `json:"text"`
Origin string `json:"origin"`
UserID courier.UserID `json:"user_id,omitempty"`
}

func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.ChannelLog) (courier.StatusUpdate, error) {
Expand All @@ -84,6 +85,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch
Identifier: msg.URN().Path(),
Text: msg.Text(),
Origin: string(msg.Origin()),
UserID: msg.CreatedByID(),
}
req, _ := http.NewRequest("POST", sendURL, bytes.NewReader(jsonx.MustMarshal(payload)))

Expand Down
15 changes: 13 additions & 2 deletions handlers/tembachat/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func setSendURL(s *httptest.Server, h courier.ChannelHandler, c courier.Channel,

var defaultSendTestCases = []OutgoingTestCase{
{
Label: "Plain Send",
Label: "Flow message",
MsgText: "Simple message ☺",
MsgURN: "webchat:65vbbDAQCdPdEWlEhDGy4utO",
MockResponseBody: `{"status": "queued"}`,
Expand All @@ -74,7 +74,18 @@ var defaultSendTestCases = []OutgoingTestCase{
SendPrep: setSendURL,
},
{
Label: "Error Sending",
Label: "Chat message",
MsgText: "Simple message ☺",
MsgURN: "webchat:65vbbDAQCdPdEWlEhDGy4utO",
MsgCreatedByID: 7,
MockResponseBody: `{"status": "queued"}`,
MockResponseStatus: 200,
ExpectedRequestBody: `{"identifier":"65vbbDAQCdPdEWlEhDGy4utO","text":"Simple message ☺","origin":"flow","user_id":7}`,
ExpectedMsgStatus: "W",
SendPrep: setSendURL,
},
{
Label: "Error sending",
MsgText: "Error message",
MsgURN: "webchat:65vbbDAQCdPdEWlEhDGy4utO",
MockResponseBody: `{"error": "boom"}`,
Expand Down
2 changes: 2 additions & 0 deletions handlers/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ type OutgoingTestCase struct {
MsgFlow *courier.FlowReference
MsgOptIn *courier.OptInReference
MsgOrigin courier.MsgOrigin
MsgCreatedByID courier.UserID
MsgContactLastSeenOn *time.Time

MockResponseStatus int
Expand Down Expand Up @@ -362,6 +363,7 @@ func RunOutgoingTestCases(t *testing.T, channel courier.Channel, handler courier

msg := mb.NewOutgoingMsg(channel, 10, urns.URN(tc.MsgURN), tc.MsgText, tc.MsgHighPriority, tc.MsgQuickReplies, tc.MsgTopic, tc.MsgResponseToExternalID, msgOrigin, tc.MsgContactLastSeenOn).(*test.MockMsg)
msg.WithLocale(tc.MsgLocale)
msg.WithCreatedByID(tc.MsgCreatedByID)

for _, a := range tc.MsgAttachments {
msg.WithAttachment(a)
Expand Down
5 changes: 5 additions & 0 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ const (
MsgOriginChat MsgOrigin = "chat"
)

type UserID null.Int64

var NilUserID = UserID(0)

//-----------------------------------------------------------------------------
// Msg interface
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -85,6 +89,7 @@ type MsgOut interface {
IsResend() bool
Flow() *FlowReference
OptIn() *OptInReference
CreatedByID() UserID
SessionStatus() string
HighPriority() bool
}
Expand Down
15 changes: 9 additions & 6 deletions test/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ type MockMsg struct {
alreadyWritten bool
isResend bool

flow *courier.FlowReference
optIn *courier.OptInReference
flow *courier.FlowReference
optIn *courier.OptInReference
createdByID courier.UserID

receivedOn *time.Time
sentOn *time.Time
Expand Down Expand Up @@ -71,6 +72,7 @@ func (m *MockMsg) SentOn() *time.Time { return m.sentOn }
func (m *MockMsg) IsResend() bool { return m.isResend }
func (m *MockMsg) Flow() *courier.FlowReference { return m.flow }
func (m *MockMsg) OptIn() *courier.OptInReference { return m.optIn }
func (m *MockMsg) CreatedByID() courier.UserID { return m.createdByID }
func (m *MockMsg) SessionStatus() string { return "" }
func (m *MockMsg) HighPriority() bool { return m.highPriority }

Expand All @@ -94,7 +96,8 @@ func (m *MockMsg) WithMetadata(metadata json.RawMessage) courier.MsgOut {
m.metadata = metadata
return m
}
func (m *MockMsg) WithFlow(flow *courier.FlowReference) courier.MsgOut { m.flow = flow; return m }
func (m *MockMsg) WithOptIn(optIn *courier.OptInReference) courier.MsgOut { m.optIn = optIn; return m }
func (m *MockMsg) WithLocale(lc i18n.Locale) courier.MsgOut { m.locale = lc; return m }
func (m *MockMsg) WithURNAuth(token string) courier.MsgOut { m.urnAuth = token; return m }
func (m *MockMsg) WithFlow(f *courier.FlowReference) courier.MsgOut { m.flow = f; return m }
func (m *MockMsg) WithOptIn(o *courier.OptInReference) courier.MsgOut { m.optIn = o; return m }
func (m *MockMsg) WithCreatedByID(u courier.UserID) courier.MsgOut { m.createdByID = u; return m }
func (m *MockMsg) WithLocale(lc i18n.Locale) courier.MsgOut { m.locale = lc; return m }
func (m *MockMsg) WithURNAuth(token string) courier.MsgOut { m.urnAuth = token; return m }

0 comments on commit c8a4f66

Please sign in to comment.