news.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. async index(session, cb) {
  43. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
  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 }, this);
  53. this.log("ERROR", "NEWS_INDEX", `Indexing news failed. "${err}"`);
  54. return cb({ status: "failure", message: err });
  55. }
  56. this.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 function create(session, data, cb) {
  69. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
  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 }, this);
  81. this.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. this.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. async newest(session, cb) {
  100. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
  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 }, this);
  110. this.log("ERROR", "NEWS_NEWEST", `Getting the latest news failed. "${err}"`);
  111. return cb({ status: "failure", message: err });
  112. }
  113. this.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 function remove(session, news, cb) {
  128. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
  129. newsModel.deleteOne({ _id: news._id }, async err => {
  130. if (err) {
  131. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  132. this.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. this.log("SUCCESS", "NEWS_REMOVE", `Removing news "${news._id}" successful by user "${session.userId}".`);
  141. return cb({
  142. status: "success",
  143. message: "Successfully removed News"
  144. });
  145. });
  146. }),
  147. /**
  148. * Removes a news item
  149. *
  150. * @param {object} session - the session object automatically added by socket.io
  151. * @param {string} _id - the news id
  152. * @param {object} news - the news object
  153. * @param {Function} cb - gets called with the result
  154. */
  155. // TODO Fix this
  156. update: isAdminRequired(async function update(session, _id, news, cb) {
  157. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
  158. newsModel.updateOne({ _id }, news, { upsert: true }, async err => {
  159. if (err) {
  160. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  161. this.log(
  162. "ERROR",
  163. "NEWS_UPDATE",
  164. `Updating news "${_id}" failed for user "${session.userId}". "${err}"`
  165. );
  166. return cb({ status: "failure", message: err });
  167. }
  168. CacheModule.runJob("PUB", { channel: "news.update", value: news });
  169. this.log("SUCCESS", "NEWS_UPDATE", `Updating news "${_id}" successful for user "${session.userId}".`);
  170. return cb({
  171. status: "success",
  172. message: "Successfully updated News"
  173. });
  174. });
  175. })
  176. };