Skip to content

Commit b89619b

Browse files
committed
Full query support for badge Increment
1 parent 7d78732 commit b89619b

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

spec/PushController.spec.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"use strict";
12
var PushController = require('../src/Controllers/PushController').PushController;
23

34
var Config = require('../src/Config');
@@ -220,5 +221,45 @@ describe('PushController', () => {
220221
});
221222

222223
});
224+
225+
it('should support full RESTQuery for increment', (done) => {
226+
var payload = {data: {
227+
alert: "Hello World!",
228+
badge: 'Increment',
229+
}}
230+
231+
var pushAdapter = {
232+
send: function(body, installations) {
233+
return Promise.resolve();
234+
},
235+
getValidPushTypes: function() {
236+
return ["ios"];
237+
}
238+
}
239+
240+
var config = new Config(Parse.applicationId);
241+
var auth = {
242+
isMaster: true
243+
}
244+
245+
let where = {
246+
'deviceToken': {
247+
'$inQuery': {
248+
'where': {
249+
'deviceType': 'ios'
250+
},
251+
className: '_Installation'
252+
}
253+
}
254+
}
255+
256+
var pushController = new PushController(pushAdapter, Parse.applicationId);
257+
pushController.sendPush(payload, where, config, auth).then((result) => {
258+
done();
259+
}).catch((err) => {
260+
fail('should not fail');
261+
done();
262+
});
263+
});
223264

224265
});

src/Controllers/PushController.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import AdaptableController from './AdaptableController';
55
import { PushAdapter } from '../Adapters/Push/PushAdapter';
66
import deepcopy from 'deepcopy';
77
import features from '../features';
8+
import RestQuery from '../RestQuery';
89

910
const FEATURE_NAME = 'push';
1011
const UNSUPPORTED_BADGE_KEY = "unsupported";
@@ -63,11 +64,21 @@ export class PushController extends AdaptableController {
6364
throw "Invalid value for badge, expected number or 'Increment'";
6465
}
6566
let updateWhere = deepcopy(where);
66-
updateWhere.deviceType = 'ios'; // Only on iOS!
6767

6868
badgeUpdate = () => {
69-
return config.database.adaptiveCollection("_Installation")
70-
.then(coll => coll.updateMany(updateWhere, op));
69+
let badgeQuery = new RestQuery(config, auth, '_Installation', updateWhere);
70+
return badgeQuery.buildRestWhere().then(() => {
71+
let restWhere = deepcopy(badgeQuery.restWhere);
72+
// Force iOS only devices
73+
if (!restWhere['$and']) {
74+
restWhere['$and'] = [badgeQuery.restWhere];
75+
}
76+
restWhere['$and'].push({
77+
'deviceType': 'ios'
78+
});
79+
return config.database.adaptiveCollection("_Installation")
80+
.then(coll => coll.updateMany(restWhere, op));
81+
})
7182
}
7283
}
7384

src/RestQuery.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ function RestQuery(config, auth, className, restWhere = {}, restOptions = {}) {
111111
// 'results' and 'count'.
112112
// TODO: consolidate the replaceX functions
113113
RestQuery.prototype.execute = function() {
114+
return Promise.resolve().then(() => {
115+
return this.buildRestWhere();
116+
}).then(() => {
117+
return this.runFind();
118+
}).then(() => {
119+
return this.runCount();
120+
}).then(() => {
121+
return this.handleInclude();
122+
}).then(() => {
123+
return this.response;
124+
});
125+
};
126+
127+
RestQuery.prototype.buildRestWhere = function() {
114128
return Promise.resolve().then(() => {
115129
return this.getUserAndRoleACL();
116130
}).then(() => {
@@ -125,16 +139,8 @@ RestQuery.prototype.execute = function() {
125139
return this.replaceInQuery();
126140
}).then(() => {
127141
return this.replaceNotInQuery();
128-
}).then(() => {
129-
return this.runFind();
130-
}).then(() => {
131-
return this.runCount();
132-
}).then(() => {
133-
return this.handleInclude();
134-
}).then(() => {
135-
return this.response;
136142
});
137-
};
143+
}
138144

139145
// Uses the Auth object to get the list of roles, adds the user id
140146
RestQuery.prototype.getUserAndRoleACL = function() {

0 commit comments

Comments
 (0)