Skip to content

Flatten custom operations in request.object in afterSave hooks. #784

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

Merged
merged 3 commits into from
Mar 3, 2016
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
5 changes: 3 additions & 2 deletions spec/OneSignalPushAdapter.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';

var OneSignalPushAdapter = require('../src/Adapters/Push/OneSignalPushAdapter');
var classifyInstallations = require('../src/Adapters/Push/PushAdapterUtils').classifyInstallations;
Expand Down Expand Up @@ -210,7 +211,7 @@ describe('OneSignalPushAdapter', () => {
expect(write).toHaveBeenCalled();

// iOS
args = write.calls.first().args;
let args = write.calls.first().args;
expect(args[0]).toEqual(JSON.stringify({
'contents': { 'en':'Example content'},
'content_available':true,
Expand All @@ -219,7 +220,7 @@ describe('OneSignalPushAdapter', () => {
'app_id':'APP ID'
}));

// Android
// Android
args = write.calls.mostRecent().args;
expect(args[0]).toEqual(JSON.stringify({
'contents': { 'en':'Example content'},
Expand Down
40 changes: 40 additions & 0 deletions spec/ParseAPI.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,46 @@ describe('miscellaneous', function() {
});
});

it('afterSave flattens custom operations', done => {
var triggerTime = 0;
// Register a mock beforeSave hook
Parse.Cloud.afterSave('GameScore', function(req, res) {
let object = req.object;
expect(object instanceof Parse.Object).toBeTruthy();
let originalObject = req.original;
if (triggerTime == 0) {
// Create
expect(object.get('yolo')).toEqual(1);
} else if (triggerTime == 1) {
// Update
expect(object.get('yolo')).toEqual(2);
// Check the originalObject
expect(originalObject.get('yolo')).toEqual(1);
} else {
res.error();
}
triggerTime++;
res.success();
});

var obj = new Parse.Object('GameScore');
obj.increment('yolo', 1);
obj.save().then(() => {
obj.increment('yolo', 1);
return obj.save();
}).then(() => {
// Make sure the checking has been triggered
expect(triggerTime).toBe(2);
// Clear mock afterSave
Parse.Cloud._removeHook("Triggers", "afterSave", "GameScore");
done();
}, error => {
console.error(error);
fail(error);
done();
});
});

it('test cloud function error handling', (done) => {
// Register a function which will fail
Parse.Cloud.define('willFail', (req, res) => {
Expand Down
23 changes: 17 additions & 6 deletions src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,22 +816,33 @@ RestWrite.prototype.runDatabaseOperation = function() {

// Returns nothing - doesn't wait for the trigger.
RestWrite.prototype.runAfterTrigger = function() {
if (!this.response || !this.response.response) {
return;
}

// Avoid doing any setup for triggers if there is no 'afterSave' trigger for this class.
if (!triggers.triggerExists(this.className, triggers.Types.afterSave, this.config.applicationId)) {
return Promise.resolve();
}

var extraData = {className: this.className};
if (this.query && this.query.objectId) {
extraData.objectId = this.query.objectId;
}

// Build the inflated object, different from beforeSave, originalData is not empty
// since developers can change data in the beforeSave.
var inflatedObject = triggers.inflate(extraData, this.originalData);
inflatedObject._finishFetch(this.data);
// Build the original object, we only do this for a update write.
var originalObject;
let originalObject;
if (this.query && this.query.objectId) {
originalObject = triggers.inflate(extraData, this.originalData);
}

triggers.maybeRunTrigger(triggers.Types.afterSave, this.auth, inflatedObject, originalObject, this.config.applicationId);
// Build the inflated object, different from beforeSave, originalData is not empty
// since developers can change data in the beforeSave.
let updatedObject = triggers.inflate(extraData, this.originalData);
updatedObject.set(Parse._decode(undefined, this.data));
updatedObject._handleSaveResponse(this.response.response, this.response.status || 200);

triggers.maybeRunTrigger(triggers.Types.afterSave, this.auth, updatedObject, originalObject, this.config.applicationId);
};

// A helper to figure out what location this operation happens at.
Expand Down