backend: Update webhook preferences handling and validation; ensure URL is optional and required when enabled

This commit is contained in:
Carlos Santos 2025-05-06 17:13:00 +02:00
parent 4344ed8c0c
commit 7167cb4445
3 changed files with 23 additions and 7 deletions

View File

@ -8,12 +8,17 @@ export const updateWebhookPreferences = async (req: Request, res: Response) => {
const logger = container.get(LoggerService);
const globalPrefService = container.get(MeetStorageService);
logger.verbose(`Updating webhooks preferences: ${JSON.stringify(req.body)}`);
logger.info(`Updating webhooks preferences: ${JSON.stringify(req.body)}`);
const webhookPreferences = req.body as WebhookPreferences;
try {
const globalPreferences = await globalPrefService.getGlobalPreferences();
globalPreferences.webhooksPreferences = webhookPreferences;
globalPreferences.webhooksPreferences.enabled = webhookPreferences.enabled;
if (webhookPreferences.url) {
globalPreferences.webhooksPreferences.url = webhookPreferences.url;
}
await globalPrefService.saveGlobalPreferences(globalPreferences);
return res.status(200).json({ message: 'Webhooks preferences updated successfully' });

View File

@ -12,10 +12,21 @@ import { NextFunction, Request, Response } from 'express';
import { z } from 'zod';
import { rejectUnprocessableRequest } from '../../models/error.model.js';
const WebhookPreferencesSchema: z.ZodType<WebhookPreferences> = z.object({
enabled: z.boolean(),
url: z.string().url()
});
const WebhookPreferencesSchema: z.ZodType<WebhookPreferences> = z
.object({
enabled: z.boolean(),
url: z.string().url().optional()
})
.refine(
(data) => {
// If webhooks are enabled, URL must be provided
return !data.enabled || Boolean(data.url);
},
{
message: 'URL is required when webhooks are enabled',
path: ['url']
}
);
const AuthModeSchema: z.ZodType<AuthMode> = z.enum([AuthMode.NONE, AuthMode.MODERATORS_ONLY, AuthMode.ALL_USERS]);

View File

@ -75,7 +75,7 @@ export class OpenViduWebhookService {
this.logger.info(`Sending webhook event ${data.event}`);
try {
await this.fetchWithRetry(webhookPreferences.url, {
await this.fetchWithRetry(webhookPreferences.url!, {
method: 'POST',
headers: {
'Content-Type': 'application/json',