-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
124 lines (109 loc) · 3.98 KB
/
index.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
const express = require('express');
const morgan = require('morgan');
const qrlogic = require('./src/logic/qr.js');
const database = require('./src/logic/queries.js');
const bodyParser = require('body-parser');
const cors = require('cors');
const mongo = require('./src/services/mongo.js')
const inputs = require('./src/models/inputs')
const t = require('tcomb-validation');
const Promise = require('bluebird')
const app = express();
const port = process.env.PORT || 3000;
const driver = 'mongodb://master:[email protected]:61012/heroku_x1kdd5jr'
app.use(morgan('tiny'));
app.use(bodyParser.json());
app.use(cors());
app.use(express.static('static'));
mongo.connect(driver, (db) => {
app.post('/picture', qrlogic.Parse);
app.get('/stuff/:id', database.getStuff);
app.post('/createSupplier', (req, res) => {
const input = req.body;
mongo.insert(db, 'supplier', input, (status) => {
res.status(200).send(status)
});
})
app.put('/scan/:id', (req, res) => {
const body = req.body
const input = {
id: parseInt(req.params.id),
location: body.location,
supplier: body.supplier,
facilityName:body.facilityName,
physicalQuality: body.physicalQuality,
chemicalContaminents: body.chemicalContaminents,
microbialSafety: body.microbialSafety,
temperatureControl: body.temperatureControl,
FSA:body.FSA
}
const result = t.validate(input, inputs.step)
if (result.isValid()) {
mongo.updatePassport(db, 'passport', input.id, input, (response) => {
res.status(response.status).send(response.message)
})
} else {
res.status(400).send(result.errors)
}
})
app.get('/scan/:id', (req, res) => {
console.log(req.params.id)
mongo.getPassport(db, 'passport', parseInt(req.params.id), (response) => {
res.status(response.status).send(response.message)
})
})
app.get('/scan/all/:id', (req, res) => {
const getPassport = (id) => {
return new Promise(function (resolve, reject) {
mongo.getPassport(db, 'passport', id, (response) => {
if (response.status === 500) {
console.log('cant get stuff from db')
reject(response.message); }
else {
resolve(response.message)
}
})
})
}
var array = []
function getPassports(id) {
return getPassport(id)
.then(item => {
array.push(item)
if (item.links.length === 0) {
console.log(item)
Promise.resolve(item)
} else {
return Promise.all(item.links.map(function (linkId) {
console.log('looking for '+linkId)
return getPassports(linkId)
}));
}
}).then((subItems) => {
console.log('------');
console.log(subItems)
return subItems
});
}
getPassports(parseInt(req.params.id))
.then((data) => {
console.log('---sending---');
res.status(200).send(array)
})
})
app.post('/createPassport', (req, res) => {
const input = req.body
console.log(input)
const result = t.validate(input, inputs.Passport)
if (result.isValid()) {
mongo.createPassport(db, 'passport', input, (response) => {
res.status(response.status).send(response.message)
})
} else {
res.status(400).send(result.errors)
}
})
});
app.listen(port, () => {
console.log(`api is listening at port : ${port}`);
});