JobContext.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { Types } from "mongoose";
  2. import BaseModule from "./BaseModule";
  3. import Job from "./Job";
  4. import JobQueue from "./JobQueue";
  5. import { Log } from "./LogBook";
  6. import { SessionSchema } from "./schemas/session";
  7. import { JobOptions } from "./types/JobOptions";
  8. import { Jobs, Modules } from "./types/Modules";
  9. import { StationType } from "./schemas/station";
  10. import { UserRole, UserSchema } from "./schemas/user";
  11. import permissions from "./permissions";
  12. import { Models } from "./types/Models";
  13. export default class JobContext {
  14. public readonly job: Job;
  15. public readonly jobQueue: JobQueue;
  16. private session?: SessionSchema;
  17. private user?: UserSchema;
  18. public constructor(job: Job, session?: SessionSchema) {
  19. this.job = job;
  20. this.jobQueue = JobQueue.getPrimaryInstance();
  21. this.session = session;
  22. }
  23. /**
  24. * Log a message in the context of the current job, which automatically sets the category and data
  25. *
  26. * @param log - Log message or object
  27. */
  28. public log(log: string | Omit<Log, "timestamp" | "category">) {
  29. return this.job.log(log);
  30. }
  31. public getSession() {
  32. return this.session;
  33. }
  34. public setSession(session?: SessionSchema) {
  35. this.session = session;
  36. }
  37. /**
  38. * executeJob - Execute a job
  39. *
  40. * @param moduleName - Module name
  41. * @param jobName - Job name
  42. * @param params - Params
  43. */
  44. public async executeJob<
  45. ModuleNameType extends keyof Jobs & keyof Modules,
  46. JobNameType extends keyof Jobs[ModuleNameType] &
  47. keyof Omit<Modules[ModuleNameType], keyof BaseModule>,
  48. PayloadType extends "payload" extends keyof Jobs[ModuleNameType][JobNameType]
  49. ? Jobs[ModuleNameType][JobNameType]["payload"] extends undefined
  50. ? Record<string, never>
  51. : Jobs[ModuleNameType][JobNameType]["payload"]
  52. : Record<string, never>,
  53. ReturnType = "returns" extends keyof Jobs[ModuleNameType][JobNameType]
  54. ? Jobs[ModuleNameType][JobNameType]["returns"]
  55. : never
  56. >(
  57. moduleName: ModuleNameType,
  58. jobName: JobNameType,
  59. payload: PayloadType,
  60. options?: JobOptions
  61. ): Promise<ReturnType> {
  62. return new Job(jobName.toString(), moduleName, payload, {
  63. session: this.session,
  64. ...(options ?? {})
  65. }).execute();
  66. }
  67. public async getModel(model: keyof Models) {
  68. return this.executeJob("data", "getModel", model);
  69. }
  70. public async getUser(refresh = false) {
  71. if (!this.session?.userId) throw new Error("No user found for session");
  72. if (this.user && !refresh) return this.user;
  73. const User = await this.getModel("user");
  74. this.user = await User.findById(this.session.userId);
  75. if (!this.user) throw new Error("No user found for session");
  76. return this.user;
  77. }
  78. public async assertLoggedIn() {
  79. if (!this.session?.userId) throw new Error("No user found for session");
  80. }
  81. public async assertPermission(
  82. permission: string,
  83. scope?: { stationId?: Types.ObjectId }
  84. ) {
  85. if (!this.session?.userId) throw new Error("Insufficient permissions");
  86. const user = await this.getUser();
  87. const roles: (UserRole | "owner" | "dj")[] = [user.role];
  88. if (scope?.stationId) {
  89. const Station = await this.getModel("station");
  90. const station = await Station.findById(scope.stationId);
  91. if (
  92. station.type === StationType.COMMUNITY &&
  93. station.owner === this.session.userId
  94. )
  95. roles.push("owner");
  96. else if (station.djs.find(dj => dj === this.session?.userId))
  97. roles.push("dj");
  98. }
  99. let hasPermission;
  100. roles.forEach(role => {
  101. if (permissions[role] && permissions[role][permission])
  102. hasPermission = true;
  103. });
  104. if (!hasPermission) throw new Error("Insufficient permissions");
  105. }
  106. }