Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
+115
@@ -0,0 +1,115 @@
|
||||
// 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>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the connection a Google push channel belongs to. The webhook entrypoint
|
||||
* has NO tenant context (Google calls a fixed URL), so this is a deliberate
|
||||
* cross-tenant lookup by the stored subscription id; the caller then scopes every
|
||||
* follow-up to the returned tenantId.
|
||||
*/
|
||||
export async function findConnectionByWebhookSubscription(subscriptionId: string): Promise<{ id: string; tenantId: string; userId: string } | null> {
|
||||
return db.calendarConnection.findFirst({
|
||||
where: { webhookSubscriptionId: subscriptionId, archivedAt: null },
|
||||
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[]>;
|
||||
}
|
||||
Reference in new issue
Block a user