PageRenderTime 59ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/ecm-server/api/notify.js

https://bitbucket.org/aavilal/vinnerensite
JavaScript | 282 lines | 264 code | 18 blank | 0 comment | 12 complexity | 7a0421c82f667db84c0112fc82c05a51 MD5 | raw file
  1. import { sendNotify } from '../utils/utils.js';
  2. import uniqid from 'uniqid';
  3. module.exports = (app, mysqlConn, admin) => {
  4. app.get('/api/schedule-notify', (req, res) => {
  5. let response;
  6. mysqlConn.query('SELECT * FROM ent_pending ORDER BY date_send ASC, time_send ASC', (err, result, fields) => {
  7. if (err) {
  8. response = {
  9. success: false,
  10. message: 'Error getting pending notifications.',
  11. pending: null
  12. }
  13. res.status(200);
  14. res.setHeader('Content-Type', 'application/json');
  15. res.send(JSON.stringify(response));
  16. return;
  17. }
  18. response = {
  19. success: true,
  20. message: '',
  21. pending: result
  22. }
  23. res.status(200);
  24. res.setHeader('Content-Type', 'application/json');
  25. res.send(JSON.stringify(response));
  26. return;
  27. });
  28. });
  29. app.post('/api/add/schedule-notify', (req, res) => {
  30. const { title, body, iconName, bucketIconName, bucketIconUrl, imgName, bucketImgName, bucketImgUrl, dateSend, timeSend, url } = req.body;
  31. let id = uniqid();
  32. let type = 'Scheduled';
  33. let clicks = 0;
  34. let message = {
  35. id,
  36. dateSend,
  37. timeSend,
  38. title,
  39. body,
  40. iconName,
  41. bucketIconName,
  42. bucketIconUrl,
  43. imgName,
  44. bucketImgName,
  45. bucketImgUrl,
  46. type,
  47. url,
  48. clicks
  49. }
  50. let response;
  51. let query = 'INSERT INTO ent_pending (hash, date_send, time_send, title, body, icon_name, bucket_icon_name, bucket_icon_url, img_name, bucket_img_name, bucket_img_url, type, url, clicks) VALUES ("';
  52. query += id + '","' + dateSend + '","' + timeSend + '","' + title + '","' + body + '","' + iconName + '","' + bucketIconName + '","' + bucketIconUrl + '","' + imgName + '","' + bucketImgName + '","' + bucketImgUrl + '","' + type + '","' + url + '",0)';
  53. mysqlConn.query(query, (err, result, fields) => {
  54. if (err) {
  55. response = {
  56. success: false,
  57. message: 'Error saving pending notification.',
  58. pending: null
  59. }
  60. res.status(200);
  61. res.setHeader('Content-Type', 'application/json');
  62. res.send(JSON.stringify(response));
  63. return;
  64. }
  65. mysqlConn.query('SELECT * FROM ent_pending ORDER BY date_send ASC, time_send ASC', (err, result, fields) => {
  66. response = {
  67. success: true,
  68. message: 'Notification added.',
  69. pending: result
  70. }
  71. res.status(200);
  72. res.setHeader('Content-Type', 'application/json');
  73. res.send(JSON.stringify(response));
  74. return;
  75. });
  76. });
  77. });
  78. app.post('/api/mod/schedule-notify', (req, res) => {
  79. const { id, title, body, iconName, bucketIconName, bucketIconUrl, imgName, bucketImgName, bucketImgUrl, dateSend, timeSend, type, url } = req.body;
  80. let clicks = 0;
  81. let message = {
  82. id,
  83. dateSend,
  84. timeSend,
  85. title,
  86. body,
  87. iconName,
  88. bucketIconName,
  89. bucketIconUrl,
  90. imgName,
  91. bucketImgName,
  92. bucketImgUrl,
  93. type,
  94. url,
  95. clicks
  96. }
  97. let response;
  98. let query = 'UPDATE ent_pending set date_send="' + dateSend + '", time_send="' + timeSend + '", title="' + title + '", body="' + body + '", icon_name="' + iconName + '", bucket_icon_name="' + bucketIconName + '", bucket_icon_url="' + bucketIconUrl + '", img_name="' + imgName + '", bucket_img_name="' + bucketImgName + '", bucket_img_url="' + bucketImgUrl + '", type="' + type + '", url="' + url + '"';
  99. query += " WHERE hash='" + id + "';"
  100. mysqlConn.query(query, (err, result, fields) => {
  101. if (err) {
  102. response = {
  103. success: false,
  104. message: 'Error updating pending notification.',
  105. pending: null
  106. }
  107. res.status(200);
  108. res.setHeader('Content-Type', 'application/json');
  109. res.send(JSON.stringify(response));
  110. return;
  111. }
  112. mysqlConn.query('SELECT * FROM ent_pending ORDER BY date_send ASC, time_send ASC', (err, result, fields) => {
  113. response = {
  114. success: true,
  115. message: 'Notification schedule updated.',
  116. pending: result
  117. }
  118. res.status(200);
  119. res.setHeader('Content-Type', 'application/json');
  120. res.send(JSON.stringify(response));
  121. return;
  122. });
  123. });
  124. });
  125. app.post('/api/del/schedule-notify', (req, res) => {
  126. const { id } = req.body;
  127. let response;
  128. let query = "DELETE FROM ent_pending WHERE hash='" + id + "'";
  129. mysqlConn.query(query, (err, result, fields) => {
  130. if (err) {
  131. response = {
  132. success: false,
  133. message: 'Error deleting pending notification.',
  134. pending: null
  135. }
  136. res.status(200);
  137. res.setHeader('Content-Type', 'application/json');
  138. res.send(JSON.stringify(response));
  139. return;
  140. }
  141. mysqlConn.query('SELECT * FROM ent_pending ORDER BY date_send ASC, time_send ASC', (err, result, fields) => {
  142. response = {
  143. success: true,
  144. message: 'Notification schedule deleted.',
  145. pending: result
  146. }
  147. res.status(200);
  148. res.setHeader('Content-Type', 'application/json');
  149. res.send(JSON.stringify(response));
  150. return;
  151. });
  152. });
  153. });
  154. app.post('/api/quick-notify', (req, res) => {
  155. const { title, body, iconName, bucketIconName, bucketIconUrl, imgName, bucketImgName, bucketImgUrl, url } = req.body;
  156. let id = uniqid();
  157. let dateSend = new Date();
  158. let timeSend = new Date();
  159. let dateS = dateSend.getFullYear() + '-';
  160. dateS += (dateSend.getMonth() + 1 < 10 ? '0' : '') + (dateSend.getMonth() + 1) + '-';
  161. dateS += (dateSend.getSeconds() < 10 ? '0' : '') + dateSend.getDate();
  162. let timeS = timeSend.getFullYear() + '-';
  163. timeS += (timeSend.getMonth() + 1 < 10 ? '0' : '') + (timeSend.getMonth() + 1) + '-';
  164. timeS += (timeSend.getSeconds() < 10 ? '0' : '') + timeSend.getDate() + ' ';
  165. timeS += (timeSend.getHours() < 10 ? '0' : '') + timeSend.getHours() + ':';
  166. timeS += (timeSend.getMinutes() < 10 ? '0' : '') + timeSend.getMinutes() + ':';
  167. timeS += (timeSend.getSeconds() < 10 ? '0' : '') + timeSend.getSeconds();
  168. dateSend = dateS;
  169. timeSend = timeS;
  170. let type = 'Quick';
  171. let clicks = 0;
  172. let message = {
  173. id,
  174. dateSend,
  175. timeSend,
  176. title,
  177. body,
  178. iconName,
  179. bucketIconName,
  180. bucketIconUrl,
  181. imgName,
  182. bucketImgName,
  183. bucketImgUrl,
  184. type,
  185. url,
  186. clicks
  187. }
  188. let subscribersSend = [];
  189. let response;
  190. mysqlConn.query('SELECT * FROM subscribers ', (err, result, fields) => {
  191. if (err) {
  192. response = {
  193. success: false,
  194. message: 'Error getting subscribers.'
  195. }
  196. res.status(200);
  197. res.setHeader('Content-Type', 'application/json');
  198. res.send(JSON.stringify(response));
  199. return;
  200. }
  201. if (result.length == 0) {
  202. response = {
  203. success: false,
  204. message: 'No subscribers.'
  205. }
  206. res.status(200);
  207. res.setHeader('Content-Type', 'application/json');
  208. res.send(JSON.stringify(response));
  209. return;
  210. }
  211. let index;
  212. for (let i = 0; i < result.length; i++) {
  213. index = Math.trunc(i/900);
  214. subscribersSend[index] = subscribersSend[index] == undefined ? [] : subscribersSend[index];
  215. subscribersSend[index].push(result[i].token);
  216. }
  217. if (subscribersSend.length == 0) {
  218. response = {
  219. success: false,
  220. message: 'No subscribers.'
  221. }
  222. res.status(200);
  223. res.setHeader('Content-Type', 'application/json');
  224. res.send(JSON.stringify(response));
  225. return;
  226. }
  227. let messagePN = {
  228. title: message.title,
  229. body: message.body,
  230. bucketIconUrl: message.bucketIconUrl,
  231. bucketImgUrl: message.bucketImgUrl,
  232. url: message.url,
  233. id: message.id
  234. }
  235. for (let i = 0; i < subscribersSend.length; i++) {
  236. admin.messaging().sendToDevice(subscribersSend[i], {data: messagePN})
  237. .then(function(response) {
  238. let query = 'INSERT INTO ent_history (hash, date_send, time_send, title, body, icon_name, bucket_icon_name, bucket_icon_url, img_name, bucket_img_name, bucket_img_url, type, url, clicks) VALUES ("';
  239. query += message.id + '","' + message.dateSend + '","' + message.timeSend + '","' + message.title + '","' + message.body + '","' + message.iconName + '","' + message.bucketIconName + '","' + message.bucketIconUrl + '","' + message.imgName + '","' + message.bucketImgName + '","' + message.bucketImgUrl + '","' + message.type + '","' + message.url + '",0)';
  240. mysqlConn.query(query, (err, result, fields) => {
  241. response = {
  242. success: true,
  243. message: 'Notification sent.'
  244. }
  245. res.status(200);
  246. res.setHeader('Content-Type', 'application/json');
  247. res.send(JSON.stringify(response));
  248. return;
  249. });
  250. })
  251. .catch(function(error) {
  252. response = {
  253. success: false,
  254. message: 'Error sending notification.'
  255. }
  256. res.status(200);
  257. res.setHeader('Content-Type', 'application/json');
  258. res.send(JSON.stringify(response));
  259. return;
  260. });
  261. }
  262. });
  263. });
  264. }