Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
+96
@@ -0,0 +1,96 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
type OnboardingPaginationDbMocks = {
|
||||
onboardingFindMany: ReturnType<typeof vi.fn>;
|
||||
onboardingCount: ReturnType<typeof vi.fn>;
|
||||
onboardingSessionFindMany: ReturnType<typeof vi.fn>;
|
||||
onboardingSessionCount: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
|
||||
function getDbMocks(): OnboardingPaginationDbMocks {
|
||||
const store = globalThis as typeof globalThis & { __onboardingPaginationDbMocks__?: OnboardingPaginationDbMocks };
|
||||
store.__onboardingPaginationDbMocks__ ??= {
|
||||
onboardingFindMany: vi.fn(),
|
||||
onboardingCount: vi.fn(),
|
||||
onboardingSessionFindMany: vi.fn(),
|
||||
onboardingSessionCount: vi.fn(),
|
||||
};
|
||||
return store.__onboardingPaginationDbMocks__;
|
||||
}
|
||||
|
||||
vi.mock("~/utils/db.server", () => ({
|
||||
db: {
|
||||
onboarding: {
|
||||
findMany: getDbMocks().onboardingFindMany,
|
||||
count: getDbMocks().onboardingCount,
|
||||
},
|
||||
onboardingSession: {
|
||||
findMany: getDbMocks().onboardingSessionFindMany,
|
||||
count: getDbMocks().onboardingSessionCount,
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
import { getOnboardingsWithPagination } from "./onboarding.db.server";
|
||||
import { getOnboardingSessionsWithPagination } from "./onboardingSessions.db.server";
|
||||
|
||||
const dbMocks = getDbMocks();
|
||||
|
||||
describe("onboarding pagination queries", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
dbMocks.onboardingFindMany.mockResolvedValue([{ id: "onboarding_1" }]);
|
||||
dbMocks.onboardingCount.mockResolvedValue(21);
|
||||
dbMocks.onboardingSessionFindMany.mockResolvedValue([{ id: "session_1" }]);
|
||||
dbMocks.onboardingSessionCount.mockResolvedValue(12);
|
||||
});
|
||||
|
||||
it("fetches onboarding page rows and totals with stable query args", async () => {
|
||||
await expect(
|
||||
getOnboardingsWithPagination({
|
||||
where: { active: true },
|
||||
pagination: { page: 2, pageSize: 10 },
|
||||
})
|
||||
).resolves.toEqual({
|
||||
items: [{ id: "onboarding_1" }],
|
||||
pagination: { page: 2, pageSize: 10, totalItems: 21, totalPages: 3 },
|
||||
});
|
||||
|
||||
expect(dbMocks.onboardingFindMany).toHaveBeenCalledWith({
|
||||
where: { active: true },
|
||||
take: 10,
|
||||
skip: 10,
|
||||
include: { filters: { orderBy: { createdAt: "asc" } }, steps: { orderBy: { order: "asc" } }, sessions: true },
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
expect(dbMocks.onboardingCount).toHaveBeenCalledWith({ where: { active: true } });
|
||||
});
|
||||
|
||||
it("fetches onboarding session page rows and totals with stable query args", async () => {
|
||||
await expect(
|
||||
getOnboardingSessionsWithPagination({
|
||||
where: { tenantId: "tenant_1" },
|
||||
pagination: { page: 3, pageSize: 5 },
|
||||
})
|
||||
).resolves.toEqual({
|
||||
items: [{ id: "session_1" }],
|
||||
pagination: { page: 3, pageSize: 5, totalItems: 12, totalPages: 3 },
|
||||
});
|
||||
|
||||
expect(dbMocks.onboardingSessionFindMany).toHaveBeenCalledWith({
|
||||
where: { tenantId: "tenant_1" },
|
||||
take: 5,
|
||||
skip: 10,
|
||||
include: {
|
||||
onboarding: true,
|
||||
user: true,
|
||||
tenant: true,
|
||||
sessionSteps: { include: { step: true }, orderBy: { step: { order: "asc" } } },
|
||||
actions: true,
|
||||
matches: { include: { onboardingFilter: true } },
|
||||
},
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
expect(dbMocks.onboardingSessionCount).toHaveBeenCalledWith({ where: { tenantId: "tenant_1" } });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,169 @@
|
||||
import { Onboarding, OnboardingFilter, OnboardingStep, OnboardingSession, Prisma } from "@toolctrl/db/prisma-browser";
|
||||
import { PaginationDto } from "~/application/dtos/data/PaginationDto";
|
||||
import { db } from "~/utils/db.server";
|
||||
import { OnboardingFilterType } from "../dtos/OnboardingFilterTypes";
|
||||
import { OnboardingSessionStatus } from "../dtos/OnboardingSessionStatus";
|
||||
|
||||
export type OnboardingWithDetails = Onboarding & {
|
||||
filters: OnboardingFilter[];
|
||||
steps: OnboardingStep[];
|
||||
sessions: OnboardingSession[];
|
||||
};
|
||||
|
||||
export async function getOnboardings({ active, realtime }: { active?: boolean; realtime?: boolean }): Promise<OnboardingWithDetails[]> {
|
||||
return await db.onboarding.findMany({
|
||||
where: { active, realtime },
|
||||
include: { filters: { orderBy: { createdAt: "asc" } }, steps: { orderBy: { order: "asc" } }, sessions: true },
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
}
|
||||
|
||||
export async function getOnboardingsWithPagination({
|
||||
where,
|
||||
pagination,
|
||||
}: {
|
||||
where?: Prisma.OnboardingWhereInput;
|
||||
pagination: { page: number; pageSize: number };
|
||||
}): Promise<{ items: OnboardingWithDetails[]; pagination: PaginationDto }> {
|
||||
const [items, totalItems] = await Promise.all([
|
||||
db.onboarding.findMany({
|
||||
where,
|
||||
take: pagination.pageSize,
|
||||
skip: (pagination.page - 1) * pagination.pageSize,
|
||||
include: { filters: { orderBy: { createdAt: "asc" } }, steps: { orderBy: { order: "asc" } }, sessions: true },
|
||||
orderBy: { createdAt: "desc" },
|
||||
}),
|
||||
db.onboarding.count({
|
||||
where,
|
||||
}),
|
||||
]);
|
||||
return {
|
||||
items,
|
||||
pagination: {
|
||||
page: pagination.page,
|
||||
pageSize: pagination.pageSize,
|
||||
totalItems,
|
||||
totalPages: Math.ceil(totalItems / pagination.pageSize),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export async function getOnboarding(id: string): Promise<OnboardingWithDetails | null> {
|
||||
return await db.onboarding.findUnique({
|
||||
where: { id },
|
||||
include: { filters: { orderBy: { createdAt: "asc" } }, steps: { orderBy: { order: "asc" } }, sessions: true },
|
||||
});
|
||||
}
|
||||
|
||||
export async function createOnboarding(data: {
|
||||
title: string;
|
||||
type: "modal" | "page";
|
||||
active: boolean;
|
||||
realtime: boolean;
|
||||
canBeDismissed: boolean;
|
||||
height: string;
|
||||
filters: { type: OnboardingFilterType; value: string | null }[];
|
||||
steps: { order: number; block: string }[];
|
||||
}): Promise<Onboarding> {
|
||||
return await db.onboarding.create({
|
||||
data: {
|
||||
title: data.title,
|
||||
type: data.type.toString(),
|
||||
active: data.active,
|
||||
realtime: data.realtime,
|
||||
canBeDismissed: data.canBeDismissed,
|
||||
height: data.height,
|
||||
filters: { create: data.filters },
|
||||
steps: { create: data.steps },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateOnboarding(
|
||||
id: string,
|
||||
data: {
|
||||
title?: string;
|
||||
type?: "modal" | "page";
|
||||
realtime?: boolean;
|
||||
active?: boolean;
|
||||
canBeDismissed?: boolean;
|
||||
height?: string;
|
||||
filters?: {
|
||||
type: OnboardingFilterType;
|
||||
value: string | null;
|
||||
}[];
|
||||
steps?: { order: number; block: string }[];
|
||||
}
|
||||
): Promise<Onboarding> {
|
||||
const update: Prisma.OnboardingUpdateInput = {
|
||||
title: data.title,
|
||||
type: data.type?.toString(),
|
||||
realtime: data.realtime,
|
||||
active: data.active,
|
||||
canBeDismissed: data.canBeDismissed,
|
||||
height: data.height,
|
||||
};
|
||||
if (data.filters) {
|
||||
update.filters = {
|
||||
deleteMany: {},
|
||||
create: data.filters,
|
||||
};
|
||||
}
|
||||
if (data.steps) {
|
||||
update.steps = {
|
||||
deleteMany: {},
|
||||
create: data.steps,
|
||||
};
|
||||
}
|
||||
return await db.onboarding.update({
|
||||
where: { id },
|
||||
data: update,
|
||||
});
|
||||
}
|
||||
|
||||
export async function setOnboardingManualSessions(
|
||||
id: string,
|
||||
data: { userId: string; tenantId: string | null; status: OnboardingSessionStatus }[]
|
||||
): Promise<Onboarding> {
|
||||
const update: Prisma.OnboardingUpdateInput = {
|
||||
sessions: {
|
||||
deleteMany: {},
|
||||
create: data,
|
||||
},
|
||||
};
|
||||
return await db.onboarding.update({
|
||||
where: { id },
|
||||
data: update,
|
||||
});
|
||||
}
|
||||
|
||||
export async function createOnboardingSession(
|
||||
onboarding: OnboardingWithDetails,
|
||||
session: { userId: string; tenantId: string | null; status: OnboardingSessionStatus; matchingFilters: OnboardingFilter[]; createdRealtime: boolean }
|
||||
) {
|
||||
return await db.onboardingSession.create({
|
||||
data: {
|
||||
onboardingId: onboarding.id,
|
||||
userId: session.userId,
|
||||
tenantId: session.tenantId,
|
||||
status: session.status,
|
||||
createdRealtime: session.createdRealtime,
|
||||
sessionSteps: {
|
||||
create: onboarding.steps.map((step) => ({
|
||||
stepId: step.id,
|
||||
})),
|
||||
},
|
||||
matches: {
|
||||
create: session.matchingFilters.map((filter) => ({
|
||||
onboardingFilterId: filter.id,
|
||||
})),
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteOnboarding(id: string): Promise<Onboarding> {
|
||||
return await db.onboarding.delete({
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { db } from "~/utils/db.server";
|
||||
|
||||
export async function updateOnboardingSessionStep(id: string, data: { seenAt?: Date; completedAt?: Date }) {
|
||||
return await db.onboardingSessionStep.update({
|
||||
where: { id },
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteOnboardingSessionSteps(ids: string[]) {
|
||||
return await db.onboardingSessionStep.deleteMany({
|
||||
where: { id: { in: ids } },
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteOnboardingSessionStepsInOnboardingSession({ onboardingSessionId, onboardingId }: { onboardingSessionId: string; onboardingId: string }) {
|
||||
return await db.onboardingSessionStep.deleteMany({
|
||||
where: {
|
||||
onboardingSessionId,
|
||||
onboardingSession: {
|
||||
onboardingId,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// @vitest-environment node
|
||||
import { describe, expect, test } from "vitest";
|
||||
import { readRepoFile } from "~/utils/testing/repoPaths";
|
||||
|
||||
describe("onboarding session DB scope", () => {
|
||||
test("deletes sessions and child steps through onboarding scope", () => {
|
||||
const sessionsSource = readRepoFile("apps/web/app/modules/onboarding/db/onboardingSessions.db.server.ts");
|
||||
const stepsSource = readRepoFile("apps/web/app/modules/onboarding/db/onboardingSessionSteps.db.server.ts");
|
||||
|
||||
expect(sessionsSource).toContain("export async function deleteOnboardingSession({ id, onboardingId }");
|
||||
expect(sessionsSource).toContain("db.onboardingSession.deleteMany");
|
||||
expect(sessionsSource).toContain("where: { id, onboardingId }");
|
||||
expect(stepsSource).toContain("export async function deleteOnboardingSessionStepsInOnboardingSession");
|
||||
expect(stepsSource).toContain("onboardingSession: {\n onboardingId,");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,222 @@
|
||||
import {
|
||||
Onboarding,
|
||||
OnboardingFilter,
|
||||
OnboardingSession,
|
||||
OnboardingSessionAction,
|
||||
OnboardingSessionFilterMatch,
|
||||
OnboardingSessionStep,
|
||||
OnboardingStep,
|
||||
Prisma,
|
||||
Tenant,
|
||||
} from "@toolctrl/db/prisma-browser";
|
||||
import { PaginationDto } from "~/application/dtos/data/PaginationDto";
|
||||
import { db } from "~/utils/db.server";
|
||||
import { UserSimple } from "~/utils/db/users.db.server";
|
||||
import { OnboardingSessionActionDto } from "../dtos/OnboardingSessionActionDto";
|
||||
import { OnboardingSessionStatus } from "../dtos/OnboardingSessionStatus";
|
||||
|
||||
export type OnboardingSessionWithDetails = OnboardingSession & {
|
||||
onboarding: Onboarding;
|
||||
user: UserSimple;
|
||||
tenant: Tenant | null;
|
||||
sessionSteps: (OnboardingSessionStep & {
|
||||
step: OnboardingStep;
|
||||
})[];
|
||||
actions: OnboardingSessionAction[];
|
||||
matches: (OnboardingSessionFilterMatch & { onboardingFilter: OnboardingFilter })[];
|
||||
};
|
||||
|
||||
export async function getOnboardingSessions({
|
||||
onboardingId,
|
||||
userId,
|
||||
tenantId,
|
||||
status,
|
||||
hasBeenStarted,
|
||||
hasBeenCompleted,
|
||||
hasBeenDismissed,
|
||||
}: {
|
||||
onboardingId?: string;
|
||||
userId?: string;
|
||||
tenantId?: string | null;
|
||||
status?: OnboardingSessionStatus;
|
||||
hasBeenStarted?: boolean;
|
||||
hasBeenCompleted?: boolean;
|
||||
hasBeenDismissed?: boolean;
|
||||
}): Promise<OnboardingSessionWithDetails[]> {
|
||||
const where: Prisma.OnboardingSessionWhereInput = {
|
||||
onboardingId,
|
||||
userId,
|
||||
tenantId,
|
||||
status: status ? status.toString() : undefined,
|
||||
};
|
||||
if (hasBeenStarted !== undefined) {
|
||||
where.startedAt = hasBeenStarted ? { not: null } : null;
|
||||
}
|
||||
if (hasBeenCompleted !== undefined) {
|
||||
where.completedAt = hasBeenCompleted ? { not: null } : null;
|
||||
}
|
||||
if (hasBeenDismissed !== undefined) {
|
||||
where.dismissedAt = hasBeenDismissed ? { not: null } : null;
|
||||
}
|
||||
return await db.onboardingSession.findMany({
|
||||
where,
|
||||
include: {
|
||||
onboarding: true,
|
||||
user: true,
|
||||
tenant: true,
|
||||
sessionSteps: { include: { step: true }, orderBy: { step: { order: "asc" } } },
|
||||
actions: true,
|
||||
matches: { include: { onboardingFilter: true } },
|
||||
},
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
}
|
||||
|
||||
export async function getOnboardingSessionsWithPagination({
|
||||
where,
|
||||
pagination,
|
||||
}: {
|
||||
where?: Prisma.OnboardingSessionWhereInput;
|
||||
pagination: { page: number; pageSize: number };
|
||||
}): Promise<{ items: OnboardingSessionWithDetails[]; pagination: PaginationDto }> {
|
||||
const [items, totalItems] = await Promise.all([
|
||||
db.onboardingSession.findMany({
|
||||
where,
|
||||
take: pagination.pageSize,
|
||||
skip: (pagination.page - 1) * pagination.pageSize,
|
||||
include: {
|
||||
onboarding: true,
|
||||
user: true,
|
||||
tenant: true,
|
||||
sessionSteps: { include: { step: true }, orderBy: { step: { order: "asc" } } },
|
||||
actions: true,
|
||||
matches: { include: { onboardingFilter: true } },
|
||||
},
|
||||
orderBy: { createdAt: "desc" },
|
||||
}),
|
||||
db.onboardingSession.count({
|
||||
where,
|
||||
}),
|
||||
]);
|
||||
return {
|
||||
items,
|
||||
pagination: {
|
||||
page: pagination.page,
|
||||
pageSize: pagination.pageSize,
|
||||
totalItems,
|
||||
totalPages: Math.ceil(totalItems / pagination.pageSize),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export async function getOnboardingSession(id: string): Promise<OnboardingSessionWithDetails | null> {
|
||||
return await db.onboardingSession.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
onboarding: true,
|
||||
user: true,
|
||||
tenant: true,
|
||||
sessionSteps: { include: { step: true }, orderBy: { step: { order: "asc" } } },
|
||||
actions: true,
|
||||
matches: { include: { onboardingFilter: true } },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getOnboardingSessionInOnboarding({ id, onboardingId }: { id: string; onboardingId: string }): Promise<OnboardingSessionWithDetails | null> {
|
||||
return await db.onboardingSession.findFirst({
|
||||
where: { id, onboardingId },
|
||||
include: {
|
||||
onboarding: true,
|
||||
user: true,
|
||||
tenant: true,
|
||||
sessionSteps: { include: { step: true }, orderBy: { step: { order: "asc" } } },
|
||||
actions: true,
|
||||
matches: { include: { onboardingFilter: true } },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getOnboardingSessionsByUser({
|
||||
userId,
|
||||
tenantId,
|
||||
status,
|
||||
}: {
|
||||
userId: string;
|
||||
tenantId: string | null;
|
||||
status?: OnboardingSessionStatus;
|
||||
}): Promise<OnboardingSessionWithDetails[]> {
|
||||
return await db.onboardingSession.findMany({
|
||||
where: { userId, tenantId, status },
|
||||
include: {
|
||||
onboarding: true,
|
||||
user: true,
|
||||
tenant: true,
|
||||
sessionSteps: { include: { step: true }, orderBy: { step: { order: "asc" } } },
|
||||
actions: true,
|
||||
matches: { include: { onboardingFilter: true } },
|
||||
},
|
||||
orderBy: { onboarding: { createdAt: "asc" } },
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateOnboardingSession(
|
||||
id: string,
|
||||
data: { status?: OnboardingSessionStatus; startedAt?: Date; completedAt?: Date; dismissedAt?: Date }
|
||||
) {
|
||||
return await db.onboardingSession.update({
|
||||
where: { id },
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
export async function setOnboardingSessionActions(id: string, data: { actions: OnboardingSessionActionDto[] }) {
|
||||
await db.onboardingSessionAction.deleteMany({
|
||||
where: { onboardingSessionId: id },
|
||||
});
|
||||
return await db.onboardingSessionAction.createMany({
|
||||
data: data.actions.map((action) => ({
|
||||
onboardingSessionId: id,
|
||||
type: action.type.toString(),
|
||||
name: action.name,
|
||||
value: action.value,
|
||||
createdAt: action.createdAt,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateOnboardingSessions(ids: string[], data: { status?: OnboardingSessionStatus }) {
|
||||
return await db.onboardingSession.updateMany({
|
||||
where: { id: { in: ids } },
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteOnboardingSession({ id, onboardingId }: { id: string; onboardingId: string }) {
|
||||
const result = await db.onboardingSession.deleteMany({
|
||||
where: { id, onboardingId },
|
||||
});
|
||||
if (result.count !== 1) {
|
||||
throw Error("Onboarding session not found");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function deleteOnboardingSessionInOnboarding({ id, onboardingId }: { id: string; onboardingId: string }) {
|
||||
return deleteOnboardingSession({ id, onboardingId });
|
||||
}
|
||||
|
||||
export async function deleteOnboardingSessions(onboardingId: string) {
|
||||
return await db.onboardingSession.deleteMany({
|
||||
where: { onboardingId },
|
||||
});
|
||||
}
|
||||
|
||||
export async function groupOnboardingSessionsByUser() {
|
||||
return await db.onboardingSession.groupBy({
|
||||
by: ["userId"],
|
||||
_count: {
|
||||
_all: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user