mirror of
https://github.com/Dokploy/dokploy.git
synced 2026-07-08 23:45:22 +02:00
fix(notifications): update Mattermost notification handling
- Changed webhookUrl validation to ensure it is a valid URL. - Updated input types for createMattermostNotification and updateMattermostNotification functions to use z.infer for better type inference. - Refactored sendMattermostNotification to improve error handling and payload construction.
This commit is contained in:
@@ -494,7 +494,7 @@ export const apiCreateMattermost = notificationsSchema
|
|||||||
serverThreshold: true,
|
serverThreshold: true,
|
||||||
})
|
})
|
||||||
.extend({
|
.extend({
|
||||||
webhookUrl: z.string().min(1),
|
webhookUrl: z.string().url(),
|
||||||
channel: z.string().optional(),
|
channel: z.string().optional(),
|
||||||
username: z.string().optional(),
|
username: z.string().optional(),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1020,7 +1020,7 @@ export const updateNotificationById = async (
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const createMattermostNotification = async (
|
export const createMattermostNotification = async (
|
||||||
input: typeof apiCreateMattermost._type,
|
input: z.infer<typeof apiCreateMattermost>,
|
||||||
organizationId: string,
|
organizationId: string,
|
||||||
) => {
|
) => {
|
||||||
await db.transaction(async (tx) => {
|
await db.transaction(async (tx) => {
|
||||||
@@ -1070,7 +1070,7 @@ export const createMattermostNotification = async (
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const updateMattermostNotification = async (
|
export const updateMattermostNotification = async (
|
||||||
input: typeof apiUpdateMattermost._type,
|
input: z.infer<typeof apiUpdateMattermost>,
|
||||||
) => {
|
) => {
|
||||||
await db.transaction(async (tx) => {
|
await db.transaction(async (tx) => {
|
||||||
const newDestination = await tx
|
const newDestination = await tx
|
||||||
|
|||||||
@@ -211,26 +211,26 @@ export const sendMattermostNotification = async (
|
|||||||
connection: typeof mattermost.$inferInsert,
|
connection: typeof mattermost.$inferInsert,
|
||||||
message: any,
|
message: any,
|
||||||
) => {
|
) => {
|
||||||
try {
|
const payload = {
|
||||||
const payload = {
|
...message,
|
||||||
...message,
|
// Only include username if it's provided and not empty
|
||||||
// Only include username if it's provided and not empty
|
...(message.username?.trim() && { username: message.username }),
|
||||||
...(message.username &&
|
// Only include channel if it's provided and not empty
|
||||||
message.username.trim() && { username: message.username }),
|
...(message.channel?.trim() && {
|
||||||
// Only include channel if it's provided and not empty
|
channel: `#${message.channel.replace("#", "")}`,
|
||||||
...(message.channel &&
|
}),
|
||||||
message.channel.trim() && {
|
};
|
||||||
channel: `#${message.channel.replace("#", "")}`,
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
|
|
||||||
await fetch(connection.webhookUrl, {
|
const response = await fetch(connection.webhookUrl, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify(payload),
|
body: JSON.stringify(payload),
|
||||||
});
|
});
|
||||||
} catch (err) {
|
|
||||||
console.log(err);
|
if (!response.ok) {
|
||||||
|
throw new Error(
|
||||||
|
`Failed to send Mattermost notification: ${response.statusText}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user