@@ -3,6 +3,7 @@ import { SuggestionModel } from "@/models/suggestion.model";
33import { TextModel } from "@/models/text.model" ;
44import { sendEmail } from "@/utils/mailSender" ;
55import { send } from "@/utils/errors" ;
6+ import { UserModel } from "@/models/user.model" ;
67
78//Asigna X cantindad de lectores a un libro
89export const assignReaders = async ( text , amount ) => {
@@ -192,14 +193,20 @@ export const calculateBetweenDatesPoints = async (initialDate, finalDate) => {
192193} ;
193194
194195//Funcion que modifica el status de la sugerencia de un lector a un libro
195- export const changeSuggestionStatus = async ( id , newStatus , previousStatus ) => {
196+ export const changeSuggestionStatus = async (
197+ id ,
198+ newStatus ,
199+ previousStatus ,
200+ session
201+ ) => {
196202 const suggestion = await SuggestionModel . findById ( id ) . populate ( "text" ) ;
197203 if ( ! suggestion ) throw { error : `Suggestion with id: ${ id } not found` } ;
198204 if ( suggestion . suggestionStatus === previousStatus ) {
199205 suggestion . suggestionStatus = newStatus ;
200206 await SuggestionModel . updateOne (
201207 { _id : suggestion . _id } ,
202- { suggestionStatus : newStatus , ...suggestion . _doc }
208+ { suggestionStatus : newStatus , ...suggestion . _doc } ,
209+ { session }
203210 ) ;
204211 return suggestion ;
205212 } else {
@@ -211,26 +218,58 @@ export const changeSuggestionStatus = async (id, newStatus, previousStatus) => {
211218
212219//Funcion que rechaza la sugerencia a un libro por parte de un lector
213220export const rejectSuggestion = ( request , response ) => {
214- send ( response , async ( ) => {
221+ send ( response , async ( session ) => {
215222 const { id } = request . params ;
216- var suggestion = await SuggestionModel . findById ( id ) ;
217- var text = await TextModel . findById ( suggestion . text ) ;
223+ const suggestion = await SuggestionModel . findById ( id ) ;
224+ const textPromise = TextModel . findById ( suggestion . text ) ;
225+ const readerPromise = ReaderModel . findById ( suggestion . reader ) ;
226+ const [ text , reader ] = await Promise . all ( [ textPromise , readerPromise ] ) ;
227+ const user = await UserModel . findById ( reader . user ) ;
218228 await assignReaders ( text , 1 ) ;
219229 const newSuggestion = await changeSuggestionStatus (
220230 id ,
221231 "Rejected" ,
222- "Pending"
232+ "Pending" ,
233+ session
223234 ) ;
235+ // Update reader rejects in a row
236+ reader . rejectsInARow ++ ;
237+ await reader . save ( { session } ) ;
238+
239+ // If reader has rejected over five suggestions, send mail
240+ if ( reader . rejectsInARow > 5 ) {
241+ const emailData = {
242+ email : user . email ,
243+ subject : "Rechazo de lecturas"
244+ } ;
245+ await sendEmail ( emailData , "reading_rejects" , { readerName : user . name } ) ;
246+ console . log ( `EMAIL SENT TO USER ${ user . name } ` ) ;
247+ }
248+
224249 return newSuggestion ;
225250 } ) ;
226251} ;
227252
228253//Funcion que acepta la sugerencia a un libro por parte de un lector
229254export const acceptSuggestion = ( request , response ) => {
230- send ( response , async ( ) => {
255+ send ( response , async ( session ) => {
231256 const { id } = request . params ;
232- const suggestion = await changeSuggestionStatus ( id , "Accepted" , "Pending" ) ;
233- return suggestion ;
257+ const suggestion = await SuggestionModel . findById ( id ) ;
258+ const newSuggestionPromise = changeSuggestionStatus (
259+ id ,
260+ "Accepted" ,
261+ "Pending" ,
262+ session
263+ ) ;
264+ const readerPromise = ReaderModel . findById ( suggestion . reader ) ;
265+ const [ newSuggestion , reader ] = await Promise . all ( [
266+ newSuggestionPromise ,
267+ readerPromise
268+ ] ) ;
269+ // Set rejects in a row to 0
270+ reader . rejectsInARow = 0 ;
271+ await reader . save ( ) ;
272+ return newSuggestion ;
234273 } ) ;
235274} ;
236275
@@ -241,7 +280,8 @@ export const completeSuggestion = (request, response) => {
241280 const suggestion = await changeSuggestionStatus (
242281 id ,
243282 "Completed" ,
244- "Accepted"
283+ "Accepted" ,
284+ session
245285 ) ;
246286 return suggestion ;
247287 } ) ;
@@ -366,7 +406,7 @@ export const getReadersWithoutSuggestion = (request, response) => {
366406 occupiedReaders . forEach ( ( element ) => {
367407 idOccupied . push ( element . reader . toString ( ) ) ;
368408 } ) ;
369- var finalArr = readers . filter ( function ( item ) {
409+ var finalArr = readers . filter ( function ( item ) {
370410 return idOccupied . indexOf ( item . _id . toString ( ) ) === - 1 ;
371411 } ) ;
372412 return finalArr ;
@@ -377,9 +417,7 @@ export const getReadersWithoutSuggestion = (request, response) => {
377417export const deleteSuggestionAdmin = ( request , response ) => {
378418 send ( response , async ( ) => {
379419 const { id } = request . params ;
380- SuggestionModel . findOne ( { _id : id } )
381- . deleteOne ( )
382- . exec ( ) ;
420+ SuggestionModel . findOne ( { _id : id } ) . deleteOne ( ) . exec ( ) ;
383421 } ) ;
384422} ;
385423
0 commit comments