news.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. 'use strict';
  2. const async = require('async');
  3. const db = require('../db');
  4. const cache = require('../cache');
  5. const utils = require('../utils');
  6. const logger = require('../logger');
  7. const hooks = require('./hooks');
  8. cache.sub('news.create', news => {
  9. utils.socketsFromUser(news.createdBy, sockets => {
  10. sockets.forEach(socket => {
  11. socket.emit('event:admin.news.created', news);
  12. });
  13. });
  14. });
  15. cache.sub('news.remove', news => {
  16. utils.socketsFromUser(news.createdBy, sockets => {
  17. sockets.forEach(socket => {
  18. socket.emit('event:admin.news.removed', news);
  19. });
  20. });
  21. });
  22. cache.sub('news.update', news => {
  23. utils.socketsFromUser(news.createdBy, sockets => {
  24. sockets.forEach(socket => {
  25. socket.emit('event:admin.news.updated', news);
  26. });
  27. });
  28. });
  29. module.exports = {
  30. /**
  31. * Gets all news items
  32. *
  33. * @param {Object} session - the session object automatically added by socket.io
  34. * @param {Function} cb - gets called with the result
  35. */
  36. index: (session, cb) => {
  37. async.waterfall([
  38. (next) => {
  39. db.models.news.find({}).sort({ createdAt: 'desc' }).exec(next);
  40. }
  41. ], (err, news) => {
  42. if (err) {
  43. err = utils.getError(err);
  44. logger.error("NEWS_INDEX", `Indexing news failed. "${err}"`);
  45. return cb({status: 'failure', message: err});
  46. }
  47. logger.success("NEWS_INDEX", `Indexing news successful.`, false);
  48. return cb({ status: 'success', data: news });
  49. });
  50. },
  51. /**
  52. * Creates a news item
  53. *
  54. * @param {Object} session - the session object automatically added by socket.io
  55. * @param {Object} data - the object of the news data
  56. * @param {Function} cb - gets called with the result
  57. * @param {String} userId - the userId automatically added by hooks
  58. */
  59. create: hooks.adminRequired((session, data, cb, userId) => {
  60. async.waterfall([
  61. (next) => {
  62. data.createdBy = userId;
  63. data.createdAt = Date.now();
  64. db.models.news.create(data, next);
  65. }
  66. ], (err, news) => {
  67. if (err) {
  68. err = utils.getError(err);
  69. logger.error("NEWS_CREATE", `Creating news failed. "${err}"`);
  70. return cb({ 'status': 'failure', 'message': err });
  71. }
  72. cache.pub('news.create', news);
  73. logger.success("NEWS_CREATE", `Creating news successful.`);
  74. return cb({ 'status': 'success', 'message': 'Successfully created News' });
  75. });
  76. }),
  77. /**
  78. * Gets the latest news item
  79. *
  80. * @param {Object} session - the session object automatically added by socket.io
  81. * @param {Function} cb - gets called with the result
  82. */
  83. newest: (session, cb) => {
  84. async.waterfall([
  85. (next) => {
  86. db.models.news.findOne({}).sort({ createdAt: 'desc' }).exec(next);
  87. }
  88. ], (err, news) => {
  89. if (err) {
  90. err = utils.getError(err);
  91. logger.error("NEWS_NEWEST", `Getting the latest news failed. "${err}"`);
  92. return cb({ 'status': 'failure', 'message': err });
  93. }
  94. logger.success("NEWS_NEWEST", `Successfully got the latest news.`, false);
  95. return cb({ status: 'success', data: news });
  96. });
  97. },
  98. /**
  99. * Removes a news item
  100. *
  101. * @param {Object} session - the session object automatically added by socket.io
  102. * @param {Object} news - the news object
  103. * @param {Function} cb - gets called with the result
  104. */
  105. //TODO Pass in an id, not an object
  106. //TODO Fix this
  107. remove: hooks.adminRequired((session, news, cb, userId) => {
  108. db.models.news.remove({ _id: news._id }, err => {
  109. if (err) {
  110. err = utils.getError(err);
  111. logger.error("NEWS_REMOVE", `Removing news "${news._id}" failed for user "${userId}". "${err}"`);
  112. return cb({ 'status': 'failure', 'message': err });
  113. } else {
  114. cache.pub('news.remove', news);
  115. logger.success("NEWS_REMOVE", `Removing news "${news._id}" successful by user "${userId}".`);
  116. return cb({ 'status': 'success', 'message': 'Successfully removed News' });
  117. }
  118. });
  119. }),
  120. /**
  121. * Removes a news item
  122. *
  123. * @param {Object} session - the session object automatically added by socket.io
  124. * @param {String} _id - the news id
  125. * @param {Object} news - the news object
  126. * @param {Function} cb - gets called with the result
  127. */
  128. //TODO Fix this
  129. update: hooks.adminRequired((session, _id, news, cb, userId) => {
  130. db.models.news.update({ _id }, news, { upsert: true }, err => {
  131. if (err) {
  132. err = utils.getError(err);
  133. logger.error("NEWS_UPDATE", `Updating news "${_id}" failed for user "${userId}". "${err}"`);
  134. return cb({ 'status': 'failure', 'message': err });
  135. } else {
  136. cache.pub('news.update', news);
  137. logger.success("NEWS_UPDATE", `Updating news "${_id}" successful for user "${userId}".`);
  138. return cb({ 'status': 'success', 'message': 'Successfully updated News' });
  139. }
  140. });
  141. }),
  142. };