-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
386 lines (339 loc) · 11.4 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
const express = require("express");
const app = express();
const session = require("express-session");
const flash = require("connect-flash");
const path = require("path");
const replicate = require("replicate");
const Amadeus = require("amadeus");
const puppeteer = require('puppeteer');
require("dotenv").config();
const util = require("util");
const bodyParser = require('body-parser');
const cookieParser = require("cookie-parser");
const crypto = require("crypto");
const { Cashfree } = require("cashfree-pg");
// --- Firebase Admin Initialization ---
const admin = require("firebase-admin");
const serviceAccount = require("./serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
// Initialize Amadeus API client
const amadeus = new Amadeus({
clientId: process.env.AMADEUS_API_KEY,
clientSecret: process.env.AMADEUS_API_SECRET,
});
// Initialize Replicate client
const replicateClient = new replicate({
auth: process.env.REPLICATE_API_TOKEN,
});
// Initialize Cashfree
Cashfree.XClientId = process.env.CASHFREE_APP_ID;
Cashfree.XClientSecret = process.env.CASHFREE_SECRET_KEY;
Cashfree.XEnvironment = Cashfree.Environment.SANDBOX;
//EJS view engine
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
//static files
app.use(express.static(path.join(__dirname, "public")));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieParser());
// Express Session
app.use(
session({
secret: "thisWebsiteISbuiltByVinay",
resave: false,
saveUninitialized: false,
})
);
app.use(flash());
app.use((req, res, next) => {
res.locals.messages = req.flash();
if (!req.session.user && req.cookies.session) {
admin
.auth()
.verifySessionCookie(req.cookies.session, true)
.then((decodedClaims) => {
req.session.user = decodedClaims;
res.locals.user = decodedClaims;
next();
})
.catch((error) => {
res.clearCookie("session");
res.locals.user = null;
next();
});
} else {
res.locals.user = req.session.user || null;
next();
}
});
// Login Page
app.get("/login", (req, res) => {
res.render("login");
});
app.post("/sessionLogin", (req, res) => {
const idToken = req.body.idToken;
//session expire after 5 days (in milliseconds)
const expiresIn = 60 * 60 * 24 * 5 * 1000;
admin
.auth()
.createSessionCookie(idToken, { expiresIn })
.then((sessionCookie) => {
const options = { maxAge: expiresIn, httpOnly: true, secure: false };
res.cookie("session", sessionCookie, options);
res.status(200).json({ status: "success" });
})
.catch((error) => {
console.error("Error creating session cookie:", error);
res.status(401).send("UNAUTHORIZED REQUEST!");
});
});
function checkAuth(req, res, next) {
const sessionCookie = req.cookies.session || "";
admin
.auth()
.verifySessionCookie(sessionCookie, true)
.then((decodedClaims) => {
req.user = decodedClaims;
req.session.user = decodedClaims;
res.locals.user = decodedClaims;
next();
})
.catch((error) => {
res.redirect("/login");
});
}
app.get("/plantrip", checkAuth, (req, res) => {
res.render("plantrip", { user: req.user });
});
app.post("/plantrip", async (req, res) => {
const { origin, destination, departureDate, travelDays, budget, travelCompanion } = req.body;
console.log(req.body);
if (!origin || !destination || !departureDate || !travelDays || !budget || !travelCompanion) {
req.flash("error", "Please provide all the required fields.");
return res.status(400).send("All Fields are Required to Plan the Trip.");
}
try {
const flightOffers = await getFlightOffers(origin, destination, departureDate);
const tripPlan = await generateTripPlan(destination, travelDays, budget, travelCompanion);
req.session.flightOffers = flightOffers;
req.session.tripPlan = tripPlan;
req.session.destination = destination;
res.render("trip", {
user: req.user,
pdfUnlocked: req.session.pdfUnlocked || false,
flightOffers: flightOffers,
tripPlan: tripPlan,
destination: destination,
CASHFREE_ENV: process.env.CASHFREE_ENV,
BASE_URL: process.env.BASE_URL
});
} catch (error) {
console.error("Error in /plantrip route:", error);
req.flash("error", "Error generating trip plan. Please try again.");
res.status(500).send("Error generating trip plan.");
}
});
app.get("/trip", checkAuth, (req, res) => {
res.render("index", {
user: req.user,
pdfUnlocked: req.session.pdfUnlocked || false,
tripPlan: req.session.tripPlan,
flightOffers: req.session.flightOffers,
destination: req.session.destination,
CASHFREE_ENV: process.env.CASHFREE_ENV,
BASE_URL: process.env.BASE_URL
});
});
app.get('/', (req, res) => {
res.render('home');
});
app.get("/logout", (req, res) => {
req.session.destroy();
res.clearCookie('session');
res.redirect("/");
});
// Create Order
app.post('/create-cashfree-order', async (req, res) => {
try {
const orderRequest = {
order_id: `order_${Date.now()}`,
order_amount: 1.00,
order_currency: "INR",
customer_details: {
customer_id: req.session.user?.uid || 'guest',
customer_phone: req.session.user?.phone || '9999999999'
},
order_meta: {
return_url: `${process.env.BASE_URL}/payment-callback`
}
};
const response = await Cashfree.PGCreateOrder("2023-08-01", orderRequest);
res.json(response.data);
} catch (error) {
console.error('Order Error:', error.response?.data || error);
res.status(500).json({ error: 'Payment initialization failed' });
}
});
// Check Payment Status
app.get('/payment-status/:orderId', async (req, res) => {
try {
const response = await Cashfree.PGOrderFetchPayments(
"2023-08-01",
req.params.orderId
);
res.json(response.data);
} catch (error) {
res.status(500).json({ error: error.response?.data?.message });
}
});
app.get('/payment-callback', async (req, res) => {
try {
const orderId = req.query.order_id;
const statusResponse = await Cashfree.PGOrderFetchPayments(
"2023-08-01",
orderId
);
const paid = statusResponse.data.some(
payment => payment.payment_status === "SUCCESS"
);
if(paid) {
req.session.pdfUnlocked = true;
res.redirect('/trip');
} else {
res.redirect('/payment-failed');
}
} catch (error) {
console.error('Callback Error:', error);
res.redirect('/payment-error');
}
});
app.post('/cashfree-webhook', express.json(), (req, res) => {
const signature = req.headers['x-cf-signature'];
console.log('Webhook Data:', req.body);
res.sendStatus(200);
});
//Check PDF download is allowed
function checkPdfAccess(req, res, next) {
if (req.session && req.session.pdfUnlocked) {
return next();
}
return res.status(403).send('You must purchase the PDF download to access this resource.');
}
app.get('/download-pdf', checkAuth, checkPdfAccess, async (req, res) => {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
const tripPlanUrl = `${req.protocol}://${req.get('host')}/secure-trip-plan?userId=${req.user.uid}`;
await page.goto(tripPlanUrl, { waitUntil: 'networkidle0' });
const pdfBuffer = await page.pdf({
format: 'Letter',
printBackground: true,
});
await browser.close();
res.set({
'Content-Type': 'application/pdf',
'Content-Disposition': 'attachment; filename="MyTripPlan.pdf"',
'Content-Length': pdfBuffer.length,
});
res.send(pdfBuffer);
} catch (error) {
console.error('Error generating PDF:', error);
res.status(500).send('Error generating PDF.');
}
});
app.get('/secure-trip-plan', checkAuth, (req, res) => {
res.render('trip-pdf', {
tripPlan: req.session.tripPlan,
destination: req.session.destination,
user: req.user
});
});
//generate trip plans using Replicate
async function generateTripPlan(destination, duration, budget, travelCompanion) {
try {
const prompt =
`Generate a detailed ${duration}day trip plan for ${destination} with ${travelCompanion} and with a ${budget} budget. \n\n` +
"**Instructions:**\n" +
'* Use `<day day="N">` tags for each day.\n' +
'* Within each `<day>`, use `<section name="Section Name">` tags.\n' +
"* Describe activities in plain text within `<section>` tags.\n" +
"* **No extra whitespace or line breaks within tags.**\n\n";
console.log("Received Prompt: ", prompt);
const response = await replicateClient.run("meta/meta-llama-3-8b-instruct", {
input: { prompt: prompt, max_tokens: 9600 },
});
const tripPlanTextArray = response;
let tripPlanText = tripPlanTextArray.join("");
tripPlanText = tripPlanText.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
console.log(tripPlanText);
let preProcessedTripPlanText = tripPlanText;
preProcessedTripPlanText = preProcessedTripPlanText.replace(/```/g, "");
preProcessedTripPlanText = preProcessedTripPlanText.replace(/\s*<day/g, "<day");
preProcessedTripPlanText = preProcessedTripPlanText.replace(/<\/day>\s*/g, "</day>");
preProcessedTripPlanText = preProcessedTripPlanText.replace(/\s*<section/g, "<section");
preProcessedTripPlanText = preProcessedTripPlanText.replace(/<\/section>\s*/g, "</section>");
preProcessedTripPlanText = preProcessedTripPlanText.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
if (!tripPlanText || tripPlanText.trim() === "") {
console.warn("AI model returned empty trip plan text.");
return [];
}
const dayRegex = /\s*<day day="(\d+)">(.*?)<\/day>/gs;
const sectionRegex = /<section name="([^"]+)">(.*?)<\/section>/gs;
const days = [];
let dayMatch;
let matchCount = 0;
while ((dayMatch = dayRegex.exec(preProcessedTripPlanText)) !== null) {
matchCount++;
console.log(`--- Day Match ${matchCount} Found ---`);
const dayNumber = dayMatch[1];
const dayContent = dayMatch[2];
const sections = [];
let sectionMatch;
while ((sectionMatch = sectionRegex.exec(dayContent)) !== null) {
const sectionName = sectionMatch[1];
const sectionActivities = sectionMatch[2].trim();
sections.push({
name: sectionName,
activities: sectionActivities,
});
}
days.push({
day: dayNumber,
sections: sections,
});
}
return days;
} catch (error) {
console.error("Error generating trip plan:", error);
throw new Error("Failed to generate trip plan");
}
}
//get flight offers using Amadeus
async function getFlightOffers(origin, destination, departureDate) {
try {
const response = await amadeus.shopping.flightOffersSearch.get({
originLocationCode: origin,
destinationLocationCode: destination,
departureDate: departureDate,
travelClass: "ECONOMY",
adults: 1,
max: 5,
currencyCode: "INR",
});
if (!response?.data || response.data.length === 0) {
console.warn("No flight offers found for the given criteria.");
return [];
}
return response.data;
} catch (error) {
console.error("Error fetching flight offers:", error.response?.result || error.message);
return { error: "Failed to fetch flight offers. Please try again later." };
}
}
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`);
});