Files

131 lines
3.9 KiB
TypeScript

import { Event } from "@hivegrid/db/prisma-browser";
import { neutralizeSpreadsheetFormula } from "@hivegrid/foundation/spreadsheet-safety";
import { FiltersDto } from "~/application/dtos/data/FiltersDto";
import { PaginationResultDto } from "~/application/dtos/data/PaginationResultDto";
import { db } from "~/utils/db.server";
import { TenantSimple } from "~/utils/db/tenants.db.server";
import { UserSimple } from "~/utils/db/users.db.server";
import RowFiltersHelper from "~/utils/helpers/RowFiltersHelper";
import TenantModelHelper from "~/utils/helpers/models/TenantModelHelper";
import UserModelHelper from "~/utils/helpers/models/UserModelHelper";
export type EventWithAttempts = Event & {
tenant: TenantSimple | null;
user: UserSimple | null;
// attempts: {
// id: string;
// startedAt: Date | null;
// finishedAt: Date | null;
// endpoint: string;
// success: boolean | null;
// status: number | null;
// message: string | null;
// }[];
};
export type EventWithDetails = Event & {
tenant: TenantSimple | null;
user: UserSimple | null;
// attempts: EventWebhookAttempt[];
};
export async function getEvents(
pagination: { current: { page: number; pageSize: number }; filters: FiltersDto },
tenantId?: string | null
): Promise<{
items: EventWithAttempts[];
pagination: PaginationResultDto;
}> {
const whereFilters = RowFiltersHelper.getFiltersCondition(pagination.filters);
const items = await db.event.findMany({
take: pagination.current.pageSize,
skip: pagination.current.pageSize * (pagination.current.page - 1),
where: {
AND: [whereFilters, { tenantId }],
},
include: {
tenant: { select: TenantModelHelper.selectSimpleTenantProperties },
user: { select: UserModelHelper.selectSimpleUserProperties },
// attempts: {
// select: {
// id: true,
// startedAt: true,
// finishedAt: true,
// endpoint: true,
// success: true,
// status: true,
// message: true,
// },
// },
},
orderBy: {
createdAt: "desc",
},
});
const totalItems = await db.event.count({
where: {
tenantId,
...whereFilters,
},
});
const totalPages = Math.ceil(totalItems / pagination.current.pageSize);
return {
items,
pagination: {
totalItems,
totalPages,
page: pagination.current.page,
pageSize: pagination.current.pageSize,
sortedBy: undefined,
query: undefined,
},
};
}
const EVENTS_CSV_HEADERS = ["createdAt", "event", "description", "user", "tenant", "data"];
function escapeCsvCell(value: string | null | undefined): string {
const safeValue = neutralizeSpreadsheetFormula(value ?? "");
if (/[",\n\r]/.test(safeValue)) {
return `"${safeValue.replace(/"/g, '""')}"`;
}
return safeValue;
}
export function serializeEventsCsv(items: readonly EventWithAttempts[]): string {
const lines = [EVENTS_CSV_HEADERS.map(escapeCsvCell).join(",")];
for (const item of items) {
lines.push(
[item.createdAt.toISOString(), item.name, item.description, item.user?.email, item.tenant?.name, item.data].map(escapeCsvCell).join(",")
);
}
return `${lines.join("\n")}\n`;
}
export async function getEvent(id: string, tenantId?: string | null): Promise<EventWithDetails | null> {
return await db.event.findFirst({
where: {
id,
...(tenantId !== undefined ? { tenantId } : {}),
},
include: {
tenant: { select: TenantModelHelper.selectSimpleTenantProperties },
user: { select: UserModelHelper.selectSimpleUserProperties },
// attempts: true,
},
});
}
export async function createEvent(data: { tenantId: string | null; userId: string | null; name: string; data: string; description: string | null }) {
return await db.event.create({
data: {
tenantId: data.tenantId,
userId: data.userId,
name: data.name,
data: data.data,
description: data.description,
},
});
}