Skip to content
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

🚀 Remove redundant updates to modified_on #306

Merged
merged 5 commits into from
Jul 1, 2020
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
13 changes: 0 additions & 13 deletions hooks/contact_groups_changed.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/gomodule/redigo/redis"
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/mailroom/models"
Expand Down Expand Up @@ -68,18 +67,6 @@ func (h *CommitGroupChangesHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *red
return errors.Wrapf(err, "error removing contacts from groups")
}

// build the list of all contact ids changed, we'll update modified_on for them
changedIDs := make([]models.ContactID, 0, len(changed))
for c := range changed {
changedIDs = append(changedIDs, c)
}
if len(changedIDs) > 0 {
_, err = tx.ExecContext(ctx, `UPDATE contacts_contact SET modified_on = NOW() WHERE id = ANY($1)`, pq.Array(changedIDs))
if err != nil {
return errors.Wrapf(err, "error updating contacts modified_on")
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done via the contactModifiedHook


return nil
}

Expand Down
11 changes: 0 additions & 11 deletions web/contact/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,17 +338,12 @@ func handleModify(ctx context.Context, s *web.Server, r *http.Request) (interfac
return nil, http.StatusInternalServerError, errors.Wrapf(err, "error starting transaction")
}

modifiedContactIDs := make([]models.ContactID, 0, len(contacts))

// apply our events
for _, scene := range scenes {
err := models.HandleEvents(ctx, tx, s.RP, org, scene, results[scene.ContactID()].Events)
if err != nil {
return nil, http.StatusInternalServerError, errors.Wrapf(err, "error applying events")
}
if len(results[scene.ContactID()].Events) > 0 {
modifiedContactIDs = append(modifiedContactIDs, scene.ContactID())
}
}

// gather all our pre commit events, group them by hook and apply them
Expand All @@ -357,12 +352,6 @@ func handleModify(ctx context.Context, s *web.Server, r *http.Request) (interfac
return nil, http.StatusInternalServerError, errors.Wrapf(err, "error applying pre commit hooks")
}

// apply modified_by
err = models.UpdateContactModifiedBy(ctx, tx, modifiedContactIDs, request.UserID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can remove modifiedContactIDs too above it is not used anymore

if err != nil {
return nil, http.StatusInternalServerError, errors.Wrapf(err, "error applying modified_by")
}

// commit our transaction
err = tx.Commit()
if err != nil {
Expand Down
90 changes: 90 additions & 0 deletions web/contact/testdata/modify.json
Original file line number Diff line number Diff line change
Expand Up @@ -1125,5 +1125,95 @@
"count": 0
}
]
},
{
"label": "multiple modifiers of different types",
"method": "POST",
"path": "/mr/contact/modify",
"body": {
"org_id": 1,
"contact_ids": [
10000
],
"modifiers": [
{
"type": "name",
"name": "Juan"
},
{
"type": "language",
"language": "spa"
},
{
"type": "groups",
"modification": "add",
"groups": [
{
"name": "Testers",
"uuid": "5e9d8fab-5e7e-4f51-b533-261af5dea70d"
}
]
},
{
"type": "urns",
"modification": "set",
"urns": [
"tel:+255788555111"
]
}
]
},
"status": 200,
"response": {
"10000": {
"contact": {
"uuid": "6393abc0-283d-4c9b-a1b3-641a035c34bf",
"id": 10000,
"name": "Juan",
"language": "spa",
"status": "active",
"timezone": "America/Los_Angeles",
"created_on": "2018-07-06T12:30:00.123457Z",
"urns": [
"tel:+255788555111"
],
"groups": [
{
"uuid": "5e9d8fab-5e7e-4f51-b533-261af5dea70d",
"name": "Testers"
}
]
},
"events": [
{
"type": "contact_name_changed",
"created_on": "2018-07-06T12:30:00.123456789Z",
"name": "Juan"
},
{
"type": "contact_language_changed",
"created_on": "2018-07-06T12:30:01.123456789Z",
"language": "spa"
},
{
"type": "contact_groups_changed",
"created_on": "2018-07-06T12:30:02.123456789Z",
"groups_added": [
{
"uuid": "5e9d8fab-5e7e-4f51-b533-261af5dea70d",
"name": "Testers"
}
]
},
{
"type": "contact_urns_changed",
"created_on": "2018-07-06T12:30:03.123456789Z",
"urns": [
"tel:+255788555111"
]
}
]
}
}
}
]