|
|
@@ -1,29 +1,31 @@
|
|
|
-import { db } from "../db/knex.js";
|
|
|
+import { Conversation } from "../models/Conversation.model.js";
|
|
|
+import { Message } from "../models/Message.model.js";
|
|
|
|
|
|
export async function listConversations(userId, { limit = 50, offset = 0 } = {}) {
|
|
|
const safeLimit = Math.min(Number(limit) || 50, 100);
|
|
|
const safeOffset = Math.max(Number(offset) || 0, 0);
|
|
|
- return db("conversations")
|
|
|
+ return Conversation.query()
|
|
|
.where({ UsuarioId: userId })
|
|
|
.orderBy("UpdatedAt", "desc")
|
|
|
.limit(safeLimit)
|
|
|
.offset(safeOffset)
|
|
|
- .select("Id", "Title", "CreatedAt", "UpdatedAt");
|
|
|
+ .select("Id", "Title", "CreatedAt", "UpdatedAt")
|
|
|
+ .select(Conversation.relatedQuery("messages").count().as("MessageCount"));
|
|
|
}
|
|
|
|
|
|
export async function createConversation(userId, title = "Nova conversa") {
|
|
|
- const [id] = await db("conversations").insert({
|
|
|
+ const conv = await Conversation.query().insert({
|
|
|
UsuarioId: userId,
|
|
|
Title: String(title).slice(0, 255)
|
|
|
});
|
|
|
- return { id, title };
|
|
|
+ return { id: conv.Id, title };
|
|
|
}
|
|
|
|
|
|
export async function getConversationMessages(conversationId, userId) {
|
|
|
- const conv = await db("conversations").where({ Id: conversationId, UsuarioId: userId }).first();
|
|
|
+ const conv = await Conversation.query().findOne({ Id: conversationId, UsuarioId: userId });
|
|
|
if (!conv) return null;
|
|
|
|
|
|
- const msgs = await db("messages")
|
|
|
+ const msgs = await Message.query()
|
|
|
.where({ ConversationId: conversationId })
|
|
|
.orderBy("SentAt", "asc")
|
|
|
.select("Id", "Role", "Content", "Sources", "SentAt");
|
|
|
@@ -32,7 +34,7 @@ export async function getConversationMessages(conversationId, userId) {
|
|
|
}
|
|
|
|
|
|
export async function verifyConversationOwner(conversationId, userId) {
|
|
|
- const conv = await db("conversations").where({ Id: conversationId, UsuarioId: userId }).first();
|
|
|
+ const conv = await Conversation.query().findOne({ Id: conversationId, UsuarioId: userId });
|
|
|
return Boolean(conv);
|
|
|
}
|
|
|
|
|
|
@@ -41,7 +43,7 @@ export async function getRecentMessages(conversationId, userId, limit = 12) {
|
|
|
const owns = await verifyConversationOwner(conversationId, userId);
|
|
|
if (!owns) return [];
|
|
|
}
|
|
|
- const msgs = await db("messages")
|
|
|
+ const msgs = await Message.query()
|
|
|
.where({ ConversationId: conversationId })
|
|
|
.orderBy("SentAt", "desc")
|
|
|
.limit(limit)
|
|
|
@@ -50,34 +52,34 @@ export async function getRecentMessages(conversationId, userId, limit = 12) {
|
|
|
}
|
|
|
|
|
|
export async function addMessage(conversationId, { role, content, sources = null }) {
|
|
|
- await db.transaction(async (trx) => {
|
|
|
- await trx("messages").insert({
|
|
|
+ await Conversation.transaction(async (trx) => {
|
|
|
+ await Message.query(trx).insert({
|
|
|
ConversationId: conversationId,
|
|
|
Role: role,
|
|
|
Content: content,
|
|
|
Sources: sources ? JSON.stringify(sources) : null
|
|
|
});
|
|
|
- await trx("conversations")
|
|
|
- .where({ Id: conversationId })
|
|
|
- .update({ UpdatedAt: trx.fn.now() });
|
|
|
+ await Conversation.query(trx)
|
|
|
+ .patch({ UpdatedAt: trx.fn.now() })
|
|
|
+ .where({ Id: conversationId });
|
|
|
});
|
|
|
}
|
|
|
|
|
|
export async function updateConversationTitle(conversationId, userId, title) {
|
|
|
- await db("conversations")
|
|
|
- .where({ Id: conversationId, UsuarioId: userId })
|
|
|
- .update({ Title: String(title).slice(0, 255), UpdatedAt: db.fn.now() });
|
|
|
+ await Conversation.query()
|
|
|
+ .patch({ Title: String(title).slice(0, 255), UpdatedAt: Conversation.knex().fn.now() })
|
|
|
+ .where({ Id: conversationId, UsuarioId: userId });
|
|
|
}
|
|
|
|
|
|
export async function deleteConversation(conversationId, userId) {
|
|
|
- await db("conversations").where({ Id: conversationId, UsuarioId: userId }).delete();
|
|
|
+ await Conversation.query().delete().where({ Id: conversationId, UsuarioId: userId });
|
|
|
}
|
|
|
|
|
|
export async function getConversationForExport(conversationId, userId) {
|
|
|
- const conv = await db("conversations").where({ Id: conversationId, UsuarioId: userId }).first();
|
|
|
+ const conv = await Conversation.query().findOne({ Id: conversationId, UsuarioId: userId });
|
|
|
if (!conv) return null;
|
|
|
|
|
|
- const msgs = await db("messages")
|
|
|
+ const msgs = await Message.query()
|
|
|
.where({ ConversationId: conversationId })
|
|
|
.orderBy("SentAt", "asc")
|
|
|
.select("Role", "Content", "SentAt");
|