Add route, controller, and DB methods for fetching maintenance window by ID
This commit is contained in:
1 parent
114cfd6cb7
commit
e3c0944062
10 files changed
+143
-16
No files matched your search
+1
-1
@@ -110,7 +110,7 @@ function App() {
|
|||||||
element={<ProtectedRoute Component={MaintenanceWithAdminProp} />}
|
element={<ProtectedRoute Component={MaintenanceWithAdminProp} />}
|
||||||
/>
|
/>
|
||||||
<Route
|
<Route
|
||||||
path="/maintenance/create"
|
path="/maintenance/create/:maintenanceWindowId?"
|
||||||
element={<CreateNewMaintenanceWindow />}
|
element={<CreateNewMaintenanceWindow />}
|
||||||
/>
|
/>
|
||||||
<Route
|
<Route
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { LocalizationProvider } from "@mui/x-date-pickers";
|
|||||||
import { DatePicker } from "@mui/x-date-pickers/DatePicker";
|
import { DatePicker } from "@mui/x-date-pickers/DatePicker";
|
||||||
import { MobileTimePicker } from "@mui/x-date-pickers/MobileTimePicker";
|
import { MobileTimePicker } from "@mui/x-date-pickers/MobileTimePicker";
|
||||||
import { maintenanceWindowValidation } from "../../../Validation/validation";
|
import { maintenanceWindowValidation } from "../../../Validation/validation";
|
||||||
|
import { createToast } from "../../../Utils/toastUtils";
|
||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
import Select from "../../../Components/Inputs/Select";
|
import Select from "../../../Components/Inputs/Select";
|
||||||
import Field from "../../../Components/Inputs/Field";
|
import Field from "../../../Components/Inputs/Field";
|
||||||
@@ -16,14 +17,14 @@ import CalendarIcon from "../../../assets/icons/calendar.svg?react";
|
|||||||
import "./index.css";
|
import "./index.css";
|
||||||
import Search from "../../../Components/Inputs/Search";
|
import Search from "../../../Components/Inputs/Search";
|
||||||
import { networkService } from "../../../main";
|
import { networkService } from "../../../main";
|
||||||
|
import { logger } from "../../../Utils/Logger";
|
||||||
import {
|
import {
|
||||||
MS_PER_SECOND,
|
MS_PER_SECOND,
|
||||||
MS_PER_MINUTE,
|
MS_PER_MINUTE,
|
||||||
MS_PER_HOUR,
|
MS_PER_HOUR,
|
||||||
MS_PER_DAY,
|
MS_PER_DAY,
|
||||||
} from "../../../Utils/timeUtils";
|
} from "../../../Utils/timeUtils";
|
||||||
import { createToast } from "../../../Utils/toastUtils";
|
import { useNavigate, useParams } from "react-router-dom";
|
||||||
import { useNavigate } from "react-router-dom";
|
|
||||||
|
|
||||||
const MS_LOOKUP = {
|
const MS_LOOKUP = {
|
||||||
seconds: MS_PER_SECOND,
|
seconds: MS_PER_SECOND,
|
||||||
@@ -75,11 +76,13 @@ const getIdByValue = (config, name) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const CreateMaintenance = () => {
|
const CreateMaintenance = () => {
|
||||||
|
const { maintenanceWindowId } = useParams();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const { user, authToken } = useSelector((state) => state.auth);
|
const { user, authToken } = useSelector((state) => state.auth);
|
||||||
const [monitors, setMonitors] = useState([]);
|
const [monitors, setMonitors] = useState([]);
|
||||||
const [search, setSearch] = useState("");
|
const [search, setSearch] = useState("");
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const [form, setForm] = useState({
|
const [form, setForm] = useState({
|
||||||
repeat: "none",
|
repeat: "none",
|
||||||
startDate: dayjs(),
|
startDate: dayjs(),
|
||||||
@@ -93,13 +96,48 @@ const CreateMaintenance = () => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchMonitors = async () => {
|
const fetchMonitors = async () => {
|
||||||
const response = await networkService.getMonitorsByTeamId({
|
setIsLoading(true);
|
||||||
authToken: authToken,
|
try {
|
||||||
teamId: user.teamId,
|
const response = await networkService.getMonitorsByTeamId({
|
||||||
limit: -1,
|
authToken: authToken,
|
||||||
types: ["http", "ping", "pagespeed"],
|
teamId: user.teamId,
|
||||||
});
|
limit: -1,
|
||||||
setMonitors(response.data.data.monitors);
|
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();
|
fetchMonitors();
|
||||||
}, [authToken, user]);
|
}, [authToken, user]);
|
||||||
@@ -190,7 +228,6 @@ const CreateMaintenance = () => {
|
|||||||
repeat,
|
repeat,
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log(submit);
|
|
||||||
if (repeat === 0) {
|
if (repeat === 0) {
|
||||||
submit.expiry = end;
|
submit.expiry = end;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,25 +14,34 @@ class Logger {
|
|||||||
const NO_OP = () => {};
|
const NO_OP = () => {};
|
||||||
|
|
||||||
if (logLevel === "none") {
|
if (logLevel === "none") {
|
||||||
|
this.info = NO_OP;
|
||||||
this.error = NO_OP;
|
this.error = NO_OP;
|
||||||
this.warn = NO_OP;
|
this.warn = NO_OP;
|
||||||
this.log = NO_OP;
|
this.log = NO_OP;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.error = console.error.bind(console);
|
|
||||||
|
|
||||||
if (logLevel === "error") {
|
if (logLevel === "error") {
|
||||||
|
this.info = NO_OP;
|
||||||
this.warn = NO_OP;
|
this.warn = NO_OP;
|
||||||
this.log = NO_OP;
|
this.log = NO_OP;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.warn = console.warn.bind(console);
|
|
||||||
|
|
||||||
if (logLevel === "warn") {
|
if (logLevel === "warn") {
|
||||||
|
this.info = NO_OP;
|
||||||
this.log = NO_OP;
|
this.log = NO_OP;
|
||||||
return;
|
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);
|
this.log = console.log.bind(console);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
* Get maintenance windows by teamId
|
||||||
@@ -735,7 +762,6 @@ class NetworkService {
|
|||||||
|
|
||||||
async getMaintenanceWindowsByTeamId(config) {
|
async getMaintenanceWindowsByTeamId(config) {
|
||||||
const { authToken, active, page, rowsPerPage, field, order } = config;
|
const { authToken, active, page, rowsPerPage, field, order } = config;
|
||||||
|
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
|
|
||||||
if (active) params.append("status", active);
|
if (active) params.append("status", active);
|
||||||
@@ -756,7 +782,7 @@ class NetworkService {
|
|||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* ************************************
|
* ************************************
|
||||||
* Get maintenance windows by teamId
|
* Delete maintenance window by id
|
||||||
* ************************************
|
* ************************************
|
||||||
*
|
*
|
||||||
* @async
|
* @async
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
const {
|
const {
|
||||||
createMaintenanceWindowBodyValidation,
|
createMaintenanceWindowBodyValidation,
|
||||||
|
getMaintenanceWindowByIdParamValidation,
|
||||||
getMaintenanceWindowsByMonitorIdParamValidation,
|
getMaintenanceWindowsByMonitorIdParamValidation,
|
||||||
getMaintenanceWindowsByTeamIdQueryValidation,
|
getMaintenanceWindowsByTeamIdQueryValidation,
|
||||||
deleteMaintenanceWindowByIdParamValidation,
|
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) => {
|
const getMaintenanceWindowsByTeamId = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
await getMaintenanceWindowsByTeamIdQueryValidation.validateAsync(req.query);
|
await getMaintenanceWindowsByTeamIdQueryValidation.validateAsync(req.query);
|
||||||
@@ -171,6 +201,7 @@ const editMaintenanceWindow = async (req, res, next) => {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
createMaintenanceWindows,
|
createMaintenanceWindows,
|
||||||
|
getMaintenanceWindowById,
|
||||||
getMaintenanceWindowsByTeamId,
|
getMaintenanceWindowsByTeamId,
|
||||||
getMaintenanceWindowsByMonitorId,
|
getMaintenanceWindowsByMonitorId,
|
||||||
deleteMaintenanceWindow,
|
deleteMaintenanceWindow,
|
||||||
|
|||||||
@@ -118,6 +118,7 @@ const {
|
|||||||
//****************************************
|
//****************************************
|
||||||
const {
|
const {
|
||||||
createMaintenanceWindow,
|
createMaintenanceWindow,
|
||||||
|
getMaintenanceWindowById,
|
||||||
getMaintenanceWindowsByTeamId,
|
getMaintenanceWindowsByTeamId,
|
||||||
getMaintenanceWindowsByMonitorId,
|
getMaintenanceWindowsByMonitorId,
|
||||||
deleteMaintenanceWindowById,
|
deleteMaintenanceWindowById,
|
||||||
@@ -183,6 +184,7 @@ module.exports = {
|
|||||||
deletePageSpeedChecksByMonitorId,
|
deletePageSpeedChecksByMonitorId,
|
||||||
createMaintenanceWindow,
|
createMaintenanceWindow,
|
||||||
getMaintenanceWindowsByTeamId,
|
getMaintenanceWindowsByTeamId,
|
||||||
|
getMaintenanceWindowById,
|
||||||
getMaintenanceWindowsByMonitorId,
|
getMaintenanceWindowsByMonitorId,
|
||||||
deleteMaintenanceWindowById,
|
deleteMaintenanceWindowById,
|
||||||
deleteMaintenanceWindowByMonitorId,
|
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.
|
* Asynchronously retrieves all MaintenanceWindow documents associated with a specific team ID.
|
||||||
* @async
|
* @async
|
||||||
@@ -202,6 +215,7 @@ const editMaintenanceWindowById = async (
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
createMaintenanceWindow,
|
createMaintenanceWindow,
|
||||||
|
getMaintenanceWindowById,
|
||||||
getMaintenanceWindowsByTeamId,
|
getMaintenanceWindowsByTeamId,
|
||||||
getMaintenanceWindowsByMonitorId,
|
getMaintenanceWindowsByMonitorId,
|
||||||
deleteMaintenanceWindowById,
|
deleteMaintenanceWindowById,
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ router.get(
|
|||||||
|
|
||||||
router.get("/team/", maintenanceWindowController.getMaintenanceWindowsByTeamId);
|
router.get("/team/", maintenanceWindowController.getMaintenanceWindowsByTeamId);
|
||||||
|
|
||||||
|
router.get("/:id", maintenanceWindowController.getMaintenanceWindowById);
|
||||||
|
|
||||||
router.put("/:id", maintenanceWindowController.editMaintenanceWindow);
|
router.put("/:id", maintenanceWindowController.editMaintenanceWindow);
|
||||||
|
|
||||||
router.delete("/:id", maintenanceWindowController.deleteMaintenanceWindow);
|
router.delete("/:id", maintenanceWindowController.deleteMaintenanceWindow);
|
||||||
|
|||||||
@@ -93,6 +93,7 @@ const successMessages = {
|
|||||||
JOB_QUEUE_RESUME_JOB: "Job resumed successfully",
|
JOB_QUEUE_RESUME_JOB: "Job resumed successfully",
|
||||||
|
|
||||||
//Maintenance Window Controller
|
//Maintenance Window Controller
|
||||||
|
MAINTENANCE_WINDOW_GET_BY_ID: "Got Maintenance Window by Id successfully",
|
||||||
MAINTENANCE_WINDOW_CREATE: "Maintenance Window created successfully",
|
MAINTENANCE_WINDOW_CREATE: "Maintenance Window created successfully",
|
||||||
MAINTENANCE_WINDOW_GET_BY_TEAM:
|
MAINTENANCE_WINDOW_GET_BY_TEAM:
|
||||||
"Got Maintenance Windows by Team successfully",
|
"Got Maintenance Windows by Team successfully",
|
||||||
|
|||||||
@@ -374,6 +374,10 @@ const createMaintenanceWindowBodyValidation = joi.object({
|
|||||||
expiry: joi.date(),
|
expiry: joi.date(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const getMaintenanceWindowByIdParamValidation = joi.object({
|
||||||
|
id: joi.string().required(),
|
||||||
|
});
|
||||||
|
|
||||||
const getMaintenanceWindowsByTeamIdQueryValidation = joi.object({
|
const getMaintenanceWindowsByTeamIdQueryValidation = joi.object({
|
||||||
active: joi.boolean(),
|
active: joi.boolean(),
|
||||||
page: joi.number(),
|
page: joi.number(),
|
||||||
@@ -469,6 +473,7 @@ module.exports = {
|
|||||||
deletePageSpeedCheckParamValidation,
|
deletePageSpeedCheckParamValidation,
|
||||||
createPageSpeedCheckBodyValidation,
|
createPageSpeedCheckBodyValidation,
|
||||||
createMaintenanceWindowBodyValidation,
|
createMaintenanceWindowBodyValidation,
|
||||||
|
getMaintenanceWindowByIdParamValidation,
|
||||||
getMaintenanceWindowsByTeamIdQueryValidation,
|
getMaintenanceWindowsByTeamIdQueryValidation,
|
||||||
getMaintenanceWindowsByMonitorIdParamValidation,
|
getMaintenanceWindowsByMonitorIdParamValidation,
|
||||||
deleteMaintenanceWindowByIdParamValidation,
|
deleteMaintenanceWindowByIdParamValidation,
|
||||||
|
|||||||
Reference in new issue
Block a user