Skip to content

Commit

Permalink
Merge branch 'main' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
potts99 committed Nov 9, 2024
2 parents 625b697 + e544e5c commit 7cbda17
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 31 deletions.
46 changes: 17 additions & 29 deletions apps/api/src/controllers/ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import { sendAssignedEmail } from "../lib/nodemailer/ticket/assigned";
import { sendComment } from "../lib/nodemailer/ticket/comment";
import { sendTicketCreate } from "../lib/nodemailer/ticket/create";
import { sendTicketStatus } from "../lib/nodemailer/ticket/status";
import { checkSession } from "../lib/session";
import { prisma } from "../prisma";
import { assignedNotification } from "../lib/notifications/issue/assigned";
import { commentNotification } from "../lib/notifications/issue/comment";
import { sendWebhookNotification } from "../lib/notifications/webhook";
import { checkSession } from "../lib/session";
import { prisma } from "../prisma";

const validateEmail = (email: string) => {
return String(email)
Expand Down Expand Up @@ -97,33 +98,20 @@ export function ticketRoutes(fastify: FastifyInstance) {

for (let i = 0; i < webhook.length; i++) {
if (webhook[i].active === true) {
const url = webhook[i].url;
if (url.includes("discord.com")) {
const message = {
content: `Ticket ${ticket.id} created by ${ticket.name} -> ${ticket.email}. Priority -> ${ticket.priority}`,
avatar_url:
"https://avatars.githubusercontent.com/u/76014454?s=200&v=4",
username: "Peppermint.sh",
};
axios
.post(url, message)
.then((response) => {
console.log("Message sent successfully!");
console.log("Discord API response:", response.data);
})
.catch((error) => {
console.error("Error sending message:", error);
});
} else {
await axios.post(`${webhook[i].url}`, {
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
data: `Ticket ${ticket.id} created by ${ticket.name} -> ${ticket.email}. Priority -> ${ticket.priority}`,
}),
});
}
const message = {
event: "ticket_created",
id: ticket.id,
title: ticket.title,
priority: ticket.priority,
email: ticket.email,
name: ticket.name,
type: ticket.type,
createdBy: ticket.createdBy,
assignedTo: ticket.assignedTo,
client: ticket.client,
};

await sendWebhookNotification(webhook[i], message);
}
}

Expand Down
103 changes: 103 additions & 0 deletions apps/api/src/lib/notifications/webhook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import axios from "axios";

function getPriorityColor(priority: string): number {
switch (priority.toLowerCase()) {
case "high":
return 16711680; // Red
case "medium":
return 16753920; // Orange
case "low":
return 65280; // Green
default:
return 8421504; // Grey
}
}

export async function sendWebhookNotification(webhook: any, message: any) {
if (!webhook.active) return;

const url = webhook.url;

if (url.includes("discord.com")) {
const discordMessage = {
embeds: [
{
title: "Issue Created",
description: "A new issue has been created",
color: getPriorityColor(message.priority), // Use the priority color function
footer: {
text: "Issue ID: " + message.id,
},
author: {
name: "peppermint.sh",
icon_url:
"https://avatars.githubusercontent.com/u/76014454?s=200&v=4",
url: "https://peppermint.sh/",
},
fields: [
{
name: "Title",
value: message.title,
inline: false,
},
{
name: "Priority Level",
value: message.priority,
inline: false,
},
{
name: "Contact Email",
value: message.email ? message.email : "No email provided",
inline: false,
},
{
name: "Created By",
value: message.createdBy.name,
inline: false,
},
{
name: "Assigned To",
value: message.assignedTo
? message.assignedTo.name
: "Unassigned",
inline: false,
},
{
name: "Client",
value: message.client
? message.client.name
: "No client assigned",
inline: false,
},
{
name: "Type",
value: message.type,
inline: false,
},
],
},
],
content: "",
};

try {
await axios.post(url, discordMessage);
console.log("Discord webhook message sent successfully!");
} catch (error: any) {
if (error.response) {
console.error("Discord API response error:", error.response.data);
} else {
console.error("Error sending Discord webhook:", error.message);
}
throw error;
}
} else {
try {
await axios.post(url, {
data: message,
});
} catch (error) {
console.error("Error sending webhook:", error);
}
}
}
4 changes: 3 additions & 1 deletion apps/client/pages/issues/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,8 @@ export default function Tickets() {
"Content-Type": "application/json",
},
body: JSON.stringify({ id: ticket.id }),
}).then(() => {
refetch();
});
}
}}
Expand All @@ -727,7 +729,7 @@ export default function Tickets() {
type="button"
className="relative block w-[400px] rounded-lg border-2 border-dashed border-gray-300 p-12 text-center hover:border-gray-400 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2"
onClick={() => {
const event = new KeyboardEvent('keydown', { key: 'c' });
const event = new KeyboardEvent("keydown", { key: "c" });
document.dispatchEvent(event);
}}
>
Expand Down
2 changes: 1 addition & 1 deletion apps/landing/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export default function Home() {

<div className="">
<span className="text-2xl font-bold tracking-tight text-gray-900 ">
Peppermint - Open Source Issue Managment
Peppermint - Open Source Issue management
</span>
<div className="mt-4 flex flex-col ">
<div className="">
Expand Down

0 comments on commit 7cbda17

Please sign in to comment.