diff --git a/server/src/__tests__/folders-service.test.ts b/server/src/__tests__/folders-service.test.ts index adeecbbd95c..579c7efead5 100644 --- a/server/src/__tests__/folders-service.test.ts +++ b/server/src/__tests__/folders-service.test.ts @@ -49,13 +49,14 @@ describeEmbeddedPostgres("folder service", () => { return companyId; } - async function seedRoutine(companyId: string, title: string, folderId?: string | null) { + async function seedRoutine(companyId: string, title: string, folderId?: string | null, status = "active") { const [routine] = await db .insert(routines) .values({ companyId, title, folderId: folderId ?? null, + status, responsibleUserId: "responsible-user", }) .returning(); @@ -109,6 +110,24 @@ describeEmbeddedPostgres("folder service", () => { ]); }); + it("excludes archived routines from folder counts", async () => { + const companyId = await seedCompany(); + const svc = folderService(db); + + const reporting = await svc.create(companyId, { kind: "routine", name: "Reporting" }); + await seedRoutine(companyId, "Active filed", reporting.id); + await seedRoutine(companyId, "Archived filed", reporting.id, "archived"); + await seedRoutine(companyId, "Active unfiled"); + await seedRoutine(companyId, "Archived unfiled", null, "archived"); + + const listed = await svc.list(companyId, "routine"); + expect(listed.allCount).toBe(2); + expect(listed.unfiledCount).toBe(1); + expect(listed.folders).toEqual([ + expect.objectContaining({ id: reporting.id, itemCount: 1 }), + ]); + }); + it("moves routines and skills to folders and back to virtual Unfiled", async () => { const companyId = await seedCompany(); const svc = folderService(db); diff --git a/server/src/services/folders.ts b/server/src/services/folders.ts index 7f12a2c1490..52c5c5824de 100644 --- a/server/src/services/folders.ts +++ b/server/src/services/folders.ts @@ -1,4 +1,4 @@ -import { and, asc, eq, max, sql } from "drizzle-orm"; +import { and, asc, eq, max, ne, sql } from "drizzle-orm"; import type { Db } from "@paperclipai/db"; import { companySkills, folders, routines } from "@paperclipai/db"; import type { @@ -67,7 +67,7 @@ export function folderService(db: Db) { count: sql`count(*)::int`, }) .from(routines) - .where(eq(routines.companyId, companyId)) + .where(and(eq(routines.companyId, companyId), ne(routines.status, "archived"))) .groupBy(routines.folderId); }