Skip to content

Commit

Permalink
Database
Browse files Browse the repository at this point in the history
  • Loading branch information
chatbookai committed Jun 1, 2024
1 parent fd63b33 commit e669341
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 534 deletions.
4 changes: 2 additions & 2 deletions express/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import isDev from 'electron-is-dev'
import settings from 'electron-settings';

import { server, getPort } from './src/app.js';
import { initChatBookSetting, initChatBookDb } from './src/utils/db.js';
import { initChatBookSetting, initChatBookDbExec } from './src/utils/db.js';

const PORT = getPort();

Expand All @@ -27,7 +27,7 @@ function createMainWindow() {
ChatBookSetting = await settings.get('chatbook');
console.log("ChatBookSetting main.js", ChatBookSetting)
await initChatBookSetting(ChatBookSetting);
await initChatBookDb();
await initChatBookDbExec();
mainWindow.loadURL('http://localhost:' + PORT);
});

Expand Down
3 changes: 2 additions & 1 deletion express/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 1 addition & 44 deletions express/src/router/user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// app.ts
import express from 'express';

import { checkUserPassword, registerUser, changeUserPasswordByToken, changeUserDetail, changeUserStatus, checkUserToken, getUsers, getUserLogsAll, getUserLogs, getOneUserByToken, getUserByEmail, updateUserImageFavorite, updateUserVideoFavorite, refreshUserToken, addUserAgent, deleteUserAgent, getUserAgents, getMyAgents, addUser, editUser, deleteUser } from '../utils/user.js';
import { checkUserPassword, registerUser, changeUserPasswordByToken, changeUserDetail, changeUserStatus, checkUserToken, getUsers, getUserLogsAll, getUserLogs, getOneUserByToken, getUserByEmail, updateUserImageFavorite, updateUserVideoFavorite, refreshUserToken, addUser, editUser, deleteUser } from '../utils/user.js';

const app = express();

Expand Down Expand Up @@ -152,47 +152,4 @@
res.end();
});

app.post('/api/user/agent/add', async (req, res) => {
const { authorization } = req.headers;
const addUserAgentData = await addUserAgent(authorization, req.body);
res.status(200).json(addUserAgentData);
res.end();
});

app.post('/api/user/agent/delete', async (req, res) => {
const { authorization } = req.headers;
const deleteUserAgentData = await deleteUserAgent(authorization, req.body);
res.status(200).json(deleteUserAgentData);
res.end();
});

app.post('/api/user/agents', async (req, res) => {
const { authorization } = req.headers;
const { pageid, pagesize } = req.body;
const checkUserTokenData = await checkUserToken(authorization);
if(checkUserTokenData && checkUserTokenData.data && checkUserTokenData.data.id && (checkUserTokenData.data.role == 'admin' || checkUserTokenData.data.role == 'user')) {
const getUserAgentsData = await getUserAgents(checkUserTokenData.data.id, Number(pageid), Number(pagesize));
res.status(200).json(getUserAgentsData);
}
else {
res.status(200).json({"status":"error", "msg":"Token is invalid", "data": null});
}
res.end();
});

app.post('/api/my/agents', async (req, res) => {
const { authorization } = req.headers;
const { pageid, pagesize } = req.body;
const checkUserTokenData = await checkUserToken(authorization);
if(checkUserTokenData && checkUserTokenData.data && checkUserTokenData.data.id && (checkUserTokenData.data.role == 'admin' || checkUserTokenData.data.role == 'user')) {
const getMyAgentsData = await getMyAgents(checkUserTokenData.data.id, Number(pageid), Number(pagesize));
res.status(200).json(getMyAgentsData);
}
else {
res.status(200).json({"status":"error", "msg":"Token is invalid", "data": null});
}
res.end();
});


export default app;
109 changes: 5 additions & 104 deletions express/src/router/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { MenuListAdmin, MenuListUser } from '../utils/const.js';
import { checkUserToken } from '../utils/user.js';
import { getLogsPage, clearShortLogs, clearAllLogs, getTemplate, getLLMSSetting, getFilesPage, getFilesDatasetId, getChatLogByKnowledgeIdAndUserId, addKnowledge, setOpenAISetting, setTemplate, getKnowledgePage, wholeSiteStatics, getAllImages, deleteUserLogByKnowledgeId, getAgentsPage, getAgentsEnabledList, addAgent, editAgent } from '../utils/utils.js';
import { getLogsPage, clearShortLogs, clearAllLogs, getTemplate, getLLMSSetting, getFilesPage, getFilesDatasetId, getChatLogByDatasetIdAndUserId, setOpenAISetting, setTemplate, wholeSiteStatics, getAllImages, deleteUserLogByDatasetId } from '../utils/utils.js';

const app = express();

Expand Down Expand Up @@ -34,27 +34,13 @@
res.end();
});

app.post('/api/addknowledge', async (req, res) => {
const { authorization } = req.headers;
const checkUserTokenData = await checkUserToken(authorization);
if(checkUserTokenData && checkUserTokenData.data && checkUserTokenData.data.email && (checkUserTokenData.data.role == 'admin' || checkUserTokenData.data.role == 'user')) {
console.log("checkUserTokenDatacheckUserTokenDatacheckUserTokenDatacheckUserTokenData", checkUserTokenData)
const addKnowledgeData = await addKnowledge({...req.body, userId: checkUserTokenData.data.id});
res.status(200).json(addKnowledgeData);
}
else {
res.status(200).json({"status":"error", "msg":"Token is invalid", "data": null});
}
res.end();
});

app.get('/api/chatlog/agent/:datasetId/:userId/:pageid/:pagesize', async (req, res) => {
const { datasetId, userId, pageid, pagesize } = req.params;
const { authorization } = req.headers;
const checkUserTokenData = await checkUserToken(authorization);
if(checkUserTokenData && checkUserTokenData.data && checkUserTokenData.data.email && (checkUserTokenData.data.role == 'admin' || checkUserTokenData.data.role == 'user')) {
const getChatLogByKnowledgeIdAndUserIdData = await getChatLogByKnowledgeIdAndUserId(datasetId, Number(userId), Number(pageid), Number(pagesize));
res.status(200).json(getChatLogByKnowledgeIdAndUserIdData);
const getChatLogByDatasetIdAndUserIdData = await getChatLogByDatasetIdAndUserId(datasetId, Number(userId), Number(pageid), Number(pagesize));
res.status(200).json(getChatLogByDatasetIdAndUserIdData);
}
else {
res.status(200).json({"status":"error", "msg":"Token is invalid", "data": null});
Expand All @@ -67,8 +53,8 @@
const { authorization } = req.headers;
const checkUserTokenData = await checkUserToken(authorization);
if(checkUserTokenData && checkUserTokenData.data && checkUserTokenData.data.email && (checkUserTokenData.data.role == 'admin' || checkUserTokenData.data.role == 'user')) {
const deleteUserLogByKnowledgeIdData = await deleteUserLogByKnowledgeId(datasetId, Number(userId));
res.status(200).json(deleteUserLogByKnowledgeIdData);
const deleteUserLogByDatasetIdData = await deleteUserLogByDatasetId(datasetId, Number(userId));
res.status(200).json(deleteUserLogByDatasetIdData);
}
else {
res.status(200).json({"status":"error", "msg":"Token is invalid", "data": null});
Expand Down Expand Up @@ -132,20 +118,6 @@
res.end();
});

app.get('/api/knowledge/:pageid/:pagesize', async (req, res) => {
const { pageid, pagesize } = req.params;
const { authorization } = req.headers;
const checkUserTokenData = await checkUserToken(authorization);
if(checkUserTokenData && checkUserTokenData.data && checkUserTokenData.data.email && (checkUserTokenData.data.role == 'admin' || checkUserTokenData.data.role == 'user')) {
const getKnowledgePageData = await getKnowledgePage(Number(pageid), Number(pagesize));
res.status(200).json(getKnowledgePageData);
}
else {
res.status(200).json({"status":"error", "msg":"Token is invalid", "data": null});
}
res.end();
});

app.get('/api/logs/:pageid/:pagesize', async (req, res) => {
const { pageid, pagesize } = req.params;
const { authorization } = req.headers;
Expand Down Expand Up @@ -233,75 +205,4 @@
res.status(200).json(generateimageData).end();
});

app.get('/api/agentsall/:pageid/:pagesize', async (req, res) => {
const { pageid, pagesize } = req.params;
const { authorization } = req.headers;
const checkUserTokenData = await checkUserToken(authorization);
if(checkUserTokenData && checkUserTokenData.data && checkUserTokenData.data.email && (checkUserTokenData.data.role == 'admin')) {
const getAgentsPageData = await getAgentsPage(Number(pageid), Number(pagesize));
res.status(200).json(getAgentsPageData);
}
else {
res.status(200).json({"status":"error", "msg":"Token is invalid", "data": null});
}
res.end();
});

app.post('/api/agents/:pageid/:pagesize', async (req, res) => {
const { pageid, pagesize } = req.params;
const { authorization } = req.headers;
const { type, search } = req.body;
const checkUserTokenData = await checkUserToken(authorization);
if(checkUserTokenData && checkUserTokenData.data && checkUserTokenData.data.email && (checkUserTokenData.data.role == 'admin' || checkUserTokenData.data.role == 'user')) {
const getAgentsPageData = await getAgentsEnabledList(Number(pageid), Number(pagesize), type, search);
res.status(200).json(getAgentsPageData);
}
else {
res.status(200).json({"status":"error", "msg":"Token is invalid", "data": null});
}
res.end();
});

app.post('/api/addagent', async (req, res) => {
const { authorization } = req.headers;
const checkUserTokenData = await checkUserToken(authorization);
if(checkUserTokenData && checkUserTokenData.data && checkUserTokenData.data.email && (checkUserTokenData.data.role == 'admin' || checkUserTokenData.data.role == 'user')) {
console.log("checkUserTokenData addagent", checkUserTokenData)
const addAgentData = await addAgent({...req.body, userId: checkUserTokenData.data.id});
res.status(200).json(addAgentData);
}
else {
res.status(200).json({"status":"error", "msg":"Token is invalid", "data": null});
}
res.end();
});

app.post('/api/editagent', async (req, res) => {
const { authorization } = req.headers;
const checkUserTokenData = await checkUserToken(authorization);
if(checkUserTokenData && checkUserTokenData.data && checkUserTokenData.data.email && (checkUserTokenData.data.role == 'admin' || checkUserTokenData.data.role == 'user')) {
console.log("checkUserTokenData editagent", checkUserTokenData)
const editAgentData = await editAgent({...req.body, userId: checkUserTokenData.data.id});
res.status(200).json(editAgentData);
}
else {
res.status(200).json({"status":"error", "msg":"Token is invalid", "data": null});
}
res.end();
});

app.post('/api/deleteagent', async (req, res) => {
const { authorization } = req.headers;
const checkUserTokenData = await checkUserToken(authorization);
if(checkUserTokenData && checkUserTokenData.data && checkUserTokenData.data.email && (checkUserTokenData.data.role == 'admin' || checkUserTokenData.data.role == 'user')) {
console.log("checkUserTokenData deleteagent", checkUserTokenData)
const editAgentData = await editAgent({...req.body, userId: checkUserTokenData.data.id});
res.status(200).json(editAgentData);
}
else {
res.status(200).json({"status":"error", "msg":"Token is invalid", "data": null});
}
res.end();
});

export default app;
Loading

0 comments on commit e669341

Please sign in to comment.