add team auth to mw routes

This commit is contained in:
Alex Holliday committed 2025-07-21 12:27:53 -07:00
1 parent 87f843df1c
commit d6d3b53846
2 files changed
+13 -14

No files matched your search

@@ -34,10 +34,11 @@ const createMaintenanceWindow = async (maintenanceWindowData) => {
} }
}; };
const getMaintenanceWindowById = async (maintenanceWindowId) => { const getMaintenanceWindowById = async ({ id, teamId }) => {
try { try {
const maintenanceWindow = await MaintenanceWindow.findById({ const maintenanceWindow = await MaintenanceWindow.findOne({
_id: maintenanceWindowId, _id: id,
teamId: teamId,
}); });
return maintenanceWindow; return maintenanceWindow;
} catch (error) { } catch (error) {
@@ -95,10 +96,11 @@ const getMaintenanceWindowsByTeamId = async (teamId, query) => {
* @returns {Promise<Array<MaintenanceWindow>>} An array of MaintenanceWindow documents. * @returns {Promise<Array<MaintenanceWindow>>} An array of MaintenanceWindow documents.
* @throws {Error} If there is an error retrieving the documents. * @throws {Error} If there is an error retrieving the documents.
*/ */
const getMaintenanceWindowsByMonitorId = async (monitorId) => { const getMaintenanceWindowsByMonitorId = async ({ monitorId, teamId }) => {
try { try {
const maintenanceWindows = await MaintenanceWindow.find({ const maintenanceWindows = await MaintenanceWindow.find({
monitorId: monitorId, monitorId: monitorId,
teamId: teamId,
}); });
return maintenanceWindows; return maintenanceWindows;
} catch (error) { } catch (error) {
@@ -116,9 +118,9 @@ const getMaintenanceWindowsByMonitorId = async (monitorId) => {
* @returns {Promise<MaintenanceWindow>} The deleted MaintenanceWindow document. * @returns {Promise<MaintenanceWindow>} The deleted MaintenanceWindow document.
* @throws {Error} If there is an error deleting the document. * @throws {Error} If there is an error deleting the document.
*/ */
const deleteMaintenanceWindowById = async (maintenanceWindowId) => { const deleteMaintenanceWindowById = async ({ id, teamId }) => {
try { try {
const maintenanceWindow = await MaintenanceWindow.findByIdAndDelete(maintenanceWindowId); const maintenanceWindow = await MaintenanceWindow.findOneAndDelete({ _id: id, teamId });
return maintenanceWindow; return maintenanceWindow;
} catch (error) { } catch (error) {
error.service = SERVICE_NAME; error.service = SERVICE_NAME;
@@ -167,9 +169,9 @@ const deleteMaintenanceWindowByUserId = async (userId) => {
} }
}; };
const editMaintenanceWindowById = async (maintenanceWindowId, maintenanceWindowData) => { const editMaintenanceWindowById = async ({ id, body, teamId }) => {
try { try {
const editedMaintenanceWindow = await MaintenanceWindow.findByIdAndUpdate(maintenanceWindowId, maintenanceWindowData, { new: true }); const editedMaintenanceWindow = await MaintenanceWindow.findByIdAndUpdate(id, body, { new: true });
return editedMaintenanceWindow; return editedMaintenanceWindow;
} catch (error) { } catch (error) {
error.service = SERVICE_NAME; error.service = SERVICE_NAME;
+3 -6
View File
@@ -1,7 +1,4 @@
import { Router } from "express"; import { Router } from "express";
import { verifyOwnership } from "../middleware/verifyOwnership.js";
import { verifyTeamAccess } from "../middleware/verifyTeamAccess.js";
import Monitor from "../db/models/Monitor.js";
import MaintenanceWindow from "../db/models/MaintenanceWindow.js"; import MaintenanceWindow from "../db/models/MaintenanceWindow.js";
class MaintenanceWindowRoutes { class MaintenanceWindowRoutes {
constructor(maintenanceWindowController) { constructor(maintenanceWindowController) {
@@ -13,11 +10,11 @@ class MaintenanceWindowRoutes {
this.router.post("/", this.mwController.createMaintenanceWindows); this.router.post("/", this.mwController.createMaintenanceWindows);
this.router.get("/team/", this.mwController.getMaintenanceWindowsByTeamId); this.router.get("/team/", this.mwController.getMaintenanceWindowsByTeamId);
this.router.get("/monitor/:monitorId", verifyOwnership(Monitor, "monitorId"), this.mwController.getMaintenanceWindowsByMonitorId); this.router.get("/monitor/:monitorId", this.mwController.getMaintenanceWindowsByMonitorId);
this.router.get("/:id", this.mwController.getMaintenanceWindowById); this.router.get("/:id", this.mwController.getMaintenanceWindowById);
this.router.put("/:id", verifyTeamAccess(MaintenanceWindow, "id"), this.mwController.editMaintenanceWindow); this.router.put("/:id", this.mwController.editMaintenanceWindow);
this.router.delete("/:id", verifyTeamAccess(MaintenanceWindow, "id"), this.mwController.deleteMaintenanceWindow); this.router.delete("/:id", this.mwController.deleteMaintenanceWindow);
} }
getRouter() { getRouter() {