JobContext.ts 3.2 KB

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