Skip to content

Commit

Permalink
fix: Cloud Code Trigger afterSave executes even if not set (#8520)
Browse files Browse the repository at this point in the history
  • Loading branch information
dplewis authored May 12, 2023
1 parent 4187a97 commit afd0515
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
22 changes: 22 additions & 0 deletions spec/ParseLiveQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const Auth = require('../lib/Auth');
const UserController = require('../lib/Controllers/UserController').UserController;
const Config = require('../lib/Config');
const triggers = require('../lib/triggers');
const validatorFail = () => {
throw 'you are not authorized';
};
Expand Down Expand Up @@ -1212,4 +1213,25 @@ describe('ParseLiveQuery', function () {
object.set({ location: secondPoint });
await object.save();
});

it('prevent afterSave trigger if not exists', async () => {
await reconfigureServer({
liveQuery: {
classNames: ['TestObject'],
},
startLiveQueryServer: true,
verbose: false,
silent: true,
});
spyOn(triggers, 'maybeRunTrigger').and.callThrough();
const object1 = new TestObject();
const object2 = new TestObject();
const object3 = new TestObject();
await Parse.Object.saveAll([object1, object2, object3]);

expect(triggers.maybeRunTrigger).toHaveBeenCalledTimes(0);
expect(object1.id).toBeDefined();
expect(object2.id).toBeDefined();
expect(object3.id).toBeDefined();
});
});
26 changes: 15 additions & 11 deletions src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -1577,17 +1577,21 @@ RestWrite.prototype.runAfterSaveTrigger = function () {
const { originalObject, updatedObject } = this.buildParseObjects();
updatedObject._handleSaveResponse(this.response.response, this.response.status || 200);

this.config.database.loadSchema().then(schemaController => {
// Notifiy LiveQueryServer if possible
const perms = schemaController.getClassLevelPermissions(updatedObject.className);
this.config.liveQueryController.onAfterSave(
updatedObject.className,
updatedObject,
originalObject,
perms
);
});

if (hasLiveQuery) {
this.config.database.loadSchema().then(schemaController => {
// Notify LiveQueryServer if possible
const perms = schemaController.getClassLevelPermissions(updatedObject.className);
this.config.liveQueryController.onAfterSave(
updatedObject.className,
updatedObject,
originalObject,
perms
);
});
}
if (!hasAfterSaveHook) {
return Promise.resolve();
}
// Run afterSave trigger
return triggers
.maybeRunTrigger(
Expand Down

0 comments on commit afd0515

Please sign in to comment.