Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+3
-3
@@ -54,7 +54,6 @@ const feedbackRow: FeedbackWithDetails = {
|
||||
icon: null,
|
||||
logo: null,
|
||||
deactivatedReason: null,
|
||||
types: [],
|
||||
active: true,
|
||||
theme: null,
|
||||
themeConfig: null,
|
||||
@@ -101,8 +100,9 @@ describe("FeedbackTable", () => {
|
||||
const repoRoot = process.cwd();
|
||||
const source = [
|
||||
"apps/web/app/modules/helpDesk/components/FeedbackTable.tsx",
|
||||
"apps/web/app/routes/admin/help-desk/index.tsx",
|
||||
"apps/web/app/routes/admin/help-desk/tickets.tsx",
|
||||
"apps/web/app/components/screens/admin/AdminSupportInboxPage.tsx",
|
||||
"apps/web/app/routes/admin/support/index.tsx",
|
||||
"apps/web/app/routes/admin/support/tickets.tsx",
|
||||
]
|
||||
.map((file) => fs.readFileSync(path.join(repoRoot, file), "utf8"))
|
||||
.join("\n");
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ export type SupportTicketWithDetails = SupportTicket & {
|
||||
export type SupportTicketCreateInput = {
|
||||
tenantId?: string | null;
|
||||
userId?: string | null;
|
||||
source: "inline" | "email" | "api" | "widget";
|
||||
source: "inline" | "email" | "api" | "widget" | "in_app_feedback_widget";
|
||||
subject: string;
|
||||
message: string;
|
||||
fromEmail?: string | null;
|
||||
|
||||
+73
-1
@@ -4,6 +4,8 @@ import { db } from "~/utils/db.server";
|
||||
import NotificationService from "~/modules/notifications/services/.server/NotificationService";
|
||||
import { feedbackRedactionMarkers } from "./FeedbackRedaction.server";
|
||||
import { createWidgetSupportTicket } from "@hivegrid/db/support-tickets.server";
|
||||
import { createFileFromBytes } from "~/utils/integrations/fileService.server";
|
||||
import { FeedbackUserError, MAX_FEEDBACK_ATTACHMENT_BYTES } from "./Feedback.server";
|
||||
|
||||
vi.mock("~/utils/db.server", () => ({
|
||||
db: {
|
||||
@@ -18,6 +20,8 @@ vi.mock("~/utils/db.server", () => ({
|
||||
|
||||
vi.mock("@hivegrid/db/support-tickets.server", () => ({
|
||||
createWidgetSupportTicket: vi.fn(),
|
||||
WIDGET_SUPPORT_TICKET_SOURCE: "in_app_feedback_widget",
|
||||
WIDGET_SUPPORT_TICKET_LEGACY_SOURCES: ["in_app_feedback_widget", "widget"],
|
||||
}));
|
||||
|
||||
vi.mock("~/modules/notifications/services/.server/NotificationService", () => ({
|
||||
@@ -26,7 +30,19 @@ vi.mock("~/modules/notifications/services/.server/NotificationService", () => ({
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/integrations/fileService.server", () => ({
|
||||
createFileFromBytes: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/db/.server/tenantRls.server", () => ({
|
||||
withTenantRLS: vi.fn(async (_tenantId: string, run: (tx: unknown) => unknown) =>
|
||||
run({ fileEntityLink: { create: fileEntityLinkCreateMock } })
|
||||
),
|
||||
}));
|
||||
|
||||
const fileEntityLinkCreateMock = vi.hoisted(() => vi.fn());
|
||||
const countWidgetTicketsMock = vi.mocked(db.supportTicket.count);
|
||||
const createFileFromBytesMock = vi.mocked(createFileFromBytes);
|
||||
const createWidgetSupportTicketMock = vi.mocked(createWidgetSupportTicket);
|
||||
const sendToRolesMock = vi.mocked(NotificationService.sendToRoles);
|
||||
const t = ((key: string) => key) as TFunction;
|
||||
@@ -36,6 +52,62 @@ describe("FeedbackServer.send", () => {
|
||||
vi.clearAllMocks();
|
||||
countWidgetTicketsMock.mockResolvedValue(0);
|
||||
createWidgetSupportTicketMock.mockResolvedValue({ ticketId: "ticket_1", tenantId: "tenant_1", subject: "Support request" });
|
||||
createFileFromBytesMock.mockResolvedValue({ id: "file_1", displayName: "bug.png", mimeType: "image/png", sizeBytes: 3 });
|
||||
});
|
||||
|
||||
// AK-P21 — the widget lets the reporter attach an image instead of the app
|
||||
// capturing a screenshot itself (decision 2026-07-21).
|
||||
const pngAttachment = { filename: "bug.png", mimeType: "image/png", bytes: new Uint8Array([1, 2, 3]) };
|
||||
|
||||
it("stores an attached image and links it to the ticket", async () => {
|
||||
const { default: FeedbackServer } = await import("./Feedback.server");
|
||||
await FeedbackServer.send({
|
||||
t,
|
||||
tenantId: "tenant_1",
|
||||
userId: "user_1",
|
||||
message: "Button kaputt",
|
||||
fromUrl: "https://app.example.test/app/acme",
|
||||
attachment: pngAttachment,
|
||||
});
|
||||
|
||||
expect(createFileFromBytesMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ tenantId: "tenant_1", userId: "user_1", filename: "bug.png", mimeType: "image/png" })
|
||||
);
|
||||
expect(fileEntityLinkCreateMock).toHaveBeenCalledWith({
|
||||
data: expect.objectContaining({ tenantId: "tenant_1", fileId: "file_1", entityType: "support_ticket", entityId: "ticket_1" }),
|
||||
});
|
||||
});
|
||||
|
||||
it("creates the ticket before the attachment so a rejected upload never eats the report", async () => {
|
||||
const { default: FeedbackServer } = await import("./Feedback.server");
|
||||
createFileFromBytesMock.mockRejectedValueOnce(new Error("scan rejected"));
|
||||
|
||||
await expect(
|
||||
FeedbackServer.send({ t, tenantId: "tenant_1", userId: "user_1", message: "Button kaputt", fromUrl: "u", attachment: pngAttachment })
|
||||
).rejects.toThrow("scan rejected");
|
||||
|
||||
expect(createWidgetSupportTicketMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["oversized", { ...pngAttachment, bytes: new Uint8Array(MAX_FEEDBACK_ATTACHMENT_BYTES + 1) }],
|
||||
["non-image", { ...pngAttachment, mimeType: "application/pdf" }],
|
||||
["empty", { ...pngAttachment, bytes: new Uint8Array(0) }],
|
||||
])("rejects an %s attachment before touching storage", async (_label, attachment) => {
|
||||
const { default: FeedbackServer } = await import("./Feedback.server");
|
||||
await expect(
|
||||
FeedbackServer.send({ t, tenantId: "tenant_1", userId: "user_1", message: "Button kaputt", fromUrl: "u", attachment })
|
||||
).rejects.toThrow();
|
||||
|
||||
expect(createFileFromBytesMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("ignores an attachment when the feedback has no tenant, because a File is always tenant-scoped", async () => {
|
||||
const { default: FeedbackServer } = await import("./Feedback.server");
|
||||
await FeedbackServer.send({ t, tenantId: null, userId: "user_1", message: "Button kaputt", fromUrl: "u", attachment: pngAttachment });
|
||||
|
||||
expect(createFileFromBytesMock).not.toHaveBeenCalled();
|
||||
expect(fileEntityLinkCreateMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("redacts sensitive message and URL values before creating a support ticket", async () => {
|
||||
@@ -94,7 +166,7 @@ describe("FeedbackServer.send", () => {
|
||||
message: " ",
|
||||
fromUrl: "/acme?token=raw-token-123",
|
||||
}),
|
||||
).rejects.toThrow("Message is required");
|
||||
).rejects.toEqual(new FeedbackUserError("feedback.messageRequired", 400));
|
||||
|
||||
expect(sendToRolesMock).not.toHaveBeenCalled();
|
||||
expect(createWidgetSupportTicketMock).not.toHaveBeenCalled();
|
||||
|
||||
+87
-6
@@ -2,44 +2,84 @@ 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";
|
||||
import { createWidgetSupportTicket, WIDGET_SUPPORT_TICKET_LEGACY_SOURCES } from "@hivegrid/db/support-tickets.server";
|
||||
import { createFileFromBytes } from "~/utils/integrations/fileService.server";
|
||||
import { withTenantRLS } from "~/utils/db/.server/tenantRls.server";
|
||||
|
||||
const MAX_FEEDBACK_PER_DAY = 3;
|
||||
const FEEDBACK_RATE_LIMIT_WINDOW_HOURS = 24;
|
||||
|
||||
/**
|
||||
* AK-P21 — the feedback widget lets the reporter attach ONE image instead of
|
||||
* capturing a screenshot itself (decision 2026-07-21, see the ticket doc).
|
||||
* The cap is deliberately small: the whole multipart body is buffered in
|
||||
* memory by the route action.
|
||||
*/
|
||||
export const MAX_FEEDBACK_ATTACHMENT_BYTES = 5 * 1024 * 1024;
|
||||
export const ALLOWED_FEEDBACK_ATTACHMENT_MIME_TYPES = ["image/png", "image/jpeg", "image/webp", "image/gif"] as const;
|
||||
|
||||
export type FeedbackAttachmentInput = {
|
||||
filename: string;
|
||||
mimeType: string;
|
||||
bytes: Uint8Array;
|
||||
};
|
||||
|
||||
export class FeedbackUserError extends Error {
|
||||
override readonly name = "FeedbackUserError";
|
||||
|
||||
constructor(
|
||||
readonly messageKey:
|
||||
| "feedback.messageRequired"
|
||||
| "feedback.limitReached"
|
||||
| "feedback.attachment.empty"
|
||||
| "feedback.attachment.tooLarge"
|
||||
| "feedback.attachment.unsupportedType",
|
||||
readonly status: 400 | 429
|
||||
) {
|
||||
super(messageKey);
|
||||
}
|
||||
}
|
||||
|
||||
export function isAllowedFeedbackAttachmentMimeType(mimeType: string): boolean {
|
||||
return (ALLOWED_FEEDBACK_ATTACHMENT_MIME_TYPES as readonly string[]).includes(mimeType.toLowerCase());
|
||||
}
|
||||
|
||||
async function send({
|
||||
t,
|
||||
tenantId,
|
||||
userId,
|
||||
message,
|
||||
fromUrl,
|
||||
attachment,
|
||||
}: {
|
||||
t: TFunction;
|
||||
tenantId: string | null;
|
||||
userId: string | null;
|
||||
message: string;
|
||||
fromUrl: string;
|
||||
attachment?: FeedbackAttachmentInput | null;
|
||||
}) {
|
||||
void t;
|
||||
const redactedMessage = redactFeedbackText(message);
|
||||
const redactedFromUrl = redactFeedbackText(fromUrl);
|
||||
if (!redactedMessage) {
|
||||
throw Error("Message is required");
|
||||
throw new FeedbackUserError("feedback.messageRequired", 400);
|
||||
}
|
||||
|
||||
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 } },
|
||||
where: { tenantId, source: { in: [...WIDGET_SUPPORT_TICKET_LEGACY_SOURCES] }, createdAt: { gte: createdSince } },
|
||||
});
|
||||
if (feedbackToday >= MAX_FEEDBACK_PER_DAY) {
|
||||
throw Error(t("feedback.limitReached"));
|
||||
throw new FeedbackUserError("feedback.limitReached", 429);
|
||||
}
|
||||
} else if (userId) {
|
||||
const feedbackToday = await db.supportTicket.count({
|
||||
where: { userId, source: "widget", createdAt: { gte: createdSince } },
|
||||
where: { userId, source: { in: [...WIDGET_SUPPORT_TICKET_LEGACY_SOURCES] }, createdAt: { gte: createdSince } },
|
||||
});
|
||||
if (feedbackToday >= MAX_FEEDBACK_PER_DAY) {
|
||||
throw Error(t("feedback.limitReached"));
|
||||
throw new FeedbackUserError("feedback.limitReached", 429);
|
||||
}
|
||||
}
|
||||
const ticket = await createWidgetSupportTicket({
|
||||
@@ -49,6 +89,35 @@ async function send({
|
||||
message: redactedMessage,
|
||||
fromUrl: redactedFromUrl,
|
||||
});
|
||||
// The attachment is stored AFTER the ticket so a rejected upload never costs
|
||||
// the reporter their written feedback. createFileFromBytes is fail-closed:
|
||||
// it runs the magic-bytes allow-list before the storage PUT and enqueues the
|
||||
// ClamAV scan, so the file stays scanStatus=pending until the worker clears
|
||||
// it. A File is always tenant-scoped, so tenant-less feedback cannot carry
|
||||
// one.
|
||||
if (attachment && tenantId && userId) {
|
||||
assertFeedbackAttachment(attachment);
|
||||
const file = await createFileFromBytes({
|
||||
tenantId,
|
||||
userId,
|
||||
filename: attachment.filename,
|
||||
mimeType: attachment.mimeType,
|
||||
bytes: attachment.bytes,
|
||||
scanContext: "mediaLibrary",
|
||||
});
|
||||
await withTenantRLS(tenantId, (tx) =>
|
||||
tx.fileEntityLink.create({
|
||||
data: {
|
||||
tenantId,
|
||||
createdByUserId: userId,
|
||||
fileId: file.id,
|
||||
entityType: "support_ticket",
|
||||
entityId: ticket.ticketId,
|
||||
linkRole: "feedback_attachment",
|
||||
},
|
||||
})
|
||||
);
|
||||
}
|
||||
await NotificationService.sendToRoles({
|
||||
channel: "admin-users",
|
||||
tenantId: null,
|
||||
@@ -59,6 +128,18 @@ async function send({
|
||||
return ticket;
|
||||
}
|
||||
|
||||
function assertFeedbackAttachment(attachment: FeedbackAttachmentInput): void {
|
||||
if (attachment.bytes.byteLength === 0) {
|
||||
throw new FeedbackUserError("feedback.attachment.empty", 400);
|
||||
}
|
||||
if (attachment.bytes.byteLength > MAX_FEEDBACK_ATTACHMENT_BYTES) {
|
||||
throw new FeedbackUserError("feedback.attachment.tooLarge", 400);
|
||||
}
|
||||
if (!isAllowedFeedbackAttachmentMimeType(attachment.mimeType)) {
|
||||
throw new FeedbackUserError("feedback.attachment.unsupportedType", 400);
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteMany({ ids }: { ids: string[] }) {
|
||||
return await db.feedback.deleteMany({
|
||||
where: {
|
||||
|
||||
Reference in new issue
Block a user