@@ -8,6 +8,24 @@ import { uploadToCloudinary, isCloudinaryConfigured } from '../utils/cloudinary.
88
99const router = express . Router ( ) ;
1010
11+ // Lazy load certificate service to avoid startup errors if pdf-lib isn't installed
12+ let generateCertificate = null ;
13+ let sendCertificateEmail = null ;
14+
15+ const loadCertificateServices = async ( ) => {
16+ if ( ! generateCertificate ) {
17+ try {
18+ const certService = await import ( '../services/certificate.service.js' ) ;
19+ generateCertificate = certService . generateCertificate ;
20+ const emailService = await import ( '../services/email.service.js' ) ;
21+ sendCertificateEmail = emailService . sendCertificateEmail ;
22+ } catch ( error ) {
23+ console . error ( 'Failed to load certificate services:' , error ) ;
24+ throw new Error ( 'Certificate generation not available. Please ensure pdf-lib is installed.' ) ;
25+ }
26+ }
27+ } ;
28+
1129// All admin routes require authentication
1230router . use ( authenticate ) ;
1331router . use ( requireOrganizer ) ;
@@ -1267,11 +1285,14 @@ router.delete('/events/:id/team/:memberId', async (req, res) => {
12671285 }
12681286} ) ;
12691287
1270- import { generateCertificate } from '../services/certificate.service.js' ;
1271- import { sendCertificateEmail } from '../services/email.service.js' ;
1272-
12731288// Send Certificates to checked-in users
12741289router . post ( '/events/:id/certificates' , async ( req , res ) => {
1290+ try {
1291+ await loadCertificateServices ( ) ;
1292+ } catch ( error ) {
1293+ return res . status ( 500 ) . json ( { error : 'Certificate service unavailable: ' + error . message } ) ;
1294+ }
1295+
12751296 try {
12761297 const { id } = req . params ;
12771298 const { dryRun } = req . body ; // if true, just return count
0 commit comments