You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import {
|
|
router,
|
|
publicProcedure,
|
|
createCallerFactory,
|
|
} from "../../trpc/server.js";
|
|
import { db, type Fact } from "../../database/lowdb.js";
|
|
|
|
export const facts = router({
|
|
fetchByConversationId: publicProcedure
|
|
.input((x) => x as { conversationId: string })
|
|
.query(async ({ input: { conversationId } }) => {
|
|
const conversationMessageIds = db.data.messages
|
|
.filter((m) => m.conversationId === conversationId)
|
|
.map((m) => m.id);
|
|
const rows = await db.data.facts.filter((f) =>
|
|
conversationMessageIds.includes(f.sourceMessageId),
|
|
);
|
|
return rows as Array<Fact>;
|
|
}),
|
|
deleteOne: publicProcedure
|
|
.input(
|
|
(x) =>
|
|
x as {
|
|
factId: string;
|
|
},
|
|
)
|
|
.mutation(async ({ input: { factId } }) => {
|
|
const deletedFactId = db.data.facts.findIndex(
|
|
(fact) => fact.id === factId,
|
|
);
|
|
if (deletedFactId === -1) throw new Error("Fact not found");
|
|
db.data.facts.splice(deletedFactId, 1);
|
|
db.write();
|
|
return { ok: true };
|
|
}),
|
|
});
|
|
|
|
export const createCaller = createCallerFactory(facts);
|