initial commit
This commit is contained in:
1 parent
7cfbc85d9b
commit
8cbde4a705
5 files changed
+95
-62
No files matched your search
@@ -20,14 +20,14 @@ export const useSettingsForm = ({ data = null }: UseSettingsFormOptions = {}) =>
|
|||||||
systemEmailAddress: data?.systemEmailAddress || "",
|
systemEmailAddress: data?.systemEmailAddress || "",
|
||||||
systemEmailConnectionHost: data?.systemEmailConnectionHost || "localhost",
|
systemEmailConnectionHost: data?.systemEmailConnectionHost || "localhost",
|
||||||
systemEmailTLSServername: data?.systemEmailTLSServername || "",
|
systemEmailTLSServername: data?.systemEmailTLSServername || "",
|
||||||
systemEmailPort: data?.systemEmailPort || "",
|
systemEmailPort: data?.systemEmailPort,
|
||||||
globalThresholds: {
|
globalThresholds: {
|
||||||
cpu: data?.globalThresholds?.cpu || "",
|
cpu: data?.globalThresholds?.cpu,
|
||||||
memory: data?.globalThresholds?.memory || "",
|
memory: data?.globalThresholds?.memory,
|
||||||
disk: data?.globalThresholds?.disk || "",
|
disk: data?.globalThresholds?.disk,
|
||||||
temperature: data?.globalThresholds?.temperature || "",
|
temperature: data?.globalThresholds?.temperature,
|
||||||
},
|
},
|
||||||
checkTTL: data?.checkTTL || 30,
|
checkTTL: data?.checkTTL ?? 30,
|
||||||
pagespeedApiKey: "",
|
pagespeedApiKey: "",
|
||||||
systemEmailPassword: "",
|
systemEmailPassword: "",
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -75,7 +75,6 @@ const SettingsStats = ({ isAdmin, HEADING_SX, errors }) => {
|
|||||||
SettingsStats.propTypes = {
|
SettingsStats.propTypes = {
|
||||||
isAdmin: PropTypes.bool,
|
isAdmin: PropTypes.bool,
|
||||||
HEADING_SX: PropTypes.object,
|
HEADING_SX: PropTypes.object,
|
||||||
errors: PropTypes.object,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default SettingsStats;
|
export default SettingsStats;
|
||||||
@@ -12,7 +12,6 @@ import SettingsGlobalThresholds from "./SettingsGlobalThresholds.jsx";
|
|||||||
import SettingsExport from "./SettingsExport.jsx";
|
import SettingsExport from "./SettingsExport.jsx";
|
||||||
import Button from "@mui/material/Button";
|
import Button from "@mui/material/Button";
|
||||||
// Utils
|
// Utils
|
||||||
import { settingsValidation } from "@/Validation/validation.js";
|
|
||||||
import { Controller, useForm } from "react-hook-form";
|
import { Controller, useForm } from "react-hook-form";
|
||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
import { useSettingsForm } from "@/Hooks/useSettingsForm.js";
|
import { useSettingsForm } from "@/Hooks/useSettingsForm.js";
|
||||||
@@ -31,7 +30,6 @@ const BREADCRUMBS = [{ name: `Settings`, path: "/settings" }];
|
|||||||
const Settings = () => {
|
const Settings = () => {
|
||||||
const { data: fetchedSettings, isLoading: isSettingsLoading } = useGet("/settings");
|
const { data: fetchedSettings, isLoading: isSettingsLoading } = useGet("/settings");
|
||||||
// Local state
|
// Local state
|
||||||
const [errors, setErrors] = useState({});
|
|
||||||
const [isApiKeySet, setIsApiKeySet] = useState(
|
const [isApiKeySet, setIsApiKeySet] = useState(
|
||||||
fetchedSettings?.pagespeedKeySet ?? false
|
fetchedSettings?.pagespeedKeySet ?? false
|
||||||
);
|
);
|
||||||
@@ -60,7 +58,7 @@ const Settings = () => {
|
|||||||
formState: { dirtyFields },
|
formState: { dirtyFields },
|
||||||
} = form;
|
} = form;
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
reset(defaults);
|
reset(defaults, { keepDirtyValues: false });
|
||||||
}, [defaults, reset]);
|
}, [defaults, reset]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -88,7 +86,23 @@ const Settings = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const onSubmit = async (data) => {
|
const onSubmit = async (data) => {
|
||||||
const toSubmit = { ...data };
|
// Convert undefined to empty strings for proper unsetting on backend
|
||||||
|
const toSubmit = Object.fromEntries(
|
||||||
|
Object.entries(data).map(([key, value]) => {
|
||||||
|
if (value === undefined) return [key, ""];
|
||||||
|
if (typeof value === "object" && value !== null) {
|
||||||
|
// Handle nested objects like globalThresholds
|
||||||
|
return [
|
||||||
|
key,
|
||||||
|
Object.fromEntries(
|
||||||
|
Object.entries(value).map(([k, v]) => [k, v === undefined ? "" : v])
|
||||||
|
),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
return [key, value];
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
if (!form.formState.dirtyFields.systemEmailPassword) {
|
if (!form.formState.dirtyFields.systemEmailPassword) {
|
||||||
delete toSubmit.systemEmailPassword;
|
delete toSubmit.systemEmailPassword;
|
||||||
}
|
}
|
||||||
@@ -135,7 +149,6 @@ const Settings = () => {
|
|||||||
<SettingsStats
|
<SettingsStats
|
||||||
isAdmin={isAdmin}
|
isAdmin={isAdmin}
|
||||||
HEADING_SX={HEADING_SX}
|
HEADING_SX={HEADING_SX}
|
||||||
errors={errors}
|
|
||||||
/>
|
/>
|
||||||
<SettingsGlobalThresholds
|
<SettingsGlobalThresholds
|
||||||
isAdmin={isAdmin}
|
isAdmin={isAdmin}
|
||||||
@@ -195,7 +208,7 @@ const Settings = () => {
|
|||||||
loading={
|
loading={
|
||||||
isSaving || isDeletingMonitorStats || isSettingsLoading || isDeletingMonitors
|
isSaving || isDeletingMonitorStats || isSettingsLoading || isDeletingMonitors
|
||||||
}
|
}
|
||||||
disabled={Object.keys(errors).length > 0}
|
disabled={!form.formState.isValid}
|
||||||
variant="contained"
|
variant="contained"
|
||||||
color="accent"
|
color="accent"
|
||||||
sx={{ px: theme.spacing(12), py: theme.spacing(8) }}
|
sx={{ px: theme.spacing(12), py: theme.spacing(8) }}
|
||||||
|
|||||||
@@ -1,56 +1,69 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
|
// Helper to normalize empty strings to undefined
|
||||||
|
const optionalString = () =>
|
||||||
|
z.preprocess(
|
||||||
|
(val) =>
|
||||||
|
val === "" || val === null || val === undefined ? undefined : String(val).trim(),
|
||||||
|
z.string().optional()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Helper for threshold fields
|
||||||
|
const thresholdField = (min: number, max: number, unit: string) =>
|
||||||
|
z.preprocess(
|
||||||
|
(val) => (val === "" || val === null || val === undefined ? undefined : val),
|
||||||
|
z.coerce
|
||||||
|
.number()
|
||||||
|
.int()
|
||||||
|
.min(min, `Min ${min}${unit}`)
|
||||||
|
.max(max, `Max ${max}${unit}`)
|
||||||
|
.optional()
|
||||||
|
);
|
||||||
|
|
||||||
export const settingsSchema = z.object({
|
export const settingsSchema = z.object({
|
||||||
systemEmailIgnoreTLS: z.boolean(),
|
systemEmailIgnoreTLS: z.boolean(),
|
||||||
systemEmailRequireTLS: z.boolean(),
|
systemEmailRequireTLS: z.boolean(),
|
||||||
systemEmailRejectUnauthorized: z.boolean(),
|
systemEmailRejectUnauthorized: z.boolean(),
|
||||||
systemEmailConnectionHost: z.string().optional(),
|
systemEmailConnectionHost: optionalString(),
|
||||||
systemEmailSecure: z.boolean().optional(),
|
systemEmailSecure: z.boolean().optional(),
|
||||||
systemEmailPool: z.boolean().optional(),
|
systemEmailPool: z.boolean().optional(),
|
||||||
showURL: z.boolean().optional(),
|
showURL: z.boolean().optional(),
|
||||||
checkTTL: z.coerce.number().int().min(1, "Please enter a value"),
|
checkTTL: z.coerce
|
||||||
pagespeedApiKey: z.string().optional(),
|
|
||||||
systemEmailHost: z.string().optional(),
|
|
||||||
systemEmailPort: z.coerce
|
|
||||||
.number()
|
.number()
|
||||||
.int()
|
.int()
|
||||||
.min(1, "Port must be at least 1")
|
.min(1, "Please enter a value")
|
||||||
.optional()
|
.max(365, "Maximum 365 days"),
|
||||||
.or(z.literal("")),
|
pagespeedApiKey: optionalString(),
|
||||||
systemEmailAddress: z
|
systemEmailHost: z.preprocess(
|
||||||
.email("Please enter a valid email address")
|
(val) =>
|
||||||
.transform((val) => val.toLowerCase().trim())
|
val === "" || val === null || val === undefined ? undefined : String(val).trim(),
|
||||||
.optional()
|
z
|
||||||
.or(z.literal("")),
|
.string()
|
||||||
systemEmailUser: z.string().optional(),
|
.regex(/^[a-zA-Z0-9.-]+$/, "Invalid hostname or IP address")
|
||||||
systemEmailPassword: z.string().optional(),
|
.optional()
|
||||||
systemEmailTLSServername: z.string().optional(),
|
),
|
||||||
|
systemEmailPort: z.preprocess(
|
||||||
|
(val) => (val === "" || val === null || val === undefined ? undefined : val),
|
||||||
|
z.coerce
|
||||||
|
.number()
|
||||||
|
.int()
|
||||||
|
.min(1, "Port must be at least 1")
|
||||||
|
.max(65535, "Port must be at most 65535")
|
||||||
|
.optional()
|
||||||
|
),
|
||||||
|
systemEmailAddress: z.preprocess((val) => {
|
||||||
|
if (val === "" || val === null || val === undefined) return undefined;
|
||||||
|
return String(val).toLowerCase().trim();
|
||||||
|
}, z.string().email("Please enter a valid email address").optional()),
|
||||||
|
systemEmailUser: optionalString(),
|
||||||
|
systemEmailPassword: optionalString(),
|
||||||
|
systemEmailTLSServername: optionalString(),
|
||||||
globalThresholds: z
|
globalThresholds: z
|
||||||
.object({
|
.object({
|
||||||
cpu: z.coerce
|
cpu: thresholdField(0, 100, "%"),
|
||||||
.number()
|
memory: thresholdField(0, 100, "%"),
|
||||||
.min(1, "Min 1%")
|
disk: thresholdField(0, 100, "%"),
|
||||||
.max(100, "Max 100%")
|
temperature: thresholdField(0, 150, "°C"),
|
||||||
.optional()
|
|
||||||
.or(z.literal("")),
|
|
||||||
memory: z.coerce
|
|
||||||
.number()
|
|
||||||
.min(1, "Min 1%")
|
|
||||||
.max(100, "Max 100%")
|
|
||||||
.optional()
|
|
||||||
.or(z.literal("")),
|
|
||||||
disk: z.coerce
|
|
||||||
.number()
|
|
||||||
.min(1, "Min 1%")
|
|
||||||
.max(100, "Max 100%")
|
|
||||||
.optional()
|
|
||||||
.or(z.literal("")),
|
|
||||||
temperature: z.coerce
|
|
||||||
.number()
|
|
||||||
.min(1, "Min 1°C")
|
|
||||||
.max(150, "Max 150°C")
|
|
||||||
.optional()
|
|
||||||
.or(z.literal("")),
|
|
||||||
})
|
})
|
||||||
.optional(),
|
.optional(),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import mongoose from "mongoose";
|
|||||||
import { ISettingsRepository } from "@/repositories/settings/ISettingsRepository.js";
|
import { ISettingsRepository } from "@/repositories/settings/ISettingsRepository.js";
|
||||||
import type { Settings } from "@/types/index.js";
|
import type { Settings } from "@/types/index.js";
|
||||||
import { AppSettingsModel, type AppSettingsDocument } from "@/db/models/index.js";
|
import { AppSettingsModel, type AppSettingsDocument } from "@/db/models/index.js";
|
||||||
import { SERVFAIL } from "dns";
|
|
||||||
|
|
||||||
class MongoSettingsRepository implements ISettingsRepository {
|
class MongoSettingsRepository implements ISettingsRepository {
|
||||||
private toStringId = (value?: mongoose.Types.ObjectId | string | null): string => {
|
private toStringId = (value?: mongoose.Types.ObjectId | string | null): string => {
|
||||||
@@ -61,16 +60,25 @@ class MongoSettingsRepository implements ISettingsRepository {
|
|||||||
};
|
};
|
||||||
|
|
||||||
update = async (settings: Partial<Settings>) => {
|
update = async (settings: Partial<Settings>) => {
|
||||||
const update: Record<string, any> = { $set: { ...settings } };
|
const update: Record<string, any> = { $set: {}, $unset: {} };
|
||||||
|
|
||||||
if (settings.pagespeedApiKey === "") {
|
// Iterate through settings and separate into $set and $unset
|
||||||
update.$unset = { pagespeedApiKey: "" };
|
Object.entries(settings).forEach(([key, value]) => {
|
||||||
delete update.$set.pagespeedApiKey;
|
if (value === undefined || value === "") {
|
||||||
|
// Unset undefined or empty string values
|
||||||
|
update.$unset[key] = "";
|
||||||
|
} else {
|
||||||
|
// Set defined values
|
||||||
|
update.$set[key] = value;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Clean up empty operators
|
||||||
|
if (Object.keys(update.$set).length === 0) {
|
||||||
|
delete update.$set;
|
||||||
}
|
}
|
||||||
|
if (Object.keys(update.$unset).length === 0) {
|
||||||
if (settings.systemEmailPassword === "") {
|
delete update.$unset;
|
||||||
update.$unset = { systemEmailPassword: "" };
|
|
||||||
delete update.$set.systemEmailPassword;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await AppSettingsModel.findOneAndUpdate({}, update, {
|
await AppSettingsModel.findOneAndUpdate({}, update, {
|
||||||
|
|||||||
Reference in new issue
Block a user