Merge pull request #3415 from bluewave-labs/fix/unset-with-explicit-null
fix: unset with explicit null
This commit is contained in:
8 files changed
+42
-55
No files matched your search
@@ -1,14 +1,14 @@
|
|||||||
import { useMemo } from "react";
|
import { useMemo } from "react";
|
||||||
import { settingsSchema } from "@/Validation/settings";
|
import { settingsSchema } from "@/Validation/settings";
|
||||||
import type { Settings } from "@/Types/Settings";
|
import type { Settings } from "@/Types/Settings";
|
||||||
import type { SettingsFormData } from "@/Validation/settings";
|
import type { SettingsFormInput } from "@/Validation/settings";
|
||||||
|
|
||||||
interface UseSettingsFormOptions {
|
interface UseSettingsFormOptions {
|
||||||
data?: Settings | null;
|
data?: Settings | null;
|
||||||
}
|
}
|
||||||
export const useSettingsForm = ({ data = null }: UseSettingsFormOptions = {}) => {
|
export const useSettingsForm = ({ data = null }: UseSettingsFormOptions = {}) => {
|
||||||
return useMemo(() => {
|
return useMemo(() => {
|
||||||
const defaults: SettingsFormData = {
|
const defaults: SettingsFormInput = {
|
||||||
systemEmailIgnoreTLS: data?.systemEmailIgnoreTLS ?? false,
|
systemEmailIgnoreTLS: data?.systemEmailIgnoreTLS ?? false,
|
||||||
systemEmailRequireTLS: data?.systemEmailRequireTLS ?? false,
|
systemEmailRequireTLS: data?.systemEmailRequireTLS ?? false,
|
||||||
systemEmailRejectUnauthorized: data?.systemEmailRejectUnauthorized ?? true,
|
systemEmailRejectUnauthorized: data?.systemEmailRejectUnauthorized ?? true,
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import { useGet, usePatch, usePost, useLazyGet } from "@/Hooks/UseApi";
|
|||||||
import { useToast } from "@/Hooks/UseToast";
|
import { useToast } from "@/Hooks/UseToast";
|
||||||
import { useSettingsForm } from "@/Hooks/useSettingsForm";
|
import { useSettingsForm } from "@/Hooks/useSettingsForm";
|
||||||
import { useIsAdmin } from "@/Hooks/useIsAdmin.js";
|
import { useIsAdmin } from "@/Hooks/useIsAdmin.js";
|
||||||
import type { SettingsFormData } from "@/Validation/settings";
|
import type { SettingsFormData, SettingsFormInput } from "@/Validation/settings";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { Controller } from "react-hook-form";
|
import { Controller } from "react-hook-form";
|
||||||
import { TextField, Button, FieldLabel, SliderWithLabel } from "@/Components/inputs";
|
import { TextField, Button, FieldLabel, SliderWithLabel } from "@/Components/inputs";
|
||||||
@@ -89,7 +89,7 @@ export const SettingsPage = () => {
|
|||||||
// Initialize form with schema and defaults
|
// Initialize form with schema and defaults
|
||||||
const { schema, defaults } = useSettingsForm({ data: fetchedSettings?.settings });
|
const { schema, defaults } = useSettingsForm({ data: fetchedSettings?.settings });
|
||||||
|
|
||||||
const form = useForm<SettingsFormData>({
|
const form = useForm<SettingsFormInput, unknown, SettingsFormData>({
|
||||||
resolver: zodResolver(schema),
|
resolver: zodResolver(schema),
|
||||||
defaultValues: defaults,
|
defaultValues: defaults,
|
||||||
mode: "onChange",
|
mode: "onChange",
|
||||||
@@ -256,20 +256,7 @@ export const SettingsPage = () => {
|
|||||||
delete (dataToSend as any).systemEmailPassword;
|
delete (dataToSend as any).systemEmailPassword;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert undefined to empty string for backend unsetting
|
const result = await patch("/settings", dataToSend as SettingsFormData);
|
||||||
const processedData = Object.entries(dataToSend).reduce((acc, [key, value]) => {
|
|
||||||
const typedKey = key as keyof SettingsFormData;
|
|
||||||
if (value === undefined) {
|
|
||||||
(acc as any)[typedKey] = "";
|
|
||||||
} else if (typeof value === "object" && value !== null) {
|
|
||||||
(acc as any)[typedKey] = value;
|
|
||||||
} else {
|
|
||||||
(acc as any)[typedKey] = value;
|
|
||||||
}
|
|
||||||
return acc;
|
|
||||||
}, {} as Partial<SettingsFormData>);
|
|
||||||
|
|
||||||
const result = await patch("/settings", processedData as SettingsFormData);
|
|
||||||
|
|
||||||
if (result?.success) {
|
if (result?.success) {
|
||||||
// Update API key state from response
|
// Update API key state from response
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ export const settingsSchema = z.object({
|
|||||||
systemEmailRejectUnauthorized: z.boolean(),
|
systemEmailRejectUnauthorized: z.boolean(),
|
||||||
systemEmailConnectionHost: z
|
systemEmailConnectionHost: z
|
||||||
.string()
|
.string()
|
||||||
.transform((val) => (val.trim() === "" ? undefined : val.trim()))
|
.transform((val) => (val.trim() === "" ? null : val.trim()))
|
||||||
.optional(),
|
.optional(),
|
||||||
systemEmailSecure: z.boolean().optional(),
|
systemEmailSecure: z.boolean().optional(),
|
||||||
systemEmailPool: z.boolean().optional(),
|
systemEmailPool: z.boolean().optional(),
|
||||||
@@ -19,31 +19,30 @@ export const settingsSchema = z.object({
|
|||||||
.max(CHECK_TTL_SENTINEL, `Maximum ${CHECK_TTL_SENTINEL}`),
|
.max(CHECK_TTL_SENTINEL, `Maximum ${CHECK_TTL_SENTINEL}`),
|
||||||
pagespeedApiKey: z
|
pagespeedApiKey: z
|
||||||
.string()
|
.string()
|
||||||
.transform((val) => (val.trim() === "" ? undefined : val.trim()))
|
.transform((val) => (val.trim() === "" ? null : val.trim()))
|
||||||
.optional(),
|
.optional(),
|
||||||
systemEmailHost: z
|
systemEmailHost: z
|
||||||
.string()
|
.string()
|
||||||
.regex(/^[a-zA-Z0-9.-]*$/, "Invalid hostname or IP address")
|
.regex(/^[a-zA-Z0-9.-]*$/, "Invalid hostname or IP address")
|
||||||
.transform((val) => (val.trim() === "" ? undefined : val.trim()))
|
.transform((val) => (val.trim() === "" ? null : val.trim()))
|
||||||
.optional(),
|
.optional(),
|
||||||
systemEmailPort: z.number().int().min(1).max(65535).optional(),
|
systemEmailPort: z.number().int().min(1).max(65535).optional(),
|
||||||
systemEmailAddress: z
|
systemEmailAddress: z
|
||||||
.string()
|
|
||||||
.email("Please enter a valid email address")
|
.email("Please enter a valid email address")
|
||||||
.or(z.literal(""))
|
.or(z.literal(""))
|
||||||
.transform((val) => (val === "" ? undefined : val.toLowerCase().trim()))
|
.transform((val) => (val === "" ? null : val.toLowerCase().trim()))
|
||||||
.optional(),
|
.optional(),
|
||||||
systemEmailUser: z
|
systemEmailUser: z
|
||||||
.string()
|
.string()
|
||||||
.transform((val) => (val.trim() === "" ? undefined : val.trim()))
|
.transform((val) => (val.trim() === "" ? null : val.trim()))
|
||||||
.optional(),
|
.optional(),
|
||||||
systemEmailPassword: z
|
systemEmailPassword: z
|
||||||
.string()
|
.string()
|
||||||
.transform((val) => (val.trim() === "" ? undefined : val.trim()))
|
.transform((val) => (val.trim() === "" ? null : val.trim()))
|
||||||
.optional(),
|
.optional(),
|
||||||
systemEmailTLSServername: z
|
systemEmailTLSServername: z
|
||||||
.string()
|
.string()
|
||||||
.transform((val) => (val.trim() === "" ? undefined : val.trim()))
|
.transform((val) => (val.trim() === "" ? null : val.trim()))
|
||||||
.optional(),
|
.optional(),
|
||||||
globalThresholds: z.object({
|
globalThresholds: z.object({
|
||||||
cpu: z.number().int().min(1).max(100),
|
cpu: z.number().int().min(1).max(100),
|
||||||
@@ -53,4 +52,5 @@ export const settingsSchema = z.object({
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export type SettingsFormInput = z.input<typeof settingsSchema>;
|
||||||
export type SettingsFormData = z.infer<typeof settingsSchema>;
|
export type SettingsFormData = z.infer<typeof settingsSchema>;
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
import type { Settings } from "@/types/index.js";
|
import type { Settings, SettingsUpdate } from "@/types/index.js";
|
||||||
export interface ISettingsRepository {
|
export interface ISettingsRepository {
|
||||||
// create
|
// create
|
||||||
create(settings: Partial<Settings>): Promise<Settings>;
|
create(settings: Partial<Settings>): Promise<Settings>;
|
||||||
// fetch
|
// fetch
|
||||||
findSingleton(): Promise<Settings | null>;
|
findSingleton(): Promise<Settings | null>;
|
||||||
// update
|
// update
|
||||||
update(settings: Partial<Settings>): Promise<Settings>;
|
update(settings: SettingsUpdate): Promise<Settings>;
|
||||||
// delete
|
// delete
|
||||||
deleteLegacy: () => Promise<boolean>;
|
deleteLegacy: () => Promise<boolean>;
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import mongoose from "mongoose";
|
import mongoose, { type UpdateQuery } 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, SettingsUpdate } from "@/types/index.js";
|
||||||
import { AppSettingsModel, type AppSettingsDocument } from "@/db/models/index.js";
|
import { AppSettingsModel, type AppSettingsDocument } from "@/db/models/index.js";
|
||||||
|
|
||||||
class MongoSettingsRepository implements ISettingsRepository {
|
class MongoSettingsRepository implements ISettingsRepository {
|
||||||
@@ -59,27 +59,23 @@ class MongoSettingsRepository implements ISettingsRepository {
|
|||||||
return this.toEntity(settings);
|
return this.toEntity(settings);
|
||||||
};
|
};
|
||||||
|
|
||||||
update = async (settings: Partial<Settings>) => {
|
update = async (settings: SettingsUpdate) => {
|
||||||
const update: Record<string, any> = { $set: {}, $unset: {} };
|
const $set: Record<string, unknown> = {};
|
||||||
|
const $unset: Record<string, string> = {};
|
||||||
|
|
||||||
// Iterate through settings and separate into $set and $unset
|
// Iterate through settings and separate into $set and $unset
|
||||||
Object.entries(settings).forEach(([key, value]) => {
|
Object.entries(settings).forEach(([key, value]) => {
|
||||||
if (value === undefined || value === "") {
|
if (value === undefined || value === null) {
|
||||||
// Unset undefined or empty string values
|
$unset[key] = "";
|
||||||
update.$unset[key] = "";
|
|
||||||
} else {
|
} else {
|
||||||
// Set defined values
|
$set[key] = value;
|
||||||
update.$set[key] = value;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Clean up empty operators
|
const update: UpdateQuery<AppSettingsDocument> = {
|
||||||
if (Object.keys(update.$set).length === 0) {
|
...(Object.keys($set).length > 0 && { $set }),
|
||||||
delete update.$set;
|
...(Object.keys($unset).length > 0 && { $unset }),
|
||||||
}
|
};
|
||||||
if (Object.keys(update.$unset).length === 0) {
|
|
||||||
delete update.$unset;
|
|
||||||
}
|
|
||||||
|
|
||||||
await AppSettingsModel.findOneAndUpdate({}, update, {
|
await AppSettingsModel.findOneAndUpdate({}, update, {
|
||||||
upsert: true,
|
upsert: true,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { ISettingsRepository } from "@/repositories/index.js";
|
import { ISettingsRepository } from "@/repositories/index.js";
|
||||||
import { Settings } from "@/types/index.js";
|
import { Settings, SettingsUpdate } from "@/types/index.js";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
import { ValidatedEnv } from "@/validation/envValidation.js";
|
import { ValidatedEnv } from "@/validation/envValidation.js";
|
||||||
import type { StringValue } from "ms";
|
import type { StringValue } from "ms";
|
||||||
@@ -19,7 +19,7 @@ export interface ISettingsService {
|
|||||||
loadSettings(): EnvConfig;
|
loadSettings(): EnvConfig;
|
||||||
getSettings(): EnvConfig;
|
getSettings(): EnvConfig;
|
||||||
getDBSettings(): Promise<Settings>;
|
getDBSettings(): Promise<Settings>;
|
||||||
updateDbSettings(newSettings: Partial<Settings>): Promise<Settings>;
|
updateDbSettings(newSettings: SettingsUpdate): Promise<Settings>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SettingsService implements ISettingsService {
|
export class SettingsService implements ISettingsService {
|
||||||
@@ -54,7 +54,7 @@ export class SettingsService implements ISettingsService {
|
|||||||
return this.settings;
|
return this.settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
updateDbSettings = async (newSettings: Partial<Settings>) => {
|
updateDbSettings = async (newSettings: SettingsUpdate) => {
|
||||||
return await this.settingsRepository.update(newSettings);
|
return await this.settingsRepository.update(newSettings);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ export interface SettingsThresholds {
|
|||||||
temperature?: number;
|
temperature?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type SettingsUpdate = {
|
||||||
|
[K in keyof Settings]?: Settings[K] | null;
|
||||||
|
};
|
||||||
|
|
||||||
export interface Settings {
|
export interface Settings {
|
||||||
id: string;
|
id: string;
|
||||||
checkTTL: number;
|
checkTTL: number;
|
||||||
|
|||||||
@@ -8,16 +8,16 @@ import { z } from "zod";
|
|||||||
export const updateAppSettingsBodyValidation = z
|
export const updateAppSettingsBodyValidation = z
|
||||||
.object({
|
.object({
|
||||||
checkTTL: z.number().int().min(1).max(CHECK_TTL_SENTINEL).optional(),
|
checkTTL: z.number().int().min(1).max(CHECK_TTL_SENTINEL).optional(),
|
||||||
systemEmailPort: z.number().optional(),
|
systemEmailPort: z.number().nullable().optional(),
|
||||||
pagespeedApiKey: z.string().optional(),
|
pagespeedApiKey: z.string().nullable().optional(),
|
||||||
language: z.string().optional(),
|
language: z.string().optional(),
|
||||||
timezone: z.string().optional(),
|
timezone: z.string().optional(),
|
||||||
systemEmailHost: z.string().optional(),
|
systemEmailHost: z.string().nullable().optional(),
|
||||||
systemEmailAddress: z.string().optional(),
|
systemEmailAddress: z.string().nullable().optional(),
|
||||||
systemEmailPassword: z.string().optional(),
|
systemEmailPassword: z.string().nullable().optional(),
|
||||||
systemEmailUser: z.string().optional(),
|
systemEmailUser: z.string().nullable().optional(),
|
||||||
systemEmailConnectionHost: z.string().optional(),
|
systemEmailConnectionHost: z.string().nullable().optional(),
|
||||||
systemEmailTLSServername: z.string().optional(),
|
systemEmailTLSServername: z.string().nullable().optional(),
|
||||||
|
|
||||||
showURL: z.boolean().optional(),
|
showURL: z.boolean().optional(),
|
||||||
systemEmailSecure: z.boolean().optional(),
|
systemEmailSecure: z.boolean().optional(),
|
||||||
|
|||||||
Reference in new issue
Block a user