From eca917346641d1e59846d1f5770d4ce7896bbbfb Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Fri, 14 Feb 2025 17:13:17 +0200 Subject: [PATCH] Add channel event UUID and start writing that --- backend.go | 2 +- backends/rapidpro/backend_test.go | 2 +- backends/rapidpro/channel_event.go | 8 ++++++-- backends/rapidpro/schema.sql | 1 + channel_event.go | 5 +++++ test/channel_event.go | 2 ++ 6 files changed, 16 insertions(+), 4 deletions(-) diff --git a/backend.go b/backend.go index fc456b09a..3e966acad 100644 --- a/backend.go +++ b/backend.go @@ -61,7 +61,7 @@ type Backend interface { // NewChannelEvent creates a new channel event for the given channel and event type NewChannelEvent(Channel, ChannelEventType, urns.URN, *ChannelLog) ChannelEvent - // WriteChannelEvent writes the passed in channel even returning any error + // WriteChannelEvent writes the passed in channel event returning any error WriteChannelEvent(context.Context, ChannelEvent, *ChannelLog) error // WriteChannelLog writes the passed in channel log to our backend diff --git a/backends/rapidpro/backend_test.go b/backends/rapidpro/backend_test.go index b27172a1a..04a74db0a 100644 --- a/backends/rapidpro/backend_test.go +++ b/backends/rapidpro/backend_test.go @@ -1641,7 +1641,7 @@ WHERE ` const sqlSelectEvent = ` -SELECT id, org_id, channel_id, contact_id, contact_urn_id, event_type, optin_id, extra, occurred_on, created_on, log_uuids +SELECT id, uuid, org_id, channel_id, contact_id, contact_urn_id, event_type, optin_id, extra, occurred_on, created_on, log_uuids FROM channels_channelevent WHERE id = $1` diff --git a/backends/rapidpro/channel_event.go b/backends/rapidpro/channel_event.go index 7b2e1cc4b..5b00e5ac1 100644 --- a/backends/rapidpro/channel_event.go +++ b/backends/rapidpro/channel_event.go @@ -14,6 +14,7 @@ import ( "github.com/lib/pq" "github.com/nyaruka/courier" "github.com/nyaruka/gocommon/urns" + "github.com/nyaruka/gocommon/uuids" "github.com/nyaruka/null/v3" ) @@ -38,6 +39,7 @@ func (i ChannelEventID) String() string { // ChannelEvent represents an event on a channel.. that isn't a new message or status update type ChannelEvent struct { ID_ ChannelEventID ` db:"id"` + UUID_ courier.ChannelEventUUID `json:"uuid" db:"uuid"` OrgID_ OrgID `json:"org_id" db:"org_id"` ChannelUUID_ courier.ChannelUUID `json:"channel_uuid" db:"channel_uuid"` ChannelID_ courier.ChannelID `json:"channel_id" db:"channel_id"` @@ -64,6 +66,7 @@ func newChannelEvent(channel courier.Channel, eventType courier.ChannelEventType dbChannel := channel.(*Channel) return &ChannelEvent{ + UUID_: courier.ChannelEventUUID(uuids.NewV4()), ChannelUUID_: dbChannel.UUID_, OrgID_: dbChannel.OrgID_, ChannelID_: dbChannel.ID_, @@ -77,6 +80,7 @@ func newChannelEvent(channel courier.Channel, eventType courier.ChannelEventType } func (e *ChannelEvent) EventID() int64 { return int64(e.ID_) } +func (e *ChannelEvent) UUID() courier.ChannelEventUUID { return e.UUID_ } func (e *ChannelEvent) ChannelID() courier.ChannelID { return e.ChannelID_ } func (e *ChannelEvent) ChannelUUID() courier.ChannelUUID { return e.ChannelUUID_ } func (e *ChannelEvent) EventType() courier.ChannelEventType { return e.EventType_ } @@ -134,8 +138,8 @@ func writeChannelEvent(ctx context.Context, b *backend, event courier.ChannelEve const sqlInsertChannelEvent = ` INSERT INTO - channels_channelevent( org_id, channel_id, contact_id, contact_urn_id, event_type, optin_id, extra, occurred_on, created_on, status, log_uuids) - VALUES(:org_id, :channel_id, :contact_id, :contact_urn_id, :event_type, :optin_id, :extra, :occurred_on, NOW(), 'P', :log_uuids) + channels_channelevent( org_id, uuid, channel_id, contact_id, contact_urn_id, event_type, optin_id, extra, occurred_on, created_on, status, log_uuids) + VALUES(:org_id, :uuid, :channel_id, :contact_id, :contact_urn_id, :event_type, :optin_id, :extra, :occurred_on, NOW(), 'P', :log_uuids) RETURNING id, created_on` // writeChannelEventToDB writes the passed in channel event to our db diff --git a/backends/rapidpro/schema.sql b/backends/rapidpro/schema.sql index a39cfd400..8651cafcf 100644 --- a/backends/rapidpro/schema.sql +++ b/backends/rapidpro/schema.sql @@ -129,6 +129,7 @@ CREATE TABLE channels_channellog ( DROP TABLE IF EXISTS channels_channelevent CASCADE; CREATE TABLE channels_channelevent ( id serial primary key, + uuid uuid NOT NULL, event_type character varying(16) NOT NULL, status character varying(1) NOT NULL, extra text, diff --git a/channel_event.go b/channel_event.go index ef26fa617..087ce1ab6 100644 --- a/channel_event.go +++ b/channel_event.go @@ -4,11 +4,15 @@ import ( "time" "github.com/nyaruka/gocommon/urns" + "github.com/nyaruka/gocommon/uuids" ) // ChannelEventType is the type of channel event this is type ChannelEventType string +// ChannelEvent is our typing of a channelevent's UUID +type ChannelEventUUID uuids.UUID + // Possible values for ChannelEventTypes const ( EventTypeNewConversation ChannelEventType = "new_conversation" @@ -27,6 +31,7 @@ const ( type ChannelEvent interface { Event + UUID() ChannelEventUUID ChannelUUID() ChannelUUID URN() urns.URN EventType() ChannelEventType diff --git a/test/channel_event.go b/test/channel_event.go index 177f5300c..04eb15e8b 100644 --- a/test/channel_event.go +++ b/test/channel_event.go @@ -8,6 +8,7 @@ import ( ) type mockChannelEvent struct { + uuid courier.ChannelEventUUID channel courier.Channel eventType courier.ChannelEventType urn urns.URN @@ -20,6 +21,7 @@ type mockChannelEvent struct { } func (e *mockChannelEvent) EventID() int64 { return 0 } +func (e *mockChannelEvent) UUID() courier.ChannelEventUUID { return e.uuid } func (e *mockChannelEvent) ChannelUUID() courier.ChannelUUID { return e.channel.UUID() } func (e *mockChannelEvent) EventType() courier.ChannelEventType { return e.eventType } func (e *mockChannelEvent) CreatedOn() time.Time { return e.createdOn }