-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
124 lines (105 loc) · 3.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const app = require("express")();
const express = require("express");
const dbConfig = require("./db/connect");
const picModel = require("./models/reports")
const multer = require("multer")
const path = require("path")
const userRoutes = require("./routes/users");
const dataRoutes = require("./routes/data")
const wardRoutes = require("./routes/ward")
const patientRoutes = require("./routes/patients")
const checkUpRoutes = require("./routes/checkup")
const cors = require("cors");
const { Server } = require('socket.io')
const http = require("http");
const { createOutput } = require("./utils").createOutput;
require("dotenv").config();
const notificationCOntroller = require("./controllers/notification")
// database configuration
dbConfig.connectDb();
//cors config
// limiting all the acces that comes from other hosting
app.use(cors());
// allowing the json and url encoded in the requesst body
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.get("/test", (req, res) => {
res.send("LOL testing wooh");
});
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/'); // Uploads will be stored in the "uploads/" directory
},
filename: (req, file, cb) => {
cb(null, Date.now() + path.extname(file.originalname)); // Unique filename
},
});
const fileFilter = (req, file, cb) => {
if (file.mimetype.startsWith('image/')) {
cb(null, true); // Accept only image files
} else {
cb(new Error('Only images are allowed!'), false);
}
};
const upload = multer({ storage, fileFilter });
app.post("/data/upload/image", upload.single("image"), function (req, res, next) {
try {
if (!req.file) {
return res.status(400).send('No file uploaded.');
}
let imgPath = path.join("uploads", req.file.filename);
// saving the image filename into database
let saved = picModel.create({ imgPath })
if (saved) res.json(createOutput(false, "file saved successful.."));
} catch (error) {
res.json(createOutput(false, error.message))
}
})
// bringing all the routes
userRoutes.userRoutes(app);
// routes for handling the data
patientRoutes.patientRoutes(app)
wardRoutes.wardRoutes(app)
checkUpRoutes.checkUpRoutes(app)
dataRoutes.dataRoutes(app)
const server = http.createServer(app)
const io = new Server(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
})
io.on("connect", (socket) => {
console.log('connected')
socket.on("serchNotification", async (ID) => {
console.log(`A user with ID: ${ID} is asking for unread notification`)
let last_notification = await notificationCOntroller.getUnReadNotification(ID)
console.log("Last Notification: ", last_notification);
if (!last_notification.error) {
socket.emit("notifications", {
idUser: ID,
data: last_notification.notification // array
})
}
})
socket.on("read", async (ID) => {
console.log(`Deleting seen Notification: ${ID}`)
let successFlag = await notificationCOntroller.DeleteReadNotification(ID)
console.log(successFlag);
// console.log("Last Notification: ", last_notification);
// if (!last_notification.error) {
// socket.emit("notifications", {
// idUser: ID,
// data: last_notification.notification // array
// })
// }
})
// console.log(socket);
socket.on("disconnect", () => {
console.log("client disconnected..");
})
})
server.listen(process.env.PORT, () => {
console.log(`App running and connected to port ${process.env.PORT}`);
});
module.exports.Socket = io