Skip to content

Commit 422723f

Browse files
davimacedoflovilmart
authored andcommitted
Fix beforeQuery trigger when getting objects through GET API (#3862)
1 parent a0d1a35 commit 422723f

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

spec/CloudCode.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,7 @@ describe('beforeFind hooks', () => {
12111211
expect(jsonQuery.include).toEqual('otherKey,otherValue');
12121212
expect(jsonQuery.limit).toEqual(100);
12131213
expect(jsonQuery.skip).toBe(undefined);
1214+
expect(req.isGet).toEqual(false);
12141215
});
12151216

12161217
const query = new Parse.Query('MyObject');
@@ -1305,6 +1306,34 @@ describe('beforeFind hooks', () => {
13051306
done();
13061307
});
13071308
});
1309+
1310+
it('should add beforeFind trigger using get API',(done) => {
1311+
const hook = {
1312+
method: function(req) {
1313+
expect(req.isGet).toEqual(true);
1314+
return Promise.resolve();
1315+
}
1316+
};
1317+
spyOn(hook, 'method').and.callThrough();
1318+
Parse.Cloud.beforeFind('MyObject', hook.method);
1319+
const obj = new Parse.Object('MyObject');
1320+
obj.set('secretField', 'SSID');
1321+
obj.save().then(function() {
1322+
rp({
1323+
method: 'GET',
1324+
uri: 'http://localhost:8378/1/classes/MyObject/' + obj.id,
1325+
headers: {
1326+
'X-Parse-Application-Id': 'test',
1327+
'X-Parse-REST-API-Key': 'rest'
1328+
},
1329+
json: true,
1330+
}).then(body => {
1331+
expect(body.secretField).toEqual('SSID');
1332+
expect(hook.method).toHaveBeenCalled();
1333+
done();
1334+
});
1335+
});
1336+
});
13081337
});
13091338

13101339
describe('afterFind hooks', () => {

src/rest.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,14 @@ function find(config, auth, className, restWhere, restOptions, clientSDK) {
3737

3838
// get is just like find but only queries an objectId.
3939
const get = (config, auth, className, objectId, restOptions, clientSDK) => {
40+
var restWhere = { objectId };
4041
enforceRoleSecurity('get', className, auth);
41-
const query = new RestQuery(config, auth, className, { objectId }, restOptions, clientSDK);
42-
return query.execute();
42+
return triggers.maybeRunQueryTrigger(triggers.Types.beforeFind, className, restWhere, restOptions, config, auth, true).then((result) => {
43+
restWhere = result.restWhere || restWhere;
44+
restOptions = result.restOptions || restOptions;
45+
const query = new RestQuery(config, auth, className, restWhere, restOptions, clientSDK);
46+
return query.execute();
47+
});
4348
}
4449

4550
// Returns a promise that doesn't resolve to any useful value.

src/triggers.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,16 @@ export function getRequestObject(triggerType, auth, parseObject, originalParseOb
155155
return request;
156156
}
157157

158-
export function getRequestQueryObject(triggerType, auth, query, count, config) {
158+
export function getRequestQueryObject(triggerType, auth, query, count, config, isGet) {
159+
isGet = !!isGet;
160+
159161
var request = {
160162
triggerName: triggerType,
161163
query,
162164
master: false,
163165
count,
164-
log: config.loggerController
166+
log: config.loggerController,
167+
isGet
165168
};
166169

167170
if (!auth) {
@@ -286,7 +289,7 @@ export function maybeRunAfterFindTrigger(triggerType, auth, className, objects,
286289
});
287290
}
288291

289-
export function maybeRunQueryTrigger(triggerType, className, restWhere, restOptions, config, auth) {
292+
export function maybeRunQueryTrigger(triggerType, className, restWhere, restOptions, config, auth, isGet) {
290293
const trigger = getTrigger(className, triggerType, config.applicationId);
291294
if (!trigger) {
292295
return Promise.resolve({
@@ -312,7 +315,7 @@ export function maybeRunQueryTrigger(triggerType, className, restWhere, restOpti
312315
}
313316
count = !!restOptions.count;
314317
}
315-
const requestObject = getRequestQueryObject(triggerType, auth, parseQuery, count, config);
318+
const requestObject = getRequestQueryObject(triggerType, auth, parseQuery, count, config, isGet);
316319
return Promise.resolve().then(() => {
317320
return trigger(requestObject);
318321
}).then((result) => {

0 commit comments

Comments
 (0)