Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -2,11 +2,14 @@ import { createHash, randomBytes, timingSafeEqual } from "node:crypto";
import { getDbClient } from "./client.server.js";
import type { Prisma, TenantUserInvitation } from "./prisma-browser.js";
import { runWithRlsContext } from "./rls-context.server.js";
import { resolveWorkspaceInviteRlsIdentity } from "./rls-resolvers.server.js";
export const WORKSPACE_INVITE_STATUS_PENDING = "pending" as const;
export const WORKSPACE_INVITE_STATUS_ACCEPTED = "accepted" as const;
export const WORKSPACE_INVITE_STATUS_REVOKED = "revoked" as const;
export const WORKSPACE_INVITE_STATUS_EXPIRED = "expired" as const;
export const WORKSPACE_INVITE_STATUS_DELIVERY_FAILED = "delivery_failed" as const;
export const WORKSPACE_INVITE_TOKEN_PREFIX_LENGTH = 12;
export const WORKSPACE_INVITE_TOKEN_BYTE_LENGTH = 32;
@@ -19,13 +22,10 @@ export type WorkspaceInviteStatus =
| typeof WORKSPACE_INVITE_STATUS_PENDING
| typeof WORKSPACE_INVITE_STATUS_ACCEPTED
| typeof WORKSPACE_INVITE_STATUS_REVOKED
| typeof WORKSPACE_INVITE_STATUS_EXPIRED;
| typeof WORKSPACE_INVITE_STATUS_EXPIRED
| typeof WORKSPACE_INVITE_STATUS_DELIVERY_FAILED;
export type WorkspaceInviteErrorCode =
| "duplicate_active_invite"
| "invite_not_found"
| "invite_expired"
| "invite_already_finalized";
export type WorkspaceInviteErrorCode = "duplicate_active_invite" | "invite_not_found" | "invite_expired" | "invite_already_finalized";
export class WorkspaceInviteError extends Error {
readonly code: WorkspaceInviteErrorCode;
@@ -85,6 +85,14 @@ export type WorkspaceInviteAcceptInput = {
inviteId: string;
token: string;
acceptedByUserId: string;
/** Set only when accepting this invite created the accepting user account. */
createdUserId?: string;
/**
* Bind a previously unbound created-user marker as part of the caller's
* existing user-creation transaction. This is deliberately rejected unless
* an explicit transaction-compatible client is supplied.
*/
bindCreatedUserId?: boolean;
now?: Date;
};
@@ -112,6 +120,7 @@ export type WorkspaceInviteClient = {
}): Promise<TenantUserInvitation[]>;
create(args: { data: Prisma.TenantUserInvitationUncheckedCreateInput }): Promise<TenantUserInvitation>;
update(args: { where: { id: string }; data: Prisma.TenantUserInvitationUncheckedUpdateInput }): Promise<TenantUserInvitation>;
updateMany(args: { where: Prisma.TenantUserInvitationWhereInput; data: Prisma.TenantUserInvitationUncheckedUpdateManyInput }): Promise<{ count: number }>;
};
};
@@ -146,6 +155,10 @@ function resolveInviteStatus(invite: Pick<TenantUserInvitation, "status" | "expi
return WORKSPACE_INVITE_STATUS_ACCEPTED;
}
if (invite.status === WORKSPACE_INVITE_STATUS_DELIVERY_FAILED) {
return WORKSPACE_INVITE_STATUS_DELIVERY_FAILED;
}
if (invite.status === WORKSPACE_INVITE_STATUS_REVOKED || invite.revokedAt) {
return WORKSPACE_INVITE_STATUS_REVOKED;
}
@@ -188,6 +201,19 @@ export async function createWorkspaceInvite(input: WorkspaceInviteCreateInput, c
const db = getClient(client);
const now = input.now ?? new Date();
const email = normalizeEmail(input.email);
await db.tenantUserInvitation.updateMany({
where: {
tenantId: input.tenantId,
email,
pending: true,
status: WORKSPACE_INVITE_STATUS_PENDING,
expiresAt: { lte: now },
},
data: {
pending: false,
status: WORKSPACE_INVITE_STATUS_EXPIRED,
},
});
const existing = await db.tenantUserInvitation.findFirst({
where: {
tenantId: input.tenantId,
@@ -204,27 +230,39 @@ export async function createWorkspaceInvite(input: WorkspaceInviteCreateInput, c
const token = input.token ?? generateWorkspaceInviteToken();
const tokenHash = hashWorkspaceInviteToken(token);
const invite = await db.tenantUserInvitation.create({
data: {
tenantId: input.tenantId,
email,
firstName: input.firstName,
lastName: input.lastName,
type: input.type,
pending: true,
status: WORKSPACE_INVITE_STATUS_PENDING,
tokenHash,
tokenPrefix: getWorkspaceInviteTokenPrefix(token),
// DoD "Einladung 48h": Default-Ablauf, wenn der Aufrufer nichts vorgibt (kein null mehr).
expiresAt: input.expiresAt ?? new Date(now.getTime() + WORKSPACE_INVITE_TTL_MS),
createdUserId: input.createdUserId ?? null,
fromUserId: input.fromUserId ?? null,
},
});
let invite: TenantUserInvitation;
try {
invite = await db.tenantUserInvitation.create({
data: {
tenantId: input.tenantId,
email,
firstName: input.firstName,
lastName: input.lastName,
type: input.type,
pending: true,
status: WORKSPACE_INVITE_STATUS_PENDING,
tokenHash,
tokenPrefix: getWorkspaceInviteTokenPrefix(token),
// DoD "Einladung 48h": Default-Ablauf, wenn der Aufrufer nichts vorgibt (kein null mehr).
expiresAt: input.expiresAt ?? new Date(now.getTime() + WORKSPACE_INVITE_TTL_MS),
createdUserId: input.createdUserId ?? null,
fromUserId: input.fromUserId ?? null,
},
});
} catch (error) {
if (isPrismaUniqueConstraintError(error)) {
throw new WorkspaceInviteError("duplicate_active_invite");
}
throw error;
}
return { ...serializeWorkspaceInvite(invite, now), token };
}
function isPrismaUniqueConstraintError(error: unknown): boolean {
return typeof error === "object" && error !== null && "code" in error && error.code === "P2002";
}
export async function listWorkspaceInvites(input: WorkspaceInviteListInput, client?: WorkspaceInviteClient): Promise<WorkspaceInviteDto[]> {
const db = getClient(client);
const invites = await db.tenantUserInvitation.findMany({
@@ -240,19 +278,34 @@ export async function listWorkspaceInvites(input: WorkspaceInviteListInput, clie
}
export async function getWorkspaceInviteByToken(input: WorkspaceInviteLookupInput, client?: WorkspaceInviteClient): Promise<WorkspaceInviteDto | null> {
if (!client) {
const identity = await resolveWorkspaceInviteRlsIdentity(hashWorkspaceInviteToken(input.token));
if (!identity) return null;
return runWithRlsContext({ tenantId: identity.tenantId }, () => getWorkspaceInviteByToken(input, getDbClient()));
}
const db = getClient(client);
const tokenHash = hashWorkspaceInviteToken(input.token);
const candidates = await db.tenantUserInvitation.findMany({
where: {
tokenPrefix: getWorkspaceInviteTokenPrefix(input.token),
},
orderBy: [{ status: "asc" }, { expiresAt: "asc" }, { id: "asc" }],
const invite = await db.tenantUserInvitation.findFirst({
where: { tokenHash },
orderBy: { id: "asc" },
});
const invite = candidates.find((candidate) => tokenHashesMatch(candidate.tokenHash, tokenHash));
return invite ? serializeWorkspaceInvite(invite, input.now) : null;
return invite && tokenHashesMatch(invite.tokenHash, tokenHash) ? serializeWorkspaceInvite(invite, input.now) : null;
}
export async function acceptWorkspaceInvite(input: WorkspaceInviteAcceptInput, client?: WorkspaceInviteClient): Promise<WorkspaceInviteDto> {
if (input.createdUserId !== undefined && input.createdUserId !== input.acceptedByUserId) {
throw new WorkspaceInviteError("invite_not_found");
}
if (input.bindCreatedUserId && (!client || input.createdUserId === undefined)) {
throw new WorkspaceInviteError("invite_not_found");
}
if (!client) {
const identity = await resolveWorkspaceInviteRlsIdentity(hashWorkspaceInviteToken(input.token));
if (!identity || identity.tenantId !== input.tenantId || identity.resourceId !== input.inviteId) {
throw new WorkspaceInviteError("invite_not_found");
}
return runWithRlsContext({ tenantId: identity.tenantId, userId: input.acceptedByUserId }, () => acceptWorkspaceInvite(input, getDbClient()));
}
const db = getClient(client);
const now = input.now ?? new Date();
const tokenHash = hashWorkspaceInviteToken(input.token);
@@ -260,7 +313,7 @@ export async function acceptWorkspaceInvite(input: WorkspaceInviteAcceptInput, c
where: {
id: input.inviteId,
tenantId: input.tenantId,
tokenPrefix: getWorkspaceInviteTokenPrefix(input.token),
tokenHash,
},
orderBy: { id: "asc" },
});
@@ -269,6 +322,12 @@ export async function acceptWorkspaceInvite(input: WorkspaceInviteAcceptInput, c
throw new WorkspaceInviteError("invite_not_found");
}
const createdUserId = input.createdUserId ?? null;
const expectedCreatedUserId = input.bindCreatedUserId ? null : createdUserId;
if (candidate.createdUserId !== expectedCreatedUserId) {
throw new WorkspaceInviteError("invite_not_found");
}
const status = resolveInviteStatus(candidate, now);
if (status === WORKSPACE_INVITE_STATUS_EXPIRED) {
throw new WorkspaceInviteError("invite_expired");
@@ -277,15 +336,54 @@ export async function acceptWorkspaceInvite(input: WorkspaceInviteAcceptInput, c
throw new WorkspaceInviteError("invite_already_finalized");
}
const invite = await db.tenantUserInvitation.update({
where: { id: candidate.id },
const transition = await db.tenantUserInvitation.updateMany({
where: {
id: candidate.id,
tenantId: input.tenantId,
tokenHash,
pending: true,
status: WORKSPACE_INVITE_STATUS_PENDING,
acceptedAt: null,
revokedAt: null,
acceptedByUserId: null,
revokedByUserId: null,
createdUserId: expectedCreatedUserId,
OR: [{ expiresAt: null }, { expiresAt: { gt: now } }],
},
data: {
status: WORKSPACE_INVITE_STATUS_ACCEPTED,
pending: false,
acceptedAt: now,
acceptedByUserId: input.acceptedByUserId,
...(input.createdUserId !== undefined ? { createdUserId: input.createdUserId } : {}),
},
});
const invite = await db.tenantUserInvitation.findFirst({
where: { id: candidate.id, tenantId: input.tenantId, tokenHash },
orderBy: { id: "asc" },
});
if (transition.count !== 1) {
if (!invite || !tokenHashesMatch(invite.tokenHash, tokenHash)) {
throw new WorkspaceInviteError("invite_not_found");
}
if (resolveInviteStatus(invite, now) === WORKSPACE_INVITE_STATUS_EXPIRED) {
throw new WorkspaceInviteError("invite_expired");
}
if (resolveInviteStatus(invite, now) !== WORKSPACE_INVITE_STATUS_PENDING) {
throw new WorkspaceInviteError("invite_already_finalized");
}
throw new WorkspaceInviteError("invite_not_found");
}
if (
!invite ||
!tokenHashesMatch(invite.tokenHash, tokenHash) ||
resolveInviteStatus(invite, now) !== WORKSPACE_INVITE_STATUS_ACCEPTED ||
invite.acceptedByUserId !== input.acceptedByUserId ||
invite.createdUserId !== createdUserId
) {
throw new WorkspaceInviteError("invite_not_found");
}
return serializeWorkspaceInvite(invite, now);
}
@@ -309,8 +407,18 @@ export async function revokeWorkspaceInvite(input: WorkspaceInviteRevokeInput, c
throw new WorkspaceInviteError("invite_already_finalized");
}
const invite = await db.tenantUserInvitation.update({
where: { id: candidate.id },
const transition = await db.tenantUserInvitation.updateMany({
where: {
id: candidate.id,
tenantId: input.tenantId,
pending: true,
status: WORKSPACE_INVITE_STATUS_PENDING,
acceptedAt: null,
revokedAt: null,
acceptedByUserId: null,
revokedByUserId: null,
OR: [{ expiresAt: null }, { expiresAt: { gt: now } }],
},
data: {
status: WORKSPACE_INVITE_STATUS_REVOKED,
pending: false,
@@ -318,6 +426,16 @@ export async function revokeWorkspaceInvite(input: WorkspaceInviteRevokeInput, c
revokedByUserId: input.revokedByUserId,
},
});
const invite = await db.tenantUserInvitation.findFirst({
where: { id: candidate.id, tenantId: input.tenantId },
orderBy: { id: "asc" },
});
if (transition.count !== 1) {
throw new WorkspaceInviteError("invite_already_finalized");
}
if (!invite || resolveInviteStatus(invite, now) !== WORKSPACE_INVITE_STATUS_REVOKED || invite.revokedByUserId !== input.revokedByUserId) {
throw new WorkspaceInviteError("invite_not_found");
}
return serializeWorkspaceInvite(invite, now);
}