Skip to content

Commit

Permalink
backend fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
piyushjaiswal1610 committed Mar 15, 2024
1 parent 7870300 commit b3aa5b9
Show file tree
Hide file tree
Showing 62 changed files with 748 additions and 559 deletions.
2 changes: 1 addition & 1 deletion backend/__tests__/login.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import request from 'supertest'
import { app } from '../src/app'
import { server as app } from '../src/app'

describe('Testing Login APIs', () => {
it('return success and 200 when login successful', async () => {
Expand Down
8 changes: 4 additions & 4 deletions backend/package-lock.json

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

2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"node": "^20.5.0",
"nodemailer": "^6.9.7",
"pg": "^8.11.3",
"socket.io": "^4.7.2",
"socket.io": "^4.7.4",
"typeorm": "^0.3.17"
},
"devDependencies": {
Expand Down
9 changes: 7 additions & 2 deletions backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import cookieParser from "cookie-parser";
import cors from "cors";
import AuthRouter from "./routes/auth.routes";
import OtpRouter from "./routes/otp.routes";
import UserRouter from "./routes/user.routes";
import { createServer } from "http";


const app = express();
const server = createServer(app)

const corsOptions = {
origin: "*", // Allow all origins
Expand All @@ -20,11 +24,12 @@ app.use(cookieParser());
app.use(express.json({ limit: "10mb" }));
app.use(express.urlencoded({ limit: "10mb", extended: true }));

app.use("/api/user", AuthRouter);
app.use("/api/auth", AuthRouter);
app.use("/api/otp", OtpRouter);
app.use("/api/user", UserRouter);

app.get("/", (req: Request, res: Response) => {
res.send("Welcome to Virtual Connect Server!!");
});

export { app };
export { server };
1 change: 1 addition & 0 deletions backend/src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ const login = async (req: Request, res: Response) => {
accessToken,
name: user.name,
email: user.email,
uid: user.id,
isVerified: user.isVerified,
},
"User logged in successfully!"
Expand Down
92 changes: 92 additions & 0 deletions backend/src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { Request, Response } from "express";
import { myDataSource } from "../db";
import userEntity from "../models/user.entity";
import { ApiError, ApiResponse } from "../utils";

const getAllUsers = async (req: Request, res: Response) => {
try {
const users = await myDataSource.getRepository(userEntity).find({
select: {
name: true,
id: true,
socketId: true,
imgUrl: true
}
});

if(!users) throw new ApiError(400, "Error fetching data!")

res
.status(200)
.json(new ApiResponse(200, users, "Fetched all users!"));
} catch (error: any) {
res.status(error.statusCode | 500).json({
success: false,
message: error.message,
});
}
};


const setUserSocketId = async (req: Request, res: Response) => {
try {
const { email, socketId } = req.body

const user = await myDataSource.getRepository(userEntity).findOne({
where: {
email: email
}
})

if (!user) throw new ApiError(401, "User does not exists!")

const isUpdated = await myDataSource.getRepository(userEntity).save({
...user,
socketId: socketId,
onlineStatus: "true"
})

if (!isUpdated) throw new ApiError(500, "User status update failed!")

res.status(200).json(new ApiResponse(200, [], "User status update success!"))

} catch (error: any) {
res.status(error.statusCode | 500).json({
success: false,
message: error.message,
})
}
};


const setUserOffline = async (req: Request, res: Response) => {
try {
const { email} = req.body

const user = await myDataSource.getRepository(userEntity).findOne({
where: {
email: email
}
})

if (!user) throw new ApiError(401, "User does not exists!")

const isUpdated = await myDataSource.getRepository(userEntity).save({
...user,
onlineStatus: "false"
})

if (!isUpdated) throw new ApiError(500, "User status update failed!")

res.status(200).json(new ApiResponse(200, [], "User status update success!"))

} catch (error: any) {
res.status(error.statusCode | 500).json({
success: false,
message: error.message,
})
}
};

export { getAllUsers, setUserOffline, setUserSocketId }

3 changes: 2 additions & 1 deletion backend/src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DataSource } from "typeorm";
import dotenv from "dotenv";
import { DB_NAME } from "../constants";
import userEntity from "../models/user.entity";
import socketEntity from "../models/socket.entity";

dotenv.config({
path: "../.env",
Expand All @@ -14,7 +15,7 @@ export const myDataSource = new DataSource({
database: DB_NAME,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
entities: [userEntity],
entities: [userEntity, socketEntity],
synchronize: true,
ssl: {
rejectUnauthorized: false,
Expand Down
9 changes: 7 additions & 2 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { app } from "./app";
import { server } from "./app";
import dotenv from "dotenv";
import connectDB from "./db";
import { createServer } from "http";
import { io } from "./socket";

dotenv.config({
path: "../.env",
});

connectDB()
.then(() => {
app.listen(process.env.PORT || 6969, () => {
server.listen(process.env.PORT || 6969, () => {
console.log(`⚙️ Server is running at port : ${process.env.PORT}`);
});
})
.then(() => {
io.listen(5001)
})
.catch((err) => {
console.log("POSTGRES db connection failed !!! ", err);
});
15 changes: 15 additions & 0 deletions backend/src/models/socket.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { EntitySchema } from "typeorm";

export default new EntitySchema({
name: "Socket Id and User Id Map",
tableName: "sid_map",
columns: {
sid: {
type: "text"
},
uid: {
type: "text",
primary: true
}
}
})
12 changes: 12 additions & 0 deletions backend/src/models/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ export default new EntitySchema({
type: "text",
nullable: true,
},
socketId: {
type: "text",
nullable: true,
},
onlineStatus: {
type: "boolean",
default: "false"
},
imgUrl: {
type: "text",
nullable: true
}
},
});

Expand Down
28 changes: 28 additions & 0 deletions backend/src/routes/user.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import express from "express";
import { check } from "express-validator";
import validateRequest from "../middlewares/validReq.middleware";
import {
getAllUsers,
setUserOffline,
setUserSocketId,
} from "../controllers/user.controller";

const router = express.Router();

router.get("/getAllUsers", getAllUsers);

router.post(
"/setUserSocketId",
[check("email").notEmpty().isEmail(), check("socketId").notEmpty()],
validateRequest,
setUserSocketId
);

router.post(
"/setUserOffline",
[check("email").notEmpty().isEmail()],
validateRequest,
setUserOffline
)

export default router;
60 changes: 60 additions & 0 deletions backend/src/socket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Server } from "socket.io";
import { server } from "./app";
import dotenv from "dotenv";
import { Message } from "./types/message.types";
import deleteUserFromSocketList from "./utils/lib/deleteUserFromSocketList";

dotenv.config();

const io = new Server(server, {
cors: {
origin: process.env.CLIENT_URL,
methods: ["GET", "POST"],
credentials: true,
},
});

interface User {
name: string;
email: string;
uid: string;
sid: string;
}

var connectedUsers: User[] = [];

io.use((socket, next) => {
//TODO verify user request
//TODO save or replace user socket id to database
next();
});

io.on("connection", (socket) => {
io.to(socket.id).emit("user-sid", {sid: socket.id})

socket.on("user-details", (user) => {
connectedUsers = deleteUserFromSocketList(connectedUsers, user);
connectedUsers.push(user);
io.emit("user-joined", connectedUsers);
});

socket.on("message", (msg: Message) => {
console.log(msg);
if (msg.receipient.sid.length === 0) return;
socket.to(msg.receipient.sid).emit("receive-message", msg);
//TODO: Asynchronously save message to db
console.log("emitted msg to respective client");
});

socket.on("disconnect", () => {
const updatedUserList: User[] = [];
connectedUsers.forEach((user) => {
if (user.sid !== socket.id) updatedUserList.push(user);
});
connectedUsers = updatedUserList
socket.disconnect()
io.emit("user-left", connectedUsers);
});
});

export { io };
13 changes: 13 additions & 0 deletions backend/src/types/message.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface Message {
msg_id: string;
sender: User;
receipient: User;
content: string;
timestamp: Date;
}

interface User {
name: string;
id: string;
sid: string
}
2 changes: 1 addition & 1 deletion backend/src/utils/jwt/verifyToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function verifyToken(type: string, token: string) {

jwt.verify(token, secret || "", (err: any) => {
if (err) return false;
else true;
else return true;
});
} catch (error) {
throw new Error();
Expand Down
18 changes: 18 additions & 0 deletions backend/src/utils/lib/deleteUserFromSocketList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
interface User {
name: string;
email: string;
uid: string;
sid: string;
}

function deleteUserFromSocketList(list: User[], user: User) {
const newList: User[] = [];
if (list.length > 0) {
list.forEach((usr) => {
if (usr.name !== user.name) newList.push(usr);
});
}
return newList;
}

export default deleteUserFromSocketList;
Loading

0 comments on commit b3aa5b9

Please sign in to comment.