-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
198 lines (163 loc) · 5.26 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Import required modules
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const helmet = require('helmet');
const bodyParser = require('body-parser');
const axios = require('axios');
// Create an Express application
const app = express();
// Connect to MongoDB using Mongoose
const mongodbURI = "mongodb+srv://user:[email protected]/?retryWrites=true&w=majority"
mongoose.connect(mongodbURI, { useNewUrlParser: true, useUnifiedTopology: true });
const db = mongoose.connection;
// Handle MongoDB connection events
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
db.once('open', () => {
console.log('Connected to MongoDB');
});
// Use middleware to enable CORS
app.use(cors());
// Use middleware to secure HTTP headers
app.use(helmet());
// Use middleware to parse JSON and URL-encoded request bodies
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const inquirySchema = new mongoose.Schema({
personalDetails: {
name: { type: String, required: true },
email: { type: String, required: true },
phoneNumber: { type: String, required: true },
},
organizationDetails: {
chooseOrg: { type: String, required: true },
nameOfOrg: { type: String },
},
productDetails: {
typeOfApparel: { type: String },
numberOfPieces: { type: String },
ApproximatePrice: { type: String },
},
query: {
text: { type: String },
imageLink: { type: String },
},
}, { timestamps: true });
const Inquiry = mongoose.model('Inquiry', inquirySchema);
function returnHTML(data) {
const htmlText = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Email Content</title>
</head>
<body>
<h2>Contact Information:</h2>
<ul>
<li><strong>Name:</strong> ${data.name}</li>
<li><strong>Email:</strong> ${data.email}</li>
<li><strong>Phone Number:</strong> ${data.phoneNumber}</li>
</ul>
<h2>Organization Information:</h2>
<ul>
<li><strong>Choose Organization:</strong> ${data.chooseOrg}</li>
<li><strong>Name of Organization:</strong> ${data.nameOfOrg}</li>
</ul>
<h2>Order Details:</h2>
<ul>
<li><strong>Type of Apparel:</strong> ${data.typeOfApparel}</li>
<li><strong>Number of Pieces:</strong> ${data.numberOfPieces}</li>
<li><strong>Approximate Price:</strong> ${data.ApproximatePrice}</li>
</ul>
<h2>Additional Information:</h2>
<ul>
<li><strong>Text:</strong> ${data.text}</li>
<li><strong>Image Link:</strong> ${data.imageLink}</li>
</ul>
</body>
</html>
`;
// console.log(htmlText);
return (htmlText)
}
async function sendEmail(emailData) {
try
{
const response = await axios.post('https://email-service-ewc0.onrender.com/send_email/', emailData, {
headers: {
'Content-Type': 'application/json',
},
});
console.log('Email sent successfully:', response.data);
return response.data;
} catch (error)
{
console.error('Error sending email:', error.response.data);
throw error.response.data;
}
}
app.post('/submitInquiry', async (req, res) => {
try
{
// Extract all details from the request body
const {
name,
email,
phoneNumber,
chooseOrg,
nameOfOrg,
typeOfApparel,
numberOfPieces,
ApproximatePrice,
text,
imageLink,
} = req.body;
const html = returnHTML(req.body);
// console.log(html)
// Create a new Inquiry instance
const newInquiry = new Inquiry({
personalDetails: {
name,
email,
phoneNumber,
},
organizationDetails: {
chooseOrg,
nameOfOrg,
},
productDetails: {
typeOfApparel,
numberOfPieces,
ApproximatePrice,
},
query: {
text,
imageLink,
},
});
// Save the new inquiry to the database
const savedInquiry = await newInquiry.save();
// Example usage:
const emailData = {
subject: 'New Feedback received',
to_email: '[email protected]',
message: html,
html_body: html,
};
sendEmail(emailData);
// Respond with the saved inquiry
res.status(201).json(savedInquiry);
} catch (error)
{
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
// Start the Express server
const port = 5000;
app.listen(process.env.PORT || port, () => {
console.log(`Server is running on http://localhost:${5000}`);
});