forked from Khagesh2409/Ayurguru-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
179 lines (147 loc) · 5.92 KB
/
app.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import express from "express";
import dotenv from "dotenv";
import cookieParser from "cookie-parser";
import cors from "cors";
import multer from "multer";
import pkg from "pg";
import fs from "fs";
import { ObjectId } from "mongodb";
import User from "./models/User.js";
import axios from "axios";
import cron from "node-cron";
import mongoose from "mongoose";
// Existing routes for MongoDB
import authRoutes from "./routes/auth.js";
import conversationRoutes from "./routes/conversations.js";
import personalizedChatRoutes from "./routes/personalized.js";
import contactRoutes from "./routes/contact.js";
// Load environment variables
dotenv.config();
const { Pool } = pkg;
const app = express();
app.use(cors());
app.use(express.json());
app.use(cookieParser());
// MongoDB connection (as in your existing setup)
import dbConnection from "./config/db.js";
dbConnection(); // This will connect to MongoDB
// PostgreSQL connection (specifically for file uploads)
const pool = new Pool({
connectionString: process.env.PG_URI,
});
pool.connect()
.then(() => console.log('PostgreSQL connected'))
.catch(err => console.error('Connection error', err.stack));
// Multer configuration for handling file uploads
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/');
},
filename: function (req, file, cb) {
cb(null, file.originalname);
},
});
const upload = multer({ storage: storage });
app.post("/upload", upload.single("file"), async (req, res) => {
// Extract fields from the request body
const { userId, mongodb_id } = req.body; // Add mongodb_id from request body
const fileData = fs.readFileSync(req.file.path); // Read the file data from the temporary file
const filename = req.file.originalname; // Get the original filename
const fileType = req.file.mimetype.startsWith("image") ? "image" : "pdf"; // Detect file type based on mimetype
// SQL query text to insert the data into PostgreSQL
const queryText = `
INSERT INTO user_files (user_id, filename, file_data, file_type, mongodb_id)
VALUES ($1, $2, $3, $4, $5) RETURNING id;
`;
const values = [userId, filename, fileData, fileType, mongodb_id]; // Include mongodb_id in the values array
try {
// Execute the query
const result = await pool.query(queryText, values);
// Remove the file from the 'uploads' folder after saving to DB
fs.unlinkSync(req.file.path);
// Send a response back to the client with the ID and filename
res.status(200).json({ id: result.rows[0].id, filename: filename });
} catch (error) {
// Log and send error response if there's an issue with the query or file operations
console.error("Error uploading file:", error);
res.status(500).send("Error uploading file.");
}
});
// Route to fetch filenames of files uploaded by a user based on userId
app.get("/userfiles/:userId", async (req, res) => {
const { userId } = req.params;
const queryText = `SELECT filename FROM user_files WHERE user_id = $1;`;
try {
const result = await pool.query(queryText, [userId]);
const filenames = result.rows.map(row => row.filename);
res.status(200).json(filenames);
} catch (error) {
console.error("Error fetching user files:", error);
res.status(500).send("Error fetching user files.");
}
});
// Route to download a file (PDF or image) by filename
app.get("/pdf/:filename", async (req, res) => {
const { filename } = req.params;
const queryText = `SELECT file_data, file_type FROM user_files WHERE filename = $1;`;
try {
const result = await pool.query(queryText, [filename]);
if (result.rows.length === 0) {
return res.status(404).send("File not found.");
}
const fileData = result.rows[0].file_data;
const fileType = result.rows[0].file_type;
res.contentType(fileType).send(fileData);
} catch (error) {
console.error("Error retrieving file:", error);
res.status(500).send("Error retrieving file.");
}
});
app.delete("/delete/:userId/:filename", async (req, res) => {
const { userId, filename } = req.params;
try {
// Step 1: Fetch the mongodb_id using the filename from PostgreSQL
const fetchQuery = `SELECT id, mongodb_id FROM user_files WHERE filename = $1;`;
const fetchResult = await pool.query(fetchQuery, [filename]);
if (fetchResult.rowCount === 0) {
return res.status(404).send("File not found in PostgreSQL.");
}
const { id, mongodb_id } = fetchResult.rows[0];
// Step 2: Delete the file from PostgreSQL using the fetched ID
const deleteQuery = `DELETE FROM user_files WHERE id = $1 RETURNING *;`;
await pool.query(deleteQuery, [id]);
// Step 3: Delete the file text from MongoDB using the fetched mongodb_id
const user = await User.findOneAndUpdate(
{ userId: userId },
{ $pull: { 'personalizedChats.filesText': { _id: new ObjectId(mongodb_id) } } },
{ new: true }
);
if (!user) {
return res.status(404).send("User not found in MongoDB.");
}
res.status(200).send("File deleted successfully from both PostgreSQL and MongoDB.");
} catch (error) {
console.error("Error deleting file:", error);
res.status(500).send("Error deleting file.");
}
});
app.get("/keep-server-active", (req, res) => {
console.log("Dummy route accessed at", new Date().toLocaleString());
res.send("Server is alive");
});
cron.schedule("*/8 * * * *", async () => {
try {
const response = await axios.get(process.env.BACKEND_URL + "/keep-server-active");
console.log("Response from dummy route:", response.data);
} catch (error) {
console.error("Error accessing the dummy route:", error.message);
}
});
// Existing MongoDB routes
app.use("/api/auth", authRoutes);
app.use("/api/conversations", conversationRoutes);
app.use("/api/personalizedChats", personalizedChatRoutes);
app.use("/api/contact", contactRoutes);
// Start the server
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));