155 lines
3.9 KiB
TypeScript
155 lines
3.9 KiB
TypeScript
import { KnowledgeBase } from "@toolctrl/db/prisma-browser";
|
|
import { clearCacheKey } from "~/utils/cache.server";
|
|
import { db, dbRead } from "~/utils/db.server";
|
|
|
|
export type KnowledgeBaseWithDetails = KnowledgeBase & {
|
|
_count: {
|
|
categories: number;
|
|
articles: number;
|
|
views: number;
|
|
};
|
|
};
|
|
|
|
const include = {
|
|
_count: {
|
|
select: { categories: true, articles: true, views: true },
|
|
},
|
|
};
|
|
|
|
function knowledgeBaseOwnerKey(tenantId?: string | null) {
|
|
return tenantId ?? "global";
|
|
}
|
|
|
|
export async function getAllKnowledgeBases({ enabled, tenantId }: { enabled?: boolean; tenantId?: string | null } = {}): Promise<KnowledgeBaseWithDetails[]> {
|
|
// Listing for KB management UI and public KB discovery — read-only.
|
|
return await dbRead.knowledgeBase.findMany({
|
|
where: { enabled, ownerKey: knowledgeBaseOwnerKey(tenantId) },
|
|
include,
|
|
orderBy: {
|
|
createdAt: "asc",
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function getAllKnowledgeBaseNames({ tenantId }: { tenantId?: string | null } = {}): Promise<{ id: string; title: string }[]> {
|
|
// Simple enumeration — read-only.
|
|
return await dbRead.knowledgeBase.findMany({
|
|
where: { ownerKey: knowledgeBaseOwnerKey(tenantId) },
|
|
select: {
|
|
id: true,
|
|
title: true,
|
|
},
|
|
orderBy: {
|
|
createdAt: "asc",
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function getKnowledgeBaseById(id: string): Promise<KnowledgeBaseWithDetails | null> {
|
|
// Read-only lookup.
|
|
return await dbRead.knowledgeBase.findUnique({
|
|
where: { id },
|
|
include,
|
|
});
|
|
}
|
|
|
|
export async function getKnowledgeBaseBySlug(slug: string, { tenantId }: { tenantId?: string | null } = {}): Promise<KnowledgeBaseWithDetails | null> {
|
|
// Public route lookup, read-only.
|
|
return await dbRead.knowledgeBase.findUnique({
|
|
where: { ownerKey_slug: { ownerKey: knowledgeBaseOwnerKey(tenantId), slug } },
|
|
include,
|
|
});
|
|
}
|
|
|
|
export async function createKnowledgeBase(data: {
|
|
basePath: string;
|
|
slug: string;
|
|
tenantId?: string | null;
|
|
title: string;
|
|
description: string;
|
|
defaultLanguage: string;
|
|
layout: string;
|
|
color: number;
|
|
enabled: boolean;
|
|
languages: string;
|
|
links: string;
|
|
logo: string;
|
|
seoImage: string;
|
|
}) {
|
|
return await db.knowledgeBase.create({
|
|
data: {
|
|
basePath: data.basePath,
|
|
slug: data.slug,
|
|
tenantId: data.tenantId ?? null,
|
|
ownerKey: knowledgeBaseOwnerKey(data.tenantId),
|
|
title: data.title,
|
|
description: data.description,
|
|
defaultLanguage: data.defaultLanguage,
|
|
layout: data.layout,
|
|
color: data.color,
|
|
enabled: data.enabled,
|
|
languages: data.languages,
|
|
links: data.links,
|
|
logo: data.logo,
|
|
seoImage: data.seoImage,
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function updateKnowledgeBase(
|
|
id: string,
|
|
data: {
|
|
basePath?: string;
|
|
slug?: string;
|
|
title?: string;
|
|
description?: string;
|
|
defaultLanguage?: string;
|
|
layout?: string;
|
|
color?: number;
|
|
enabled?: boolean;
|
|
languages?: string;
|
|
links?: string;
|
|
logo?: string;
|
|
seoImage?: string;
|
|
}
|
|
) {
|
|
return await db.knowledgeBase
|
|
.update({
|
|
where: { id },
|
|
data: {
|
|
basePath: data.basePath,
|
|
slug: data.slug,
|
|
title: data.title,
|
|
description: data.description,
|
|
defaultLanguage: data.defaultLanguage,
|
|
layout: data.layout,
|
|
color: data.color,
|
|
enabled: data.enabled,
|
|
languages: data.languages,
|
|
links: data.links,
|
|
logo: data.logo,
|
|
seoImage: data.seoImage,
|
|
},
|
|
})
|
|
.then((item) => {
|
|
clearCacheKey(`kb:${item.slug}`);
|
|
return item;
|
|
});
|
|
}
|
|
|
|
export async function deleteKnowledgeBase(id: string) {
|
|
return await db.knowledgeBase
|
|
.delete({
|
|
where: { id },
|
|
})
|
|
.then((item) => {
|
|
clearCacheKey(`kb:${item.slug}`);
|
|
return item;
|
|
});
|
|
}
|
|
|
|
export async function countKnowledgeBases() {
|
|
// Dashboard count — read-only.
|
|
return await dbRead.knowledgeBase.count();
|
|
}
|