Files

126 lines
4.4 KiB
TypeScript

// AK-P16a · calendar DB access (thin Prisma wrappers). Every read is tenant- AND
// user-scoped (leak-protection, DoD 3). Token ciphertext columns are NEVER
// selected into these read shapes — only the connect/refresh services touch them.
import { db } from "~/utils/db.server";
/** Connection fields safe to surface (no token ciphertext). */
export type CalendarConnectionListRow = {
id: string;
userId: string;
provider: "google";
externalAccountEmail: string;
status: "active" | "paused" | "error" | "revoked";
syncDirection: "bidirectional" | "hivegrid_to_external_only" | "external_to_hivegrid_only";
lastSyncAt: Date | null;
lastErrorText: string | null;
selectedCalendars: unknown;
};
const CONNECTION_SAFE_SELECT = {
id: true,
userId: true,
provider: true,
externalAccountEmail: true,
status: true,
syncDirection: true,
lastSyncAt: true,
lastErrorText: true,
selectedCalendars: true,
} as const;
/** The current user's own calendar connections (newest first). */
export async function listConnectionsForUser(tenantId: string, userId: string): Promise<CalendarConnectionListRow[]> {
return db.calendarConnection.findMany({
where: { tenantId, userId, archivedAt: null },
select: CONNECTION_SAFE_SELECT,
orderBy: { createdAt: "desc" },
}) as unknown as Promise<CalendarConnectionListRow[]>;
}
/** One connection by id (tenant-scoped). Returns userId so the route owner-checks. */
export async function getConnectionForTenant(tenantId: string, connectionId: string): Promise<CalendarConnectionListRow | null> {
return db.calendarConnection.findFirst({
where: { id: connectionId, tenantId, archivedAt: null },
select: CONNECTION_SAFE_SELECT,
}) as unknown as Promise<CalendarConnectionListRow | null>;
}
/** Exact full-row re-read after the narrow bootstrap resolver selected context. */
export async function getGoogleWebhookConnection(input: {
tenantId: string;
userId: string;
connectionId: string;
channelId: string;
channelToken: string;
}): Promise<{ id: string; tenantId: string; userId: string } | null> {
return db.calendarConnection.findFirst({
where: {
id: input.connectionId,
tenantId: input.tenantId,
userId: input.userId,
provider: "google",
status: "active",
archivedAt: null,
webhookSubscriptionId: input.channelId,
AND: { id: input.channelToken },
},
select: { id: true, tenantId: true, userId: true },
});
}
/** Persist an inbound webhook notification as pending (idempotency + retry). */
export async function recordWebhookEvent(input: {
tenantId: string;
connectionId: string;
notificationPayload: Record<string, unknown>;
externalResourceState: string | null;
}): Promise<void> {
await db.calendarWebhookEvent.create({
data: {
tenantId: input.tenantId,
connectionId: input.connectionId,
provider: "google",
notificationPayload: input.notificationPayload as never,
processingStatus: "pending",
externalResourceState: input.externalResourceState,
},
});
}
export type CalendarConflictRow = {
id: string;
resolution: "external_won" | "hivegrid_won" | "external_deleted" | "hivegrid_deleted";
descriptionText: string;
createdAt: Date;
};
/** The last `take` conflicts of a connection, newest first (DoD 4). */
export async function listConflictsForConnection(tenantId: string, connectionId: string, take = 50): Promise<CalendarConflictRow[]> {
return db.calendarConflict.findMany({
where: { tenantId, connectionId },
select: { id: true, resolution: true, descriptionText: true, createdAt: true },
orderBy: { createdAt: "desc" },
take,
}) as unknown as Promise<CalendarConflictRow[]>;
}
export type CalendarEventRow = {
id: string;
source: "hivegrid_internal" | "external_synced";
title: string | null;
startsAt: Date;
endsAt: Date;
isAllDay: boolean;
internalEntityLink: unknown;
};
/** The current user's own events in a window (for the calendar view, DoD 5). */
export async function listEventsForUser(tenantId: string, userId: string, rangeStart: Date, rangeEnd: Date): Promise<CalendarEventRow[]> {
return db.calendarEvent.findMany({
where: { tenantId, userId, archivedAt: null, startsAt: { gte: rangeStart, lte: rangeEnd } },
select: { id: true, source: true, title: true, startsAt: true, endsAt: true, isAllDay: true, internalEntityLink: true },
orderBy: { startsAt: "asc" },
}) as unknown as Promise<CalendarEventRow[]>;
}