Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/db/src/accounts.server.ts
T
2026-07-08 16:15:37 +02:00

250 lines
7.1 KiB
TypeScript

import { getDbClient } from "./client.server.js";
import type { Tenant, TenantInboundAddress, TenantIpAddress, TenantType, TenantUser } from "./prisma-browser.js";
import type { TenantSubscriptionWithDetails } from "./subscriptions.server.js";
const ACTIVE_TENANT_USER_STATUS = 2;
export type LegacyTenantSimple = Tenant & {
subscription: { id: string } | null;
types: TenantType[];
};
export type LegacyTenantSummary = Pick<Tenant, "id" | "name" | "slug">;
export type LegacyTenantMemberSimple = Pick<
Tenant,
"id" | "name" | "slug" | "icon" | "logo" | "deactivatedReason" | "active" | "theme" | "themeConfig"
> & {
types: TenantType[];
};
export type AuthBoundaryTenantMembership = TenantUser & {
tenant: Tenant;
user: LegacyTenantUserSummary;
};
export type LegacyTenantUserSummary = {
id: string;
email: string;
firstName: string;
lastName: string;
phone: string | null;
locale: string | null;
createdAt: Date;
};
export type LegacyTenantWithDetails = Tenant & {
inboundAddresses: TenantInboundAddress[];
users: (TenantUser & {
user: LegacyTenantUserSummary;
})[];
subscription: TenantSubscriptionWithDetails | null;
types: TenantType[];
};
export type LegacyTenantAccountReaderClient = {
tenant: {
findMany(args: {
include: typeof legacyTenantDetailsInclude;
orderBy: { createdAt: "desc" };
}): Promise<LegacyTenantWithDetails[]>;
findMany(args: {
select: { id: true; name: true; slug: true };
}): Promise<LegacyTenantSummary[]>;
findMany(args: {
where: { active: true; users: { some: { userId: string; status: typeof ACTIVE_TENANT_USER_STATUS } } };
select: typeof selectLegacyTenantMemberSimple;
orderBy: { name: "asc" };
}): Promise<LegacyTenantMemberSimple[]>;
findMany(args: {
where: { active: true };
select: typeof selectLegacyTenantMemberSimple;
orderBy: { name: "asc" };
}): Promise<LegacyTenantMemberSimple[]>;
findUnique(args: {
where: { id: string };
include: typeof legacyTenantDetailsInclude;
}): Promise<LegacyTenantWithDetails | null>;
findFirst(args: {
where: { OR: [{ id: string }, { slug: string }] };
include: { subscription: true; types: true };
}): Promise<LegacyTenantSimple | null>;
};
tenantUser: {
findFirst(args: {
where: { tenantId: string; userId: string; status: typeof ACTIVE_TENANT_USER_STATUS; tenant: { active: true } };
include: typeof includeAuthBoundaryTenantMembership;
}): Promise<AuthBoundaryTenantMembership | null>;
};
};
export type LegacyTenantIpAddressCreateData = {
ip: string;
fromUrl: string;
tenantId: string;
userId?: string | null;
apiKeyId?: string | null;
};
export type LegacyTenantIpAddressWriterClient = {
tenantIpAddress: {
findFirst(args: {
where: {
ip: string;
tenantId: string;
userId?: string | null;
apiKeyId?: string | null;
};
}): Promise<TenantIpAddress | null>;
create(args: { data: LegacyTenantIpAddressCreateData }): Promise<TenantIpAddress>;
};
};
const selectLegacyTenantUserSummary = {
id: true,
email: true,
firstName: true,
lastName: true,
phone: true,
locale: true,
createdAt: true,
} as const;
const selectLegacyTenantMemberSimple = {
id: true,
name: true,
slug: true,
icon: true,
logo: true,
deactivatedReason: true,
types: true,
active: true,
theme: true,
themeConfig: true,
} as const;
const includeLegacyTenantSubscriptionProductDetails = {
subscriptionProduct: { include: { features: true } },
prices: { include: { subscriptionPrice: true, subscriptionUsageBasedPrice: { include: { tiers: true } } } },
} as const;
const includeAuthBoundaryTenantMembership = {
tenant: true,
user: { select: selectLegacyTenantUserSummary },
} as const;
export const legacyTenantDetailsInclude = {
inboundAddresses: true,
users: {
include: {
user: {
select: selectLegacyTenantUserSummary,
},
},
},
subscription: {
include: {
products: {
include: includeLegacyTenantSubscriptionProductDetails,
},
},
},
types: true,
} as const;
export async function adminGetAllLegacyTenants(
client: LegacyTenantAccountReaderClient = getDbClient() as unknown as LegacyTenantAccountReaderClient,
): Promise<LegacyTenantWithDetails[]> {
return client.tenant.findMany({
include: legacyTenantDetailsInclude,
orderBy: { createdAt: "desc" },
});
}
export async function adminGetAllLegacyTenantSummaries(
client: LegacyTenantAccountReaderClient = getDbClient() as unknown as LegacyTenantAccountReaderClient,
): Promise<LegacyTenantSummary[]> {
return client.tenant.findMany({
select: { id: true, name: true, slug: true },
});
}
export async function getLegacyTenantMembershipsForUser(
userId: string,
client: LegacyTenantAccountReaderClient = getDbClient() as unknown as LegacyTenantAccountReaderClient,
): Promise<LegacyTenantMemberSimple[]> {
return client.tenant.findMany({
where: { active: true, users: { some: { userId, status: ACTIVE_TENANT_USER_STATUS } } },
select: selectLegacyTenantMemberSimple,
orderBy: { name: "asc" },
});
}
export async function adminGetAllActiveLegacyTenants(
client: LegacyTenantAccountReaderClient = getDbClient() as unknown as LegacyTenantAccountReaderClient,
): Promise<LegacyTenantMemberSimple[]> {
return client.tenant.findMany({
where: { active: true },
select: selectLegacyTenantMemberSimple,
orderBy: { name: "asc" },
});
}
export async function findTenantMembershipByIds(
tenantId: string,
userId: string,
client: LegacyTenantAccountReaderClient = getDbClient() as unknown as LegacyTenantAccountReaderClient,
): Promise<AuthBoundaryTenantMembership | null> {
return client.tenantUser.findFirst({
where: { tenantId, userId, status: ACTIVE_TENANT_USER_STATUS, tenant: { active: true } },
include: includeAuthBoundaryTenantMembership,
});
}
export async function createUniqueLegacyTenantIpAddress(
data: LegacyTenantIpAddressCreateData,
client: LegacyTenantIpAddressWriterClient = getDbClient() as unknown as LegacyTenantIpAddressWriterClient,
): Promise<TenantIpAddress | undefined> {
const existing = await client.tenantIpAddress.findFirst({
where: {
ip: data.ip,
tenantId: data.tenantId,
userId: data.userId,
apiKeyId: data.apiKeyId,
},
});
if (existing) {
return undefined;
}
return client.tenantIpAddress.create({
data,
});
}
export async function findLegacyTenantById(
id: string,
client: LegacyTenantAccountReaderClient = getDbClient() as unknown as LegacyTenantAccountReaderClient,
): Promise<LegacyTenantWithDetails | null> {
return client.tenant.findUnique({
where: { id },
include: legacyTenantDetailsInclude,
});
}
export async function findLegacyTenantByIdOrSlug(
idOrSlug: string,
client: LegacyTenantAccountReaderClient = getDbClient() as unknown as LegacyTenantAccountReaderClient,
): Promise<LegacyTenantSimple | null> {
return client.tenant.findFirst({
where: {
OR: [{ id: idOrSlug }, { slug: idOrSlug }],
},
include: {
subscription: true,
types: true,
},
});
}