ruleController.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. const db = require('../config/dbConfig');
  2. const url = require('url');
  3. const querystring = require('querystring');
  4. exports.getRules = (req, res) => {
  5. const query = 'SELECT * FROM rules WHERE deleted = 0 ORDER BY rule_id DESC';
  6. // Execute the query
  7. db.query(query, (error, results) => {
  8. if (error) {
  9. res.status(500).json({ error: 'Internal Server Error' });
  10. } else {
  11. // Convert the results to JSON
  12. const jsonResult = JSON.stringify(results);
  13. // Send the JSON response
  14. res.json({ rules: jsonResult });
  15. }
  16. });
  17. }
  18. exports.getRule = (req, res) => {
  19. const query = 'SELECT * FROM rules where rule_id = ?';
  20. const parsedUrl = url.parse(req.originalUrl);
  21. const queryParams = querystring.parse(parsedUrl.query);
  22. db.query(query, [req.query.ruleId], (error, results) => {
  23. if (error) {
  24. res.status(500).json({ error: 'Internal Server Error' });
  25. } else {
  26. // Convert the results to JSON
  27. const jsonResult = JSON.stringify(results);
  28. // Send the JSON response
  29. res.json({ rules: jsonResult });
  30. }
  31. });
  32. }
  33. exports.deleteRule = (req, res) => {
  34. const query = 'UPDATE rules SET deleted = 1 where rule_id = ?';
  35. console.log("ruld to delete" + req.body.ruleID)
  36. db.query(query, [req.body.ruleID], (error, results) => {
  37. if (error) {
  38. res.status(500).json({ error: 'Internal Server Error' });
  39. } else {
  40. const jsonResult = JSON.stringify(results);
  41. res.json({ rules: jsonResult });
  42. }
  43. });
  44. }
  45. exports.publishRule = async (req, res) => {
  46. const edit = req.body.edit;
  47. if (!edit) {
  48. const publishQuery = "INSERT INTO rules "
  49. + "(rule_id, time_stamp, name, lineage, summary, modules, creator_name, creator_url, deleted, list, icon, latest_version, email)"
  50. + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  51. db.query(publishQuery, [req.body.ruleID, req.body.timestamp, req.body.name, req.body.lineage, req.body.summary, req.body.modules, req.body.creatorName, req.body.creatorUrl, 0, req.body.list, req.body.icon, req.body.latest_version, req.body.email], (err, results) => {
  52. if (err) {
  53. console.error('Error executing query:', err);
  54. res.status(500).json({ success: false, message: 'OTP Validated but issue while creating entry for rule ' });
  55. return;
  56. }
  57. res.status(200).json({ success: true, message: 'OTP is valid and your rule is now published' });
  58. })
  59. } else {
  60. const publishQueryVersionHistory = "INSERT INTO version_history "
  61. + "(rule_id, time_stamp, name, lineage, summary, modules, creator_name, creator_url, list, icon, rule_version)"
  62. + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  63. const selectVersionHistory = "SELECT * FROM rules WHERE rule_id = ?"
  64. db.query(selectVersionHistory, [req.body.ruleID], (err, results) => {
  65. if (err) {
  66. console.error('Error executing query:', err);
  67. res.status(500).json({ success: false, message: 'Internal Server Error' });
  68. return;
  69. }
  70. console.log("rule =>" + req.body.ruleID)
  71. if (results.length > 0) {
  72. const versionHistoryData = results[0];
  73. // Execute the second query to insert data
  74. db.query(
  75. publishQueryVersionHistory,
  76. [
  77. versionHistoryData.rule_id,
  78. versionHistoryData.time_stamp,
  79. versionHistoryData.name,
  80. versionHistoryData.lineage,
  81. versionHistoryData.summary,
  82. versionHistoryData.modules,
  83. versionHistoryData.creator_name,
  84. versionHistoryData.creator_url,
  85. versionHistoryData.list,
  86. versionHistoryData.icon,
  87. req.body.latest_version - 1
  88. ],
  89. (err, insertResults) => {
  90. if (err) {
  91. console.log(err)
  92. console.error('Error executing insert query:', err);
  93. res.status(500).json({ success: false, message: 'Internal Server Error' });
  94. return;
  95. }
  96. const updateQuery = "UPDATE rules SET time_stamp = ?, name = ?, lineage = ?, summary = ?, modules = ?, creator_name = ?, creator_url = ?, version = ?, list = ?, icon = ?, latest_version = ? WHERE rule_id = ?;"
  97. db.query(updateQuery, [req.body.timestamp, req.body.name, req.body.lineage, req.body.summary, req.body.modules, req.body.creatorName, req.body.creatorUrl, req.body.version, req.body.list, req.body.icon, req.body.latest_version, req.body.ruleID], (err, results) => {
  98. if (err) {
  99. console.error('Error executing query:', err);
  100. res.status(500).json({ success: false, message: 'OTP Validated but issue while creating entry for rule ' });
  101. return;
  102. }
  103. res.status(200).json({ success: true, message: 'OTP is valid and your rule is now published' });
  104. });
  105. });
  106. }
  107. });
  108. }
  109. }