11import Request from "../schema/request" ;
22import Comment from "../schema/comment" ;
33import Torrent from "../schema/torrent" ;
4+ import User from "../schema/user" ;
45
56export const createRequest = async ( req , res , next ) => {
67 if ( req . body . title && req . body . body ) {
@@ -153,7 +154,7 @@ export const fetchRequest = async (req, res, next) => {
153154 $lookup : {
154155 from : "torrents" ,
155156 as : "candidates" ,
156- let : { torrentIds : "$candidates" } ,
157+ let : { torrentIds : "$candidates.torrent " } ,
157158 pipeline : [
158159 { $match : { $expr : { $in : [ "$_id" , "$$torrentIds" ] } } } ,
159160 {
@@ -254,7 +255,7 @@ export const addCandidate = async (req, res, next) => {
254255
255256 if (
256257 request . candidates
257- . map ( ( c ) => c . toString ( ) )
258+ . map ( ( c ) => c . torrent . toString ( ) )
258259 . includes ( torrent . _id . toString ( ) )
259260 ) {
260261 res . status ( 409 ) . send ( "Torrent has already been suggested" ) ;
@@ -263,7 +264,11 @@ export const addCandidate = async (req, res, next) => {
263264
264265 await Request . findOneAndUpdate (
265266 { _id : req . params . requestId } ,
266- { $addToSet : { candidates : torrent . _id } }
267+ {
268+ $addToSet : {
269+ candidates : { torrent : torrent . _id , suggestedBy : req . userId } ,
270+ } ,
271+ }
267272 ) ;
268273
269274 res . status ( 200 ) . send ( { torrent } ) ;
@@ -293,11 +298,11 @@ export const acceptCandidate = async (req, res, next) => {
293298 infoHash : req . body . infoHash ,
294299 } ) . lean ( ) ;
295300
296- if (
297- ! request . candidates . some (
298- ( candidate ) => candidate . toString ( ) === torrent . _id . toString ( )
299- )
300- ) {
301+ const candidate = request . candidates . find (
302+ ( c ) => c . torrent . toString ( ) === torrent . _id . toString ( )
303+ ) ;
304+
305+ if ( ! candidate ) {
301306 res
302307 . status ( 403 )
303308 . send ( "Cannot accept a torrent that has not been suggested" ) ;
@@ -309,6 +314,20 @@ export const acceptCandidate = async (req, res, next) => {
309314 { $set : { fulfilledBy : torrent . _id } }
310315 ) ;
311316
317+ await User . findOneAndUpdate (
318+ { _id : candidate . suggestedBy } ,
319+ {
320+ $inc : {
321+ bonusPoints :
322+ process . env . SQ_BP_EARNED_PER_FILLED_REQUEST *
323+ ( torrent . uploadedBy . toString ( ) ===
324+ candidate . suggestedBy . toString ( )
325+ ? 2
326+ : 1 ) ,
327+ } ,
328+ }
329+ ) ;
330+
312331 res . status ( 200 ) . send ( { torrent : torrent . _id } ) ;
313332 } catch ( e ) {
314333 next ( e ) ;
0 commit comments