Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+90
-52
@@ -1,19 +1,67 @@
|
||||
import { db } from "../db.server";
|
||||
import {
|
||||
createRegistrationCapability,
|
||||
findRegistrationBillingPreferenceCapability,
|
||||
findRegistrationByTokenCapability,
|
||||
findRegistrationSummaryByEmail,
|
||||
rotateRegistrationVerificationCapability,
|
||||
updateRegistrationCapability,
|
||||
} from "@hivegrid/db/pretenant-security.server";
|
||||
import { deriveVerificationCreationRequestKey } from "~/domain/tenant-bootstrap/signupCreationRequestKey.server";
|
||||
|
||||
export async function getRegistrationByEmail(email: string) {
|
||||
return await db.registration.findFirst({
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
});
|
||||
export const REGISTRATION_VERIFICATION_TTL_MS = 24 * 60 * 60 * 1000;
|
||||
const REGISTRATION_VERIFICATION_TOKEN_PATTERN = /^[a-f0-9]{40}$/iu;
|
||||
type RegistrationRecord = NonNullable<Awaited<ReturnType<typeof findRegistrationByTokenCapability>>>;
|
||||
|
||||
export type RegistrationVerificationState =
|
||||
| { status: "invalid" }
|
||||
| { status: "expired"; registration: RegistrationRecord }
|
||||
| { status: "used"; registration: RegistrationRecord }
|
||||
| { status: "pending"; registration: RegistrationRecord };
|
||||
|
||||
export class RegistrationVerificationRotationConflictError extends Error {
|
||||
constructor() {
|
||||
super("Registration verification rotation capability was rejected");
|
||||
this.name = "RegistrationVerificationRotationConflictError";
|
||||
}
|
||||
}
|
||||
|
||||
export async function getRegistrationByToken(token: string) {
|
||||
return await db.registration.findUnique({
|
||||
where: {
|
||||
token,
|
||||
},
|
||||
});
|
||||
export async function getRegistrationByEmail(email: string) {
|
||||
return await findRegistrationSummaryByEmail(email);
|
||||
}
|
||||
|
||||
export async function getRegistrationBillingPreference(tenantId: string, userId: string) {
|
||||
return await findRegistrationBillingPreferenceCapability(tenantId, userId);
|
||||
}
|
||||
|
||||
export async function getRegistrationByToken(token: string, now = new Date()) {
|
||||
const state = await getRegistrationVerificationStateByToken(token, now);
|
||||
return state.status === "pending" || state.status === "used" ? state.registration : null;
|
||||
}
|
||||
|
||||
export async function getRegistrationVerificationStateByToken(token: string, now = new Date()): Promise<RegistrationVerificationState> {
|
||||
if (!REGISTRATION_VERIFICATION_TOKEN_PATTERN.test(token)) return { status: "invalid" };
|
||||
const registration = await findRegistrationByTokenCapability(token);
|
||||
if (!registration) return { status: "invalid" };
|
||||
if (registration.createdTenantId) return { status: "used", registration };
|
||||
if (!isRegistrationVerificationFresh(registration.createdAt, now)) return { status: "expired", registration };
|
||||
return { status: "pending", registration };
|
||||
}
|
||||
|
||||
/** Minimal projection for the public verification page. */
|
||||
export async function getRegistrationVerificationViewByToken(token: string, now = new Date()) {
|
||||
const state = await getRegistrationVerificationStateByToken(token, now);
|
||||
if (state.status !== "pending") return { status: state.status } as const;
|
||||
return {
|
||||
status: "pending" as const,
|
||||
email: state.registration.email,
|
||||
creationRequestKey: deriveVerificationCreationRequestKey(state.registration.id),
|
||||
};
|
||||
}
|
||||
|
||||
export function isRegistrationVerificationFresh(createdAt: Date | string, now = new Date()): boolean {
|
||||
const createdAtMs = new Date(createdAt).getTime();
|
||||
const nowMs = now.getTime();
|
||||
return Number.isFinite(createdAtMs) && nowMs >= createdAtMs && nowMs - createdAtMs < REGISTRATION_VERIFICATION_TTL_MS;
|
||||
}
|
||||
|
||||
export async function createRegistration(data: {
|
||||
@@ -23,53 +71,43 @@ export async function createRegistration(data: {
|
||||
token: string;
|
||||
company: string | null;
|
||||
selectedSubscriptionPriceId: string | null;
|
||||
selectedOfficeSeatQuantity: number | null;
|
||||
selectedFieldSeatQuantity: number | null;
|
||||
leadConversionLeadId?: string | null;
|
||||
leadConversionToken?: string | null;
|
||||
leadConversionPlanId?: string | null;
|
||||
ipAddress: string | null;
|
||||
slug: string | null;
|
||||
}) {
|
||||
return await db.registration.create({
|
||||
data: {
|
||||
email: data.email,
|
||||
firstName: data.firstName,
|
||||
lastName: data.lastName,
|
||||
token: data.token,
|
||||
company: data.company,
|
||||
selectedSubscriptionPriceId: data.selectedSubscriptionPriceId,
|
||||
leadConversionLeadId: data.leadConversionLeadId ?? null,
|
||||
leadConversionToken: data.leadConversionToken ?? null,
|
||||
leadConversionPlanId: data.leadConversionPlanId ?? null,
|
||||
ipAddress: data.ipAddress,
|
||||
slug: data.slug,
|
||||
},
|
||||
return await createRegistrationCapability({
|
||||
email: data.email,
|
||||
firstName: data.firstName,
|
||||
lastName: data.lastName,
|
||||
token: data.token,
|
||||
company: data.company,
|
||||
selectedSubscriptionPriceId: data.selectedSubscriptionPriceId,
|
||||
selectedOfficeSeatQuantity: data.selectedOfficeSeatQuantity,
|
||||
selectedFieldSeatQuantity: data.selectedFieldSeatQuantity,
|
||||
leadConversionLeadId: data.leadConversionLeadId ?? null,
|
||||
leadConversionToken: data.leadConversionToken ?? null,
|
||||
leadConversionPlanId: data.leadConversionPlanId ?? null,
|
||||
ipAddress: data.ipAddress,
|
||||
slug: data.slug,
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateRegistration(
|
||||
id: string,
|
||||
data: {
|
||||
firstName?: string;
|
||||
lastName?: string;
|
||||
company?: string | null;
|
||||
createdTenantId?: string | null;
|
||||
token?: string;
|
||||
leadConversionLeadId?: string | null;
|
||||
leadConversionToken?: string | null;
|
||||
leadConversionPlanId?: string | null;
|
||||
export async function rotateRegistrationVerification(id: string, expectedToken: string, token: string) {
|
||||
const updated = await rotateRegistrationVerificationCapability({ id, expectedToken, token });
|
||||
if (!updated) {
|
||||
throw new RegistrationVerificationRotationConflictError();
|
||||
}
|
||||
) {
|
||||
return await db.registration.update({
|
||||
where: { id },
|
||||
data: {
|
||||
firstName: data.firstName,
|
||||
lastName: data.lastName,
|
||||
company: data.company,
|
||||
createdTenantId: data.createdTenantId,
|
||||
token: data.token,
|
||||
leadConversionLeadId: data.leadConversionLeadId,
|
||||
leadConversionToken: data.leadConversionToken,
|
||||
leadConversionPlanId: data.leadConversionPlanId,
|
||||
},
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function updateRegistration(id: string, capability: { expectedToken: string }, data: { createdTenantId: string }) {
|
||||
const updated = await updateRegistrationCapability({ id, expectedToken: capability.expectedToken, patch: data });
|
||||
if (!updated) {
|
||||
throw new Error("Registration update capability was rejected");
|
||||
}
|
||||
return updated;
|
||||
}
|
||||
Reference in new issue
Block a user