CalendarEvent and FeedCard now automatically get a ClientTimelineEntry when created/updated with a contactId. This ensures events created by the agent (or any other code path) appear in the contact timeline without needing explicit upsertClientTimelineEntry calls. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
import { PrismaClient } from "@prisma/client";
|
|
|
|
declare global {
|
|
// eslint-disable-next-line no-var
|
|
var __prisma: PrismaClient | undefined;
|
|
}
|
|
|
|
export const prisma =
|
|
globalThis.__prisma ??
|
|
new PrismaClient({
|
|
log: ["error", "warn"],
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Auto-sync ClientTimelineEntry for CalendarEvent, WorkspaceDocument, FeedCard
|
|
// ---------------------------------------------------------------------------
|
|
const TIMELINE_MODEL_MAP: Record<string, string> = {
|
|
CalendarEvent: "CALENDAR_EVENT",
|
|
FeedCard: "RECOMMENDATION",
|
|
};
|
|
|
|
prisma.$use(async (params, next) => {
|
|
const result = await next(params);
|
|
|
|
const contentType = params.model ? TIMELINE_MODEL_MAP[params.model] : undefined;
|
|
if (!contentType) return result;
|
|
if (params.action !== "create" && params.action !== "update" && params.action !== "upsert") return result;
|
|
if (!result || typeof result !== "object") return result;
|
|
|
|
const row = result as Record<string, any>;
|
|
const teamId = row.teamId as string | undefined;
|
|
const contactId = row.contactId as string | undefined;
|
|
const id = row.id as string | undefined;
|
|
if (!teamId || !contactId || !id) return result;
|
|
|
|
const datetime = row.startsAt ?? row.happenedAt ?? row.createdAt ?? new Date();
|
|
|
|
prisma.clientTimelineEntry
|
|
.upsert({
|
|
where: {
|
|
teamId_contentType_contentId: { teamId, contentType, contentId: id },
|
|
},
|
|
create: { teamId, contactId, contentType, contentId: id, datetime },
|
|
update: { contactId, datetime },
|
|
select: { id: true },
|
|
})
|
|
.catch(() => {});
|
|
|
|
return result;
|
|
});
|
|
|
|
if (process.env.NODE_ENV !== "production") {
|
|
globalThis.__prisma = prisma;
|
|
}
|
|
|