Skip to content

Commit

Permalink
Cleanup use of constants for event types
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Sep 13, 2023
1 parent 60f71a5 commit b5ca282
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 40 deletions.
22 changes: 11 additions & 11 deletions core/models/channel_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ type ChannelEventID int64

// channel event types
const (
NewConversationEventType = ChannelEventType("new_conversation")
WelcomeMessageEventType = ChannelEventType("welcome_message")
ReferralEventType = ChannelEventType("referral")
MOMissEventType = ChannelEventType("mo_miss")
MOCallEventType = ChannelEventType("mo_call")
StopContactEventType = ChannelEventType("stop_contact")
EventTypeNewConversation ChannelEventType = "new_conversation"
EventTypeWelcomeMessage ChannelEventType = "welcome_message"
EventTypeReferral ChannelEventType = "referral"
EventTypeMissedCall ChannelEventType = "mo_miss"
EventTypeIncomingCall ChannelEventType = "mo_call"
EventTypeStopContact ChannelEventType = "stop_contact"
)

// ContactSeenEvents are those which count as the contact having been seen
var ContactSeenEvents = map[ChannelEventType]bool{
NewConversationEventType: true,
ReferralEventType: true,
MOMissEventType: true,
MOCallEventType: true,
StopContactEventType: true,
EventTypeNewConversation: true,
EventTypeReferral: true,
EventTypeMissedCall: true,
EventTypeIncomingCall: true,
EventTypeStopContact: true,
}

// ChannelEvent represents an event that occurred associated with a channel, such as a referral, missed call, etc..
Expand Down
4 changes: 2 additions & 2 deletions core/models/channel_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func TestChannelEvents(t *testing.T) {
start := time.Now()

// no extra
e := models.NewChannelEvent(models.MOMissEventType, testdata.Org1.ID, testdata.TwilioChannel.ID, testdata.Cathy.ID, testdata.Cathy.URNID, nil, false)
e := models.NewChannelEvent(models.EventTypeMissedCall, testdata.Org1.ID, testdata.TwilioChannel.ID, testdata.Cathy.ID, testdata.Cathy.URNID, nil, false)
err := e.Insert(ctx, rt.DB)
assert.NoError(t, err)
assert.NotZero(t, e.ID())
assert.Equal(t, map[string]any{}, e.Extra())
assert.True(t, e.OccurredOn().After(start))

// with extra
e2 := models.NewChannelEvent(models.MOMissEventType, testdata.Org1.ID, testdata.TwilioChannel.ID, testdata.Cathy.ID, testdata.Cathy.URNID, map[string]any{"referral_id": "foobar"}, false)
e2 := models.NewChannelEvent(models.EventTypeMissedCall, testdata.Org1.ID, testdata.TwilioChannel.ID, testdata.Cathy.ID, testdata.Cathy.URNID, map[string]any{"referral_id": "foobar"}, false)
err = e2.Insert(ctx, rt.DB)
assert.NoError(t, err)
assert.NotZero(t, e2.ID())
Expand Down
29 changes: 12 additions & 17 deletions core/tasks/handler/contact_tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,11 @@ import (
)

const (
MOMissEventType = string(models.MOMissEventType)
NewConversationEventType = "new_conversation"
WelcomeMessageEventType = "welcome_message"
ReferralEventType = "referral"
StopEventType = "stop_event"
MsgEventType = "msg_event"
ExpirationEventType = "expiration_event"
TimeoutEventType = "timeout_event"
TicketClosedEventType = "ticket_closed"
MsgDeletedType = "msg_deleted"
MsgEventType = "msg_event"
ExpirationEventType = "expiration_event"
TimeoutEventType = "timeout_event"
TicketClosedEventType = "ticket_closed"
MsgDeletedType = "msg_deleted"
)

// handleTimedEvent is called for timeout events
Expand Down Expand Up @@ -185,19 +180,19 @@ func HandleChannelEvent(ctx context.Context, rt *runtime.Runtime, eventType mode

switch eventType {

case models.NewConversationEventType:
case models.EventTypeNewConversation:
trigger = models.FindMatchingNewConversationTrigger(oa, channel)

case models.ReferralEventType:
case models.EventTypeReferral:
trigger = models.FindMatchingReferralTrigger(oa, channel, event.ExtraString("referrer_id"))

case models.MOMissEventType:
case models.EventTypeMissedCall:
trigger = models.FindMatchingMissedCallTrigger(oa)

case models.MOCallEventType:
case models.EventTypeIncomingCall:
trigger = models.FindMatchingIncomingCallTrigger(oa, contact)

case models.WelcomeMessageEventType:
case models.EventTypeWelcomeMessage:
trigger = nil

default:
Expand Down Expand Up @@ -253,13 +248,13 @@ func HandleChannelEvent(ctx context.Context, rt *runtime.Runtime, eventType mode
var flowTrigger flows.Trigger
switch eventType {

case models.NewConversationEventType, models.ReferralEventType, models.MOMissEventType:
case models.EventTypeNewConversation, models.EventTypeReferral, models.EventTypeMissedCall:
flowTrigger = triggers.NewBuilder(oa.Env(), flow.Reference(), contact).
Channel(channel.ChannelReference(), triggers.ChannelEventType(eventType)).
WithParams(params).
Build()

case models.MOCallEventType:
case models.EventTypeIncomingCall:
urn := contacts[0].URNForID(event.URNID())
flowTrigger = triggers.NewBuilder(oa.Env(), flow.Reference(), contact).
Channel(channel.ChannelReference(), triggers.ChannelEventTypeIncomingCall).
Expand Down
4 changes: 2 additions & 2 deletions core/tasks/handler/handle_contact_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ func (t *HandleContactEventTask) Perform(ctx context.Context, rt *runtime.Runtim
// hand off to the appropriate handler
switch contactEvent.Type {

case StopEventType:
case string(models.EventTypeStopContact):
evt := &StopEvent{}
err = json.Unmarshal(contactEvent.Task, evt)
if err != nil {
return errors.Wrapf(err, "error unmarshalling stop event: %s", event)
}
err = handleStopEvent(ctx, rt, evt)

case NewConversationEventType, ReferralEventType, MOMissEventType, WelcomeMessageEventType:
case string(models.EventTypeNewConversation), string(models.EventTypeReferral), string(models.EventTypeMissedCall), string(models.EventTypeWelcomeMessage):
evt := &models.ChannelEvent{}
err = json.Unmarshal(contactEvent.Task, evt)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions core/tasks/handler/handle_contact_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,11 @@ func TestChannelEvents(t *testing.T) {
Response string
UpdateLastSeen bool
}{
{handler.NewConversationEventType, testdata.Cathy.ID, testdata.Cathy.URNID, testdata.Org1.ID, testdata.TwitterChannel.ID, nil, "What is your favorite color?", true},
{handler.NewConversationEventType, testdata.Cathy.ID, testdata.Cathy.URNID, testdata.Org1.ID, testdata.VonageChannel.ID, nil, "", true},
{handler.WelcomeMessageEventType, testdata.Cathy.ID, testdata.Cathy.URNID, testdata.Org1.ID, testdata.VonageChannel.ID, nil, "", false},
{handler.ReferralEventType, testdata.Cathy.ID, testdata.Cathy.URNID, testdata.Org1.ID, testdata.TwitterChannel.ID, nil, "", true},
{handler.ReferralEventType, testdata.Cathy.ID, testdata.Cathy.URNID, testdata.Org1.ID, testdata.VonageChannel.ID, nil, "Pick a number between 1-10.", true},
{models.EventTypeNewConversation, testdata.Cathy.ID, testdata.Cathy.URNID, testdata.Org1.ID, testdata.TwitterChannel.ID, nil, "What is your favorite color?", true},
{models.EventTypeNewConversation, testdata.Cathy.ID, testdata.Cathy.URNID, testdata.Org1.ID, testdata.VonageChannel.ID, nil, "", true},
{models.EventTypeWelcomeMessage, testdata.Cathy.ID, testdata.Cathy.URNID, testdata.Org1.ID, testdata.VonageChannel.ID, nil, "", false},
{models.EventTypeReferral, testdata.Cathy.ID, testdata.Cathy.URNID, testdata.Org1.ID, testdata.TwitterChannel.ID, nil, "", true},
{models.EventTypeReferral, testdata.Cathy.ID, testdata.Cathy.URNID, testdata.Org1.ID, testdata.VonageChannel.ID, nil, "Pick a number between 1-10.", true},
}

models.FlushCache()
Expand Down Expand Up @@ -523,7 +523,7 @@ func TestStopEvent(t *testing.T) {
eventJSON, err := json.Marshal(event)
require.NoError(t, err)
task := &queue.Task{
Type: handler.StopEventType,
Type: string(models.EventTypeStopContact),
OrgID: int(testdata.Org1.ID),
Task: eventJSON,
}
Expand Down
4 changes: 2 additions & 2 deletions web/ivr/ivr.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func handleIncoming(ctx context.Context, rt *runtime.Runtime, oa *models.OrgAsse
}

// we first create an incoming call channel event and see if that matches
event := models.NewChannelEvent(models.MOCallEventType, oa.OrgID(), ch.ID(), contact.ID(), urnID, nil, false)
event := models.NewChannelEvent(models.EventTypeIncomingCall, oa.OrgID(), ch.ID(), contact.ID(), urnID, nil, false)

externalID, err := svc.CallIDForRequest(r)
if err != nil {
Expand All @@ -130,7 +130,7 @@ func handleIncoming(ctx context.Context, rt *runtime.Runtime, oa *models.OrgAsse
}

// try to handle this event
session, err := handler.HandleChannelEvent(ctx, rt, models.MOCallEventType, event, call)
session, err := handler.HandleChannelEvent(ctx, rt, models.EventTypeIncomingCall, event, call)
if err != nil {
logrus.WithError(err).WithField("http_request", r).Error("error handling incoming call")

Expand Down

0 comments on commit b5ca282

Please sign in to comment.