2023-09-28 23:00:03 +02:00
|
|
|
const prisma = require("../utils/prisma");
|
2023-06-08 06:31:35 +02:00
|
|
|
const slugify = require("slugify");
|
|
|
|
const { Document } = require("./documents");
|
2023-07-25 19:37:04 +02:00
|
|
|
const { WorkspaceUser } = require("./workspaceUsers");
|
2024-01-22 23:14:01 +01:00
|
|
|
const { ROLES } = require("../utils/middleware/multiUserProtected");
|
2024-01-31 22:38:21 +01:00
|
|
|
const { v4: uuidv4 } = require("uuid");
|
2023-06-04 04:28:07 +02:00
|
|
|
|
|
|
|
const Workspace = {
|
2023-06-15 08:12:59 +02:00
|
|
|
writable: [
|
|
|
|
// Used for generic updates so we can validate keys in request body
|
|
|
|
"name",
|
|
|
|
"slug",
|
|
|
|
"vectorTag",
|
|
|
|
"openAiTemp",
|
2023-06-28 21:53:26 +02:00
|
|
|
"openAiHistory",
|
2023-06-15 08:12:59 +02:00
|
|
|
"lastUpdatedAt",
|
2023-07-20 20:14:23 +02:00
|
|
|
"openAiPrompt",
|
2023-11-07 01:49:29 +01:00
|
|
|
"similarityThreshold",
|
2024-01-17 21:59:25 +01:00
|
|
|
"chatModel",
|
2024-01-18 21:34:20 +01:00
|
|
|
"topN",
|
2024-02-16 23:50:40 +01:00
|
|
|
"chatMode",
|
2024-03-12 19:36:59 +01:00
|
|
|
"pfpFilename",
|
2023-06-15 08:12:59 +02:00
|
|
|
],
|
|
|
|
|
2023-07-25 19:37:04 +02:00
|
|
|
new: async function (name = null, creatorId = null) {
|
2023-06-08 06:31:35 +02:00
|
|
|
if (!name) return { result: null, message: "name cannot be null" };
|
2023-06-15 08:12:59 +02:00
|
|
|
var slug = slugify(name, { lower: true });
|
2024-01-31 22:38:21 +01:00
|
|
|
slug = slug || uuidv4();
|
2023-06-15 08:12:59 +02:00
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
const existingBySlug = await this.get({ slug });
|
2023-06-15 08:12:59 +02:00
|
|
|
if (existingBySlug !== null) {
|
|
|
|
const slugSeed = Math.floor(10000000 + Math.random() * 90000000);
|
|
|
|
slug = slugify(`${name}-${slugSeed}`, { lower: true });
|
|
|
|
}
|
2023-06-04 04:28:07 +02:00
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
try {
|
|
|
|
const workspace = await prisma.workspaces.create({
|
|
|
|
data: { name, slug },
|
2023-06-08 06:31:35 +02:00
|
|
|
});
|
2023-06-15 08:12:59 +02:00
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
// If created with a user then we need to create the relationship as well.
|
|
|
|
// If creating with an admin User it wont change anything because admins can
|
|
|
|
// view all workspaces anyway.
|
|
|
|
if (!!creatorId) await WorkspaceUser.create(creatorId, workspace.id);
|
|
|
|
return { workspace, message: null };
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return { workspace: 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-06-15 08:12:59 +02:00
|
|
|
update: async function (id = null, data = {}) {
|
|
|
|
if (!id) throw new Error("No workspace id provided for update");
|
|
|
|
|
|
|
|
const validKeys = Object.keys(data).filter((key) =>
|
|
|
|
this.writable.includes(key)
|
|
|
|
);
|
2023-09-28 23:00:03 +02:00
|
|
|
if (validKeys.length === 0)
|
2023-06-15 08:12:59 +02:00
|
|
|
return { workspace: { id }, message: "No valid fields to update!" };
|
2023-06-28 21:54:18 +02:00
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
try {
|
|
|
|
const workspace = await prisma.workspaces.update({
|
|
|
|
where: { id },
|
2024-02-16 23:50:40 +01:00
|
|
|
data, // TODO: strict validation on writables here.
|
2023-06-15 08:12:59 +02:00
|
|
|
});
|
2023-09-28 23:00:03 +02:00
|
|
|
return { workspace, message: null };
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return { workspace: null, message: error.message };
|
2023-06-15 08:12:59 +02:00
|
|
|
}
|
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
|
|
|
getWithUser: async function (user = null, clause = {}) {
|
2024-01-22 23:14:01 +01:00
|
|
|
if ([ROLES.admin, ROLES.manager].includes(user.role))
|
|
|
|
return this.get(clause);
|
2023-07-25 19:37:04 +02:00
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
try {
|
|
|
|
const workspace = await prisma.workspaces.findFirst({
|
|
|
|
where: {
|
|
|
|
...clause,
|
|
|
|
workspace_users: {
|
|
|
|
some: {
|
|
|
|
user_id: user?.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
workspace_users: true,
|
|
|
|
documents: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!workspace) return null;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...workspace,
|
|
|
|
documents: await Document.forWorkspace(workspace.id),
|
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return null;
|
|
|
|
}
|
2023-07-25 19:37:04 +02:00
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
|
|
|
get: async function (clause = {}) {
|
|
|
|
try {
|
|
|
|
const workspace = await prisma.workspaces.findFirst({
|
|
|
|
where: clause,
|
|
|
|
include: {
|
|
|
|
documents: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return workspace || 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.workspaces.delete({
|
|
|
|
where: clause,
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return false;
|
|
|
|
}
|
2023-06-04 04:28:07 +02:00
|
|
|
},
|
2023-06-15 08:12:59 +02:00
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
where: async function (clause = {}, limit = null, orderBy = null) {
|
|
|
|
try {
|
|
|
|
const results = await prisma.workspaces.findMany({
|
|
|
|
where: clause,
|
|
|
|
...(limit !== null ? { take: limit } : {}),
|
|
|
|
...(orderBy !== null ? { orderBy } : {}),
|
|
|
|
});
|
|
|
|
return results;
|
|
|
|
} 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
|
|
|
whereWithUser: async function (
|
|
|
|
user,
|
2023-09-28 23:00:03 +02:00
|
|
|
clause = {},
|
2023-07-25 19:37:04 +02:00
|
|
|
limit = null,
|
|
|
|
orderBy = null
|
|
|
|
) {
|
2024-01-22 23:14:01 +01:00
|
|
|
if ([ROLES.admin, ROLES.manager].includes(user.role))
|
2023-11-13 23:51:16 +01:00
|
|
|
return await this.where(clause, limit, orderBy);
|
2023-09-28 23:00:03 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
const workspaces = await prisma.workspaces.findMany({
|
|
|
|
where: {
|
|
|
|
...clause,
|
|
|
|
workspace_users: {
|
|
|
|
some: {
|
|
|
|
user_id: user.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
...(limit !== null ? { take: limit } : {}),
|
|
|
|
...(orderBy !== null ? { orderBy } : {}),
|
|
|
|
});
|
|
|
|
return workspaces;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return [];
|
|
|
|
}
|
2023-07-25 19:37:04 +02:00
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
|
|
|
whereWithUsers: async function (clause = {}, limit = null, orderBy = null) {
|
|
|
|
try {
|
|
|
|
const workspaces = await this.where(clause, limit, orderBy);
|
|
|
|
for (const workspace of workspaces) {
|
|
|
|
const userIds = (
|
|
|
|
await WorkspaceUser.where({ workspace_id: Number(workspace.id) })
|
|
|
|
).map((rel) => rel.user_id);
|
|
|
|
workspace.userIds = userIds;
|
|
|
|
}
|
|
|
|
return workspaces;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return [];
|
2023-07-25 19:37:04 +02:00
|
|
|
}
|
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
2023-07-25 19:37:04 +02:00
|
|
|
updateUsers: async function (workspaceId, userIds = []) {
|
2023-09-28 23:00:03 +02:00
|
|
|
try {
|
|
|
|
await WorkspaceUser.delete({ workspace_id: Number(workspaceId) });
|
|
|
|
await WorkspaceUser.createManyUsers(userIds, workspaceId);
|
|
|
|
return { success: true, error: null };
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return { success: false, error: error.message };
|
|
|
|
}
|
2023-07-25 19:37:04 +02:00
|
|
|
},
|
2024-01-17 21:59:25 +01:00
|
|
|
|
|
|
|
resetWorkspaceChatModels: async () => {
|
|
|
|
try {
|
|
|
|
await prisma.workspaces.updateMany({
|
|
|
|
data: {
|
|
|
|
chatModel: null,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return { success: true, error: null };
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error resetting workspace chat models:", error.message);
|
|
|
|
return { success: false, error: error.message };
|
|
|
|
}
|
|
|
|
},
|
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 = { Workspace };
|