'use strict'; const Joi = require('joi'); const Jwt = require('@hapi/jwt'); module.exports = [ { method: 'POST', path: '/movie', options: { auth: { scope : 'admin' }, tags: ['api'], validate: { payload: Joi.object({ title: Joi.string().required().min(3).example('The Matrix').description('Title of the movie'), description: Joi.string().required().min(3).example('A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers.').description('Description of the movie'), director: Joi.string().required().min(3).example('Lana Wachowski').description('Director of the movie'), releaseDate: Joi.date().required().example('1999-03-31').description('Release date of the movie'), }) } }, handler: async (request, h) => { const { User } = request.models(); const { movieService } = request.services(); const { title, description, director, releaseDate } = request.payload; //send email to all users const { emailService } = request.services(); const users = await User.query(); for (const user of users) { await emailService.sendEmail( user.email, user.firstName, user.lastName, 'New movie added', '\n' + '\n' + '\n' + '\n' + ' \n' + ' \n' + ' A new movie was added !\n' + ' \n' + '\n' + '\n' + '\n' + '
\n' + '
\n' + '

A new movie was added !

\n' + '
\n' + '
\n' + '

Hello,

\n' + '

A new movie was just added to the library.

\n' + '

Go discover it now !

\n' + ' Discover new movie\n' + '

Happy exploring!

\n' + '
\n' + ' \n' + '
\n' + '\n' + '\n' + '\n' ); } // Appelle la méthode create du movieService en passant les champs nécessaires return await movieService.create({ title: title, description: description, director: director, releaseDate: releaseDate }); } }, { method: 'GET', path: '/movies', options: { tags: ['api'], auth: false }, handler: async (request, h) => { const { Movie } = request.models(); const { movieService } = request.services(); // Appelle la méthode list du movieService pour récupérer tous les films return await movieService.list(); } }, { method: 'GET', path: '/movie/{id}', options: { tags: ['api'], auth: false, validate: { params: Joi.object({ id: Joi.number().integer().required().example(1).description('ID of the movie') }) } }, handler: async (request, h) => { const { Movie } = request.models(); const { movieService } = request.services(); const { id } = request.params; // Appelle la méthode get du movieService pour récupérer le film par son ID return await movieService.get(id); } }, { method: 'DELETE', path: '/movie/{id}', options: { auth: { scope : 'admin' }, tags: ['api'], validate: { params: Joi.object({ id: Joi.number().integer().positive().required().example(1).description('ID of the movie to delete') }) } }, handler: async (request, h) => { const { movieService } = request.services(); const { id } = request.params; // Appelle la méthode delete du movieService pour supprimer le film par son ID await movieService.delete(id); // Renvoie une réponse vide lorsque la suppression se déroule correctement return 'deleted'; } }, { method: 'PATCH', path: '/movie/{id}', options: { auth: { scope : 'admin' }, tags: ['api'], validate: { params: Joi.object({ id: Joi.number().integer().positive().required().example(1).description('ID of the movie to update') }), payload: Joi.object({ title: Joi.string().min(3).example('The Matrix').description('Title of the movie'), description: Joi.string().min(3).example('A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers.').description('Description of the movie'), director: Joi.string().min(3).example('Lana Wachowski').description('Director of the movie'), releaseDate: Joi.date().example('1999-03-31').description('Release date of the movie'), }) } }, handler: async (request, h) => { const { movieService } = request.services(); const { id } = request.params; //Email users who have favorited the movie const { emailService } = request.services(); const { Favorite } = request.models(); const { User } = request.models(); const favorites = await Favorite.query().where('movie_id', id); for (const favorite of favorites) { const user = await User.query().findById(favorite.user_id); await emailService.sendEmail( user.email, user.firstName, user.lastName, 'Movie updated', '\n' + '\n' + '\n' + '\n' + ' \n' + ' \n' + ' A movie was updated !\n' + ' \n' + '\n' + '\n' + '\n' + '
\n' + '
\n' + '

A movie was updated !

\n' + '
\n' + '
\n' + '

Hello,

\n' + '

The movie ' + request.payload.title + ' was just updated in the library.

\n' + '

Go check it out now !

\n' + ' Discover updated movie\n' + '

Happy exploring!

\n' + '
\n' + ' \n' + '
\n' + '\n' + '\n' + '\n' ); } // Appelle la méthode update du movieService pour mettre à jour le film par son ID return await movieService.update(id, request.payload); } }, { method: 'GET', path: '/movie/csv', options: { auth: { scope: 'admin' }, tags: ['api'] }, handler: async (request, h) => { const { rabbitmqService } = request.services(); const { Movie } = request.models(); const { movieService } = request.services(); try { // Récupérer les films depuis la base de données const movies = await Movie.query(); // Générer le fichier CSV const csvData = await movieService.generateCsvData(movies); console.log(csvData); // Envoyer le fichier CSV via RabbitMQ pour l'envoi par e-mail await rabbitmqService.sendCsvToQueue(); //When message is received, send the csv to the admin email await rabbitmqService.receiveAndReturnCSV().then( //Send the csv to the admin email async () => { const {emailService} = request.services(); await emailService.sendEmailWithAttachment( request.auth.credentials.email, 'Admin', 'Admin', 'Movie list in CSV', 'Find the movie list in CSV format attached to this email.', csvData ); }); return h.response({ message: 'CSV sent to the admin email' }).code(200); } catch (error) { console.error('Erreur lors de l\'export CSV :', error); return h.response({ message: 'Erreur lors du traitement de la requête' + error }).code(500); } } } ]