|
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'); |
3 | 5 |
|
4 | 6 | export class CloudCodeRouter extends PromiseRouter {
|
5 | 7 | 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); |
7 | 13 | }
|
8 | 14 |
|
9 | 15 | 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) { |
10 | 24 | const config = req.config;
|
11 | 25 | 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), |
16 | 31 | }
|
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 | + } |
18 | 63 | });
|
19 | 64 | }
|
20 | 65 | }
|
0 commit comments