Skip to content

Commit

Permalink
simplify getcontact callO
Browse files Browse the repository at this point in the history
  • Loading branch information
nicpottier committed Feb 13, 2020
1 parent 8b9dc91 commit f0f592f
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 29 deletions.
4 changes: 2 additions & 2 deletions ivr/ivr.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func StartIVRFlow(
}

// our flow contact
contact, err := c.FlowContact(org, org.SessionAssets())
contact, err := c.FlowContact(org)
if err != nil {
return errors.Wrapf(err, "error loading flow contact")
}
Expand Down Expand Up @@ -407,7 +407,7 @@ func ResumeIVRFlow(
org *models.OrgAssets, channel *models.Channel, conn *models.ChannelConnection, c *models.Contact, urn urns.URN,
r *http.Request, w http.ResponseWriter) error {

contact, err := c.FlowContact(org, org.SessionAssets())
contact, err := c.FlowContact(org)
if err != nil {
return errors.Wrapf(err, "error creating flow contact")
}
Expand Down
7 changes: 0 additions & 7 deletions models/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ type OrgAssets struct {
}

var orgCache = cache.New(time.Hour, time.Minute*5)
var assetCache = cache.New(5*time.Second, time.Minute*5)
var ErrNotFound = errors.New("not found")

const cacheTimeout = time.Second * 5
Expand All @@ -78,7 +77,6 @@ const locationCacheTimeout = time.Hour
// FlushCache clears our entire org cache
func FlushCache() {
orgCache.Flush()
assetCache.Flush()
}

// NewOrgAssets creates and returns a new org assets objects, potentially using the previous
Expand Down Expand Up @@ -252,11 +250,6 @@ func GetOrgAssets(ctx context.Context, db *sqlx.DB, orgID OrgID) (*OrgAssets, er
return o, nil
}

// GetSessionAssets returns a goflow session assets object for the passed in org assets
func GetSessionAssets(org *OrgAssets) (flows.SessionAssets, error) {
return org.sessionAssets, nil
}

func (a *OrgAssets) OrgID() OrgID { return a.orgID }

func (a *OrgAssets) Env() envs.Environment { return a.env }
Expand Down
6 changes: 3 additions & 3 deletions models/contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func ContactIDsForQuery(ctx context.Context, client *elastic.Client, org *OrgAss
}

// FlowContact converts our mailroom contact into a flow contact for use in the engine
func (c *Contact) FlowContact(org *OrgAssets, session flows.SessionAssets) (*flows.Contact, error) {
func (c *Contact) FlowContact(org *OrgAssets) (*flows.Contact, error) {
// convert our groups to a list of asset groups
groups := make([]assets.Group, len(c.groups))
for i, g := range c.groups {
Expand All @@ -342,7 +342,7 @@ func (c *Contact) FlowContact(org *OrgAssets, session flows.SessionAssets) (*flo

// create our flow contact
contact, err := flows.NewContact(
session,
org.SessionAssets(),
c.uuid,
flows.ContactID(c.id),
c.name,
Expand Down Expand Up @@ -663,7 +663,7 @@ func CreateContact(ctx context.Context, db *sqlx.DB, org *OrgAssets, assets flow
return NilContactID, errors.Wrapf(err, "error loading new contact")
}

flowContact, err := contacts[0].FlowContact(org, assets)
flowContact, err := contacts[0].FlowContact(org)
if err != nil {
tx.Rollback()
return NilContactID, errors.Wrapf(err, "error creating flow contact")
Expand Down
12 changes: 5 additions & 7 deletions models/contacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,6 @@ func TestContacts(t *testing.T) {
org, err := GetOrgAssets(ctx, db, 1)
assert.NoError(t, err)

session := org.SessionAssets()

db.MustExec(
`INSERT INTO contacts_contacturn(org_id, contact_id, scheme, path, identity, priority)
VALUES(1, $1, 'whatsapp', '250788373373', 'whatsapp:250788373373', 100)`, BobID)
Expand All @@ -176,7 +174,7 @@ func TestContacts(t *testing.T) {
// convert to goflow contacts
contacts := make([]*flows.Contact, len(modelContacts))
for i := range modelContacts {
contacts[i], err = modelContacts[i].FlowContact(org, org.SessionAssets())
contacts[i], err = modelContacts[i].FlowContact(org)
assert.NoError(t, err)
}

Expand Down Expand Up @@ -209,7 +207,7 @@ func TestContacts(t *testing.T) {
err = modelContacts[1].UpdatePreferredURN(ctx, db, org, BobURNID, channel)
assert.NoError(t, err)

bob, err := modelContacts[1].FlowContact(org, session)
bob, err := modelContacts[1].FlowContact(org)
assert.NoError(t, err)
assert.Equal(t, "tel:+16055742222?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=10001&priority=1000", bob.URNs()[0].String())
assert.Equal(t, "whatsapp:250788373373?id=20121&priority=999", bob.URNs()[1].String())
Expand All @@ -227,7 +225,7 @@ func TestContacts(t *testing.T) {
err = modelContacts[0].UpdatePreferredURN(ctx, db, org, URNID(20122), channel)
assert.NoError(t, err)

bob, err = modelContacts[0].FlowContact(org, session)
bob, err = modelContacts[0].FlowContact(org)
assert.NoError(t, err)
assert.Equal(t, "tel:+250788373393?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=20122&priority=1000", bob.URNs()[0].String())
assert.Equal(t, "tel:+16055742222?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=10001&priority=999", bob.URNs()[1].String())
Expand All @@ -237,7 +235,7 @@ func TestContacts(t *testing.T) {
err = modelContacts[0].UpdatePreferredURN(ctx, db, org, URNID(20122), channel)
assert.NoError(t, err)

bob, err = modelContacts[0].FlowContact(org, session)
bob, err = modelContacts[0].FlowContact(org)
assert.NoError(t, err)
assert.Equal(t, "tel:+250788373393?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=20122&priority=1000", bob.URNs()[0].String())
assert.Equal(t, "tel:+16055742222?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=10001&priority=999", bob.URNs()[1].String())
Expand All @@ -247,7 +245,7 @@ func TestContacts(t *testing.T) {
err = modelContacts[0].UpdatePreferredURN(ctx, db, org, URNID(20122), nil)
assert.NoError(t, err)

bob, err = modelContacts[0].FlowContact(org, session)
bob, err = modelContacts[0].FlowContact(org)
assert.NoError(t, err)
assert.Equal(t, "tel:+250788373393?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=20122&priority=1000", bob.URNs()[0].String())
assert.Equal(t, "tel:+16055742222?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=10001&priority=999", bob.URNs()[1].String())
Expand Down
2 changes: 1 addition & 1 deletion models/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func AddContactsToGroupAndCampaigns(ctx context.Context, db *sqlx.DB, org *OrgAs
// convert to flow contacts
fcs := make([]*flows.Contact, len(contacts))
for i, c := range contacts {
fcs[i], err = c.FlowContact(org, org.SessionAssets())
fcs[i], err = c.FlowContact(org)
if err != nil {
tx.Rollback()
return errors.Wrapf(err, "error converting contact to flow contact: %s", c.UUID())
Expand Down
2 changes: 1 addition & 1 deletion models/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ func CreateBroadcastMessages(ctx context.Context, db Queryer, rp *redis.Pool, or
return nil, nil
}

contact, err := c.FlowContact(org, sa)
contact, err := c.FlowContact(org)
if err != nil {
return nil, errors.Wrapf(err, "error creating flow contact")
}
Expand Down
4 changes: 2 additions & 2 deletions models/triggers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ func TestTriggers(t *testing.T) {
contacts, err := LoadContacts(ctx, db, org, contactIDs)
assert.NoError(t, err)

cathy, err := contacts[0].FlowContact(org, org.SessionAssets())
cathy, err := contacts[0].FlowContact(org)
assert.NoError(t, err)

greg, err := contacts[1].FlowContact(org, org.SessionAssets())
greg, err := contacts[1].FlowContact(org)
assert.NoError(t, err)

tcs := []struct {
Expand Down
2 changes: 1 addition & 1 deletion runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ func StartFlow(
// ok, we've filtered our contacts, build our triggers
triggers := make([]flows.Trigger, 0, len(locked))
for _, c := range contacts {
contact, err := c.FlowContact(org, org.SessionAssets())
contact, err := c.FlowContact(org)
if err != nil {
return nil, errors.Wrapf(err, "error creating flow contact")
}
Expand Down
2 changes: 1 addition & 1 deletion runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func TestContactRuns(t *testing.T) {
contacts, err := models.LoadContacts(ctx, db, org, []models.ContactID{models.CathyID})
assert.NoError(t, err)

contact, err := contacts[0].FlowContact(org, org.SessionAssets())
contact, err := contacts[0].FlowContact(org)
assert.NoError(t, err)

trigger := triggers.NewManual(org.Env(), flow.FlowReference(), contact, nil)
Expand Down
6 changes: 3 additions & 3 deletions tasks/handler/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func handleTimedEvent(ctx context.Context, db *sqlx.DB, rp *redis.Pool, eventTyp
modelContact := contacts[0]

// build our flow contact
contact, err := modelContact.FlowContact(org, org.SessionAssets())
contact, err := modelContact.FlowContact(org)
if err != nil {
return errors.Wrapf(err, "error creating flow contact")
}
Expand Down Expand Up @@ -350,7 +350,7 @@ func HandleChannelEvent(ctx context.Context, db *sqlx.DB, rp *redis.Pool, eventT
}

// build our flow contact
contact, err := modelContact.FlowContact(org, org.SessionAssets())
contact, err := modelContact.FlowContact(org)
if err != nil {
return nil, errors.Wrapf(err, "error creating flow contact")
}
Expand Down Expand Up @@ -500,7 +500,7 @@ func handleMsgEvent(ctx context.Context, db *sqlx.DB, rp *redis.Pool, event *Msg
}

// build our flow contact
contact, err := modelContact.FlowContact(org, org.SessionAssets())
contact, err := modelContact.FlowContact(org)
if err != nil {
return errors.Wrapf(err, "error creating flow contact")
}
Expand Down
2 changes: 1 addition & 1 deletion web/surveyor/surveyor.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func handleSubmit(ctx context.Context, s *web.Server, r *http.Request) (interfac
}

// load our flow contact
flowContact, err := contacts[0].FlowContact(org, org.SessionAssets())
flowContact, err := contacts[0].FlowContact(org)
if err != nil {
return nil, http.StatusInternalServerError, errors.Wrapf(err, "error loading flow contact")
}
Expand Down

0 comments on commit f0f592f

Please sign in to comment.