Index.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { HydratedDocument } from "mongoose";
  2. import { forEachIn } from "@common/utils/forEachIn";
  3. import DataModule from "@/modules/DataModule";
  4. import DataModuleJob from "@/modules/DataModule/DataModuleJob";
  5. import isDj from "@/modules/DataModule/permissions/modelPermissions/isDj";
  6. import isOwner from "@/modules/DataModule/permissions/modelPermissions/isOwner";
  7. import isPublic from "@/modules/DataModule/permissions/modelPermissions/isPublic";
  8. import { StationModel, StationSchema } from "../schema";
  9. export default class Index extends DataModuleJob {
  10. protected static _modelName = "stations";
  11. protected static _hasPermission = true;
  12. protected override async _validate() {
  13. if (
  14. typeof this._payload !== "object" &&
  15. typeof this._payload !== "undefined" &&
  16. this._payload !== null
  17. )
  18. throw new Error("Payload must be an object or undefined");
  19. if (
  20. typeof this._payload?.adminFilter !== "boolean" &&
  21. typeof this._payload?.adminFilter !== "undefined" &&
  22. this._payload?.adminFilter !== null
  23. )
  24. throw new Error("Admin filter must be a boolean or undefined");
  25. }
  26. protected async _execute() {
  27. const model = await DataModule.getModel<StationModel>(
  28. this.getModelName()
  29. );
  30. const data = await model.find();
  31. const user = await this._context.getUser().catch(() => null);
  32. const stations: HydratedDocument<StationSchema>[] = [];
  33. await forEachIn(data, async station => {
  34. if (
  35. isPublic(station) ||
  36. (user && (isOwner(station, user) || isDj(station, user))) ||
  37. (this._payload?.adminFilter &&
  38. (await this._context
  39. .assertPermission("data.stations.index:adminFilter") // TODO fix in new permission system
  40. .then(() => true)
  41. .catch(() => false)))
  42. )
  43. stations.push(station);
  44. });
  45. return stations;
  46. }
  47. }