Skip to content

Add channel event UUID and start writing that #835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion backends/rapidpro/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
8 changes: 6 additions & 2 deletions backends/rapidpro/channel_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"github.com/lib/pq"
"github.com/nyaruka/courier"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/null/v3"
)

Expand All @@ -38,6 +39,7 @@
// 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"`
Expand All @@ -64,6 +66,7 @@
dbChannel := channel.(*Channel)

return &ChannelEvent{
UUID_: courier.ChannelEventUUID(uuids.NewV4()),
ChannelUUID_: dbChannel.UUID_,
OrgID_: dbChannel.OrgID_,
ChannelID_: dbChannel.ID_,
Expand All @@ -77,6 +80,7 @@
}

func (e *ChannelEvent) EventID() int64 { return int64(e.ID_) }
func (e *ChannelEvent) UUID() courier.ChannelEventUUID { return e.UUID_ }

Check warning on line 83 in backends/rapidpro/channel_event.go

View check run for this annotation

Codecov / codecov/patch

backends/rapidpro/channel_event.go#L83

Added line #L83 was not covered by tests
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_ }
Expand Down Expand Up @@ -134,8 +138,8 @@

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
Expand Down
1 change: 1 addition & 0 deletions backends/rapidpro/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions channel_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -27,6 +31,7 @@ const (
type ChannelEvent interface {
Event

UUID() ChannelEventUUID
ChannelUUID() ChannelUUID
URN() urns.URN
EventType() ChannelEventType
Expand Down
2 changes: 2 additions & 0 deletions test/channel_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
)

type mockChannelEvent struct {
uuid courier.ChannelEventUUID
channel courier.Channel
eventType courier.ChannelEventType
urn urns.URN
Expand All @@ -20,6 +21,7 @@
}

func (e *mockChannelEvent) EventID() int64 { return 0 }
func (e *mockChannelEvent) UUID() courier.ChannelEventUUID { return e.uuid }

Check warning on line 24 in test/channel_event.go

View check run for this annotation

Codecov / codecov/patch

test/channel_event.go#L24

Added line #L24 was not covered by tests
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 }
Expand Down
Loading