Skip to content

Commit

Permalink
Merge pull request #387 from nyaruka/internal_ticketers
Browse files Browse the repository at this point in the history
🎟️ Add support for internal type ticketers
  • Loading branch information
rowanseymour authored Dec 10, 2020
2 parents 43beb1f + 8d0e44b commit 76adbf7
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
48 changes: 48 additions & 0 deletions services/tickets/internal/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package internal

import (
"net/http"

"github.com/nyaruka/gocommon/httpx"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/utils"
"github.com/nyaruka/mailroom/core/models"
)

const (
typeInternal = "internal"
)

func init() {
models.RegisterTicketService(typeInternal, NewService)
}

type service struct {
ticketer *flows.Ticketer
}

// NewService creates a new internal ticket service
func NewService(httpClient *http.Client, httpRetries *httpx.RetryConfig, ticketer *flows.Ticketer, config map[string]string) (models.TicketService, error) {
return &service{ticketer: ticketer}, nil
}

// Open just returns a new ticket - no external service to notify
func (s *service) Open(session flows.Session, subject, body string, logHTTP flows.HTTPLogCallback) (*flows.Ticket, error) {
return flows.NewTicket(flows.TicketUUID(uuids.New()), s.ticketer.Reference(), subject, body, ""), nil
}

// Forward is a noop
func (s *service) Forward(ticket *models.Ticket, msgUUID flows.MsgUUID, text string, attachments []utils.Attachment, logHTTP flows.HTTPLogCallback) error {
return nil
}

// Close is a noop
func (s *service) Close(tickets []*models.Ticket, logHTTP flows.HTTPLogCallback) error {
return nil
}

// Reopen is a noop
func (s *service) Reopen(tickets []*models.Ticket, logHTTP flows.HTTPLogCallback) error {
return nil
}
91 changes: 91 additions & 0 deletions services/tickets/internal/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package internal_test

import (
"net/http"
"testing"

"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/assets/static/types"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/test"
"github.com/nyaruka/goflow/utils"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/services/tickets/internal"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestOpenAndForward(t *testing.T) {
session, _, err := test.CreateTestSession("", envs.RedactionPolicyNone)
require.NoError(t, err)

defer uuids.SetGenerator(uuids.DefaultGenerator)
uuids.SetGenerator(uuids.NewSeededGenerator(12345))

ticketer := flows.NewTicketer(types.NewTicketer(assets.TicketerUUID(uuids.New()), "Support", "internal"))

svc, err := internal.NewService(
http.DefaultClient,
nil,
ticketer,
nil,
)
require.NoError(t, err)

logger := &flows.HTTPLogger{}

ticket, err := svc.Open(session, "Need help", "Where are my cookies?", logger.Log)
assert.NoError(t, err)
assert.Equal(t, &flows.Ticket{
UUID: flows.TicketUUID("e7187099-7d38-4f60-955c-325957214c42"),
Ticketer: ticketer.Reference(),
Subject: "Need help",
Body: "Where are my cookies?",
ExternalID: "",
}, ticket)

assert.Equal(t, 0, len(logger.Logs))

dbTicket := models.NewTicket(ticket.UUID, models.Org1, models.CathyID, models.MailgunID, "", "Need help", "Where are my cookies?", nil)

logger = &flows.HTTPLogger{}
err = svc.Forward(
dbTicket,
flows.MsgUUID("ca5607f0-cba8-4c94-9cd5-c4fbc24aa767"),
"It's urgent",
[]utils.Attachment{utils.Attachment("image/jpg:http://myfiles.com/media/0123/attachment1.jpg")},
logger.Log,
)

// forwarding is a NOOP for internal ticketers
assert.NoError(t, err)
assert.Equal(t, 0, len(logger.Logs))
}

func TestCloseAndReopen(t *testing.T) {
defer uuids.SetGenerator(uuids.DefaultGenerator)
uuids.SetGenerator(uuids.NewSeededGenerator(12345))

ticketer := flows.NewTicketer(types.NewTicketer(assets.TicketerUUID(uuids.New()), "Support", "mailgun"))
svc, err := internal.NewService(http.DefaultClient, nil, ticketer, nil)
require.NoError(t, err)

logger := &flows.HTTPLogger{}
ticket1 := models.NewTicket("88bfa1dc-be33-45c2-b469-294ecb0eba90", models.Org1, models.CathyID, models.ZendeskID, "12", "New ticket", "Where my cookies?", nil)
ticket2 := models.NewTicket("645eee60-7e84-4a9e-ade3-4fce01ae28f1", models.Org1, models.BobID, models.ZendeskID, "14", "Second ticket", "Where my shoes?", nil)

err = svc.Close([]*models.Ticket{ticket1, ticket2}, logger.Log)

// NOOP
assert.NoError(t, err)
assert.Equal(t, 0, len(logger.Logs))

err = svc.Reopen([]*models.Ticket{ticket2}, logger.Log)

// NOOP
assert.NoError(t, err)
assert.Equal(t, 0, len(logger.Logs))
}

0 comments on commit 76adbf7

Please sign in to comment.