activities.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const CoreClass = require("../core.js");
  2. const async = require("async");
  3. const mongoose = require("mongoose");
  4. class ActivitiesModule extends CoreClass {
  5. constructor() {
  6. super("activities");
  7. }
  8. initialize() {
  9. return new Promise((resolve, reject) => {
  10. this.db = this.moduleManager.modules["db"];
  11. this.io = this.moduleManager.modules["io"];
  12. this.utils = this.moduleManager.modules["utils"];
  13. resolve();
  14. });
  15. }
  16. // TODO: Migrate
  17. ADD_ACTIVITY(payload) {
  18. //userId, activityType, payload
  19. return new Promise((resolve, reject) => {
  20. async.waterfall(
  21. [
  22. (next) => {
  23. this.db
  24. .runJob("GET_MODEL", { modelName: "activity" })
  25. .then((res) => {
  26. next(null, res);
  27. })
  28. .catch(next);
  29. },
  30. (activityModel, next) => {
  31. const activity = new activityModel({
  32. userId: payload.userId,
  33. activityType: payload.activityType,
  34. payload: payload.payload,
  35. });
  36. activity.save((err, activity) => {
  37. if (err) return next(err);
  38. next(null, activity);
  39. });
  40. },
  41. (activity, next) => {
  42. this.utils
  43. .runJob("SOCKETS_FROM_USER", {
  44. userId: activity.userId,
  45. })
  46. .then((response) => {
  47. response.sockets.forEach((socket) => {
  48. socket.emit(
  49. "event:activity.create",
  50. activity
  51. );
  52. });
  53. next();
  54. })
  55. .catch(next);
  56. },
  57. ],
  58. async (err, activity) => {
  59. if (err) {
  60. err = await this.utils.runJob("GET_ERROR", {
  61. error: err,
  62. });
  63. reject(new Error(err));
  64. } else {
  65. resolve({ activity });
  66. }
  67. }
  68. );
  69. });
  70. }
  71. }
  72. module.exports = new ActivitiesModule();