74 lines
1.9 KiB
TypeScript
74 lines
1.9 KiB
TypeScript
import { TFunction } from "i18next";
|
|
import { db } from "~/utils/db.server";
|
|
import NotificationService from "~/modules/notifications/services/.server/NotificationService";
|
|
import { redactFeedbackText } from "./FeedbackRedaction.server";
|
|
import { createWidgetSupportTicket } from "@hivegrid/db/support-tickets.server";
|
|
|
|
const MAX_FEEDBACK_PER_DAY = 3;
|
|
const FEEDBACK_RATE_LIMIT_WINDOW_HOURS = 24;
|
|
|
|
async function send({
|
|
t,
|
|
tenantId,
|
|
userId,
|
|
message,
|
|
fromUrl,
|
|
}: {
|
|
t: TFunction;
|
|
tenantId: string | null;
|
|
userId: string | null;
|
|
message: string;
|
|
fromUrl: string;
|
|
}) {
|
|
const redactedMessage = redactFeedbackText(message);
|
|
const redactedFromUrl = redactFeedbackText(fromUrl);
|
|
if (!redactedMessage) {
|
|
throw Error("Message is required");
|
|
}
|
|
|
|
const createdSince = new Date(Date.now() - FEEDBACK_RATE_LIMIT_WINDOW_HOURS * 60 * 60 * 1_000);
|
|
if (tenantId) {
|
|
const feedbackToday = await db.supportTicket.count({
|
|
where: { tenantId, source: "widget", createdAt: { gte: createdSince } },
|
|
});
|
|
if (feedbackToday >= MAX_FEEDBACK_PER_DAY) {
|
|
throw Error(t("feedback.limitReached"));
|
|
}
|
|
} else if (userId) {
|
|
const feedbackToday = await db.supportTicket.count({
|
|
where: { userId, source: "widget", createdAt: { gte: createdSince } },
|
|
});
|
|
if (feedbackToday >= MAX_FEEDBACK_PER_DAY) {
|
|
throw Error(t("feedback.limitReached"));
|
|
}
|
|
}
|
|
const ticket = await createWidgetSupportTicket({
|
|
tenantId,
|
|
userId,
|
|
subject: undefined,
|
|
message: redactedMessage,
|
|
fromUrl: redactedFromUrl,
|
|
});
|
|
await NotificationService.sendToRoles({
|
|
channel: "admin-users",
|
|
tenantId: null,
|
|
notification: {
|
|
message: "[New Support Ticket] " + ticket.subject,
|
|
},
|
|
});
|
|
return ticket;
|
|
}
|
|
|
|
async function deleteMany({ ids }: { ids: string[] }) {
|
|
return await db.feedback.deleteMany({
|
|
where: {
|
|
id: { in: ids },
|
|
},
|
|
});
|
|
}
|
|
|
|
export default {
|
|
send,
|
|
deleteMany,
|
|
};
|