1+ import { s3 } from '../middlewares/s3.upload.middleware.js' ;
12import { Workshop } from '../models/workshop.model.js' ;
23import { WorkshopRegistration } from '../models/workshopRegistration.model.js' ;
34import { User } from '../models/users.model.js' ;
@@ -350,6 +351,93 @@ const getPendingRegistrations = asyncHandler(async (req, res) => {
350351 ) ;
351352} ) ;
352353
354+ const uploadCertificate = asyncHandler ( async ( req , res ) => {
355+ const { registrationId } = req . params ;
356+ const adminId = req . user . _id ;
357+
358+ if ( ! req . file ) {
359+ throw new ApiError ( 400 , 'Certificate file is required' ) ;
360+ }
361+
362+ const registration = await WorkshopRegistration . findById ( registrationId ) ;
363+
364+ if ( ! registration ) {
365+ throw new ApiError ( 404 , 'Registration not found' ) ;
366+ }
367+
368+ if ( registration . certificate && registration . certificate . key ) {
369+ try {
370+ await s3 . deleteObject ( {
371+ Bucket : process . env . S3_BUCKET_NAME ,
372+ Key : registration . certificate . key
373+ } ) . promise ( ) ;
374+ } catch ( error ) {
375+ console . error ( 'Error deleting old certificate from S3:' , error ) ;
376+ }
377+ }
378+
379+ registration . certificate = {
380+ url : req . file . location ,
381+ key : req . file . key ,
382+ uploadedAt : new Date ( ) ,
383+ uploadedBy : adminId
384+ } ;
385+
386+ await registration . save ( ) ;
387+
388+ await User . updateOne (
389+ { _id : registration . user , 'workshopRegistrations.workshop' : registration . workshop } ,
390+ {
391+ $set : {
392+ 'workshopRegistrations.$.certificate' : req . file . location
393+ }
394+ }
395+ ) ;
396+
397+ return res . status ( 200 ) . json (
398+ new ApiResponse ( 200 , registration , 'Certificate uploaded successfully to AWS S3' )
399+ ) ;
400+ } ) ;
401+
402+ const deleteCertificate = asyncHandler ( async ( req , res ) => {
403+ const { registrationId } = req . params ;
404+
405+ const registration = await WorkshopRegistration . findById ( registrationId ) ;
406+
407+ if ( ! registration ) {
408+ throw new ApiError ( 404 , 'Registration not found' ) ;
409+ }
410+
411+ if ( registration . certificate && registration . certificate . key ) {
412+ try {
413+ await s3 . deleteObject ( {
414+ Bucket : process . env . S3_BUCKET_NAME ,
415+ Key : registration . certificate . key
416+ } ) . promise ( ) ;
417+ } catch ( error ) {
418+ console . error ( 'Error deleting certificate from S3:' , error ) ;
419+ throw new ApiError ( 500 , 'Failed to delete certificate from AWS S3' ) ;
420+ }
421+ }
422+
423+ registration . certificate = undefined ;
424+ await registration . save ( ) ;
425+
426+ await User . updateOne (
427+ { _id : registration . user , 'workshopRegistrations.workshop' : registration . workshop } ,
428+ {
429+ $set : {
430+ 'workshopRegistrations.$.certificate' : null
431+ }
432+ }
433+ ) ;
434+
435+ return res . status ( 200 ) . json (
436+ new ApiResponse ( 200 , registration , 'Certificate deleted successfully from AWS S3' )
437+ ) ;
438+ } ) ;
439+
440+
353441export {
354442 createWorkshop ,
355443 getAllWorkshops ,
@@ -362,5 +450,7 @@ export {
362450 cancelRegistration ,
363451 confirmRegistration ,
364452 rejectRegistration ,
365- getPendingRegistrations
453+ getPendingRegistrations ,
454+ uploadCertificate ,
455+ deleteCertificate
366456} ;
0 commit comments