news.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. "use strict";
  2. const async = require("async");
  3. const hooks = require("./hooks");
  4. const moduleManager = require("../../index");
  5. const db = require("../db");
  6. const cache = require("../cache");
  7. const utils = require("../utils");
  8. // const logger = require("logger");
  9. cache.runJob("SUB", {
  10. channel: "news.create",
  11. cb: (news) => {
  12. utils.runJob("SOCKETS_FROM_USER", {
  13. userId: news.createdBy,
  14. cb: (response) => {
  15. response.sockets.forEach((socket) => {
  16. socket.emit("event:admin.news.created", news);
  17. });
  18. },
  19. });
  20. },
  21. });
  22. cache.runJob("SUB", {
  23. channel: "news.remove",
  24. cb: (news) => {
  25. utils.runJob("SOCKETS_FROM_USER", {
  26. userId: news.createdBy,
  27. cb: (response) => {
  28. response.sockets.forEach((socket) => {
  29. socket.emit("event:admin.news.removed", news);
  30. });
  31. },
  32. });
  33. },
  34. });
  35. cache.runJob("SUB", {
  36. channel: "news.update",
  37. cb: (news) => {
  38. utils.runJob("SOCKETS_FROM_USER", {
  39. userId: news.createdBy,
  40. cb: (response) => {
  41. response.sockets.forEach((socket) => {
  42. socket.emit("event:admin.news.updated", news);
  43. });
  44. },
  45. });
  46. },
  47. });
  48. module.exports = {
  49. /**
  50. * Gets all news items
  51. *
  52. * @param {Object} session - the session object automatically added by socket.io
  53. * @param {Function} cb - gets called with the result
  54. */
  55. index: async (session, cb) => {
  56. const newsModel = await db.runJob("GET_MODEL", { modelName: "news" });
  57. async.waterfall(
  58. [
  59. (next) => {
  60. newsModel
  61. .find({})
  62. .sort({ createdAt: "desc" })
  63. .exec(next);
  64. },
  65. ],
  66. async (err, news) => {
  67. if (err) {
  68. err = await utils.runJob("GET_ERROR", { error: err });
  69. console.log(
  70. "ERROR",
  71. "NEWS_INDEX",
  72. `Indexing news failed. "${err}"`
  73. );
  74. return cb({ status: "failure", message: err });
  75. }
  76. console.log(
  77. "SUCCESS",
  78. "NEWS_INDEX",
  79. `Indexing news successful.`,
  80. false
  81. );
  82. return cb({ status: "success", data: news });
  83. }
  84. );
  85. },
  86. /**
  87. * Creates a news item
  88. *
  89. * @param {Object} session - the session object automatically added by socket.io
  90. * @param {Object} data - the object of the news data
  91. * @param {Function} cb - gets called with the result
  92. */
  93. create: hooks.adminRequired(async (session, data, cb) => {
  94. const newsModel = await db.runJob("GET_MODEL", { modelName: "news" });
  95. async.waterfall(
  96. [
  97. (next) => {
  98. data.createdBy = session.userId;
  99. data.createdAt = Date.now();
  100. newsModel.create(data, next);
  101. },
  102. ],
  103. async (err, news) => {
  104. if (err) {
  105. err = await utils.runJob("GET_ERROR", { error: err });
  106. console.log(
  107. "ERROR",
  108. "NEWS_CREATE",
  109. `Creating news failed. "${err}"`
  110. );
  111. return cb({ status: "failure", message: err });
  112. }
  113. cache.runJob("PUB", { channel: "news.create", value: news });
  114. console.log(
  115. "SUCCESS",
  116. "NEWS_CREATE",
  117. `Creating news successful.`
  118. );
  119. return cb({
  120. status: "success",
  121. message: "Successfully created News",
  122. });
  123. }
  124. );
  125. }),
  126. /**
  127. * Gets the latest news item
  128. *
  129. * @param {Object} session - the session object automatically added by socket.io
  130. * @param {Function} cb - gets called with the result
  131. */
  132. newest: async (session, cb) => {
  133. const newsModel = await db.runJob("GET_MODEL", { modelName: "news" });
  134. async.waterfall(
  135. [
  136. (next) => {
  137. newsModel
  138. .findOne({})
  139. .sort({ createdAt: "desc" })
  140. .exec(next);
  141. },
  142. ],
  143. async (err, news) => {
  144. if (err) {
  145. err = await utils.runJob("GET_ERROR", { error: err });
  146. console.log(
  147. "ERROR",
  148. "NEWS_NEWEST",
  149. `Getting the latest news failed. "${err}"`
  150. );
  151. return cb({ status: "failure", message: err });
  152. }
  153. console.log(
  154. "SUCCESS",
  155. "NEWS_NEWEST",
  156. `Successfully got the latest news.`,
  157. false
  158. );
  159. return cb({ status: "success", data: news });
  160. }
  161. );
  162. },
  163. /**
  164. * Removes a news item
  165. *
  166. * @param {Object} session - the session object automatically added by socket.io
  167. * @param {Object} news - the news object
  168. * @param {Function} cb - gets called with the result
  169. */
  170. //TODO Pass in an id, not an object
  171. //TODO Fix this
  172. remove: hooks.adminRequired(async (session, news, cb) => {
  173. const newsModel = await db.runJob("GET_MODEL", { modelName: "news" });
  174. newsModel.deleteOne({ _id: news._id }, async (err) => {
  175. if (err) {
  176. err = await utils.runJob("GET_ERROR", { error: err });
  177. console.log(
  178. "ERROR",
  179. "NEWS_REMOVE",
  180. `Removing news "${news._id}" failed for user "${session.userId}". "${err}"`
  181. );
  182. return cb({ status: "failure", message: err });
  183. } else {
  184. cache.runJob("PUB", { channel: "news.remove", value: news });
  185. console.log(
  186. "SUCCESS",
  187. "NEWS_REMOVE",
  188. `Removing news "${news._id}" successful by user "${session.userId}".`
  189. );
  190. return cb({
  191. status: "success",
  192. message: "Successfully removed News",
  193. });
  194. }
  195. });
  196. }),
  197. /**
  198. * Removes a news item
  199. *
  200. * @param {Object} session - the session object automatically added by socket.io
  201. * @param {String} _id - the news id
  202. * @param {Object} news - the news object
  203. * @param {Function} cb - gets called with the result
  204. */
  205. //TODO Fix this
  206. update: hooks.adminRequired(async (session, _id, news, cb) => {
  207. const newsModel = await db.runJob("GET_MODEL", { modelName: "news" });
  208. newsModel.updateOne({ _id }, news, { upsert: true }, async (err) => {
  209. if (err) {
  210. err = await utils.runJob("GET_ERROR", { error: err });
  211. console.log(
  212. "ERROR",
  213. "NEWS_UPDATE",
  214. `Updating news "${_id}" failed for user "${session.userId}". "${err}"`
  215. );
  216. return cb({ status: "failure", message: err });
  217. } else {
  218. cache.runJob("PUB", { channel: "news.update", value: news });
  219. console.log(
  220. "SUCCESS",
  221. "NEWS_UPDATE",
  222. `Updating news "${_id}" successful for user "${session.userId}".`
  223. );
  224. return cb({
  225. status: "success",
  226. message: "Successfully updated News",
  227. });
  228. }
  229. });
  230. }),
  231. };