activities.js 1.5 KB

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