diff --git a/packages/server/src/db/schema/notification.ts b/packages/server/src/db/schema/notification.ts index 24523201b..baed63a5f 100644 --- a/packages/server/src/db/schema/notification.ts +++ b/packages/server/src/db/schema/notification.ts @@ -494,7 +494,7 @@ export const apiCreateMattermost = notificationsSchema serverThreshold: true, }) .extend({ - webhookUrl: z.string().min(1), + webhookUrl: z.string().url(), channel: z.string().optional(), username: z.string().optional(), }) diff --git a/packages/server/src/services/notification.ts b/packages/server/src/services/notification.ts index 0387eb029..ad06c3513 100644 --- a/packages/server/src/services/notification.ts +++ b/packages/server/src/services/notification.ts @@ -1020,7 +1020,7 @@ export const updateNotificationById = async ( }; export const createMattermostNotification = async ( - input: typeof apiCreateMattermost._type, + input: z.infer, organizationId: string, ) => { await db.transaction(async (tx) => { @@ -1070,7 +1070,7 @@ export const createMattermostNotification = async ( }; export const updateMattermostNotification = async ( - input: typeof apiUpdateMattermost._type, + input: z.infer, ) => { await db.transaction(async (tx) => { const newDestination = await tx diff --git a/packages/server/src/utils/notifications/utils.ts b/packages/server/src/utils/notifications/utils.ts index e81651c19..9b224d9d6 100644 --- a/packages/server/src/utils/notifications/utils.ts +++ b/packages/server/src/utils/notifications/utils.ts @@ -211,26 +211,26 @@ export const sendMattermostNotification = async ( connection: typeof mattermost.$inferInsert, message: any, ) => { - try { - const payload = { - ...message, - // Only include username if it's provided and not empty - ...(message.username && - message.username.trim() && { username: message.username }), - // Only include channel if it's provided and not empty - ...(message.channel && - message.channel.trim() && { - channel: `#${message.channel.replace("#", "")}`, - }), - }; + const payload = { + ...message, + // Only include username if it's provided and not empty + ...(message.username?.trim() && { username: message.username }), + // Only include channel if it's provided and not empty + ...(message.channel?.trim() && { + channel: `#${message.channel.replace("#", "")}`, + }), + }; - await fetch(connection.webhookUrl, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(payload), - }); - } catch (err) { - console.log(err); + const response = await fetch(connection.webhookUrl, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(payload), + }); + + if (!response.ok) { + throw new Error( + `Failed to send Mattermost notification: ${response.statusText}`, + ); } };