270 lines
8.0 KiB
TypeScript
270 lines
8.0 KiB
TypeScript
import { getDbClient } from "./client.server.js";
|
|
import type { AdminUser, Tenant, TenantInboundAddress, TenantIpAddress, 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;
|
|
};
|
|
|
|
export type LegacyTenantSummary = Pick<Tenant, "id" | "name" | "slug">;
|
|
export type PlatformOperatorIdentity = { userId: AdminUser["id"] };
|
|
|
|
export type LegacyTenantMemberSimple = Pick<
|
|
Tenant,
|
|
"id" | "name" | "slug" | "icon" | "logo" | "deactivatedReason" | "active" | "theme" | "themeConfig"
|
|
>;
|
|
|
|
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;
|
|
};
|
|
|
|
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 };
|
|
}): 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>;
|
|
};
|
|
};
|
|
|
|
export type PlatformOperatorReaderClient = {
|
|
adminUser: {
|
|
findUnique(args: { where: { id: string }; select: { id: true } }): Promise<Pick<AdminUser, "id"> | null>;
|
|
};
|
|
};
|
|
|
|
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,
|
|
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,
|
|
},
|
|
},
|
|
},
|
|
} as const;
|
|
|
|
export async function adminGetAllLegacyTenants(
|
|
client: LegacyTenantAccountReaderClient = getDbClient() as unknown as LegacyTenantAccountReaderClient,
|
|
): Promise<LegacyTenantWithDetails[]> {
|
|
return client.tenant.findMany({
|
|
include: legacyTenantDetailsInclude,
|
|
orderBy: { createdAt: "desc" },
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Revalidates the caller-bound app.user_id against the RLS-protected
|
|
* tenant-less AdminUser identity. Callers must bind the same userId before invoking this
|
|
* reader; a supplied identifier alone never grants platform access.
|
|
*/
|
|
export async function findPlatformOperatorByUserId(
|
|
userId: string,
|
|
client: PlatformOperatorReaderClient = getDbClient() as unknown as PlatformOperatorReaderClient,
|
|
): Promise<PlatformOperatorIdentity | null> {
|
|
const normalizedUserId = userId.trim();
|
|
if (!normalizedUserId || normalizedUserId.length > 191 || normalizedUserId.includes("\u0000")) {
|
|
return null;
|
|
}
|
|
const operator = await client.adminUser.findUnique({
|
|
where: { id: normalizedUserId },
|
|
select: { id: true },
|
|
});
|
|
return operator ? { userId: operator.id } : null;
|
|
}
|
|
|
|
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,
|
|
},
|
|
});
|
|
}
|