Event.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { HydratedDocument } from "mongoose";
  2. import { UserSchema } from "@models/users/schema";
  3. import { GetPermissionsResult } from "../DataModule/models/users/jobs/GetPermissions";
  4. import DataModule from "../DataModule";
  5. export default abstract class Event {
  6. protected static _namespace: string;
  7. protected static _name: string;
  8. protected static _type: "event" | "schedule" = "event";
  9. protected _data: any;
  10. protected _scope?: string;
  11. public constructor(data: any, scope?: string) {
  12. this._data = data;
  13. this._scope = scope;
  14. }
  15. public static getNamespace() {
  16. return this._namespace;
  17. }
  18. public static getName() {
  19. return this._name;
  20. }
  21. public static getPath() {
  22. return `${this.getNamespace()}.${this.getName()}`;
  23. }
  24. public static getKey(scope?: string) {
  25. const path = this.getPath();
  26. if (scope) return `${path}:${scope}`;
  27. return path;
  28. }
  29. public static parseKey(key: string) {
  30. const [path, scope] = key.split(":");
  31. return {
  32. path,
  33. scope
  34. };
  35. }
  36. public static getType() {
  37. return this._type;
  38. }
  39. public static async hasPermission(
  40. user: HydratedDocument<UserSchema> | null,
  41. scope?: string
  42. ) {
  43. const GetPermissions = DataModule.getJob("users.getPermissions");
  44. const permissions = (await new GetPermissions({
  45. _id: user?._id
  46. }).execute()) as GetPermissionsResult;
  47. return !!(
  48. permissions[`event:${this.getKey(scope)}`] ||
  49. permissions[`event:${this.getPath()}:*`]
  50. );
  51. }
  52. public static makeMessage(data: any) {
  53. if (["object", "array"].includes(typeof data))
  54. return JSON.stringify(data);
  55. return data;
  56. }
  57. public static parseMessage(message: string) {
  58. let parsedMessage = message;
  59. if (parsedMessage.startsWith("[") || parsedMessage.startsWith("{"))
  60. try {
  61. parsedMessage = JSON.parse(parsedMessage);
  62. } catch (err) {
  63. console.error(err);
  64. }
  65. else if (parsedMessage.startsWith('"') && parsedMessage.endsWith('"'))
  66. parsedMessage = parsedMessage
  67. .substring(1)
  68. .substring(0, parsedMessage.length - 2);
  69. return parsedMessage;
  70. }
  71. public getNamespace() {
  72. return (this.constructor as typeof Event).getNamespace();
  73. }
  74. public getName() {
  75. return (this.constructor as typeof Event).getName();
  76. }
  77. public getPath() {
  78. return (this.constructor as typeof Event).getPath();
  79. }
  80. public getKey() {
  81. return (this.constructor as typeof Event).getKey(this._scope);
  82. }
  83. public getData() {
  84. return this._data;
  85. }
  86. public makeMessage() {
  87. return (this.constructor as typeof Event).makeMessage(this._data);
  88. }
  89. }
  90. export type EventClass = {
  91. new (...params: ConstructorParameters<typeof Event>): Event;
  92. };