Skip to content

Commit

Permalink
Add the fields that triggered update notifications (#1364)
Browse files Browse the repository at this point in the history
## What does this PR do ?

See the PR title

https://jira.kaliop.net/browse/KZL-1127

### How should this be manually tested?

Execute this script and see the notification:

```
const {
    Kuzzle,
    WebSocket
} = require('kuzzle-sdk');

const kuzzle = new Kuzzle(
    new WebSocket('localhost')
);

function callback (notification) {
    console.log(notification);
}

kuzzle.on('networkError', error => {
    console.error('Network Error:', error);
});

const run = async () => {
    try {
        await kuzzle.connect();
        await kuzzle.document.createOrReplace(
            'nyc-open-data',
            'yellow-taxi',
            'some-id',
            { capacity: 4, members: 5 },
        );
        const filters = { exists: 'capacity' };
        await kuzzle.realtime.subscribe('nyc-open-data', 'yellow-taxi', filters, callback);
        const response = await kuzzle.document.update(
            'nyc-open-data',
            'yellow-taxi',
            'some-id',
            { capacity: 100, members: 50 }
        );
        console.log(response);
    } catch (error) {
        console.error(error.message);
    } finally {
        kuzzle.disconnect();
    }
};
run();
```
  • Loading branch information
Yoann-Abbes authored and scottinet committed Aug 7, 2019
1 parent f78aa09 commit 3a69c80
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions features/kuzzle.feature
Expand Up @@ -326,6 +326,7 @@ Feature: Kuzzle functional tests
When I write the document "documentAda"
Then I update the document with value "Hopper" in field "lastName"
Then I should receive a document notification with field action equal to "update"
And The notification should have "_updatedFields" array with 1 element
And The notification should have a "_source" member
And The notification should have volatile

Expand Down
11 changes: 11 additions & 0 deletions features/step_definitions/notifications.js
Expand Up @@ -37,6 +37,17 @@ Then(/^I should receive a ?(.*?) notification with field ?(.*?) equal to "([^"]*
});
});


Then(/^The notification should have "([^"]*)" array with ([\d]*) element/, function (member, n_elements, callback) {
if (this.api.responses.result[member].length === parseInt(n_elements)) {
callback();
} else {
console.log('Wrong notification received: ');
console.dir(this.api.responses, { colors: true, depth: null });
callback('The document was supposed to contain the member "' + member + '" with ' + n_elements + ' elements : Has '+ this.api.responses.result[member].length +'.');
}
});

Then(/^The notification should ?(not)* have a "([^"]*)" member/, function (not, member, callback) {
if ((this.api.responses.result[member] || not) && !(this.api.responses.result[member] && not)) {
callback();
Expand Down
7 changes: 6 additions & 1 deletion lib/api/core/notifier.js
Expand Up @@ -340,11 +340,16 @@ class NotifierController {
result._source,
result._id);


const updatedFields = Object.keys(request.input.body)
.filter(_updatedFields => _updatedFields !== '_kuzzle_info');

return this.notifyDocument(
matchedRooms, request, 'in', 'done', 'update', {
_meta: updatedDocument._meta || {},
_id: updatedDocument._id,
_source: updatedDocument._source
_source: updatedDocument._source,
_updatedFields: updatedFields
});
})
.then(() => this.cache.get(cachePrefix + updatedDocument._id))
Expand Down
3 changes: 2 additions & 1 deletion test/api/core/notifier/notifyDocumentUpdate.test.js
Expand Up @@ -65,7 +65,8 @@ describe('Test: notifier.notifyDocumentUpdate', () => {
{
_id,
_meta: {canIhas: 'cheezburgers?'},
_source: {foo: 'bar'}
_source: {foo: 'bar'},
_updatedFields: ['foo']
});

should(notifier.notifyDocument.getCall(1)).calledWith(
Expand Down

0 comments on commit 3a69c80

Please sign in to comment.