details
This commit is contained in:
1 parent
0453b790af
commit
b4135e26ce
7 files changed
+171
-11
No files matched your search
@@ -10,6 +10,7 @@ const logger = getChildLogger("IncidentController");
|
||||
|
||||
export interface IIncidentsController {
|
||||
getIncidents: (req: Request, res: Response, next: NextFunction) => void;
|
||||
getIncidentById: (req: Request, res: Response, next: NextFunction) => void;
|
||||
resolveIncident: (req: Request, res: Response, next: NextFunction) => void;
|
||||
}
|
||||
|
||||
@@ -58,6 +59,34 @@ class IncidentsController implements IIncidentsController {
|
||||
}
|
||||
};
|
||||
|
||||
getIncidentById = async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const userContext = req.user;
|
||||
if (!userContext) {
|
||||
return res.status(401).json({ message: "Unauthorized" });
|
||||
}
|
||||
|
||||
const teamId = userContext.currentTeamId;
|
||||
if (!teamId) {
|
||||
throw new ApiError("No team ID", 400);
|
||||
}
|
||||
|
||||
const { id } = req.params;
|
||||
if (!id) {
|
||||
throw new ApiError("No incident ID", 400);
|
||||
}
|
||||
|
||||
const incident = await this.incidentService.get(teamId, id);
|
||||
if (!incident) {
|
||||
throw new ApiError("Incident not found", 404);
|
||||
}
|
||||
|
||||
res.status(200).json({ message: "OK", data: incident });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
resolveIncident = async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const userContext = req.user;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Router } from "express";
|
||||
|
||||
import { ChecksController, IncidentsController } from "@/controllers/index.js";
|
||||
import { IncidentsController } from "@/controllers/index.js";
|
||||
import { verifyToken } from "@/middleware/VerifyToken.js";
|
||||
import { addUserContext } from "@/middleware/AddUserContext.js";
|
||||
import { validateQuery, validateBody } from "@/middleware/validation.js";
|
||||
@@ -8,6 +8,7 @@ import { verifyOrgPermission } from "@/middleware/VerifyPermission.js";
|
||||
import { PERMISSIONS } from "@/types/permissions.js";
|
||||
import {
|
||||
getIncidentsQuerySchema,
|
||||
getIncidentQuerySchema,
|
||||
patchIncidentsBodySchema,
|
||||
} from "@/validation/index.js";
|
||||
|
||||
@@ -29,6 +30,13 @@ class IncidentsRoutes {
|
||||
validateQuery(getIncidentsQuerySchema),
|
||||
this.controller.getIncidents
|
||||
);
|
||||
this.router.get(
|
||||
"/:id",
|
||||
verifyToken,
|
||||
addUserContext,
|
||||
verifyOrgPermission([PERMISSIONS.incidents.read]),
|
||||
this.controller.getIncidentById
|
||||
);
|
||||
|
||||
this.router.patch(
|
||||
"/:id/resolve",
|
||||
|
||||
@@ -26,10 +26,7 @@ export interface IIncidentService {
|
||||
monitorId: mongoose.Types.ObjectId,
|
||||
startCheckId: mongoose.Types.ObjectId
|
||||
) => Promise<IIncident>;
|
||||
get: (
|
||||
teamId: mongoose.Types.ObjectId,
|
||||
incidentId: mongoose.Types.ObjectId
|
||||
) => Promise<IIncident | null>;
|
||||
get: (teamId: string, incidentId: string) => Promise<IIncident | null>;
|
||||
getAll: (
|
||||
teamId: string,
|
||||
monitorId: string,
|
||||
@@ -191,10 +188,7 @@ class IncidentService implements IIncidentService {
|
||||
return incident;
|
||||
};
|
||||
|
||||
get = async (
|
||||
teamId: mongoose.Types.ObjectId,
|
||||
incidentId: mongoose.Types.ObjectId
|
||||
) => {
|
||||
get = async (teamId: string, incidentId: string) => {
|
||||
const incident = await Incident.findOne({
|
||||
_id: new mongoose.Types.ObjectId(incidentId),
|
||||
teamId: new mongoose.Types.ObjectId(teamId),
|
||||
|
||||
@@ -517,6 +517,12 @@ export const profileSchema = z
|
||||
}
|
||||
);
|
||||
|
||||
export const getIncidentQuerySchema = z.object({
|
||||
id: z.string().refine((val) => mongoose.Types.ObjectId.isValid(val), {
|
||||
message: "Invalid MongoDB ObjectId",
|
||||
}),
|
||||
});
|
||||
|
||||
export const getIncidentsQuerySchema = z.object({
|
||||
resolutionType: z.enum(ResolutionTypes).optional(),
|
||||
resolved: z
|
||||
|
||||
Reference in new issue
Block a user