From 1e723490263f21f33a813380c87a3b06abb45577 Mon Sep 17 00:00:00 2001 From: hpcwoess Date: Fri, 28 Aug 2020 21:50:53 +0200 Subject: [PATCH] update visibility --- models/Track.js | 2 ++ routes/api/tracks.js | 15 +++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/models/Track.js b/models/Track.js index b94928e..edb165d 100644 --- a/models/Track.js +++ b/models/Track.js @@ -9,6 +9,7 @@ var TrackSchema = new mongoose.Schema({ title: String, description: String, body: String, + visible: Boolean, numEvents: {type: Number, default: 0}, comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }], author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }, @@ -37,6 +38,7 @@ TrackSchema.methods.toJSONFor = function(user){ createdAt: this.createdAt, updatedAt: this.updatedAt, visibleForAll: this.author ? this.author.areTracksVisibleForAll : false, + visible: this.visible, author: this.author.toProfileJSONFor(user) }; }; diff --git a/routes/api/tracks.js b/routes/api/tracks.js index e61554d..71ae71f 100644 --- a/routes/api/tracks.js +++ b/routes/api/tracks.js @@ -180,7 +180,7 @@ router.get('/', auth.optional, function(req, res, next) { .skip(Number(offset)) .sort({createdAt: 'desc'}) .populate('author') - .where('visibleForAll || author == user') + .where('visible').equals(true) .exec(), Track.countDocuments(query).exec(), req.payload ? User.findById(req.payload.id) : null, @@ -191,10 +191,10 @@ router.get('/', auth.optional, function(req, res, next) { //console.log(tracks); var retTracks = []; for (t of tracks) { - console.log(t); - if (t.author.areTracksVisibleForAll || t.author == user) { + //console.log(t); + //if (t.author.areTracksVisibleForAll || t.author == user) { retTracks.push(t); - } + //} } return res.json({ tracks: retTracks.map(function(track){ @@ -277,6 +277,7 @@ router.post('/', auth.required, function(req, res, next) { track.trackData = trackData._id; track.author = user; + track.visible = track.author.areTracksVisibleForAll; trackData.save(function (err){ if(err){ console.log("failed to save trackData"); @@ -422,10 +423,12 @@ router.put('/:track', auth.required, function(req, res, next) { } if(typeof req.body.track.tagList !== 'undefined'){ - req.track.tagList = req.body.track.tagList + req.track.tagList = req.body.track.tagList; } + req.track.visible = req.body.track.visible; + console.log("saving track"); - req.body.track.save().then(function(track){ + req.track.save().then(function(track){ return res.json({track: track.toJSONFor(user)}); }).catch(next); } else {