-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathbookingsController.js
More file actions
357 lines (300 loc) · 11.8 KB
/
Copy pathbookingsController.js
File metadata and controls
357 lines (300 loc) · 11.8 KB
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
const paypal = require('@paypal/checkout-server-sdk');
const nodemailer = require('nodemailer');
const Joi = require('joi');
require('dotenv').config();
const environment =
process.env.NODE_ENV === 'production'
? new paypal.core.LiveEnvironment(
process.env.PAYPAL_CLIENT_ID,
process.env.PAYPAL_CLIENT_SECRET,
)
: new paypal.core.SandboxEnvironment(
process.env.PAYPAL_CLIENT_ID,
process.env.PAYPAL_CLIENT_SECRET,
);
const client = new paypal.core.PayPalHttpClient(environment);
const bookingsController = (Booking, Listing, User) => {
const calculatePrice = (startDate, endDate, basePrice) => {
const start = new Date(startDate);
const end = new Date(endDate);
const days = Math.ceil((end - start) / (1000 * 60 * 60 * 24));
if (days <= 0) throw new Error('Invalid date range');
return { totalAmount: parseFloat((days * basePrice).toFixed(2)), days };
};
const sendBookingNotifications = async (bookingId) => {
try {
const booking = await Booking.findById(bookingId)
.populate('userId', 'email firstName lastName')
.populate('listingId');
if (!booking) {
console.error('Booking not found for notifications');
return;
}
if (!booking.userId?.email) {
console.error('Guest email not found');
return;
}
const host = await User.findById(booking.listingId.createdBy);
if (!host?.email) {
console.error('Host email not found');
return;
}
if (!process.env.EMAIL_USER || !process.env.EMAIL_PASS) {
console.warn('Email credentials not configured');
return;
}
const transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
},
});
const formatDate = (date) => new Date(date).toLocaleDateString();
const guestMailOptions = {
from: process.env.EMAIL_USER,
to: booking.userId.email,
subject: `Booking Confirmed: ${booking.listingId.title}`,
html: `
<h2>Booking Confirmed! 🎉</h2>
<p>Your booking for <strong>"${booking.listingId.title}"</strong> has been confirmed.</p>
<p><strong>Dates:</strong> ${formatDate(booking.startDate)} to ${formatDate(booking.endDate)}</p>
<p><strong>Total Amount:</strong> $${booking.totalPrice}</p>
<p><strong>Booking ID:</strong> ${booking._id}</p>
<br>
<p>Thank you for your booking!</p>
`,
};
const hostMailOptions = {
from: process.env.EMAIL_USER,
to: host.email,
subject: `New Booking: ${booking.listingId.title}`,
html: `
<h2>New Booking! 🎉</h2>
<p>Your listing <strong>"${booking.listingId.title}"</strong> has been booked!</p>
<p><strong>Dates:</strong> ${formatDate(booking.startDate)} to ${formatDate(booking.endDate)}</p>
<p><strong>Total Amount:</strong> $${booking.totalPrice}</p>
<p><strong>Guest:</strong> ${booking.userId.firstName} ${booking.userId.lastName}</p>
<p><strong>Guest Email:</strong> ${booking.userId.email}</p>
<p><strong>Booking ID:</strong> ${booking._id}</p>
`,
};
await transporter.sendMail(guestMailOptions);
await transporter.sendMail(hostMailOptions);
} catch (error) {
console.error('Email notification error:', error.message);
}
};
const createPaymentIntent = async (req, res) => {
try {
const schema = Joi.object({
listingId: Joi.string().hex().length(24).required().messages({
'string.hex': 'Invalid listing ID format',
'string.length': 'Listing ID must be 24 characters',
}),
startDate: Joi.date().iso().greater('now').required().messages({
'date.iso': 'Start date must be in ISO format (YYYY-MM-DD)',
'date.greater': 'Start date must be in the future',
}),
endDate: Joi.date().iso().greater(Joi.ref('startDate')).required().messages({
'date.iso': 'End date must be in ISO format (YYYY-MM-DD)',
'date.greater': 'End date must be after start date',
}),
currency: Joi.string().valid('USD', 'EUR', 'GBP').default('USD'),
}).unknown(true);
const { error, value } = schema.validate(req.body);
if (error) {
return res.status(400).json({
error: 'Validation failed',
details: error.details.map((detail) => detail.message),
});
}
const { listingId, startDate, endDate, currency } = value;
const userId = req.body.requestor?.requestorId;
if (!userId) {
return res.status(401).json({ error: 'Unauthorized - User authentication required' });
}
const listing = await Listing.findById(listingId);
if (!listing) {
return res.status(404).json({ error: 'Listing not found' });
}
const { totalAmount } = calculatePrice(startDate, endDate, listing.price);
const startDateSimple = new Date(startDate).toISOString().split('T')[0];
const endDateSimple = new Date(endDate).toISOString().split('T')[0];
const request = new paypal.orders.OrdersCreateRequest();
request.prefer('return=representation');
request.requestBody({
intent: 'CAPTURE',
purchase_units: [
{
amount: {
currency_code: currency,
value: totalAmount.toString(),
},
description: `Booking for ${listing.title} (${startDate} to ${endDate})`,
custom_id: `dates|${startDateSimple}|${endDateSimple}|${listingId}`,
},
],
});
const order = await client.execute(request);
res.status(200).json({
success: true,
orderId: order.result.id,
totalAmount,
listingId,
currency,
});
} catch (err) {
console.error('PayPal Error:', err.message);
if (err.statusCode >= 400 && err.statusCode < 500) {
return res.status(400).json({
error: 'Payment processing failed',
details: err.message,
});
}
res.status(500).json({
error: 'Failed to create payment order',
details: process.env.NODE_ENV === 'development' ? err.message : 'Internal server error',
});
}
};
const createBooking = async (req, res) => {
try {
const schema = Joi.object({
listingId: Joi.string().hex().length(24).required(),
startDate: Joi.date().iso().required(),
endDate: Joi.date().iso().required(),
paypalOrderId: Joi.string().required(),
}).unknown(true);
const { error, value } = schema.validate(req.body);
if (error) {
return res.status(400).json({
error: 'Validation failed',
details: error.details.map((detail) => detail.message),
});
}
const { listingId, startDate, endDate, paypalOrderId } = value;
const userId = req.body.requestor?.requestorId;
if (!userId) {
return res.status(401).json({ error: 'Unauthorized - User authentication required' });
}
const startDateObj = new Date(startDate);
const endDateObj = new Date(endDate);
const today = new Date();
today.setHours(0, 0, 0, 0);
if (startDateObj <= today) {
return res.status(400).json({ error: 'Start date must be in the future' });
}
if (endDateObj <= startDateObj) {
return res.status(400).json({ error: 'End date must be after start date' });
}
const paymentRequest = new paypal.orders.OrdersGetRequest(paypalOrderId);
const paymentDetails = await client.execute(paymentRequest);
if (!['CREATED', 'COMPLETED'].includes(paymentDetails.result.status)) {
return res.status(400).json({
error: `Payment not completed. Status: ${paymentDetails.result.status}`,
});
}
const listing = await Listing.findById(listingId);
if (!listing) {
return res.status(404).json({ error: 'Listing not found' });
}
const paymentAmount = parseFloat(paymentDetails.result.purchase_units[0].amount.value);
const { totalAmount } = calculatePrice(startDate, endDate, listing.price);
if (Math.abs(paymentAmount - totalAmount) > 0.01) {
return res.status(400).json({
error: 'Payment amount does not match booking amount',
details: `Paid: $${paymentAmount}, Expected: $${totalAmount}`,
});
}
const customId = paymentDetails.result.purchase_units[0].custom_id;
if (!customId || !customId.startsWith('dates|')) {
return res.status(400).json({
error: 'Invalid payment session',
details: 'This payment was not created for booking. Please create a new payment session.',
});
}
const parts = customId.split('|');
if (parts.length !== 4) {
return res.status(400).json({
error: 'Payment session format mismatch',
details: `Payment session data is corrupted. Found ${parts.length} parts, expected 4. Please create a new payment session.`,
});
}
const [, paidStartDate, paidEndDate, paidListingId] = parts;
if (paidListingId !== listingId) {
return res.status(400).json({
error: 'Listing mismatch',
details: `Payment was created for listing ${paidListingId}, but trying to book listing ${listingId}`,
});
}
const normalizedRequestStart = new Date(startDate).toISOString().split('T')[0];
const normalizedRequestEnd = new Date(endDate).toISOString().split('T')[0];
if (paidStartDate !== normalizedRequestStart || paidEndDate !== normalizedRequestEnd) {
return res.status(400).json({
error: 'Booking dates do not match payment dates',
details: `Payment was created for dates: ${paidStartDate} to ${paidEndDate}, but trying to book: ${normalizedRequestStart} to ${normalizedRequestEnd}`,
});
}
const conflict = await Booking.findOne({
listingId,
$or: [{ startDate: { $lt: endDateObj }, endDate: { $gt: startDateObj } }],
status: { $in: ['pending', 'confirmed'] },
});
if (conflict) {
return res.status(400).json({
error: 'Selected dates are unavailable. Please choose different dates.',
});
}
const newBooking = new Booking({
userId,
listingId,
startDate: startDateObj,
endDate: endDateObj,
totalPrice: totalAmount,
paypalOrderId,
status: 'confirmed',
});
await newBooking.save();
await sendBookingNotifications(newBooking._id);
const populatedBooking = await Booking.findById(newBooking._id).populate(
'listingId',
'title',
);
res.status(201).json({
success: true,
message: 'Booking created successfully',
bookingId: newBooking._id,
bookingDetails: {
listing: populatedBooking.listingId.title,
dates: {
startDate: startDateObj,
endDate: endDateObj,
},
totalAmount,
status: 'confirmed',
},
});
} catch (err) {
console.error('Booking Error:', err.message);
if (err.name === 'ValidationError') {
const errors = Object.values(err.errors).map((error) => error.message);
return res.status(400).json({
error: 'Validation failed',
details: errors,
});
}
if (err.name === 'MongoError' && err.code === 11000) {
return res.status(400).json({
error: 'Duplicate booking detected',
});
}
res.status(500).json({
error: 'Failed to create booking',
details: process.env.NODE_ENV === 'development' ? err.message : undefined,
});
}
};
return { createPaymentIntent, createBooking };
};
module.exports = bookingsController;