-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPubliccontroller.js
205 lines (197 loc) · 8.96 KB
/
Publiccontroller.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
router.use(bodyParser.urlencoded({ extended: true }));
router.use(bodyParser.json());
var User = require('./User');
var Logs = require('./Logs');
var Job = require('./Job');
const Reviews = require('./Review');
const Nft = require('./Nft');
const Nft_user = require('./Nft_user');
var jwt = require('jsonwebtoken');
var config = require('./config');
const time = new Date();
//to handle publicc queries as the JABblockexplorer
const SSC = require('sscjs');
const ssc = new SSC(config.SSC_node);
const sscMain = new SSC(config.SSC_main);
///////////////////////////////////////////////////////////////////////
//////Public routes for user
router.get('/getField', function(req, res){
const query = req.headers['query'];
const jsonFields = JSON.parse(query);
if(config.testingData){ console.log('To find:', jsonFields)};
if(!jsonFields || jsonFields === ""){
if(config.testingData){console.log('Empty query from public users req');}
return res.status(404).send({ status: 'failed', message: 'I cannot query emptiness.'});
}else{
User.findOne({ username: jsonFields.username },function(err,found){
if(err){
if(config.testingData){ console.log('Error getting field from user, public request.',err)};
return res.status(500).send({ error: err });
}
if(config.testingData){ console.log('Found from public request on fields',found)};
return res.status(200).send({ status: 'sucess', result: found});
});
}
})
//////Public routes for Jobs
router.get('/getActiveJobs', function(req, res){
const username = req.headers['username'];
if(config.testingData){ console.log('To find:', username)};
if(!username || username === ""){
if(config.testingData){console.log('Empty query from public users req');}
return res.status(404).send({ status: 'failed', message: 'I cannot query emptiness. PR.'});
}else{
Job.find({ username: username, active: true },function(err,found){
if(err){
if(config.testingData){ console.log('Error getting jobs from user, public request.',err)};
return res.status(500).send({ error: err });
}
if(config.testingData){ console.log('Found from public request on Jobs',found)};
return res.status(200).send({ status: 'sucess', result: found});
});
}
})
router.get('/JobsQuery',function(req,res){
const query = JSON.parse(req.headers['query']);
const limit = Number(req.headers['limit']) || 0;
const sortby = JSON.parse(req.headers['sortby']) || { createdAt: -1 };
if(config.testingData){ console.log(`Public query on jobs ${new Date().toLocaleDateString()}`,query) };
Job.find(query, function(err, jobs){
if(err) {
console.log('Error finding jobs',err);
return res.status(500).send({ status: 'failed', message: err });
}
return res.status(200).send({ status: 'sucess', result: jobs});
}).sort(sortby).limit(limit)
});
///////////////////////////////////////////////////////////////////////
////////Public routes for blocks on hive node main
////The tx for transfers always will live under the main NET
////We may choose bewteen: ssc for test node and sscMain for main.
// on sscMain:
// - tx money transfers
// on ssc:
// - nft created, actual nft, instances.
///public route for hive contract/table queries
router.get('/publicQueryContractTable', function(req,res){
const query = req.headers['query']; //as { contract: '', table: '', query: {}, limit: 0, offset: 0, indexes: [] };
if(!query) return res.status(404).send({ status: 'failed', message: 'No query provided!' });
const pQuery = JSON.parse(query);
if(config.testingData){ console.log('About to process:', pQuery)};
sscMain.find(pQuery.contract, pQuery.table, pQuery.query, pQuery.limit, pQuery.offset, pQuery.indexes, (err, result) => {
if(err){
if(config.testingData){ console.log('Error on query:', query) };
return res.status(500).send({ status: 'failed', message: err });
}
if(config.testingData){ console.log('Results', result)};
return res.status(200).send({ status: 'sucess', result: result });
});
});
/////
router.get('/tx', function(req, res){ //loop up for money transfers.
//testing to look up a particular tx on the test SSC server
const tx = req.headers['tx'];
if(!tx){
return res.status(404).send({ status: 'failed', message: 'No Tx id was provided. Funny guy!'});
}
console.log(`Public Looking into tx:${tx}`);
sscMain.getTransactionInfo(tx, function(err, result){
// if(result === null){ return res.status(200).send({ status: 'askAgain'})}
if(err){
if(config.testingData){console.log('Error fetching from RPC API hive.',err);}
return res.status(500).send({ result: 'error', error: err});
}
// if(config.testingData){console.log(result);}
res.status(200).send(result);
});
});
////////END Public routes for blocks on hive node
///////////Public routes for NFTs///////////
//get all token based on query, handling the query on headers.
router.get('/getNFTquery', function(req,res){
const query = req.headers['query'];
const limit = Number(req.headers['limit']);
const sortby = JSON.parse(req.headers['sortby']);
const jsonQuery = JSON.parse(query);
if(!jsonQuery) {
console.log('A null || public empty query has been made!');
return res.status(404).send({ status: 'funny', message: "I cannot process that!"});
}
//TODO process the query check for nulls || "" and create teh newQuery.
const newNode = {};
Object.entries(jsonQuery).forEach(([key, val]) => {
if(val !== null && val !== ""){
return (newNode[key] = val);
}
});
console.log('New public query to process on NFTs');
console.log(newNode, `Limit:${limit}`);
console.log('Sortby:',sortby);
Nft.find(newNode,function(err,tokens){
if(err){
if(config.testingData){
console.log('Error finding Nft',err);
}
return res.status(500).send({ error: 'Error searching for Nft', message: err});
}
return res.status(200).send({ status: 'sucess', result: tokens });
}).limit(limit).sort(sortby.hasOwnProperty("null") ? null : sortby);
});
///get all instances on mongoDB based on query
router.get('/getNFTInstancesQuery', function(req,res){
const query = req.headers['query'];
const limit = Number(req.headers['limit']);
const sortby = JSON.parse(req.headers['sortby']);
const jsonQuery = JSON.parse(query);
if(!jsonQuery) {
console.log('A null || empty public query has been made!');
return res.status(404).send({ status: 'funny', message: "I cannot process that!"});
}
//TODO process the query check for nulls || "" and create teh newQuery.
const newNode = {};
Object.entries(jsonQuery).forEach(([key, val]) => {
if(val !== null && val !== ""){
return (newNode[key] = val);
}
});
console.log('New public query to process::::');
console.log(newNode, `Limit:${limit}`);
console.log('Sortby:',sortby);
Nft_user.find(newNode,function(err,tokens){
if(err){
if(config.testingData){
console.log('Error finding Nft on public query',err);
}
return res.status(500).send({ error: 'Error searching for Nft', message: err});
}
return res.status(200).send({ status: 'sucess', result: tokens });
}).limit(limit).sort(sortby.hasOwnProperty("null") ? null : sortby);
});
////////////////////////////////////////////
/////////Public routes for Reviews//////////
router.get('/queryReview', function(req,res){ //for now as public
const query = req.headers['query'];
const limit = Number(req.headers['limit']);
const sortby = JSON.parse(req.headers['sortby']);
const jsonQuery = JSON.parse(query);
if(!jsonQuery) {
console.log('A null || empty public query has been made!');
return res.status(404).send({ status: 'funny', message: "I cannot process that!"});
}
//TODO process the query check for nulls || "" and create teh newQuery.
const newNode = {};
Object.entries(jsonQuery).forEach(([key, val]) => { if(val !== null && val !== ""){ return (newNode[key] = val) } });
console.log('New public query to process::::', { newNode, limit, sortby});
Reviews.find(newNode,function(err, reviews){
if(err){
if(config.testingData){ console.log('Error finding Review on public query',err) };
return res.status(500).send({ status: 'failed', message: err});
}
return res.status(200).send({ status: 'sucess', result: reviews });
}).limit(limit).sort(sortby.hasOwnProperty("null") ? null : sortby);
});
/////////END Public routes for Reviews//////////
module.exports = router;