Repository navigation
Expand file tree
/
Copy pathfeedback.ts
More file actions
50 lines (44 loc) · 1.33 KB
/
Copy pathfeedback.ts
File metadata and controls
50 lines (44 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { Sentiment } from ".prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { prisma } from "../../clients/prisma";
export interface FeedbackBody {
topic: string;
helpful: boolean;
feedback?: string;
}
export default async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method == "POST") {
const body: FeedbackBody = JSON.parse(req.body);
const feedbackMessage =
body.feedback?.trim() === "" ? null : body.feedback?.trim();
// Save result in prisma
await prisma.feedback.create({
data: {
sentiment: body.helpful ? Sentiment.POSITIVE : Sentiment.NEGATIVE,
topic: body.topic,
message: feedbackMessage,
},
});
// Only send Discord message if negative
if (!body.helpful) {
const discordBody = {
content: `:speaking_head:
Topic: ${body.topic}
Was it helpful?: ${body.helpful ? "Yes" : "No"}
Feedback: ${
feedbackMessage != null && feedbackMessage.length === 0
? "N/A"
: feedbackMessage
}`,
};
await fetch(`${process.env.DISCORD_WEBHOOK}`, {
body: JSON.stringify(discordBody),
method: "POST",
headers: {
"content-type": "application/json",
},
});
}
}
res.status(200).json({ status: "Success" });
};