diff --git a/core/handlers/base_test.go b/core/handlers/base_test.go index 946f36144..0a2c206cf 100644 --- a/core/handlers/base_test.go +++ b/core/handlers/base_test.go @@ -41,6 +41,7 @@ type TestCase struct { Actions ContactActionMap Msgs ContactMsgMap Modifiers ContactModifierMap + ModifierUser *testdata.User Assertions []Assertion SQLAssertions []SQLAssertion } @@ -222,7 +223,7 @@ func RunTestCases(t *testing.T, ctx context.Context, rt *runtime.Runtime, tcs [] Events: make([]flows.Event, 0, len(mods)), } - scene := models.NewSceneForContact(flowContact) + scene := models.NewSceneForContact(flowContact, tc.ModifierUser.SafeID()) // apply our modifiers for _, mod := range mods { diff --git a/core/handlers/ticket_opened.go b/core/handlers/ticket_opened.go index 119bf01e6..0d878cd7e 100644 --- a/core/handlers/ticket_opened.go +++ b/core/handlers/ticket_opened.go @@ -46,9 +46,20 @@ func handleTicketOpened(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx, o assigneeID = assignee.ID() } + var openedInID models.FlowID + if scene.Session() != nil { + run, _ := scene.Session().FindStep(e.StepUUID()) + flowAsset, _ := oa.FlowByUUID(run.FlowReference().UUID) + if flowAsset != nil { + openedInID = flowAsset.(*models.Flow).ID() + } + } + ticket := models.NewTicket( event.Ticket.UUID, oa.OrgID(), + scene.UserID(), + openedInID, scene.ContactID(), ticketer.ID(), event.Ticket.ExternalID, diff --git a/core/handlers/ticket_opened_test.go b/core/handlers/ticket_opened_test.go index da57129c9..b909ce6d1 100644 --- a/core/handlers/ticket_opened_test.go +++ b/core/handlers/ticket_opened_test.go @@ -47,7 +47,7 @@ func TestTicketOpened(t *testing.T) { oa := testdata.Org1.Load(rt) // an existing ticket - cathyTicket := models.NewTicket(flows.TicketUUID(uuids.New()), testdata.Org1.ID, testdata.Cathy.ID, testdata.Mailgun.ID, "748363", testdata.DefaultTopic.ID, "Who?", models.NilUserID, nil) + cathyTicket := models.NewTicket(flows.TicketUUID(uuids.New()), testdata.Org1.ID, testdata.Admin.ID, models.NilFlowID, testdata.Cathy.ID, testdata.Mailgun.ID, "748363", testdata.DefaultTopic.ID, "Who?", models.NilUserID, nil) err := models.InsertTickets(ctx, db, oa, []*models.Ticket{cathyTicket}) require.NoError(t, err) diff --git a/core/models/contacts.go b/core/models/contacts.go index f29507e47..215bb0b2a 100644 --- a/core/models/contacts.go +++ b/core/models/contacts.go @@ -328,7 +328,7 @@ func LoadContacts(ctx context.Context, db Queryer, oa *OrgAssets, ids []ContactI for _, t := range e.Tickets { ticketer := oa.TicketerByID(t.TicketerID) if ticketer != nil { - tickets = append(tickets, NewTicket(t.UUID, oa.OrgID(), contact.ID(), ticketer.ID(), t.ExternalID, t.TopicID, t.Body, t.AssigneeID, nil)) + tickets = append(tickets, NewTicket(t.UUID, oa.OrgID(), NilUserID, NilFlowID, contact.ID(), ticketer.ID(), t.ExternalID, t.TopicID, t.Body, t.AssigneeID, nil)) } } contact.tickets = tickets diff --git a/core/models/events.go b/core/models/events.go index 5c09ee1d4..0b7860213 100644 --- a/core/models/events.go +++ b/core/models/events.go @@ -16,6 +16,7 @@ import ( type Scene struct { contact *flows.Contact session *Session + userID UserID preCommits map[EventCommitHook][]interface{} postCommits map[EventCommitHook][]interface{} @@ -23,25 +24,24 @@ type Scene struct { // NewSceneForSession creates a new scene for the passed in session func NewSceneForSession(session *Session) *Scene { - s := &Scene{ + return &Scene{ contact: session.Contact(), session: session, preCommits: make(map[EventCommitHook][]interface{}), postCommits: make(map[EventCommitHook][]interface{}), } - return s } // NewSceneForContact creates a new scene for the passed in contact, session will be nil -func NewSceneForContact(contact *flows.Contact) *Scene { - s := &Scene{ +func NewSceneForContact(contact *flows.Contact, userID UserID) *Scene { + return &Scene{ contact: contact, + userID: userID, preCommits: make(map[EventCommitHook][]interface{}), postCommits: make(map[EventCommitHook][]interface{}), } - return s } // SessionID returns the session id for this scene if any @@ -57,9 +57,10 @@ func (s *Scene) ContactID() ContactID { return ContactID(s.contact.ID( func (s *Scene) ContactUUID() flows.ContactUUID { return s.contact.UUID() } // Session returns the session for this scene if any -func (s *Scene) Session() *Session { - return s.session -} +func (s *Scene) Session() *Session { return s.session } + +// User returns the user ID for this scene if any +func (s *Scene) UserID() UserID { return s.userID } // AppendToEventPreCommitHook adds a new event to be handled by a pre commit hook func (s *Scene) AppendToEventPreCommitHook(hook EventCommitHook, event interface{}) { @@ -186,11 +187,11 @@ func ApplyEventPostCommitHooks(ctx context.Context, rt *runtime.Runtime, tx *sql } // HandleAndCommitEvents takes a set of contacts and events, handles the events and applies any hooks, and commits everything -func HandleAndCommitEvents(ctx context.Context, rt *runtime.Runtime, oa *OrgAssets, contactEvents map[*flows.Contact][]flows.Event) error { +func HandleAndCommitEvents(ctx context.Context, rt *runtime.Runtime, oa *OrgAssets, userID UserID, contactEvents map[*flows.Contact][]flows.Event) error { // create scenes for each contact scenes := make([]*Scene, 0, len(contactEvents)) for contact := range contactEvents { - scene := NewSceneForContact(contact) + scene := NewSceneForContact(contact, userID) scenes = append(scenes, scene) } @@ -239,7 +240,9 @@ func HandleAndCommitEvents(ctx context.Context, rt *runtime.Runtime, oa *OrgAsse } // ApplyModifiers modifies contacts by applying modifiers and handling the resultant events -func ApplyModifiers(ctx context.Context, rt *runtime.Runtime, oa *OrgAssets, modifiersByContact map[*flows.Contact][]flows.Modifier) (map[*flows.Contact][]flows.Event, error) { +// Note that we don't load the user object from org assets because it's possible that the user isn't part +// of the org, e.g. customer support. +func ApplyModifiers(ctx context.Context, rt *runtime.Runtime, oa *OrgAssets, userID UserID, modifiersByContact map[*flows.Contact][]flows.Modifier) (map[*flows.Contact][]flows.Event, error) { // create an environment instance with location support env := flows.NewEnvironment(oa.Env(), oa.SessionAssets().Locations()) @@ -256,7 +259,7 @@ func ApplyModifiers(ctx context.Context, rt *runtime.Runtime, oa *OrgAssets, mod eventsByContact[contact] = events } - err := HandleAndCommitEvents(ctx, rt, oa, eventsByContact) + err := HandleAndCommitEvents(ctx, rt, oa, userID, eventsByContact) if err != nil { return nil, errors.Wrap(err, "error commiting events") } diff --git a/core/models/imports.go b/core/models/imports.go index bd2ada923..5a51b37f8 100644 --- a/core/models/imports.go +++ b/core/models/imports.go @@ -161,7 +161,8 @@ func (b *ContactImportBatch) tryImport(ctx context.Context, rt *runtime.Runtime, } // and apply in bulk - _, err = ApplyModifiers(ctx, rt, oa, modifiersByContact) + // TODO pass user here who created the import? + _, err = ApplyModifiers(ctx, rt, oa, NilUserID, modifiersByContact) if err != nil { return errors.Wrap(err, "error applying modifiers") } diff --git a/core/models/tickets.go b/core/models/tickets.go index 86048eb6f..1f23262d9 100644 --- a/core/models/tickets.go +++ b/core/models/tickets.go @@ -91,6 +91,8 @@ type Ticket struct { AssigneeID UserID `db:"assignee_id"` Config null.Map `db:"config"` OpenedOn time.Time `db:"opened_on"` + OpenedByID UserID `db:"opened_by_id"` + OpenedInID FlowID `db:"opened_in_id"` RepliedOn *time.Time `db:"replied_on"` ModifiedOn time.Time `db:"modified_on"` ClosedOn *time.Time `db:"closed_on"` @@ -99,10 +101,12 @@ type Ticket struct { } // NewTicket creates a new open ticket -func NewTicket(uuid flows.TicketUUID, orgID OrgID, contactID ContactID, ticketerID TicketerID, externalID string, topicID TopicID, body string, assigneeID UserID, config map[string]interface{}) *Ticket { +func NewTicket(uuid flows.TicketUUID, orgID OrgID, userID UserID, flowID FlowID, contactID ContactID, ticketerID TicketerID, externalID string, topicID TopicID, body string, assigneeID UserID, config map[string]interface{}) *Ticket { t := &Ticket{} t.t.UUID = uuid t.t.OrgID = orgID + t.t.OpenedByID = userID + t.t.OpenedInID = flowID t.t.ContactID = contactID t.t.TicketerID = ticketerID t.t.ExternalID = null.String(externalID) @@ -184,28 +188,28 @@ func (t *Ticket) ForwardIncoming(ctx context.Context, rt *runtime.Runtime, oa *O const sqlSelectOpenTickets = ` SELECT - t.id AS id, - t.uuid AS uuid, - t.org_id AS org_id, - t.contact_id AS contact_id, - t.ticketer_id AS ticketer_id, - t.external_id AS external_id, - t.status AS status, - t.topic_id AS topic_id, - t.body AS body, - t.assignee_id AS assignee_id, - t.config AS config, - t.opened_on AS opened_on, + t.id, + t.uuid, + t.org_id, + t.contact_id, + t.ticketer_id, + t.external_id, + t.status, + t.topic_id, + t.body, + t.assignee_id, + t.config, + t.opened_on, + t.opened_by_id, + t.opened_in_id, t.replied_on, - t.modified_on AS modified_on, - t.closed_on AS closed_on, - t.last_activity_on AS last_activity_on + t.modified_on, + t.closed_on, + t.last_activity_on FROM tickets_ticket t WHERE - t.contact_id = $1 AND - t.status = 'O' -` + t.contact_id = $1 AND t.status = 'O'` // LoadOpenTicketsForContact looks up the open tickets for the passed in contact func LoadOpenTicketsForContact(ctx context.Context, db Queryer, contact *Contact) ([]*Ticket, error) { @@ -214,27 +218,28 @@ func LoadOpenTicketsForContact(ctx context.Context, db Queryer, contact *Contact const sqlSelectTicketsByID = ` SELECT - t.id AS id, - t.uuid AS uuid, - t.org_id AS org_id, - t.contact_id AS contact_id, - t.ticketer_id AS ticketer_id, - t.external_id AS external_id, - t.status AS status, - t.topic_id AS topic_id, - t.body AS body, - t.assignee_id AS assignee_id, - t.config AS config, - t.opened_on AS opened_on, + t.id, + t.uuid, + t.org_id, + t.contact_id, + t.ticketer_id, + t.external_id, + t.status, + t.topic_id, + t.body, + t.assignee_id, + t.config, + t.opened_on, + t.opened_by_id, + t.opened_in_id, t.replied_on, - t.modified_on AS modified_on, - t.closed_on AS closed_on, - t.last_activity_on AS last_activity_on + t.modified_on, + t.closed_on, + t.last_activity_on FROM tickets_ticket t WHERE - t.id = ANY($1) -` + t.id = ANY($1)` // LoadTickets loads all of the tickets with the given ids func LoadTickets(ctx context.Context, db Queryer, ids []TicketID) ([]*Ticket, error) { @@ -263,27 +268,28 @@ func loadTickets(ctx context.Context, db Queryer, query string, params ...interf const sqlSelectTicketByUUID = ` SELECT - t.id AS id, - t.uuid AS uuid, - t.org_id AS org_id, - t.contact_id AS contact_id, - t.ticketer_id AS ticketer_id, - t.external_id AS external_id, - t.status AS status, - t.topic_id AS topic_id, - t.body AS body, - t.assignee_id AS assignee_id, - t.config AS config, - t.opened_on AS opened_on, + t.id, + t.uuid, + t.org_id, + t.contact_id, + t.ticketer_id, + t.external_id, + t.status, + t.topic_id, + t.body, + t.assignee_id, + t.config, + t.opened_on, + t.opened_by_id, + t.opened_in_id, t.replied_on, - t.modified_on AS modified_on, - t.closed_on AS closed_on, - t.last_activity_on AS last_activity_on + t.modified_on, + t.closed_on, + t.last_activity_on FROM tickets_ticket t WHERE - t.uuid = $1 -` + t.uuid = $1` // LookupTicketByUUID looks up the ticket with the passed in UUID func LookupTicketByUUID(ctx context.Context, db *sqlx.DB, uuid flows.TicketUUID) (*Ticket, error) { @@ -292,28 +298,28 @@ func LookupTicketByUUID(ctx context.Context, db *sqlx.DB, uuid flows.TicketUUID) const sqlSelectTicketByExternalID = ` SELECT - t.id AS id, - t.uuid AS uuid, - t.org_id AS org_id, - t.contact_id AS contact_id, - t.ticketer_id AS ticketer_id, - t.external_id AS external_id, - t.status AS status, - t.topic_id AS topic_id, - t.body AS body, - t.assignee_id AS assignee_id, - t.config AS config, - t.opened_on AS opened_on, + t.id, + t.uuid, + t.org_id, + t.contact_id, + t.ticketer_id, + t.external_id, + t.status, + t.topic_id, + t.body, + t.assignee_id, + t.config, + t.opened_on, + t.opened_by_id, + t.opened_in_id, t.replied_on, - t.modified_on AS modified_on, - t.closed_on AS closed_on, - t.last_activity_on AS last_activity_on + t.modified_on, + t.closed_on, + t.last_activity_on FROM tickets_ticket t WHERE - t.ticketer_id = $1 AND - t.external_id = $2 -` + t.ticketer_id = $1 AND t.external_id = $2` // LookupTicketByExternalID looks up the ticket with the passed in ticketer and external ID func LookupTicketByExternalID(ctx context.Context, db Queryer, ticketerID TicketerID, externalID string) (*Ticket, error) { @@ -342,8 +348,8 @@ func lookupTicket(ctx context.Context, db Queryer, query string, params ...inter const sqlInsertTicket = ` INSERT INTO - tickets_ticket(uuid, org_id, contact_id, ticketer_id, external_id, status, topic_id, body, assignee_id, config, opened_on, modified_on, last_activity_on) - VALUES( :uuid, :org_id, :contact_id, :ticketer_id, :external_id, :status, :topic_id, :body, :assignee_id, :config, NOW(), NOW() , NOW()) + tickets_ticket(uuid, org_id, contact_id, ticketer_id, external_id, status, topic_id, body, assignee_id, config, opened_on, opened_by_id, opened_in_id, modified_on, last_activity_on) + VALUES( :uuid, :org_id, :contact_id, :ticketer_id, :external_id, :status, :topic_id, :body, :assignee_id, :config, NOW(), :opened_by_id, :opened_in_id, NOW() , NOW()) RETURNING id ` diff --git a/core/models/tickets_test.go b/core/models/tickets_test.go index e4541f013..fb542df9a 100644 --- a/core/models/tickets_test.go +++ b/core/models/tickets_test.go @@ -69,6 +69,8 @@ func TestTickets(t *testing.T) { ticket1 := models.NewTicket( "2ef57efc-d85f-4291-b330-e4afe68af5fe", testdata.Org1.ID, + testdata.Admin.ID, + models.NilFlowID, testdata.Cathy.ID, testdata.Mailgun.ID, "EX12345", @@ -82,6 +84,8 @@ func TestTickets(t *testing.T) { ticket2 := models.NewTicket( "64f81be1-00ff-48ef-9e51-97d6f924c1a4", testdata.Org1.ID, + testdata.Admin.ID, + models.NilFlowID, testdata.Bob.ID, testdata.Zendesk.ID, "EX7869", @@ -93,6 +97,8 @@ func TestTickets(t *testing.T) { ticket3 := models.NewTicket( "28ef8ddc-b221-42f3-aeae-ee406fc9d716", testdata.Org1.ID, + models.NilUserID, + testdata.Favorites.ID, testdata.Alexandria.ID, testdata.Zendesk.ID, "EX6677", diff --git a/core/tasks/handler/worker.go b/core/tasks/handler/worker.go index 4f3528a2c..b77fc609f 100644 --- a/core/tasks/handler/worker.go +++ b/core/tasks/handler/worker.go @@ -752,7 +752,7 @@ func handleAsInbox(ctx context.Context, rt *runtime.Runtime, oa *models.OrgAsset contact.SetLastSeenOn(msgEvent.CreatedOn()) contactEvents := map[*flows.Contact][]flows.Event{contact: {msgEvent}} - err := models.HandleAndCommitEvents(ctx, rt, oa, contactEvents) + err := models.HandleAndCommitEvents(ctx, rt, oa, models.NilUserID, contactEvents) if err != nil { return errors.Wrap(err, "error handling inbox message events") } diff --git a/mailroom_test.dump b/mailroom_test.dump index ed45e4541..b8d2d8a07 100644 Binary files a/mailroom_test.dump and b/mailroom_test.dump differ diff --git a/services/tickets/intern/service_test.go b/services/tickets/intern/service_test.go index 1b303b4e1..e97f01c22 100644 --- a/services/tickets/intern/service_test.go +++ b/services/tickets/intern/service_test.go @@ -53,7 +53,7 @@ func TestOpenAndForward(t *testing.T) { assert.Equal(t, "", ticket.ExternalID()) assert.Equal(t, 0, len(logger.Logs)) - dbTicket := models.NewTicket(ticket.UUID(), testdata.Org1.ID, testdata.Cathy.ID, testdata.Internal.ID, "", testdata.DefaultTopic.ID, "Where are my cookies?", models.NilUserID, nil) + dbTicket := models.NewTicket(ticket.UUID(), testdata.Org1.ID, testdata.Admin.ID, models.NilFlowID, testdata.Cathy.ID, testdata.Internal.ID, "", testdata.DefaultTopic.ID, "Where are my cookies?", models.NilUserID, nil) logger = &flows.HTTPLogger{} err = svc.Forward( @@ -80,8 +80,8 @@ func TestCloseAndReopen(t *testing.T) { require.NoError(t, err) logger := &flows.HTTPLogger{} - ticket1 := models.NewTicket("88bfa1dc-be33-45c2-b469-294ecb0eba90", testdata.Org1.ID, testdata.Cathy.ID, testdata.Internal.ID, "12", testdata.DefaultTopic.ID, "Where my cookies?", models.NilUserID, nil) - ticket2 := models.NewTicket("645eee60-7e84-4a9e-ade3-4fce01ae28f1", testdata.Org1.ID, testdata.Bob.ID, testdata.Internal.ID, "14", testdata.DefaultTopic.ID, "Where my shoes?", models.NilUserID, nil) + ticket1 := models.NewTicket("88bfa1dc-be33-45c2-b469-294ecb0eba90", testdata.Org1.ID, testdata.Admin.ID, models.NilFlowID, testdata.Cathy.ID, testdata.Internal.ID, "12", testdata.DefaultTopic.ID, "Where my cookies?", models.NilUserID, nil) + ticket2 := models.NewTicket("645eee60-7e84-4a9e-ade3-4fce01ae28f1", testdata.Org1.ID, testdata.Admin.ID, models.NilFlowID, testdata.Bob.ID, testdata.Internal.ID, "14", testdata.DefaultTopic.ID, "Where my shoes?", models.NilUserID, nil) err = svc.Close([]*models.Ticket{ticket1, ticket2}, logger.Log) diff --git a/services/tickets/mailgun/service_test.go b/services/tickets/mailgun/service_test.go index 220bb5818..7b4868e72 100644 --- a/services/tickets/mailgun/service_test.go +++ b/services/tickets/mailgun/service_test.go @@ -98,7 +98,7 @@ func TestOpenAndForward(t *testing.T) { assert.Equal(t, 1, len(logger.Logs)) test.AssertSnapshot(t, "open_ticket", logger.Logs[0].Request) - dbTicket := models.NewTicket(ticket.UUID(), testdata.Org1.ID, testdata.Cathy.ID, testdata.Mailgun.ID, "", testdata.DefaultTopic.ID, "Where are my cookies?", models.NilUserID, map[string]interface{}{ + dbTicket := models.NewTicket(ticket.UUID(), testdata.Org1.ID, testdata.Admin.ID, models.NilFlowID, testdata.Cathy.ID, testdata.Mailgun.ID, "", testdata.DefaultTopic.ID, "Where are my cookies?", models.NilUserID, map[string]interface{}{ "contact-uuid": string(testdata.Cathy.UUID), "contact-display": "Cathy", }) @@ -158,8 +158,8 @@ func TestCloseAndReopen(t *testing.T) { require.NoError(t, err) logger := &flows.HTTPLogger{} - ticket1 := models.NewTicket("88bfa1dc-be33-45c2-b469-294ecb0eba90", testdata.Org1.ID, testdata.Cathy.ID, testdata.Zendesk.ID, "12", testdata.DefaultTopic.ID, "Where my cookies?", models.NilUserID, nil) - ticket2 := models.NewTicket("645eee60-7e84-4a9e-ade3-4fce01ae28f1", testdata.Org1.ID, testdata.Bob.ID, testdata.Zendesk.ID, "14", testdata.DefaultTopic.ID, "Where my shoes?", models.NilUserID, nil) + ticket1 := models.NewTicket("88bfa1dc-be33-45c2-b469-294ecb0eba90", testdata.Org1.ID, testdata.Admin.ID, models.NilFlowID, testdata.Cathy.ID, testdata.Zendesk.ID, "12", testdata.DefaultTopic.ID, "Where my cookies?", models.NilUserID, nil) + ticket2 := models.NewTicket("645eee60-7e84-4a9e-ade3-4fce01ae28f1", testdata.Org1.ID, testdata.Admin.ID, models.NilFlowID, testdata.Bob.ID, testdata.Zendesk.ID, "14", testdata.DefaultTopic.ID, "Where my shoes?", models.NilUserID, nil) err = svc.Close([]*models.Ticket{ticket1, ticket2}, logger.Log) diff --git a/services/tickets/rocketchat/service_test.go b/services/tickets/rocketchat/service_test.go index 4305f6f06..a5978a41a 100644 --- a/services/tickets/rocketchat/service_test.go +++ b/services/tickets/rocketchat/service_test.go @@ -88,7 +88,7 @@ func TestOpenAndForward(t *testing.T) { assert.Equal(t, 1, len(logger.Logs)) test.AssertSnapshot(t, "open_ticket", logger.Logs[0].Request) - dbTicket := models.NewTicket(ticket.UUID(), testdata.Org1.ID, testdata.Cathy.ID, testdata.RocketChat.ID, "", testdata.DefaultTopic.ID, "Where are my cookies?", models.NilUserID, map[string]interface{}{ + dbTicket := models.NewTicket(ticket.UUID(), testdata.Org1.ID, testdata.Admin.ID, models.NilFlowID, testdata.Cathy.ID, testdata.RocketChat.ID, "", testdata.DefaultTopic.ID, "Where are my cookies?", models.NilUserID, map[string]interface{}{ "contact-uuid": string(testdata.Cathy.UUID), "contact-display": "Cathy", }) @@ -136,8 +136,8 @@ func TestCloseAndReopen(t *testing.T) { ) require.NoError(t, err) - ticket1 := models.NewTicket("88bfa1dc-be33-45c2-b469-294ecb0eba90", testdata.Org1.ID, testdata.Cathy.ID, testdata.RocketChat.ID, "X5gwXeaxbnGDaq8Q3", testdata.DefaultTopic.ID, "Where my cookies?", models.NilUserID, nil) - ticket2 := models.NewTicket("645eee60-7e84-4a9e-ade3-4fce01ae28f1", testdata.Org1.ID, testdata.Bob.ID, testdata.RocketChat.ID, "cq7AokJHKkGhAMoBK", testdata.DefaultTopic.ID, "Where my shoes?", models.NilUserID, nil) + ticket1 := models.NewTicket("88bfa1dc-be33-45c2-b469-294ecb0eba90", testdata.Org1.ID, testdata.Admin.ID, models.NilFlowID, testdata.Cathy.ID, testdata.RocketChat.ID, "X5gwXeaxbnGDaq8Q3", testdata.DefaultTopic.ID, "Where my cookies?", models.NilUserID, nil) + ticket2 := models.NewTicket("645eee60-7e84-4a9e-ade3-4fce01ae28f1", testdata.Org1.ID, testdata.Admin.ID, models.NilFlowID, testdata.Bob.ID, testdata.RocketChat.ID, "cq7AokJHKkGhAMoBK", testdata.DefaultTopic.ID, "Where my shoes?", models.NilUserID, nil) logger := &flows.HTTPLogger{} err = svc.Close([]*models.Ticket{ticket1, ticket2}, logger.Log) diff --git a/services/tickets/utils_test.go b/services/tickets/utils_test.go index f917ad80f..9bc25c280 100644 --- a/services/tickets/utils_test.go +++ b/services/tickets/utils_test.go @@ -164,6 +164,8 @@ func TestCloseTicket(t *testing.T) { ticket1 := models.NewTicket( "2ef57efc-d85f-4291-b330-e4afe68af5fe", testdata.Org1.ID, + testdata.Admin.ID, + models.NilFlowID, testdata.Cathy.ID, testdata.Mailgun.ID, "EX12345", diff --git a/services/tickets/zendesk/service_test.go b/services/tickets/zendesk/service_test.go index 32eadda68..acb62ec65 100644 --- a/services/tickets/zendesk/service_test.go +++ b/services/tickets/zendesk/service_test.go @@ -104,7 +104,7 @@ func TestOpenAndForward(t *testing.T) { assert.Equal(t, 1, len(logger.Logs)) test.AssertSnapshot(t, "open_ticket", logger.Logs[0].Request) - dbTicket := models.NewTicket(ticket.UUID(), testdata.Org1.ID, testdata.Cathy.ID, testdata.Zendesk.ID, "", testdata.DefaultTopic.ID, "Where are my cookies?", models.NilUserID, map[string]interface{}{ + dbTicket := models.NewTicket(ticket.UUID(), testdata.Org1.ID, testdata.Admin.ID, models.NilFlowID, testdata.Cathy.ID, testdata.Zendesk.ID, "", testdata.DefaultTopic.ID, "Where are my cookies?", models.NilUserID, map[string]interface{}{ "contact-uuid": string(testdata.Cathy.UUID), "contact-display": "Cathy", }) @@ -167,8 +167,8 @@ func TestCloseAndReopen(t *testing.T) { require.NoError(t, err) logger := &flows.HTTPLogger{} - ticket1 := models.NewTicket("88bfa1dc-be33-45c2-b469-294ecb0eba90", testdata.Org1.ID, testdata.Cathy.ID, testdata.Zendesk.ID, "12", testdata.DefaultTopic.ID, "Where my cookies?", models.NilUserID, nil) - ticket2 := models.NewTicket("645eee60-7e84-4a9e-ade3-4fce01ae28f1", testdata.Org1.ID, testdata.Bob.ID, testdata.Zendesk.ID, "14", testdata.DefaultTopic.ID, "Where my shoes?", models.NilUserID, nil) + ticket1 := models.NewTicket("88bfa1dc-be33-45c2-b469-294ecb0eba90", testdata.Org1.ID, testdata.Admin.ID, models.NilFlowID, testdata.Cathy.ID, testdata.Zendesk.ID, "12", testdata.DefaultTopic.ID, "Where my cookies?", models.NilUserID, nil) + ticket2 := models.NewTicket("645eee60-7e84-4a9e-ade3-4fce01ae28f1", testdata.Org1.ID, testdata.Admin.ID, models.NilFlowID, testdata.Bob.ID, testdata.Zendesk.ID, "14", testdata.DefaultTopic.ID, "Where my shoes?", models.NilUserID, nil) err = svc.Close([]*models.Ticket{ticket1, ticket2}, logger.Log) diff --git a/web/contact/contact.go b/web/contact/contact.go index db42d3dd6..7ee66b090 100644 --- a/web/contact/contact.go +++ b/web/contact/contact.go @@ -38,7 +38,7 @@ func init() { // type createRequest struct { OrgID models.OrgID `json:"org_id" validate:"required"` - UserID models.UserID `json:"user_id"` + UserID models.UserID `json:"user_id" validate:"required"` Contact *models.ContactSpec `json:"contact" validate:"required"` } @@ -66,7 +66,7 @@ func handleCreate(ctx context.Context, rt *runtime.Runtime, r *http.Request) (in } modifiersByContact := map[*flows.Contact][]flows.Modifier{contact: c.Mods} - _, err = models.ApplyModifiers(ctx, rt, oa, modifiersByContact) + _, err = models.ApplyModifiers(ctx, rt, oa, request.UserID, modifiersByContact) if err != nil { return nil, http.StatusInternalServerError, errors.Wrap(err, "error modifying new contact") } @@ -154,7 +154,7 @@ func handleModify(ctx context.Context, rt *runtime.Runtime, r *http.Request) (in modifiersByContact[flowContact] = mods } - eventsByContact, err := models.ApplyModifiers(ctx, rt, oa, modifiersByContact) + eventsByContact, err := models.ApplyModifiers(ctx, rt, oa, request.UserID, modifiersByContact) if err != nil { return nil, http.StatusBadRequest, err } diff --git a/web/contact/testdata/create.json b/web/contact/testdata/create.json index 75178278f..e11b8c29f 100644 --- a/web/contact/testdata/create.json +++ b/web/contact/testdata/create.json @@ -5,7 +5,7 @@ "path": "/mr/contact/create", "body": { "org_id": 1, - "user_id": 1 + "user_id": 3 }, "status": 400, "response": { @@ -50,7 +50,7 @@ "path": "/mr/contact/create", "body": { "org_id": 1, - "user_id": 1, + "user_id": 3, "contact": { "name": "José", "language": "spa", @@ -103,7 +103,7 @@ "path": "/mr/contact/create", "body": { "org_id": 1, - "user_id": 1, + "user_id": 3, "contact": { "name": "María", "language": "xyz" @@ -120,7 +120,7 @@ "path": "/mr/contact/create", "body": { "org_id": 1, - "user_id": 1, + "user_id": 3, "contact": { "name": "María", "urns": [ @@ -139,7 +139,7 @@ "path": "/mr/contact/create", "body": { "org_id": 1, - "user_id": 1, + "user_id": 3, "contact": { "name": "María", "urns": [ diff --git a/web/contact/testdata/modify.json b/web/contact/testdata/modify.json index 11bc49945..235edf044 100644 --- a/web/contact/testdata/modify.json +++ b/web/contact/testdata/modify.json @@ -5,6 +5,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -37,6 +38,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 1, "contact_ids": [ 10000 ], @@ -80,7 +82,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, - "user_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -124,6 +126,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -166,7 +169,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, - "user_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -209,7 +212,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, - "user_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -252,7 +255,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, - "user_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -295,6 +298,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -352,6 +356,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -438,6 +443,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -504,6 +510,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -574,6 +581,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -693,6 +701,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -752,6 +761,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -800,6 +810,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -853,6 +864,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -895,6 +907,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -938,6 +951,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -989,6 +1003,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -1038,6 +1053,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -1089,6 +1105,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -1133,6 +1150,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -1188,6 +1206,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -1254,6 +1273,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 3, "contact_ids": [ 10000 ], @@ -1409,6 +1429,7 @@ "path": "/mr/contact/modify", "body": { "org_id": 1, + "user_id": 3, "contact_ids": [ 10000 ],