news.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import async from "async";
  2. import { isAdminRequired } from "./hooks";
  3. import moduleManager from "../../index";
  4. const DBModule = moduleManager.modules.db;
  5. const UtilsModule = moduleManager.modules.utils;
  6. const IOModule = moduleManager.modules.io;
  7. const CacheModule = moduleManager.modules.cache;
  8. CacheModule.runJob("SUB", {
  9. channel: "news.create",
  10. cb: news => {
  11. IOModule.runJob("EMIT_TO_ROOM", {
  12. room: "admin.news",
  13. args: ["event:admin.news.created", news]
  14. });
  15. }
  16. });
  17. CacheModule.runJob("SUB", {
  18. channel: "news.remove",
  19. cb: news => {
  20. IOModule.runJob("EMIT_TO_ROOM", {
  21. room: "admin.news",
  22. args: ["event:admin.news.removed", news]
  23. });
  24. }
  25. });
  26. CacheModule.runJob("SUB", {
  27. channel: "news.update",
  28. cb: news => {
  29. IOModule.runJob("EMIT_TO_ROOM", {
  30. room: "admin.news",
  31. args: ["event:admin.news.updated", news]
  32. });
  33. }
  34. });
  35. export default {
  36. /**
  37. * Gets all news items
  38. *
  39. * @param {object} session - the session object automatically added by socket.io
  40. * @param {Function} cb - gets called with the result
  41. */
  42. index: async (session, cb) => {
  43. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" });
  44. async.waterfall(
  45. [
  46. next => {
  47. newsModel.find({}).sort({ createdAt: "desc" }).exec(next);
  48. }
  49. ],
  50. async (err, news) => {
  51. if (err) {
  52. err = await UtilsModule.runJob("GET_ERROR", { error: err });
  53. console.log("ERROR", "NEWS_INDEX", `Indexing news failed. "${err}"`);
  54. return cb({ status: "failure", message: err });
  55. }
  56. console.log("SUCCESS", "NEWS_INDEX", `Indexing news successful.`, false);
  57. return cb({ status: "success", data: news });
  58. }
  59. );
  60. },
  61. /**
  62. * Creates a news item
  63. *
  64. * @param {object} session - the session object automatically added by socket.io
  65. * @param {object} data - the object of the news data
  66. * @param {Function} cb - gets called with the result
  67. */
  68. create: isAdminRequired(async (session, data, cb) => {
  69. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" });
  70. async.waterfall(
  71. [
  72. next => {
  73. data.createdBy = session.userId;
  74. data.createdAt = Date.now();
  75. newsModel.create(data, next);
  76. }
  77. ],
  78. async (err, news) => {
  79. if (err) {
  80. err = await UtilsModule.runJob("GET_ERROR", { error: err });
  81. console.log("ERROR", "NEWS_CREATE", `Creating news failed. "${err}"`);
  82. return cb({ status: "failure", message: err });
  83. }
  84. CacheModule.runJob("PUB", { channel: "news.create", value: news });
  85. console.log("SUCCESS", "NEWS_CREATE", `Creating news successful.`);
  86. return cb({
  87. status: "success",
  88. message: "Successfully created News"
  89. });
  90. }
  91. );
  92. }),
  93. /**
  94. * Gets the latest news item
  95. *
  96. * @param {object} session - the session object automatically added by socket.io
  97. * @param {Function} cb - gets called with the result
  98. */
  99. newest: async (session, cb) => {
  100. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" });
  101. async.waterfall(
  102. [
  103. next => {
  104. newsModel.findOne({}).sort({ createdAt: "desc" }).exec(next);
  105. }
  106. ],
  107. async (err, news) => {
  108. if (err) {
  109. err = await UtilsModule.runJob("GET_ERROR", { error: err });
  110. console.log("ERROR", "NEWS_NEWEST", `Getting the latest news failed. "${err}"`);
  111. return cb({ status: "failure", message: err });
  112. }
  113. console.log("SUCCESS", "NEWS_NEWEST", `Successfully got the latest news.`, false);
  114. return cb({ status: "success", data: news });
  115. }
  116. );
  117. },
  118. /**
  119. * Removes a news item
  120. *
  121. * @param {object} session - the session object automatically added by socket.io
  122. * @param {object} news - the news object
  123. * @param {Function} cb - gets called with the result
  124. */
  125. // TODO Pass in an id, not an object
  126. // TODO Fix this
  127. remove: isAdminRequired(async (session, news, cb) => {
  128. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" });
  129. newsModel.deleteOne({ _id: news._id }, async err => {
  130. if (err) {
  131. err = await UtilsModule.runJob("GET_ERROR", { error: err });
  132. console.log(
  133. "ERROR",
  134. "NEWS_REMOVE",
  135. `Removing news "${news._id}" failed for user "${session.userId}". "${err}"`
  136. );
  137. return cb({ status: "failure", message: err });
  138. }
  139. CacheModule.runJob("PUB", { channel: "news.remove", value: news });
  140. console.log(
  141. "SUCCESS",
  142. "NEWS_REMOVE",
  143. `Removing news "${news._id}" successful by user "${session.userId}".`
  144. );
  145. return cb({
  146. status: "success",
  147. message: "Successfully removed 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 {string} _id - the news id
  156. * @param {object} news - the news object
  157. * @param {Function} cb - gets called with the result
  158. */
  159. // TODO Fix this
  160. update: isAdminRequired(async (session, _id, news, cb) => {
  161. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" });
  162. newsModel.updateOne({ _id }, news, { upsert: true }, async err => {
  163. if (err) {
  164. err = await UtilsModule.runJob("GET_ERROR", { error: err });
  165. console.log(
  166. "ERROR",
  167. "NEWS_UPDATE",
  168. `Updating news "${_id}" failed for user "${session.userId}". "${err}"`
  169. );
  170. return cb({ status: "failure", message: err });
  171. }
  172. CacheModule.runJob("PUB", { channel: "news.update", value: news });
  173. console.log("SUCCESS", "NEWS_UPDATE", `Updating news "${_id}" successful for user "${session.userId}".`);
  174. return cb({
  175. status: "success",
  176. message: "Successfully updated News"
  177. });
  178. });
  179. })
  180. };