diff --git a/Backend/controllers/bazaarVendorApplicationsController.js b/Backend/controllers/bazaarVendorApplicationsController.js index 94a3505..ae68405 100644 --- a/Backend/controllers/bazaarVendorApplicationsController.js +++ b/Backend/controllers/bazaarVendorApplicationsController.js @@ -217,6 +217,8 @@ const updateBazaarVendorApplication = async (req, res) => { const doc = await BazaarVendorApplication.findById(id); if (!doc) return res.status(404).json({ error: 'Not found' }); + const previousStatus = doc.status; + if (req.body.vendorId) { if (!isValidObjectId(req.body.vendorId)) { return res.status(400).json({ error: 'Invalid vendorId' }); @@ -244,11 +246,100 @@ const updateBazaarVendorApplication = async (req, res) => { } if (req.body.status) doc.status = req.body.status; + if (req.body.paidAt) doc.paidAt = req.body.paidAt; + if (req.body.stripeSessionId) doc.stripeSessionId = req.body.stripeSessionId; await doc.save(); const refreshed = await BazaarVendorApplication.findById(id) .populate('bazaarId', 'name location description startDate endDate'); + // If status changed to accepted, payment is complete - generate and send QR codes + if (refreshed.status === 'accepted' && previousStatus !== 'accepted') { + + console.log(`🎫 Payment completed for bazaar application ${id}, generating QR codes...`); + + try { + // Update payment status to paid + await BazaarVendorApplication.findByIdAndUpdate(id, { paymentStatus: 'paid' }); + + // Generate QR codes for all attendees + if (refreshed.attendeeNames && refreshed.attendeeNames.length > 0) { + const qrCodes = []; + + for (let i = 0; i < refreshed.attendeeNames.length; i++) { + const attendeeName = refreshed.attendeeNames[i]; + const attendeeEmail = refreshed.attendeeEmails && refreshed.attendeeEmails[i] ? refreshed.attendeeEmails[i] : ''; + + // Create QR code data with attendee information + const qrData = { + applicationId: refreshed._id.toString(), + applicationType: 'bazaar', + vendorId: refreshed.vendorId.toString(), + attendeeName: attendeeName, + attendeeEmail: attendeeEmail, + attendeeIndex: i, + bazaarName: refreshed.bazaarId?.name || 'Bazaar Event', + boothSize: refreshed.boothSize, + generatedAt: new Date().toISOString() + }; + + // Generate QR code image + const qrCodeDataUrl = await QRCode.toDataURL(JSON.stringify(qrData), { + errorCorrectionLevel: 'H', + type: 'image/png', + width: 300, + margin: 2 + }); + + qrCodes.push({ + attendeeName: attendeeName, + attendeeEmail: attendeeEmail, + qrCodeDataUrl: qrCodeDataUrl, + qrData: qrData + }); + + // Send individual QR code to attendee if email provided + if (attendeeEmail) { + try { + const vendor = await Vendor.findById(refreshed.vendorId); + await sendAttendeeQRCodeEmail( + attendeeEmail, + attendeeName, + qrCodeDataUrl, + refreshed.bazaarId?.name || 'Bazaar Event', + vendor?.companyName || 'Vendor', + 'bazaar' + ); + console.log(`✅ QR code sent to attendee: ${attendeeName} (${attendeeEmail})`); + } catch (emailError) { + console.error(`⚠️ Failed to send QR code to attendee ${attendeeEmail}:`, emailError.message); + } + } + } + + // Send all QR codes to vendor + const vendor = await Vendor.findById(refreshed.vendorId); + if (vendor && qrCodes.length > 0) { + try { + await sendVendorQRCodesEmail( + vendor.email, + vendor.companyName, + qrCodes, + refreshed.bazaarId?.name || 'Bazaar Event', + 'bazaar' + ); + console.log(`✅ All QR codes sent to vendor: ${vendor.companyName} (${vendor.email})`); + } catch (emailError) { + console.error(`⚠️ Failed to send QR codes to vendor:`, emailError.message); + } + } + } + } catch (qrError) { + console.error('⚠️ Error generating/sending QR codes:', qrError.message); + // Don't fail the update if QR generation fails - just log the error + } + } + res.status(200).json(formatApplication(refreshed, req)); } catch (err) { res.status(400).json({ error: err.message });