2023-09-28 23:00:03 +02:00
|
|
|
const prisma = require("../utils/prisma");
|
2023-06-15 08:12:59 +02:00
|
|
|
|
2023-06-04 04:28:07 +02:00
|
|
|
const WorkspaceChats = {
|
2023-07-25 19:37:04 +02:00
|
|
|
new: async function ({ workspaceId, prompt, response = {}, user = null }) {
|
2023-09-28 23:00:03 +02:00
|
|
|
try {
|
|
|
|
const chat = await prisma.workspace_chats.create({
|
|
|
|
data: {
|
|
|
|
workspaceId,
|
|
|
|
prompt,
|
|
|
|
response: JSON.stringify(response),
|
|
|
|
user_id: user?.id || null,
|
|
|
|
},
|
2023-06-08 06:31:35 +02:00
|
|
|
});
|
2023-09-28 23:00:03 +02:00
|
|
|
return { chat, message: null };
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return { chat: null, message: error.message };
|
2023-06-15 08:12:59 +02:00
|
|
|
}
|
2023-06-04 04:28:07 +02:00
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
2023-07-25 19:37:04 +02:00
|
|
|
forWorkspaceByUser: async function (
|
|
|
|
workspaceId = null,
|
|
|
|
userId = null,
|
2023-11-01 22:12:27 +01:00
|
|
|
limit = null,
|
|
|
|
orderBy = null
|
2023-07-25 19:37:04 +02:00
|
|
|
) {
|
|
|
|
if (!workspaceId || !userId) return [];
|
2023-09-28 23:00:03 +02:00
|
|
|
try {
|
|
|
|
const chats = await prisma.workspace_chats.findMany({
|
|
|
|
where: {
|
|
|
|
workspaceId,
|
|
|
|
user_id: userId,
|
2023-10-23 22:10:34 +02:00
|
|
|
include: true,
|
2023-09-28 23:00:03 +02:00
|
|
|
},
|
|
|
|
...(limit !== null ? { take: limit } : {}),
|
2023-11-01 22:12:27 +01:00
|
|
|
...(orderBy !== null ? { orderBy } : { orderBy: { id: "asc" } }),
|
2023-09-28 23:00:03 +02:00
|
|
|
});
|
|
|
|
return chats;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return [];
|
|
|
|
}
|
2023-07-25 19:37:04 +02:00
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
2023-11-01 22:12:27 +01:00
|
|
|
forWorkspace: async function (
|
|
|
|
workspaceId = null,
|
|
|
|
limit = null,
|
|
|
|
orderBy = null
|
|
|
|
) {
|
2023-06-04 04:28:07 +02:00
|
|
|
if (!workspaceId) return [];
|
2023-09-28 23:00:03 +02:00
|
|
|
try {
|
|
|
|
const chats = await prisma.workspace_chats.findMany({
|
|
|
|
where: {
|
|
|
|
workspaceId,
|
2023-10-23 22:10:34 +02:00
|
|
|
include: true,
|
2023-09-28 23:00:03 +02:00
|
|
|
},
|
|
|
|
...(limit !== null ? { take: limit } : {}),
|
2023-11-01 22:12:27 +01:00
|
|
|
...(orderBy !== null ? { orderBy } : { orderBy: { id: "asc" } }),
|
2023-09-28 23:00:03 +02:00
|
|
|
});
|
|
|
|
return chats;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return [];
|
|
|
|
}
|
2023-06-04 04:28:07 +02:00
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
2023-07-25 19:37:04 +02:00
|
|
|
markHistoryInvalid: async function (workspaceId = null, user = null) {
|
2023-06-04 04:28:07 +02:00
|
|
|
if (!workspaceId) return;
|
2023-09-28 23:00:03 +02:00
|
|
|
try {
|
|
|
|
await prisma.workspace_chats.updateMany({
|
|
|
|
where: {
|
|
|
|
workspaceId,
|
|
|
|
user_id: user?.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
include: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
}
|
2023-06-04 04:28:07 +02:00
|
|
|
},
|
2023-06-15 08:12:59 +02:00
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
get: async function (clause = {}, limit = null, orderBy = null) {
|
|
|
|
try {
|
|
|
|
const chat = await prisma.workspace_chats.findFirst({
|
|
|
|
where: clause,
|
|
|
|
...(limit !== null ? { take: limit } : {}),
|
|
|
|
...(orderBy !== null ? { orderBy } : {}),
|
|
|
|
});
|
|
|
|
return chat || null;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return null;
|
|
|
|
}
|
2023-06-04 04:28:07 +02:00
|
|
|
},
|
2023-06-15 08:12:59 +02:00
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
delete: async function (clause = {}) {
|
|
|
|
try {
|
|
|
|
await prisma.workspace_chats.deleteMany({
|
|
|
|
where: clause,
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return false;
|
|
|
|
}
|
2023-06-04 04:28:07 +02:00
|
|
|
},
|
2023-07-25 19:37:04 +02:00
|
|
|
|
2023-11-03 00:12:29 +01:00
|
|
|
where: async function (
|
|
|
|
clause = {},
|
|
|
|
limit = null,
|
|
|
|
orderBy = null,
|
|
|
|
offset = null
|
|
|
|
) {
|
2023-09-28 23:00:03 +02:00
|
|
|
try {
|
|
|
|
const chats = await prisma.workspace_chats.findMany({
|
|
|
|
where: clause,
|
|
|
|
...(limit !== null ? { take: limit } : {}),
|
2023-11-03 00:12:29 +01:00
|
|
|
...(offset !== null ? { skip: offset } : {}),
|
2023-09-28 23:00:03 +02:00
|
|
|
...(orderBy !== null ? { orderBy } : {}),
|
|
|
|
});
|
|
|
|
return chats;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return [];
|
|
|
|
}
|
2023-07-25 19:37:04 +02:00
|
|
|
},
|
2023-06-15 08:12:59 +02:00
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
count: async function (clause = {}) {
|
|
|
|
try {
|
|
|
|
const count = await prisma.workspace_chats.count({
|
|
|
|
where: clause,
|
|
|
|
});
|
|
|
|
return count;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return 0;
|
|
|
|
}
|
2023-07-25 19:37:04 +02:00
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
2023-11-03 00:12:29 +01:00
|
|
|
whereWithData: async function (
|
|
|
|
clause = {},
|
|
|
|
limit = null,
|
|
|
|
offset = null,
|
|
|
|
orderBy = null
|
|
|
|
) {
|
2023-07-25 19:37:04 +02:00
|
|
|
const { Workspace } = require("./workspace");
|
|
|
|
const { User } = require("./user");
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
try {
|
2023-11-03 00:12:29 +01:00
|
|
|
const results = await this.where(clause, limit, orderBy, offset);
|
2023-09-28 23:00:03 +02:00
|
|
|
|
|
|
|
for (const res of results) {
|
|
|
|
const workspace = await Workspace.get({ id: res.workspaceId });
|
|
|
|
res.workspace = workspace
|
|
|
|
? { name: workspace.name, slug: workspace.slug }
|
|
|
|
: { name: "deleted workspace", slug: null };
|
|
|
|
|
|
|
|
const user = await User.get({ id: res.user_id });
|
|
|
|
res.user = user
|
|
|
|
? { username: user.username }
|
2023-11-09 02:36:54 +01:00
|
|
|
: { username: "unknown user" };
|
2023-09-28 23:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return [];
|
2023-07-25 19:37:04 +02:00
|
|
|
}
|
2023-06-04 04:28:07 +02:00
|
|
|
},
|
2023-06-08 06:31:35 +02:00
|
|
|
};
|
2023-06-04 04:28:07 +02:00
|
|
|
|
2023-06-08 06:31:35 +02:00
|
|
|
module.exports = { WorkspaceChats };
|