news.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 WSModule = moduleManager.modules.ws;
  7. const CacheModule = moduleManager.modules.cache;
  8. CacheModule.runJob("SUB", {
  9. channel: "news.create",
  10. cb: news => {
  11. WSModule.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. WSModule.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. WSModule.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 the websocket
  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. * Gets a news item by id
  63. *
  64. * @param {object} session - the session object automatically added by the websocket
  65. * @param {string} newsId - the news id
  66. * @param {Function} cb - gets called with the result
  67. */
  68. async getNewsFromId(session, newsId, cb) {
  69. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
  70. async.waterfall(
  71. [
  72. next => {
  73. newsModel.findOne({ _id: newsId }, next);
  74. }
  75. ],
  76. async (err, news) => {
  77. if (err) {
  78. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  79. this.log("ERROR", "GET_NEWS_FROM_ID", `Getting news failed. "${err}"`);
  80. return cb({ status: "failure", message: err });
  81. }
  82. this.log("SUCCESS", "GET_NEWS_FROM_ID", `Got news successful.`, false);
  83. return cb({ status: "success", data: news });
  84. }
  85. );
  86. },
  87. /**
  88. * Creates a news item
  89. *
  90. * @param {object} session - the session object automatically added by the websocket
  91. * @param {object} data - the object of the news data
  92. * @param {Function} cb - gets called with the result
  93. */
  94. create: isAdminRequired(async function create(session, data, cb) {
  95. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
  96. async.waterfall(
  97. [
  98. next => {
  99. data.createdBy = session.userId;
  100. data.createdAt = Date.now();
  101. newsModel.create(data, next);
  102. }
  103. ],
  104. async (err, news) => {
  105. if (err) {
  106. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  107. this.log("ERROR", "NEWS_CREATE", `Creating news failed. "${err}"`);
  108. return cb({ status: "failure", message: err });
  109. }
  110. CacheModule.runJob("PUB", { channel: "news.create", value: news });
  111. this.log("SUCCESS", "NEWS_CREATE", `Creating news successful.`);
  112. return cb({
  113. status: "success",
  114. message: "Successfully created News"
  115. });
  116. }
  117. );
  118. }),
  119. /**
  120. * Gets the latest news item
  121. *
  122. * @param {object} session - the session object automatically added by the websocket
  123. * @param {Function} cb - gets called with the result
  124. */
  125. async newest(session, cb) {
  126. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
  127. async.waterfall(
  128. [
  129. next => {
  130. newsModel.findOne({}).sort({ createdAt: "desc" }).exec(next);
  131. }
  132. ],
  133. async (err, news) => {
  134. if (err) {
  135. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  136. this.log("ERROR", "NEWS_NEWEST", `Getting the latest news failed. "${err}"`);
  137. return cb({ status: "failure", message: err });
  138. }
  139. this.log("SUCCESS", "NEWS_NEWEST", `Successfully got the latest news.`, false);
  140. return cb({ status: "success", data: news });
  141. }
  142. );
  143. },
  144. /**
  145. * Removes a news item
  146. *
  147. * @param {object} session - the session object automatically added by the websocket
  148. * @param {object} news - the news object
  149. * @param {Function} cb - gets called with the result
  150. */
  151. // TODO Pass in an id, not an object
  152. // TODO Fix this
  153. remove: isAdminRequired(async function remove(session, news, cb) {
  154. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
  155. newsModel.deleteOne({ _id: news._id }, async err => {
  156. if (err) {
  157. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  158. this.log(
  159. "ERROR",
  160. "NEWS_REMOVE",
  161. `Removing news "${news._id}" failed for user "${session.userId}". "${err}"`
  162. );
  163. return cb({ status: "failure", message: err });
  164. }
  165. CacheModule.runJob("PUB", { channel: "news.remove", value: news });
  166. this.log("SUCCESS", "NEWS_REMOVE", `Removing news "${news._id}" successful by user "${session.userId}".`);
  167. return cb({
  168. status: "success",
  169. message: "Successfully removed News"
  170. });
  171. });
  172. }),
  173. /**
  174. * Removes a news item
  175. *
  176. * @param {object} session - the session object automatically added by the websocket
  177. * @param {string} _id - the news id
  178. * @param {object} news - the news object
  179. * @param {Function} cb - gets called with the result
  180. */
  181. // TODO Fix this
  182. update: isAdminRequired(async function update(session, _id, news, cb) {
  183. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
  184. newsModel.updateOne({ _id }, news, { upsert: true }, async err => {
  185. if (err) {
  186. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  187. this.log(
  188. "ERROR",
  189. "NEWS_UPDATE",
  190. `Updating news "${_id}" failed for user "${session.userId}". "${err}"`
  191. );
  192. return cb({ status: "failure", message: err });
  193. }
  194. CacheModule.runJob("PUB", { channel: "news.update", value: news });
  195. this.log("SUCCESS", "NEWS_UPDATE", `Updating news "${_id}" successful for user "${session.userId}".`);
  196. return cb({
  197. status: "success",
  198. message: "Successfully updated News"
  199. });
  200. });
  201. })
  202. };