news.js 5.3 KB

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