oneuptime/Common/Server/API/UserOnCallLogTimelineAPI.ts

165 lines
4.8 KiB
TypeScript
Raw Normal View History

import DatabaseConfig from "../DatabaseConfig";
import NotificationMiddleware from "../Middleware/NotificationMiddleware";
2023-07-21 13:15:33 +00:00
import UserOnCallLogTimelineService, {
Service as UserNotificationLogTimelineServiceType,
} from "../Services/UserOnCallLogTimelineService";
2023-07-03 21:15:17 +00:00
import {
ExpressRequest,
ExpressResponse,
OneUptimeRequest,
} from "../Utils/Express";
import Response from "../Utils/Response";
import BaseAPI from "./BaseAPI";
import { DashboardRoute } from "Common/ServiceRoute";
import Hostname from "Common/Types/API/Hostname";
import Protocol from "Common/Types/API/Protocol";
import URL from "Common/Types/API/URL";
import OneUptimeDate from "Common/Types/Date";
import BadDataException from "Common/Types/Exception/BadDataException";
import { JSONObject } from "Common/Types/JSON";
import ObjectID from "Common/Types/ObjectID";
import UserNotificationStatus from "Common/Types/UserNotification/UserNotificationStatus";
import UserOnCallLogTimeline from "Common/Models/DatabaseModels/UserOnCallLogTimeline";
2023-07-03 21:15:17 +00:00
export default class UserNotificationLogTimelineAPI extends BaseAPI<
UserOnCallLogTimeline,
UserNotificationLogTimelineServiceType
2023-07-03 21:15:17 +00:00
> {
public constructor() {
super(UserOnCallLogTimeline, UserOnCallLogTimelineService);
this.router.post(
`${new this.entityType()
.getCrudApiPath()
?.toString()}/call/gather-input/:itemId`,
NotificationMiddleware.isValidCallNotificationRequest,
async (req: ExpressRequest, res: ExpressResponse) => {
req = req as OneUptimeRequest;
if (!req.params["itemId"]) {
return Response.sendErrorResponse(
req,
res,
new BadDataException("Invalid item ID"),
);
}
const token: JSONObject = (req as any).callTokenData;
const itemId: ObjectID = new ObjectID(req.params["itemId"]);
const timelineItem: UserOnCallLogTimeline | null =
await this.service.findOneById({
id: itemId,
select: {
_id: true,
projectId: true,
triggeredByIncidentId: true,
},
props: {
isRoot: true,
},
});
if (!timelineItem) {
return Response.sendErrorResponse(
req,
res,
new BadDataException("Invalid item Id"),
);
}
// check digits.
if (req.body["Digits"] === "1") {
// then ack incident
await this.service.updateOneById({
id: itemId,
data: {
acknowledgedAt: OneUptimeDate.getCurrentDate(),
isAcknowledged: true,
status: UserNotificationStatus.Acknowledged,
statusMessage: "Notification Acknowledged",
},
props: {
isRoot: true,
},
});
}
return NotificationMiddleware.sendResponse(req, res, token as any);
},
);
this.router.get(
`${new this.entityType()
.getCrudApiPath()
?.toString()}/acknowledge/:itemId`,
async (req: ExpressRequest, res: ExpressResponse) => {
req = req as OneUptimeRequest;
if (!req.params["itemId"]) {
return Response.sendErrorResponse(
req,
res,
new BadDataException("Item ID is required"),
);
}
const itemId: ObjectID = new ObjectID(req.params["itemId"]);
const timelineItem: UserOnCallLogTimeline | null =
await this.service.findOneById({
id: itemId,
select: {
_id: true,
projectId: true,
triggeredByIncidentId: true,
},
props: {
isRoot: true,
},
});
if (!timelineItem) {
return Response.sendErrorResponse(
req,
res,
new BadDataException("Invalid item Id"),
);
}
await this.service.updateOneById({
id: itemId,
data: {
acknowledgedAt: OneUptimeDate.getCurrentDate(),
isAcknowledged: true,
status: UserNotificationStatus.Acknowledged,
statusMessage: "Notification Acknowledged",
},
props: {
isRoot: true,
},
});
// redirect to dashboard to incidents page.
const host: Hostname = await DatabaseConfig.getHost();
const httpProtocol: Protocol = await DatabaseConfig.getHttpProtocol();
return Response.redirect(
req,
res,
new URL(
httpProtocol,
host,
DashboardRoute.addRoute(
`/${timelineItem.projectId?.toString()}/incidents/${timelineItem.triggeredByIncidentId!.toString()}`,
),
),
2023-07-03 21:15:17 +00:00
);
},
);
}
2023-07-03 21:15:17 +00:00
}