Skip to content

Commit f23f3f1

Browse files
committed
Restores jobs endpoints for creation, update and deletion
1 parent dee35d2 commit f23f3f1

File tree

3 files changed

+59
-21
lines changed

3 files changed

+59
-21
lines changed

src/Controllers/SchemaController.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ const defaultColumns = Object.freeze({
9898
_JobSchedule: {
9999
"jobName": {type:'String'},
100100
"description": {type:'String'},
101-
"params": {type:'Object'},
102-
"startAfter": {type:'Date'},
101+
"params": {type:'String'},
102+
"startAfter": {type:'String'},
103103
"daysOfWeek": {type:'Array'},
104104
"timeOfDay": {type:'String'},
105105
"lastRun": {type:'Number'},
106-
"repeatMinutes":{type:'String'}
106+
"repeatMinutes":{type:'Number'}
107107
},
108108
_Hooks: {
109109
"functionName": {type:'String'},

src/Routers/CloudCodeRouter.js

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,65 @@
1-
import PromiseRouter from '../PromiseRouter';
2-
const triggers = require('../triggers');
1+
import PromiseRouter from '../PromiseRouter';
2+
import rest from '../rest';
3+
const triggers = require('../triggers');
4+
const middleware = require('../middlewares');
35

46
export class CloudCodeRouter extends PromiseRouter {
57
mountRoutes() {
6-
this.route('GET',`/cloud_code/jobs`, CloudCodeRouter.getJobs);
8+
this.route('GET', '/cloud_code/jobs', middleware.promiseEnforceMasterKeyAccess, CloudCodeRouter.getJobs);
9+
this.route('GET', '/cloud_code/jobs/data', middleware.promiseEnforceMasterKeyAccess, CloudCodeRouter.getJobsData);
10+
this.route('POST', '/cloud_code/jobs', middleware.promiseEnforceMasterKeyAccess, CloudCodeRouter.createJob);
11+
this.route('PUT', '/cloud_code/jobs/:objectId', middleware.promiseEnforceMasterKeyAccess, CloudCodeRouter.editJob);
12+
this.route('DELETE', '/cloud_code/jobs/:objectId', middleware.promiseEnforceMasterKeyAccess, CloudCodeRouter.deleteJob);
713
}
814

915
static getJobs(req) {
16+
return rest.find(req.config, req.auth, '_JobSchedule', {}, {}).then((scheduledJobs) => {
17+
return {
18+
response: scheduledJobs.results
19+
}
20+
});
21+
}
22+
23+
static getJobsData(req) {
1024
const config = req.config;
1125
const jobs = triggers.getJobs(config.applicationId) || {};
12-
return Promise.resolve({
13-
response: Object.keys(jobs).map((jobName) => {
14-
return {
15-
jobName,
26+
return rest.find(req.config, req.auth, '_JobSchedule', {}, {}).then((scheduledJobs) => {
27+
return {
28+
response: {
29+
in_use: scheduledJobs.results.map((job) => job.jobName),
30+
jobs: Object.keys(jobs),
1631
}
17-
})
32+
};
33+
});
34+
}
35+
36+
static createJob(req) {
37+
const { job_schedule } = req.body;
38+
if (typeof job_schedule.startAfter === 'undefined') {
39+
job_schedule.startAfter = new Date().toISOString();
40+
}
41+
return rest.create(req.config, req.auth, '_JobSchedule', job_schedule, req.client);
42+
}
43+
44+
static editJob(req) {
45+
const { objectId } = req.params;
46+
const { job_schedule } = req.body;
47+
if (typeof job_schedule.startAfter === 'undefined') {
48+
job_schedule.startAfter = new Date().toISOString();
49+
}
50+
return rest.update(req.config, req.auth, '_JobSchedule', { objectId }, job_schedule).then((response) => {
51+
return {
52+
response
53+
}
54+
});
55+
}
56+
57+
static deleteJob(req) {
58+
const { objectId } = req.params;
59+
return rest.del(req.config, req.auth, '_JobSchedule', objectId).then((response) => {
60+
return {
61+
response
62+
}
1863
});
1964
}
2065
}

src/rest.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,9 @@ function update(config, auth, className, restWhere, restObject, clientSDK) {
134134
});
135135
}
136136

137-
const classesWithMasterOnlyAccess = ['_PushStatus', '_Hooks', '_GlobalConfig', '_JobStatus', '_JobSchedule'];
138-
// Disallowing access to the _Role collection except by master key
137+
const classesWithMasterOnlyAccess = ['_JobStatus', '_PushStatus', '_Hooks', '_GlobalConfig', '_JobSchedule'];
138+
// Disallowing access to the _Role collection except by master key
139139
function enforceRoleSecurity(method, className, auth) {
140-
if (classesWithMasterOnlyAccess.indexOf(className) > -1 && !auth.isMaster) {
141-
const error = new Error();
142-
error.status = 403;
143-
error.message = "unauthorized: master key is required";
144-
throw error;
145-
}
146140
if (className === '_Installation' && !auth.isMaster) {
147141
if (method === 'delete' || method === 'find') {
148142
const error = `Clients aren't allowed to perform the ${method} operation on the installation collection.`
@@ -151,8 +145,7 @@ function enforceRoleSecurity(method, className, auth) {
151145
}
152146

153147
//all volatileClasses are masterKey only
154-
const volatileClasses = ['_JobStatus', '_PushStatus', '_Hooks', '_GlobalConfig'];
155-
if(volatileClasses.includes(className) && !auth.isMaster){
148+
if(classesWithMasterOnlyAccess.includes(className) && !auth.isMaster){
156149
const error = `Clients aren't allowed to perform the ${method} operation on the ${className} collection.`
157150
throw new Parse.Error(Parse.Error.OPERATION_FORBIDDEN, error);
158151
}

0 commit comments

Comments
 (0)