-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
131 lines (107 loc) · 4.05 KB
/
index.js
File metadata and controls
131 lines (107 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var express = require('express');
var Parse = require('parse/node');
var cloud = require('./lib/cloud');
var CloudCode = require('./lib/hooks');
var scheduler = require('./lib/scheduler');
var bodyParser = require('body-parser');
var Promise = require('bluebird');
var app = express();
app.use( bodyParser.urlencoded( { extended: false } ) );
app.use( bodyParser.json() );
app.post( '/function/:functionName', function(req, res) {
var functionName = req.params.functionName,
body = req.body,
app_id = req.get( 'x-parse-application-id' );
Parse.initialize( app_id, "zVw4LTe6k2QmD4n2L2gPdMradqoobe5QXTwsirHE" );
Parse.serverURL = 'https://api.ga.mysupplylive.com/1';
CloudCode.triggerFunction( functionName, body ).then( function( response ) {
res.send( JSON.stringify( response ) );
});
});
app.post( '/beforeSave', function(req, res) {
var type = req.body.type,
data = req.body.data,
isNew = req.body.new,
user = req.body.user,
app_id = req.get( 'x-parse-application-id' );
parseObject( isNew, type, data, app_id ).then( function( obj ) {
CloudCode.trigger( 'beforeSave', type, { object: obj, user: user } ).then( function( response ){
res.send( JSON.stringify( response ) );
return;
});
});
});
app.post( '/afterSave', function(req, res) {
var type = req.body.type,
data = req.body.data,
isNew = false,
user = req.body.user,
app_id = req.get( 'x-parse-application-id' );
parseObject( isNew, type, data, app_id ).then( function( obj ) {
if ( Object.keys(obj).length === 0 && obj.constructor === Object ) {
res.send( JSON.stringify( { 'error': 'Could not connect to api server.' } ) );
return;
}
console.log( 'trigger afterSave for ' + type + ' by user ' + user + ' with data ' + JSON.stringify( data ) );
CloudCode.trigger( 'afterSave', type, { object: obj, user: user } ).then( function( response ){
res.send( JSON.stringify( response ) );
return;
});
});
});
app.post( '/beforeDelete', function(req, res) {
var type = req.body.type,
data = req.body.data,
isNew = false,
user = req.body.user,
app_id = req.get( 'x-parse-application-id' );
parseObject( isNew, type, data, app_id ).then( function( obj ) {
CloudCode.trigger( 'beforeDelete', type, { object: obj, user: user } ).then( function( response ){
res.send( JSON.stringify( response ) );
return;
});
});
});
app.post( '/afterDelete', function(req, res) {
var type = req.body.type,
data = req.body.data,
isNew = false,
user = req.body.user,
app_id = req.get( 'x-parse-application-id' );
parseObject( isNew, type, data, app_id ).then( function( obj ) {
CloudCode.trigger( 'afterDelete', type, { object: obj, user: user } ).then( function( response ){
res.send( JSON.stringify( response ) );
return;
});
});
});
app.listen(3000, function(){
scheduler.initTasks();
});
function parseObject( isNew, type, data, app_id ) {
Parse.initialize( app_id, "zVw4LTe6k2QmD4n2L2gPdMradqoobe5QXTwsirHE" );
Parse.serverURL = 'https://api.ga.mysupplylive.com/1';
return new Promise( function( resolve, reject ) {
var object = Parse.Object.extend( type );
if ( isNew ) {
var self = new object();
for ( var key in data ) {
self.set( key, data[key] );
}
resolve( self );
} else {
var query = new Parse.Query( object );
query.get( data.objectId ).then(
function( object ) {
resolve( object );
return;
},
function( error ) {
console.log( error );
resolve( {} );
return;
}
);
}
});
}