backend: Update webhook preferences handling and validation; ensure URL is optional and required when enabled
This commit is contained in:
parent
4344ed8c0c
commit
7167cb4445
@ -8,12 +8,17 @@ export const updateWebhookPreferences = async (req: Request, res: Response) => {
|
|||||||
const logger = container.get(LoggerService);
|
const logger = container.get(LoggerService);
|
||||||
const globalPrefService = container.get(MeetStorageService);
|
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;
|
const webhookPreferences = req.body as WebhookPreferences;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const globalPreferences = await globalPrefService.getGlobalPreferences();
|
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);
|
await globalPrefService.saveGlobalPreferences(globalPreferences);
|
||||||
|
|
||||||
return res.status(200).json({ message: 'Webhooks preferences updated successfully' });
|
return res.status(200).json({ message: 'Webhooks preferences updated successfully' });
|
||||||
|
|||||||
@ -12,10 +12,21 @@ import { NextFunction, Request, Response } from 'express';
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { rejectUnprocessableRequest } from '../../models/error.model.js';
|
import { rejectUnprocessableRequest } from '../../models/error.model.js';
|
||||||
|
|
||||||
const WebhookPreferencesSchema: z.ZodType<WebhookPreferences> = z.object({
|
const WebhookPreferencesSchema: z.ZodType<WebhookPreferences> = z
|
||||||
enabled: z.boolean(),
|
.object({
|
||||||
url: z.string().url()
|
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]);
|
const AuthModeSchema: z.ZodType<AuthMode> = z.enum([AuthMode.NONE, AuthMode.MODERATORS_ONLY, AuthMode.ALL_USERS]);
|
||||||
|
|
||||||
|
|||||||
@ -75,7 +75,7 @@ export class OpenViduWebhookService {
|
|||||||
this.logger.info(`Sending webhook event ${data.event}`);
|
this.logger.info(`Sending webhook event ${data.event}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await this.fetchWithRetry(webhookPreferences.url, {
|
await this.fetchWithRetry(webhookPreferences.url!, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user