Track HiveGrid Cloud planning reference

This commit is contained in:
david committed 2026-06-26 20:48:01 +02:00
1 parent b3d5ea6764
commit e341c5938a
5784 files changed
+1104929 -3

No files matched your search

@@ -0,0 +1,46 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
const createUniqueTenantIpAddressRecord = vi.fn();
vi.mock("@toolctrl/db/auth-boundary.server", () => ({
createUniqueTenantIpAddressRecord,
}));
vi.mock("~/utils/db.server", () => ({
db: {
tenantIpAddress: {
findMany: vi.fn(),
count: vi.fn(),
},
},
}));
vi.mock("~/utils/helpers/models/UserModelHelper", () => ({
default: { selectSimpleUserProperties: { id: true } },
}));
vi.mock("~/application/Constants", () => ({
default: { DEFAULT_PAGE_SIZE: 10 },
}));
describe("createUniqueTenantIpAddress", () => {
beforeEach(() => {
createUniqueTenantIpAddressRecord.mockReset();
});
it("delegates tenant shell IP writes through the auth boundary", async () => {
const data = {
ip: "127.0.0.1",
fromUrl: "/acme",
tenantId: "tenant_1",
userId: "user_1",
apiKeyId: null,
};
createUniqueTenantIpAddressRecord.mockResolvedValue({ id: "ip_1" });
const { createUniqueTenantIpAddress } = await import("./tenantIpAddress.db.server");
await expect(createUniqueTenantIpAddress(data)).resolves.toEqual({ id: "ip_1" });
expect(createUniqueTenantIpAddressRecord).toHaveBeenCalledWith(data);
});
});
@@ -0,0 +1,50 @@
import { createUniqueTenantIpAddressRecord } from "@toolctrl/db/auth-boundary.server";
import { ApiKey, Tenant, TenantIpAddress } from "@toolctrl/db/prisma-browser";
import { db } from "~/utils/db.server";
import { UserSimple } from "../users.db.server";
import UserModelHelper from "~/utils/helpers/models/UserModelHelper";
import { PaginationDto } from "~/application/dtos/data/PaginationDto";
import Constants from "~/application/Constants";
export type TenantIpAddressWithDetails = TenantIpAddress & {
tenant: Tenant;
user: UserSimple | null;
apiKey: ApiKey | null;
};
export async function getAllTenantIpAddresses(pagination?: {
page: number;
pageSize: number;
}): Promise<{ items: TenantIpAddressWithDetails[]; pagination: PaginationDto }> {
const items = await db.tenantIpAddress.findMany({
skip: pagination ? pagination?.pageSize * (pagination?.page - 1) : undefined,
take: pagination ? pagination?.pageSize : undefined,
include: {
tenant: true,
user: {
select: UserModelHelper.selectSimpleUserProperties,
},
apiKey: true,
},
orderBy: [
{
createdAt: "desc",
},
],
});
const totalItems = await db.tenantIpAddress.count({});
return {
items,
pagination: {
page: pagination?.page ?? 1,
pageSize: pagination?.pageSize ?? Constants.DEFAULT_PAGE_SIZE,
totalItems,
totalPages: Math.ceil(totalItems / (pagination?.pageSize ?? Constants.DEFAULT_PAGE_SIZE)),
},
};
}
export async function createUniqueTenantIpAddress(data: { ip: string; fromUrl: string; tenantId: string; userId?: string | null; apiKeyId?: string | null }) {
return createUniqueTenantIpAddressRecord(data);
}
@@ -0,0 +1,43 @@
// @vitest-environment node
import { beforeEach, describe, expect, it, vi } from "vitest";
const mocks = vi.hoisted(() => ({
tenantTypeFindMany: vi.fn(),
}));
vi.mock("~/utils/db.server", () => ({
db: {
tenantType: {
findMany: mocks.tenantTypeFindMany,
},
},
}));
import { getTenantTypeOptions } from "./tenantTypes.db.server";
describe("tenant type db helpers", () => {
beforeEach(() => {
mocks.tenantTypeFindMany.mockReset();
mocks.tenantTypeFindMany.mockResolvedValue([
{ id: "type_1", title: "Workspace" },
{ id: "type_2", title: "Customer" },
]);
});
it("loads lightweight tenant type options for account forms", async () => {
await expect(getTenantTypeOptions()).resolves.toEqual([
{ id: "type_1", title: "Workspace" },
{ id: "type_2", title: "Customer" },
]);
expect(mocks.tenantTypeFindMany).toHaveBeenCalledWith({
select: {
id: true,
title: true,
},
orderBy: {
createdAt: "asc",
},
});
});
});
@@ -0,0 +1,110 @@
import { Prisma, SubscriptionProduct, TenantType } from "@toolctrl/db/prisma-browser";
import { db } from "~/utils/db.server";
export type TenantTypeWithDetails = TenantType & {
subscriptionProducts: SubscriptionProduct[];
_count: { tenants: number };
};
const includeDetails = {
subscriptionProducts: true,
_count: { select: { tenants: true } },
};
export async function getAllTenantTypes(): Promise<TenantTypeWithDetails[]> {
return await db.tenantType.findMany({
include: {
subscriptionProducts: true,
_count: { select: { tenants: true } },
},
orderBy: {
createdAt: "asc",
},
});
}
export async function getTenantTypeOptions(): Promise<Array<Pick<TenantType, "id" | "title">>> {
return await db.tenantType.findMany({
select: {
id: true,
title: true,
},
orderBy: {
createdAt: "asc",
},
});
}
export async function getDefaultTenantTypes() {
return await db.tenantType.findMany({
where: { isDefault: true },
orderBy: {
createdAt: "asc",
},
});
}
export async function getTenantType(id: string): Promise<TenantTypeWithDetails | null> {
return await db.tenantType.findUnique({
where: { id: id },
include: includeDetails,
});
}
export async function getTenantTypeByTitle(title: string): Promise<TenantTypeWithDetails | null> {
return await db.tenantType.findUnique({
where: { title },
include: includeDetails,
});
}
export async function createTenantType(data: {
title: string;
titlePlural: string;
description: string | null;
isDefault?: boolean;
subscriptionProducts: string[];
}) {
return await db.tenantType.create({
data: {
title: data.title,
titlePlural: data.titlePlural,
description: data.description,
isDefault: data.isDefault,
subscriptionProducts: {
connect: data.subscriptionProducts.map((id) => ({ id })),
},
},
});
}
export async function updateTenantType(
id: string,
data: {
title?: string;
titlePlural?: string;
description?: string | null;
isDefault?: boolean;
subscriptionProducts?: string[];
}
) {
const update: Prisma.TenantTypeUncheckedUpdateInput = {
title: data.title,
titlePlural: data.titlePlural,
description: data.description,
isDefault: data.isDefault,
};
if (data.subscriptionProducts) {
update.subscriptionProducts = {
set: data.subscriptionProducts.map((id) => ({ id })),
};
}
return await db.tenantType.update({
where: { id: id },
data: update,
});
}
export async function deleteTenantType(id: string) {
return await db.tenantType.delete({
where: { id: id },
});
}