Skip to content

Commit b89fc0f

Browse files
authored
Merge pull request #835 from nyaruka/channel-event-uuid
Add channel event UUID and start writing that
2 parents b7ac2a9 + eca9173 commit b89fc0f

File tree

6 files changed

+16
-4
lines changed

6 files changed

+16
-4
lines changed

Diff for: backend.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type Backend interface {
6161
// NewChannelEvent creates a new channel event for the given channel and event type
6262
NewChannelEvent(Channel, ChannelEventType, urns.URN, *ChannelLog) ChannelEvent
6363

64-
// WriteChannelEvent writes the passed in channel even returning any error
64+
// WriteChannelEvent writes the passed in channel event returning any error
6565
WriteChannelEvent(context.Context, ChannelEvent, *ChannelLog) error
6666

6767
// WriteChannelLog writes the passed in channel log to our backend

Diff for: backends/rapidpro/backend_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,7 @@ WHERE
16411641
`
16421642

16431643
const sqlSelectEvent = `
1644-
SELECT id, org_id, channel_id, contact_id, contact_urn_id, event_type, optin_id, extra, occurred_on, created_on, log_uuids
1644+
SELECT id, uuid, org_id, channel_id, contact_id, contact_urn_id, event_type, optin_id, extra, occurred_on, created_on, log_uuids
16451645
FROM channels_channelevent
16461646
WHERE id = $1`
16471647

Diff for: backends/rapidpro/channel_event.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/lib/pq"
1515
"github.com/nyaruka/courier"
1616
"github.com/nyaruka/gocommon/urns"
17+
"github.com/nyaruka/gocommon/uuids"
1718
"github.com/nyaruka/null/v3"
1819
)
1920

@@ -38,6 +39,7 @@ func (i ChannelEventID) String() string {
3839
// ChannelEvent represents an event on a channel.. that isn't a new message or status update
3940
type ChannelEvent struct {
4041
ID_ ChannelEventID ` db:"id"`
42+
UUID_ courier.ChannelEventUUID `json:"uuid" db:"uuid"`
4143
OrgID_ OrgID `json:"org_id" db:"org_id"`
4244
ChannelUUID_ courier.ChannelUUID `json:"channel_uuid" db:"channel_uuid"`
4345
ChannelID_ courier.ChannelID `json:"channel_id" db:"channel_id"`
@@ -64,6 +66,7 @@ func newChannelEvent(channel courier.Channel, eventType courier.ChannelEventType
6466
dbChannel := channel.(*Channel)
6567

6668
return &ChannelEvent{
69+
UUID_: courier.ChannelEventUUID(uuids.NewV4()),
6770
ChannelUUID_: dbChannel.UUID_,
6871
OrgID_: dbChannel.OrgID_,
6972
ChannelID_: dbChannel.ID_,
@@ -77,6 +80,7 @@ func newChannelEvent(channel courier.Channel, eventType courier.ChannelEventType
7780
}
7881

7982
func (e *ChannelEvent) EventID() int64 { return int64(e.ID_) }
83+
func (e *ChannelEvent) UUID() courier.ChannelEventUUID { return e.UUID_ }
8084
func (e *ChannelEvent) ChannelID() courier.ChannelID { return e.ChannelID_ }
8185
func (e *ChannelEvent) ChannelUUID() courier.ChannelUUID { return e.ChannelUUID_ }
8286
func (e *ChannelEvent) EventType() courier.ChannelEventType { return e.EventType_ }
@@ -134,8 +138,8 @@ func writeChannelEvent(ctx context.Context, b *backend, event courier.ChannelEve
134138

135139
const sqlInsertChannelEvent = `
136140
INSERT INTO
137-
channels_channelevent( org_id, channel_id, contact_id, contact_urn_id, event_type, optin_id, extra, occurred_on, created_on, status, log_uuids)
138-
VALUES(:org_id, :channel_id, :contact_id, :contact_urn_id, :event_type, :optin_id, :extra, :occurred_on, NOW(), 'P', :log_uuids)
141+
channels_channelevent( org_id, uuid, channel_id, contact_id, contact_urn_id, event_type, optin_id, extra, occurred_on, created_on, status, log_uuids)
142+
VALUES(:org_id, :uuid, :channel_id, :contact_id, :contact_urn_id, :event_type, :optin_id, :extra, :occurred_on, NOW(), 'P', :log_uuids)
139143
RETURNING id, created_on`
140144

141145
// writeChannelEventToDB writes the passed in channel event to our db

Diff for: backends/rapidpro/schema.sql

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ CREATE TABLE channels_channellog (
129129
DROP TABLE IF EXISTS channels_channelevent CASCADE;
130130
CREATE TABLE channels_channelevent (
131131
id serial primary key,
132+
uuid uuid NOT NULL,
132133
event_type character varying(16) NOT NULL,
133134
status character varying(1) NOT NULL,
134135
extra text,

Diff for: channel_event.go

+5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ import (
44
"time"
55

66
"github.com/nyaruka/gocommon/urns"
7+
"github.com/nyaruka/gocommon/uuids"
78
)
89

910
// ChannelEventType is the type of channel event this is
1011
type ChannelEventType string
1112

13+
// ChannelEvent is our typing of a channelevent's UUID
14+
type ChannelEventUUID uuids.UUID
15+
1216
// Possible values for ChannelEventTypes
1317
const (
1418
EventTypeNewConversation ChannelEventType = "new_conversation"
@@ -27,6 +31,7 @@ const (
2731
type ChannelEvent interface {
2832
Event
2933

34+
UUID() ChannelEventUUID
3035
ChannelUUID() ChannelUUID
3136
URN() urns.URN
3237
EventType() ChannelEventType

Diff for: test/channel_event.go

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
)
99

1010
type mockChannelEvent struct {
11+
uuid courier.ChannelEventUUID
1112
channel courier.Channel
1213
eventType courier.ChannelEventType
1314
urn urns.URN
@@ -20,6 +21,7 @@ type mockChannelEvent struct {
2021
}
2122

2223
func (e *mockChannelEvent) EventID() int64 { return 0 }
24+
func (e *mockChannelEvent) UUID() courier.ChannelEventUUID { return e.uuid }
2325
func (e *mockChannelEvent) ChannelUUID() courier.ChannelUUID { return e.channel.UUID() }
2426
func (e *mockChannelEvent) EventType() courier.ChannelEventType { return e.eventType }
2527
func (e *mockChannelEvent) CreatedOn() time.Time { return e.createdOn }

0 commit comments

Comments
 (0)