Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.62 KB | None | 0 0
  1.  
  2. app.use(cors());
  3. app.use(bodyParser.json({limit:'4096mb'}));
  4.  
  5. mongoose.connect('mongodb://localhost:27017/Taxonomy', { useNewUrlParser: true, useUnifiedTopology: true } );
  6.  
  7. const connection = mongoose.connection;
  8.  
  9. connection.once('open', () => {
  10. console.log('MongoDB connection established.');
  11. });
  12.  
  13. router.route('/login').post((req, res) => {
  14. let userLogin = false;
  15. let email = req.body.email;
  16. let password = req.body.password;
  17. let userResponseObj;
  18. User.find((err, users) => {
  19. users.forEach(user => {
  20. if (user.email == email && user.password == password) {
  21. userResponseObj = {
  22. email: user.email,
  23. company: user.company
  24. };
  25. userLogin = true;
  26. }
  27. });
  28. (!!userLogin) ? res.status(200).json(userResponseObj) : res.status(403).json('wrong password / username');
  29. });
  30. });
  31.  
  32. router.route('/users').get((req, res) => {
  33. User.find((err, users) => {
  34. (err) ? console.log(err) : res.json(users);
  35. });
  36. });
  37.  
  38. router.route('/users/:email').get((req, res) => {
  39. let paramenterEmail = req.params.email;
  40. User.find((err, users) => {
  41. if (err) {
  42. res.status(400).send('Fetch failed.');
  43. } else {
  44. users.forEach(user => {
  45. if (user.email == paramenterEmail) {
  46. res.json(user);
  47. return;
  48. }
  49. })
  50. }
  51. });
  52. });
  53.  
  54. router.route('/users/update').post((req, res) => {
  55. User.deleteMany({}, (err) => {
  56. if (err) {
  57. res.status(400).send('Fetch failed.');
  58. } else {
  59. let users = req.body;
  60. users.forEach(user => {
  61. let newUser = new User(user);
  62. newUser.save();
  63. });
  64. res.status(200).send('Update successful.');
  65. }
  66. });
  67. });
  68.  
  69. router.route('/mandants').get((req, res) => {
  70. Mandant.find((err, mandants) => {
  71. (err) ? console.log(err) : res.json(mandants);
  72. });
  73. });
  74.  
  75. router.route('/mandants/:id').get((req, res) => {
  76. Mandant.findOne({'id': req.params.id}, (err, mandant) => {
  77. if (err) {
  78. res.status(400).send('Fetch failed.');
  79. } else {
  80. (!mandant) ? res.status(200).json({}) : res.status(200).json(mandant);
  81. }
  82. })
  83. });
  84.  
  85. router.route('/addmandant').post((req, res) => {
  86. Mandant.findOne({'id': req.body.id}, (err, mandant) => {
  87. if (err) {
  88. res.status(400).send('Fetch failed.');
  89. } else {
  90. if (!!mandant) {
  91. res.status(403).json('mandant already exists');
  92. } else {
  93. let mandant = new Mandant({
  94. name: req.body.name,
  95. tools: req.body.tools,
  96. vendors: req.body.vendors,
  97. id: req.body.id,
  98. thresHoldLicenceUrgent: req.body.thresHoldLicenceUrgent,
  99. thresHoldLicenceMedium: req.body.thresHoldLicenceMedium
  100. });
  101. mandant.save(err => {
  102. (err) ? console.log(err) : res.status(200).json('Mandant saved successfully.');
  103. });
  104. }
  105. }
  106. })
  107.  
  108. });
  109.  
  110. router.route('/mandants/update/:id').post((req, res) => {
  111. Mandant.findById({_id: req.params.id}, (err, mandant) => {
  112. if (!mandant) {
  113. res.status(400).send('Update failed.');
  114. } else {
  115. mandant.name = req.body.name;
  116. mandant.tools = req.body.tools;
  117. mandant.vendors = req.body.vendors;
  118. mandant.thresHoldLicenceUrgent = req.body.thresHoldLicenceUrgent;
  119. mandant.thresHoldLicenceMedium = req.body.thresHoldLicenceMedium;
  120. mandant.save()
  121. .then(() => {
  122. res.json('Update done.');
  123. })
  124. .catch((err) => {
  125. res.status(400).send('Update failed.');
  126. });
  127. }
  128. })
  129. });
  130.  
  131. router.route('/mandants/updateprocess/:id').post((req, res) => {
  132. Mandant.findById({_id: req.params.id}, (err, mandant) => {
  133. if (!mandant) {
  134. res.status(400).send('Update failed.');
  135. } else {
  136. mandant.process = req.body.process;
  137. mandant.save()
  138. .then(() => {
  139. res.json('Update done.');
  140. })
  141. .catch(() => {
  142. res.status(400).send('Update failed.');
  143. });
  144. }
  145. })
  146. });
  147.  
  148. router.route('/mandants/delete/:id').get((req, res) => {
  149. Mandant.findByIdAndRemove({_id: req.params.id}, (err, mandant) => {
  150. (err) ? res.json(err) : res.json('Remove successfully.');
  151. });
  152. });
  153.  
  154. router.route('/tools').get((req, res) => {
  155. Product.find((err, tools) => {
  156. (err) ? console.log(err) : res.json(tools);
  157. });
  158. });
  159.  
  160. router.route('/vendors').get((req, res) => {
  161. Vendor.find((err, vendors) => {
  162. (err) ? console.log(err) : res.json(vendors);
  163. });
  164. });
  165.  
  166. router.route('/category').get((req, res) => {
  167. Category.find((err, category) => {
  168. (err) ? console.log(err) : res.json(category);
  169. });
  170. });
  171.  
  172. router.route('/taxonomy').get((req, res) => {
  173. Taxonomy.find((err, taxonomy) => {
  174. (err) ? console.log(err) : res.json(taxonomy);
  175. });
  176. });
  177.  
  178. const fetchDCSOData = () => {
  179. fetchDCSOVendors();
  180. fetchDCSOProducts();
  181. fetchDCSOTaxonomy();
  182. fetchDCSOCategories();
  183. }
  184.  
  185. const fetchDCSOVendors = () => {
  186. requestify.request(dcsoApiCfg.url + '/vendors', {
  187. method: 'GET',
  188. headers: { 'x-api-key': dcsoApiCfg.key },
  189. dataType: 'json'
  190. })
  191. .then(res => {
  192. Vendor.deleteMany({}, (err) => {
  193. if (err) {
  194. console.log(err);
  195. } else {
  196. let vendors = JSON.parse(res.body);
  197. vendors.forEach(vendor => {
  198. let newVendor = new Vendor(vendor);
  199. newVendor.save();
  200. });
  201. }
  202. });
  203. })
  204. .fail(res => {
  205. console.log(res);
  206. });
  207. }
  208.  
  209. const fetchDCSOProducts = () => {
  210. requestify.request(dcsoApiCfg.url + '/products', {
  211. method: 'GET',
  212. headers: { 'x-api-key': dcsoApiCfg.key },
  213. dataType: 'json'
  214. })
  215. .then(res => {
  216. Product.deleteMany({}, (err) => {
  217. if (err) {
  218. console.log(err);
  219. } else {
  220. let products = JSON.parse(res.body);
  221. products.forEach(product => {
  222. fetchDCSOProductsById(product.id);
  223. });
  224. }
  225. });
  226. })
  227. .fail(res => {
  228. console.log(res);
  229. });
  230. }
  231.  
  232. const fetchDCSOProductsById = (id) => {
  233. requestify.request(dcsoApiCfg.url + '/products/' + id, {
  234. method: 'GET',
  235. headers: { 'x-api-key': dcsoApiCfg.key },
  236. dataType: 'json'
  237. })
  238. .then(res => {
  239. let product = JSON.parse(res.body);
  240. let newProduct = new Product(product);
  241. newProduct.save();
  242. })
  243. .fail(res => {
  244. console.log(res);
  245. });
  246. }
  247.  
  248. const fetchDCSOTaxonomy = () => {
  249. requestify.request(dcsoApiCfg.url + '/taxonomy', {
  250. method: 'GET',
  251. headers: { 'x-api-key': dcsoApiCfg.key },
  252. dataType: 'json'
  253. })
  254. .then(res => {
  255. Taxonomy.deleteMany({}, (err) => {
  256. if (err) {
  257. console.log(err);
  258. } else {
  259. let taxonomy = JSON.parse(res.body);
  260. taxonomy.forEach(taxonomyEntry => {
  261. let newTaxonomy = new Taxonomy(taxonomyEntry);
  262. newTaxonomy.save();
  263. });
  264. }
  265. });
  266. })
  267. .fail(res => {
  268. console.log(res);
  269. });
  270. }
  271.  
  272. const fetchDCSOCategories = () => {
  273. requestify.request(dcsoApiCfg.url + '/categories', {
  274. method: 'GET',
  275. headers: { 'x-api-key': dcsoApiCfg.key },
  276. dataType: 'json'
  277. })
  278. .then(res => {
  279. Category.deleteMany({}, (err) => {
  280. if (err) {
  281. console.log(err);
  282. } else {
  283. let categories = JSON.parse(res.body);
  284. categories.forEach(category => {
  285. let newCategory = new Category(category);
  286. newCategory.save();
  287. })
  288. }
  289. });
  290. })
  291. .fail(res => {
  292. console.log(res);
  293. });
  294. }
  295.  
  296. fetchDCSOData();
  297.  
  298. setInterval(() => {
  299. fetchDCSOData();
  300. }, 86400000);
  301.  
  302. app.use('/', router);
  303. app.listen(4000, () => console.log('Server started.'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement