From afd0515e207bd947840579d3f245980dffa6f804 Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Thu, 11 May 2023 19:39:54 -0500 Subject: [PATCH] fix: Cloud Code Trigger `afterSave` executes even if not set (#8520) --- spec/ParseLiveQuery.spec.js | 22 ++++++++++++++++++++++ src/RestWrite.js | 26 +++++++++++++++----------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/spec/ParseLiveQuery.spec.js b/spec/ParseLiveQuery.spec.js index 38259f50d0..959df18cf9 100644 --- a/spec/ParseLiveQuery.spec.js +++ b/spec/ParseLiveQuery.spec.js @@ -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'; }; @@ -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(); + }); }); diff --git a/src/RestWrite.js b/src/RestWrite.js index 3a8385e52a..c703ee50bb 100644 --- a/src/RestWrite.js +++ b/src/RestWrite.js @@ -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(