news.js 7.5 KB

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