1
0

JobContext.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { SessionSchema } from "@/modules/DataModule/models/sessions/schema";
  2. import Job from "@/Job";
  3. import { Log } from "@/LogBook";
  4. import { JobOptions } from "@/types/JobOptions";
  5. import DataModule from "@/modules/DataModule";
  6. import { UserModel } from "@/modules/DataModule/models/users/schema";
  7. export default class JobContext {
  8. public readonly job: Job;
  9. private _session?: SessionSchema;
  10. private readonly _socketId?: string;
  11. private readonly _callbackRef?: string;
  12. public constructor(
  13. job: Job,
  14. options?: {
  15. session?: SessionSchema;
  16. socketId?: string;
  17. callbackRef?: string;
  18. }
  19. ) {
  20. this.job = job;
  21. this._session = options?.session;
  22. this._socketId = options?.socketId;
  23. this._callbackRef = options?.callbackRef;
  24. }
  25. /**
  26. * Log a message in the context of the current job, which automatically sets the category and data
  27. *
  28. * @param log - Log message or object
  29. */
  30. public log(log: string | Omit<Log, "timestamp" | "category">) {
  31. return this.job.log(log);
  32. }
  33. public getSession() {
  34. return this._session;
  35. }
  36. public setSession(session?: SessionSchema) {
  37. this._session = session;
  38. }
  39. public getSocketId() {
  40. return this._socketId;
  41. }
  42. public getCallbackRef() {
  43. return this._callbackRef;
  44. }
  45. public executeJob(
  46. JobClass: typeof Job,
  47. payload?: unknown,
  48. options?: JobOptions
  49. ) {
  50. return new JobClass(payload, {
  51. session: this._session,
  52. socketId: this._socketId,
  53. ...(options ?? {})
  54. }).execute();
  55. }
  56. public async getUser() {
  57. if (!this._session?.userId)
  58. throw new Error("No user found for session");
  59. const User = await DataModule.getModel<UserModel>("users");
  60. const user = await User.findById(this._session.userId);
  61. if (!user) throw new Error("No user found for session");
  62. return user;
  63. }
  64. public async assertLoggedIn() {
  65. if (!this._session?.userId)
  66. throw new Error("No user found for session");
  67. }
  68. public async assertPermission(permission: string) {
  69. let hasPermission = false;
  70. const [, moduleName, modelOrJobName, jobName, modelId] =
  71. /^([a-z]+)\.([a-z]+)\.([A-z]+)\.?([A-z0-9]+)?$/.exec(permission) ??
  72. [];
  73. if (moduleName === "data" && modelOrJobName && jobName) {
  74. const GetModelPermissions = DataModule.getJob(
  75. "users.getModelPermissions"
  76. );
  77. const permissions = await this.executeJob(GetModelPermissions, {
  78. modelName: modelOrJobName,
  79. modelId
  80. });
  81. hasPermission = permissions[`data.${modelOrJobName}.${jobName}`];
  82. } else {
  83. const GetPermissions = DataModule.getJob("users.getPermissions");
  84. const permissions = await this.executeJob(GetPermissions);
  85. hasPermission = permissions[permission];
  86. }
  87. if (!hasPermission) throw new Error("Insufficient permissions");
  88. }
  89. }