Add route, controller, and DB methods for fetching maintenance window by ID

This commit is contained in:
Alex Holliday committed 2024-10-09 12:06:01 +08:00
1 parent 114cfd6cb7
commit e3c0944062
10 files changed
+143 -16

No files matched your search

+1 -1
View File
@@ -110,7 +110,7 @@ function App() {
element={<ProtectedRoute Component={MaintenanceWithAdminProp} />}
/>
<Route
path="/maintenance/create"
path="/maintenance/create/:maintenanceWindowId?"
element={<CreateNewMaintenanceWindow />}
/>
<Route
@@ -8,6 +8,7 @@ import { LocalizationProvider } from "@mui/x-date-pickers";
import { DatePicker } from "@mui/x-date-pickers/DatePicker";
import { MobileTimePicker } from "@mui/x-date-pickers/MobileTimePicker";
import { maintenanceWindowValidation } from "../../../Validation/validation";
import { createToast } from "../../../Utils/toastUtils";
import dayjs from "dayjs";
import Select from "../../../Components/Inputs/Select";
import Field from "../../../Components/Inputs/Field";
@@ -16,14 +17,14 @@ import CalendarIcon from "../../../assets/icons/calendar.svg?react";
import "./index.css";
import Search from "../../../Components/Inputs/Search";
import { networkService } from "../../../main";
import { logger } from "../../../Utils/Logger";
import {
MS_PER_SECOND,
MS_PER_MINUTE,
MS_PER_HOUR,
MS_PER_DAY,
} from "../../../Utils/timeUtils";
import { createToast } from "../../../Utils/toastUtils";
import { useNavigate } from "react-router-dom";
import { useNavigate, useParams } from "react-router-dom";
const MS_LOOKUP = {
seconds: MS_PER_SECOND,
@@ -75,11 +76,13 @@ const getIdByValue = (config, name) => {
};
const CreateMaintenance = () => {
const { maintenanceWindowId } = useParams();
const navigate = useNavigate();
const theme = useTheme();
const { user, authToken } = useSelector((state) => state.auth);
const [monitors, setMonitors] = useState([]);
const [search, setSearch] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [form, setForm] = useState({
repeat: "none",
startDate: dayjs(),
@@ -93,13 +96,48 @@ const CreateMaintenance = () => {
useEffect(() => {
const fetchMonitors = async () => {
const response = await networkService.getMonitorsByTeamId({
authToken: authToken,
teamId: user.teamId,
limit: -1,
types: ["http", "ping", "pagespeed"],
});
setMonitors(response.data.data.monitors);
setIsLoading(true);
try {
const response = await networkService.getMonitorsByTeamId({
authToken: authToken,
teamId: user.teamId,
limit: -1,
types: ["http", "ping", "pagespeed"],
});
setMonitors(response.data.data.monitors);
if (maintenanceWindowId === undefined) {
return;
}
const maintenanceWindow = await networkService.getMaintenanceWindowById(
{
authToken: authToken,
maintenanceWindowId: maintenanceWindowId,
}
);
logger.info(maintenanceWindow);
// const { name, start, end, repeat, monitors } =
// maintenanceWindow.data.data.maintenanceWindow;
// const startDate = dayjs(start);
// const startTime = dayjs(start);
// const duration = dayjs(end).diff(dayjs(start), "seconds");
// const durationUnit = "seconds";
// setForm({
// name,
// startDate,
// startTime,
// duration,
// durationUnit,
// repeat,
// monitors,
// });
} catch (error) {
createToast({ body: "Failed to fetch data" });
logger.error("Failed to fetch monitors", error);
} finally {
setIsLoading(false);
}
};
fetchMonitors();
}, [authToken, user]);
@@ -190,7 +228,6 @@ const CreateMaintenance = () => {
repeat,
};
console.log(submit);
if (repeat === 0) {
submit.expiry = end;
}
+12 -3
View File
@@ -14,25 +14,34 @@ class Logger {
const NO_OP = () => {};
if (logLevel === "none") {
this.info = NO_OP;
this.error = NO_OP;
this.warn = NO_OP;
this.log = NO_OP;
return;
}
this.error = console.error.bind(console);
if (logLevel === "error") {
this.info = NO_OP;
this.warn = NO_OP;
this.log = NO_OP;
return;
}
this.warn = console.warn.bind(console);
if (logLevel === "warn") {
this.info = NO_OP;
this.log = NO_OP;
return;
}
if (logLevel === "info") {
this.log = NO_OP;
return;
}
this.error = console.error.bind(console);
this.warn = console.warn.bind(console);
this.info = console.info.bind(console);
this.log = console.log.bind(console);
}
+28 -2
View File
@@ -716,6 +716,33 @@ class NetworkService {
}
);
}
/**
* ************************************
* Get maintenance window by id
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @param {string} [config.maintenanceWindowId] - The id of the maintenance window to delete.
* @returns {Promise<AxiosResponse>} The response from the axios POST request.
*
*/
async getMaintenanceWindowById(config) {
const { authToken, maintenanceWindowId } = config;
return this.axiosInstance.get(
`/maintenance-window/${maintenanceWindowId}`,
{
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
}
);
}
/**
* ************************************
* Get maintenance windows by teamId
@@ -735,7 +762,6 @@ class NetworkService {
async getMaintenanceWindowsByTeamId(config) {
const { authToken, active, page, rowsPerPage, field, order } = config;
const params = new URLSearchParams();
if (active) params.append("status", active);
@@ -756,7 +782,7 @@ class NetworkService {
}
/**
* ************************************
* Get maintenance windows by teamId
* Delete maintenance window by id
* ************************************
*
* @async
@@ -1,5 +1,6 @@
const {
createMaintenanceWindowBodyValidation,
getMaintenanceWindowByIdParamValidation,
getMaintenanceWindowsByMonitorIdParamValidation,
getMaintenanceWindowsByTeamIdQueryValidation,
deleteMaintenanceWindowByIdParamValidation,
@@ -50,6 +51,35 @@ const createMaintenanceWindows = async (req, res, next) => {
}
};
const getMaintenanceWindowById = async (req, res, next) => {
try {
await getMaintenanceWindowByIdParamValidation.validateAsync(req.params);
} catch (error) {
error.status = 422;
error.service = SERVICE_NAME;
error.message =
error.details?.[0]?.message || error.message || "Validation Error";
next(error);
return;
}
try {
const maintenanceWindow = await req.db.getMaintenanceWindowById(
req.params.id
);
return res.status(200).json({
success: true,
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_ID,
data: maintenanceWindow,
});
} catch (error) {
error.service === undefined ? (error.service = SERVICE_NAME) : null;
error.method === undefined
? (error.method = "getMaintenanceWindowById")
: null;
next(error);
}
};
const getMaintenanceWindowsByTeamId = async (req, res, next) => {
try {
await getMaintenanceWindowsByTeamIdQueryValidation.validateAsync(req.query);
@@ -171,6 +201,7 @@ const editMaintenanceWindow = async (req, res, next) => {
module.exports = {
createMaintenanceWindows,
getMaintenanceWindowById,
getMaintenanceWindowsByTeamId,
getMaintenanceWindowsByMonitorId,
deleteMaintenanceWindow,
+2
View File
@@ -118,6 +118,7 @@ const {
//****************************************
const {
createMaintenanceWindow,
getMaintenanceWindowById,
getMaintenanceWindowsByTeamId,
getMaintenanceWindowsByMonitorId,
deleteMaintenanceWindowById,
@@ -183,6 +184,7 @@ module.exports = {
deletePageSpeedChecksByMonitorId,
createMaintenanceWindow,
getMaintenanceWindowsByTeamId,
getMaintenanceWindowById,
getMaintenanceWindowsByMonitorId,
deleteMaintenanceWindowById,
deleteMaintenanceWindowByMonitorId,
@@ -45,6 +45,19 @@ const createMaintenanceWindow = async (maintenanceWindowData) => {
}
};
const getMaintenanceWindowById = async (maintenanceWindowId) => {
try {
const maintenanceWindow = await MaintenanceWindow.findById({
_id: maintenanceWindowId,
});
return maintenanceWindow;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "getMaintenanceWindowById";
throw error;
}
};
/**
* Asynchronously retrieves all MaintenanceWindow documents associated with a specific team ID.
* @async
@@ -202,6 +215,7 @@ const editMaintenanceWindowById = async (
module.exports = {
createMaintenanceWindow,
getMaintenanceWindowById,
getMaintenanceWindowsByTeamId,
getMaintenanceWindowsByMonitorId,
deleteMaintenanceWindowById,
+2
View File
@@ -13,6 +13,8 @@ router.get(
router.get("/team/", maintenanceWindowController.getMaintenanceWindowsByTeamId);
router.get("/:id", maintenanceWindowController.getMaintenanceWindowById);
router.put("/:id", maintenanceWindowController.editMaintenanceWindow);
router.delete("/:id", maintenanceWindowController.deleteMaintenanceWindow);
+1
View File
@@ -93,6 +93,7 @@ const successMessages = {
JOB_QUEUE_RESUME_JOB: "Job resumed successfully",
//Maintenance Window Controller
MAINTENANCE_WINDOW_GET_BY_ID: "Got Maintenance Window by Id successfully",
MAINTENANCE_WINDOW_CREATE: "Maintenance Window created successfully",
MAINTENANCE_WINDOW_GET_BY_TEAM:
"Got Maintenance Windows by Team successfully",
+5
View File
@@ -374,6 +374,10 @@ const createMaintenanceWindowBodyValidation = joi.object({
expiry: joi.date(),
});
const getMaintenanceWindowByIdParamValidation = joi.object({
id: joi.string().required(),
});
const getMaintenanceWindowsByTeamIdQueryValidation = joi.object({
active: joi.boolean(),
page: joi.number(),
@@ -469,6 +473,7 @@ module.exports = {
deletePageSpeedCheckParamValidation,
createPageSpeedCheckBodyValidation,
createMaintenanceWindowBodyValidation,
getMaintenanceWindowByIdParamValidation,
getMaintenanceWindowsByTeamIdQueryValidation,
getMaintenanceWindowsByMonitorIdParamValidation,
deleteMaintenanceWindowByIdParamValidation,