├── .gitignore ├── routes ├── user.js └── login.js ├── package.json ├── lib └── db.js ├── controller ├── userController.js └── authController.js ├── modals └── chatSchema.js ├── index.html └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | yarn.lock 3 | package-lock.json 4 | node_modules -------------------------------------------------------------------------------- /routes/user.js: -------------------------------------------------------------------------------- 1 | import express from "express"; 2 | import { allUsers, getData } from "../controller/userController.js"; 3 | 4 | const user = express.Router(); 5 | 6 | user.get("/", allUsers); 7 | user.post('/getdata' , getData) 8 | 9 | export default user; 10 | -------------------------------------------------------------------------------- /routes/login.js: -------------------------------------------------------------------------------- 1 | import express from "express"; 2 | import { 3 | getroute, 4 | CreateNewAccount, 5 | DeleteNewAccount, 6 | } from "../controller/authController.js"; 7 | 8 | const auth = express.Router(); 9 | 10 | auth.get("/", getroute); 11 | 12 | auth.post("/newuser", CreateNewAccount); 13 | auth.get("/delete", DeleteNewAccount); 14 | 15 | export default auth; 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "chatbot-backend", 3 | "version": "1.0.0", 4 | "main": "index.js", 5 | "license": "MIT", 6 | "type": "module", 7 | "scripts": { 8 | "start": "nodemon index.js" 9 | }, 10 | "dependencies": { 11 | "@socket.io/admin-ui": "^0.5.1", 12 | "body-parser": "^1.20.2", 13 | "cors": "^2.8.5", 14 | "dotenv": "^16.0.3", 15 | "express": "^4.18.2", 16 | "mongoose": "^7.0.2", 17 | "nodemon": "^2.0.21", 18 | "socket.io": "^4.6.1" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /lib/db.js: -------------------------------------------------------------------------------- 1 | import mongoose from "mongoose"; 2 | import { config } from "dotenv"; 3 | config(); 4 | 5 | const dbConnect = async () => { 6 | try { 7 | const uri = 8 | process.env.NODE_ENV === "production" 9 | ? process.env.MONGODB_URI_DEV 10 | : process.env.MONGODB_URI_PROD; 11 | 12 | await mongoose.connect(uri, { 13 | useNewUrlParser: true, 14 | useUnifiedTopology: true, 15 | }); 16 | return uri; 17 | } catch (err) { 18 | return console.error(err); 19 | } 20 | }; 21 | 22 | export default dbConnect; 23 | -------------------------------------------------------------------------------- /controller/userController.js: -------------------------------------------------------------------------------- 1 | import UserModal from "../modals/chatSchema.js"; 2 | 3 | export const allUsers = async (req, res) => { 4 | try { 5 | let ActiveUser = await UserModal.find(); 6 | res.status(200).send(ActiveUser); 7 | } catch (err) { 8 | res.status(500).send(err.message); 9 | } 10 | }; 11 | 12 | export const getData = async (req, res) => { 13 | try { 14 | let { id, subject } = req.body; 15 | console.log(id); 16 | let userData = await UserModal.findById(id); 17 | let data = userData.chats.filter((item) => item.chatFrom === subject); 18 | res.send( 19 | data); 20 | } catch (err) { 21 | res.status(500).send(err.message); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /controller/authController.js: -------------------------------------------------------------------------------- 1 | import mongoose from "mongoose"; 2 | import UserModal from "../modals/chatSchema.js"; 3 | 4 | export const getroute = (req, res) => { 5 | res.send("hello world"); 6 | }; 7 | 8 | export const CreateNewAccount = async (req, res) => { 9 | try { 10 | const { user_id, name, avatar_url } = req.body; 11 | 12 | const isUserExist = await UserModal.findOne({ user_id }); 13 | 14 | if (isUserExist) { 15 | return res.send(isUserExist); 16 | } 17 | const newUser = await UserModal({ 18 | name: name, 19 | avatar_url: avatar_url || "", 20 | user_id: user_id, 21 | chats: [], 22 | }); 23 | 24 | newUser.save(); 25 | return res.send(newUser); 26 | } catch (err) { 27 | console.log(err); 28 | return res.send("error: " + err.message, err); 29 | } 30 | }; 31 | export const DeleteNewAccount = (req, res) => {}; 32 | -------------------------------------------------------------------------------- /modals/chatSchema.js: -------------------------------------------------------------------------------- 1 | import mongoose from "mongoose"; 2 | 3 | const UserSchema = new mongoose.Schema( 4 | { 5 | // _id: { 6 | // type: mongoose.Schema.Types.ObjectId, 7 | // require: true, 8 | // }, 9 | // clientId: { 10 | // type: String, 11 | // require: true, 12 | // }, 13 | name: { 14 | type: String, 15 | require: true, 16 | }, 17 | socketId: { 18 | type: String, 19 | default: "", 20 | }, 21 | avatar_url: { 22 | type: String, 23 | // require: true, 24 | }, 25 | user_id: { 26 | type: Number, 27 | require: true, 28 | }, 29 | chats: [ 30 | { 31 | 32 | chatFrom: { 33 | type: String, 34 | ref: "User", 35 | // require: true, 36 | }, 37 | chatTo: { 38 | type: String, 39 | // require: true, 40 | }, 41 | data: [ 42 | { 43 | key: { 44 | type: String, 45 | // required: true, 46 | }, 47 | value: { 48 | type: String, 49 | // required: true, 50 | }, 51 | }, 52 | ], 53 | }, 54 | ], 55 | }, 56 | { 57 | versionKey: false, 58 | } 59 | ); 60 | 61 | const UserModal = mongoose.model("Chat", UserSchema); 62 | export default UserModal; 63 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |