backend: Update webhook preferences handling and validation logic

This commit is contained in:
Carlos Santos 2025-05-07 12:36:09 +02:00
parent 39b53f537d
commit 4cb9452ac1
2 changed files with 13 additions and 5 deletions

View File

@ -13,11 +13,15 @@ export const updateWebhookPreferences = async (req: Request, res: Response) => {
try {
const globalPreferences = await globalPrefService.getGlobalPreferences();
globalPreferences.webhooksPreferences.enabled = webhookPreferences.enabled;
if (webhookPreferences.url) {
globalPreferences.webhooksPreferences.url = webhookPreferences.url;
}
// TODO: Validate the URL if webhooks are enabled by making a test request
globalPreferences.webhooksPreferences = {
enabled: webhookPreferences.enabled,
url:
webhookPreferences.url === undefined
? globalPreferences.webhooksPreferences.url
: webhookPreferences.url
};
await globalPrefService.saveGlobalPreferences(globalPreferences);

View File

@ -15,7 +15,11 @@ import { rejectUnprocessableRequest } from '../../models/error.model.js';
const WebhookPreferencesSchema: z.ZodType<WebhookPreferences> = z
.object({
enabled: z.boolean(),
url: z.string().url().optional()
url: z
.string()
.url('Must be a valid URL')
.regex(/^https?:\/\//, { message: 'URL must start with http:// or https://' })
.optional()
})
.refine(
(data) => {