news.js 5.3 KB

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