8 files changed
+162
-43
No files matched your search
@@ -67,7 +67,7 @@ export const update = createAsyncThunk(
|
|||||||
fd.append("deleteProfileImage", form.deleteProfileImage);
|
fd.append("deleteProfileImage", form.deleteProfileImage);
|
||||||
|
|
||||||
const res = await networkService.updateUser(token, user._id, fd);
|
const res = await networkService.updateUser(token, user._id, fd);
|
||||||
|
console.log(res);
|
||||||
return res.data;
|
return res.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.response && error.response.data) {
|
if (error.response && error.response.data) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
import { Box, Stack, styled, Typography } from "@mui/material";
|
import { Box, Stack, Typography } from "@mui/material";
|
||||||
import Field from "../../Components/Inputs/Field";
|
import Field from "../../Components/Inputs/Field";
|
||||||
import Link from "../../Components/Link";
|
import Link from "../../Components/Link";
|
||||||
import Select from "../../Components/Inputs/Select";
|
import Select from "../../Components/Inputs/Select";
|
||||||
@@ -12,20 +12,92 @@ import {
|
|||||||
addDemoMonitors,
|
addDemoMonitors,
|
||||||
deleteAllMonitors,
|
deleteAllMonitors,
|
||||||
} from "../../Features/UptimeMonitors/uptimeMonitorsSlice";
|
} from "../../Features/UptimeMonitors/uptimeMonitorsSlice";
|
||||||
|
import { update } from "../../Features/Auth/authSlice";
|
||||||
import PropTypes from "prop-types";
|
import PropTypes from "prop-types";
|
||||||
import LoadingButton from "@mui/lab/LoadingButton";
|
import LoadingButton from "@mui/lab/LoadingButton";
|
||||||
import { setTimezone } from "../../Features/UI/uiSlice";
|
import { setTimezone } from "../../Features/UI/uiSlice";
|
||||||
import timezones from "../../Utils/timezones.json";
|
import timezones from "../../Utils/timezones.json";
|
||||||
|
import { useState } from "react";
|
||||||
|
import { ConfigBox } from "./styled";
|
||||||
|
import { networkService } from "../../main";
|
||||||
|
import { settingsValidation } from "../../Validation/validation";
|
||||||
|
|
||||||
|
const SECONDS_PER_DAY = 86400;
|
||||||
|
|
||||||
const Settings = ({ isAdmin }) => {
|
const Settings = ({ isAdmin }) => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const { user, authToken } = useSelector((state) => state.auth);
|
const { user, authToken } = useSelector((state) => state.auth);
|
||||||
|
const { checkTTL } = user;
|
||||||
const { isLoading } = useSelector((state) => state.uptimeMonitors);
|
const { isLoading } = useSelector((state) => state.uptimeMonitors);
|
||||||
|
const { isLoading: authIsLoading } = useSelector((state) => state.auth);
|
||||||
const { timezone } = useSelector((state) => state.ui);
|
const { timezone } = useSelector((state) => state.ui);
|
||||||
|
const [checksIsLoading, setChecksIsLoading] = useState(false);
|
||||||
|
const [form, setForm] = useState({
|
||||||
|
ttl: (checkTTL / SECONDS_PER_DAY).toString(),
|
||||||
|
});
|
||||||
|
const [errors, setErrors] = useState({});
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
|
|
||||||
|
const handleChange = (event) => {
|
||||||
|
const { value, id } = event.target;
|
||||||
|
const { error } = settingsValidation.validate(
|
||||||
|
{ [id]: value },
|
||||||
|
{
|
||||||
|
abortEarly: false,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
if (!error || error.details.length === 0) {
|
||||||
|
setErrors({});
|
||||||
|
} else {
|
||||||
|
const newErrors = {};
|
||||||
|
error.details.forEach((err) => {
|
||||||
|
newErrors[err.path[0]] = err.message;
|
||||||
|
});
|
||||||
|
setErrors(newErrors);
|
||||||
|
console.log(newErrors);
|
||||||
|
logger.error("Validation errors:", error.details);
|
||||||
|
}
|
||||||
|
let inputValue = value;
|
||||||
|
id === "ttl" && (inputValue = value.replace(/[^0-9]/g, ""));
|
||||||
|
setForm((prev) => ({
|
||||||
|
...prev,
|
||||||
|
[id]: inputValue,
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
// TODO Handle saving
|
// TODO Handle saving
|
||||||
const handleSave = async () => {};
|
const handleSave = async () => {
|
||||||
|
try {
|
||||||
|
setChecksIsLoading(true);
|
||||||
|
await networkService.updateChecksTTL(authToken, form.ttl);
|
||||||
|
const updatedUser = { ...user, checkTTL: form.ttl };
|
||||||
|
const action = await dispatch(
|
||||||
|
update({ authToken, localData: updatedUser })
|
||||||
|
);
|
||||||
|
if (action.payload.success) {
|
||||||
|
createToast({
|
||||||
|
body: "Settings saved successfully",
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
if (action.payload) {
|
||||||
|
// dispatch errors
|
||||||
|
createToast({
|
||||||
|
body: action.payload.msg,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// unknown errors
|
||||||
|
createToast({
|
||||||
|
body: "Unknown error.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
createToast({ body: "Failed to save settings" });
|
||||||
|
} finally {
|
||||||
|
setChecksIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const handleClearStats = async () => {
|
const handleClearStats = async () => {
|
||||||
try {
|
try {
|
||||||
@@ -72,33 +144,6 @@ const Settings = ({ isAdmin }) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const ConfigBox = styled("div")({
|
|
||||||
display: "flex",
|
|
||||||
flexDirection: "row",
|
|
||||||
justifyContent: "space-between",
|
|
||||||
gap: theme.spacing(20),
|
|
||||||
paddingTop: theme.spacing(12),
|
|
||||||
paddingInline: theme.spacing(15),
|
|
||||||
paddingBottom: theme.spacing(25),
|
|
||||||
backgroundColor: theme.palette.background.main,
|
|
||||||
border: 1,
|
|
||||||
borderStyle: "solid",
|
|
||||||
borderColor: theme.palette.border.light,
|
|
||||||
borderRadius: theme.spacing(2),
|
|
||||||
"& > div:first-of-type": {
|
|
||||||
flex: 0.7,
|
|
||||||
},
|
|
||||||
"& > div:last-of-type": {
|
|
||||||
flex: 1,
|
|
||||||
},
|
|
||||||
"& h1, & h2": {
|
|
||||||
color: theme.palette.text.secondary,
|
|
||||||
},
|
|
||||||
"& p": {
|
|
||||||
color: theme.palette.text.tertiary,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
className="settings"
|
className="settings"
|
||||||
@@ -143,21 +188,19 @@ const Settings = ({ isAdmin }) => {
|
|||||||
</Box>
|
</Box>
|
||||||
<Stack gap={theme.spacing(20)}>
|
<Stack gap={theme.spacing(20)}>
|
||||||
<Field
|
<Field
|
||||||
type="text"
|
id="ttl"
|
||||||
id="history-monitoring"
|
|
||||||
label="The days you want to keep monitoring history."
|
label="The days you want to keep monitoring history."
|
||||||
isOptional={true}
|
|
||||||
optionalLabel="0 for infinite"
|
optionalLabel="0 for infinite"
|
||||||
placeholder="90"
|
value={form.ttl}
|
||||||
value=""
|
onChange={handleChange}
|
||||||
onChange={() => logger.warn("Disabled")}
|
error={errors.ttl}
|
||||||
/>
|
/>
|
||||||
<Box>
|
<Box>
|
||||||
<Typography>Clear all stats. This is irreversible.</Typography>
|
<Typography>Clear all stats. This is irreversible.</Typography>
|
||||||
<LoadingButton
|
<LoadingButton
|
||||||
variant="contained"
|
variant="contained"
|
||||||
color="error"
|
color="error"
|
||||||
loading={isLoading}
|
loading={isLoading || authIsLoading || checksIsLoading}
|
||||||
onClick={handleClearStats}
|
onClick={handleClearStats}
|
||||||
sx={{ mt: theme.spacing(4) }}
|
sx={{ mt: theme.spacing(4) }}
|
||||||
>
|
>
|
||||||
@@ -181,7 +224,7 @@ const Settings = ({ isAdmin }) => {
|
|||||||
<LoadingButton
|
<LoadingButton
|
||||||
variant="contained"
|
variant="contained"
|
||||||
color="primary"
|
color="primary"
|
||||||
loading={isLoading}
|
loading={isLoading || authIsLoading || checksIsLoading}
|
||||||
onClick={handleInsertDemoMonitors}
|
onClick={handleInsertDemoMonitors}
|
||||||
sx={{ mt: theme.spacing(4) }}
|
sx={{ mt: theme.spacing(4) }}
|
||||||
>
|
>
|
||||||
@@ -193,7 +236,7 @@ const Settings = ({ isAdmin }) => {
|
|||||||
<LoadingButton
|
<LoadingButton
|
||||||
variant="contained"
|
variant="contained"
|
||||||
color="error"
|
color="error"
|
||||||
loading={isLoading}
|
loading={isLoading || authIsLoading || checksIsLoading}
|
||||||
onClick={handleDeleteAllMonitors}
|
onClick={handleDeleteAllMonitors}
|
||||||
sx={{ mt: theme.spacing(4) }}
|
sx={{ mt: theme.spacing(4) }}
|
||||||
>
|
>
|
||||||
@@ -223,7 +266,8 @@ const Settings = ({ isAdmin }) => {
|
|||||||
</ConfigBox>
|
</ConfigBox>
|
||||||
<Stack direction="row" justifyContent="flex-end">
|
<Stack direction="row" justifyContent="flex-end">
|
||||||
<LoadingButton
|
<LoadingButton
|
||||||
loading={false}
|
loading={isLoading || authIsLoading || checksIsLoading}
|
||||||
|
disabled={Object.keys(errors).length > 0}
|
||||||
variant="contained"
|
variant="contained"
|
||||||
color="primary"
|
color="primary"
|
||||||
sx={{ px: theme.spacing(12), mt: theme.spacing(20) }}
|
sx={{ px: theme.spacing(12), mt: theme.spacing(20) }}
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import { Stack, styled } from "@mui/material";
|
||||||
|
|
||||||
|
export const ConfigBox = styled(Stack)(({ theme }) => ({
|
||||||
|
display: "flex",
|
||||||
|
flexDirection: "row",
|
||||||
|
justifyContent: "space-between",
|
||||||
|
gap: theme.spacing(20),
|
||||||
|
paddingTop: theme.spacing(12),
|
||||||
|
paddingInline: theme.spacing(15),
|
||||||
|
paddingBottom: theme.spacing(25),
|
||||||
|
backgroundColor: theme.palette.background.main,
|
||||||
|
border: 1,
|
||||||
|
borderStyle: "solid",
|
||||||
|
borderColor: theme.palette.border.light,
|
||||||
|
borderRadius: theme.spacing(2),
|
||||||
|
"& > div:first-of-type": {
|
||||||
|
flex: 0.7,
|
||||||
|
},
|
||||||
|
"& > div:last-of-type": {
|
||||||
|
flex: 1,
|
||||||
|
},
|
||||||
|
"& h1, & h2": {
|
||||||
|
color: theme.palette.text.secondary,
|
||||||
|
},
|
||||||
|
"& p": {
|
||||||
|
color: theme.palette.text.tertiary,
|
||||||
|
},
|
||||||
|
}));
|
||||||
@@ -582,6 +582,19 @@ class NetworkService {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async updateChecksTTL(authToken, ttl) {
|
||||||
|
return this.axiosInstance.put(
|
||||||
|
`/checks/ttl`,
|
||||||
|
{ ttl },
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${authToken}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default NetworkService;
|
export default NetworkService;
|
||||||
@@ -146,9 +146,16 @@ const maintenanceWindowValidation = joi.object({
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const settingsValidation = joi.object({
|
||||||
|
ttl: joi.number().required().messages({
|
||||||
|
"string.empty": "TTL is required",
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
export {
|
export {
|
||||||
credentials,
|
credentials,
|
||||||
imageValidation,
|
imageValidation,
|
||||||
monitorValidation,
|
monitorValidation,
|
||||||
maintenanceWindowValidation,
|
maintenanceWindowValidation,
|
||||||
|
settingsValidation,
|
||||||
};
|
};
|
||||||
@@ -10,6 +10,8 @@ const {
|
|||||||
updateChecksTTLBodyValidation,
|
updateChecksTTLBodyValidation,
|
||||||
} = require("../validation/joi");
|
} = require("../validation/joi");
|
||||||
const { successMessages } = require("../utils/messages");
|
const { successMessages } = require("../utils/messages");
|
||||||
|
const jwt = require("jsonwebtoken");
|
||||||
|
const { getTokenFromHeaders } = require("../utils/utils");
|
||||||
const SERVICE_NAME = "checkController";
|
const SERVICE_NAME = "checkController";
|
||||||
|
|
||||||
const createCheck = async (req, res, next) => {
|
const createCheck = async (req, res, next) => {
|
||||||
@@ -144,6 +146,8 @@ const deleteChecksByTeamId = async (req, res, next) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const updateChecksTTL = async (req, res, next) => {
|
const updateChecksTTL = async (req, res, next) => {
|
||||||
|
const SECONDS_PER_DAY = 86400;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await updateChecksTTLBodyValidation.validateAsync(req.body);
|
await updateChecksTTLBodyValidation.validateAsync(req.body);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -157,8 +161,11 @@ const updateChecksTTL = async (req, res, next) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const ttl = req.body.ttl;
|
// Get user's teamId
|
||||||
await req.db.updateChecksTTL(1, ttl);
|
const token = getTokenFromHeaders(req.headers);
|
||||||
|
const { teamId } = jwt.verify(token, process.env.JWT_SECRET);
|
||||||
|
const ttl = parseInt(req.body.ttl, 10) * SECONDS_PER_DAY;
|
||||||
|
await req.db.updateChecksTTL(teamId, ttl);
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.CHECK_UPDATE_TTL,
|
msg: successMessages.CHECK_UPDATE_TTL,
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
const Check = require("../../../models/Check");
|
const Check = require("../../../models/Check");
|
||||||
const Monitor = require("../../../models/Monitor");
|
const Monitor = require("../../../models/Monitor");
|
||||||
|
const User = require("../../../models/user");
|
||||||
const logger = require("../../../utils/logger");
|
const logger = require("../../../utils/logger");
|
||||||
const SERVICE_NAME = "checkModule";
|
const SERVICE_NAME = "checkModule";
|
||||||
const dateRangeLookup = {
|
const dateRangeLookup = {
|
||||||
@@ -248,6 +249,14 @@ const deleteChecksByTeamId = async (teamId) => {
|
|||||||
const updateChecksTTL = async (teamId, ttl) => {
|
const updateChecksTTL = async (teamId, ttl) => {
|
||||||
try {
|
try {
|
||||||
await Check.collection.dropIndex("expiry_1");
|
await Check.collection.dropIndex("expiry_1");
|
||||||
|
} catch (error) {
|
||||||
|
logger.error("Failed to drop index", {
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "updateChecksTTL",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
await Check.collection.createIndex(
|
await Check.collection.createIndex(
|
||||||
{ expiry: 1 },
|
{ expiry: 1 },
|
||||||
{ expireAfterSeconds: ttl } // TTL in seconds, adjust as necessary
|
{ expireAfterSeconds: ttl } // TTL in seconds, adjust as necessary
|
||||||
@@ -257,6 +266,14 @@ const updateChecksTTL = async (teamId, ttl) => {
|
|||||||
error.method = "updateChecksTTL";
|
error.method = "updateChecksTTL";
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
// Update user
|
||||||
|
try {
|
||||||
|
await User.updateMany({ teamId: teamId }, { checkTTL: ttl });
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.method = "updateChecksTTL";
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
@@ -45,6 +45,9 @@ const UserSchema = mongoose.Schema(
|
|||||||
ref: "Team",
|
ref: "Team",
|
||||||
immutable: true,
|
immutable: true,
|
||||||
},
|
},
|
||||||
|
checkTTL: {
|
||||||
|
type: Number,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
timestamps: true,
|
timestamps: true,
|
||||||
|
|||||||
Reference in new issue
Block a user