-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgetKnowledgeDocs.ts
More file actions
50 lines (44 loc) · 1.2 KB
/
getKnowledgeDocs.ts
File metadata and controls
50 lines (44 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'use server';
import { getDatabase } from '@utils/mongodb/mongoClient.mjs';
import { HackDocType } from '@typeDefs/hackbot';
export interface KnowledgeDoc {
id: string;
type: HackDocType;
title: string;
content: string;
url: string | null;
createdAt: string;
updatedAt: string;
}
export interface GetKnowledgeDocsResult {
ok: boolean;
docs: KnowledgeDoc[];
error?: string;
}
export default async function getKnowledgeDocs(): Promise<GetKnowledgeDocsResult> {
try {
const db = await getDatabase();
const raw = await db
.collection('hackbot_knowledge')
.find({})
.sort({ updatedAt: -1 })
.toArray();
const docs: KnowledgeDoc[] = raw.map((d: any) => ({
id: String(d._id),
type: d.type,
title: d.title,
content: d.content,
url: d.url ?? null,
createdAt: d.createdAt?.toISOString?.() ?? new Date().toISOString(),
updatedAt: d.updatedAt?.toISOString?.() ?? new Date().toISOString(),
}));
return { ok: true, docs };
} catch (e) {
console.error('[getKnowledgeDocs] Error', e);
return {
ok: false,
docs: [],
error: e instanceof Error ? e.message : 'Failed to load knowledge docs',
};
}
}