2017-03-25 08:43:55 +01:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import $ from 'cafy';
|
2018-03-29 13:32:18 +02:00
|
|
|
import Vote from '../../../../../models/poll-vote';
|
2018-04-07 19:30:37 +02:00
|
|
|
import Note, { pack } from '../../../../../models/note';
|
2017-03-25 08:43:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get recommended polls
|
|
|
|
*/
|
|
|
|
module.exports = (params, user) => new Promise(async (res, rej) => {
|
|
|
|
// Get 'limit' parameter
|
2018-05-02 11:06:16 +02:00
|
|
|
const [limit = 10, limitErr] = $.num.optional().range(1, 100).get(params.limit);
|
2017-03-25 08:43:55 +01:00
|
|
|
if (limitErr) return rej('invalid limit param');
|
|
|
|
|
|
|
|
// Get 'offset' parameter
|
2018-05-02 11:06:16 +02:00
|
|
|
const [offset = 0, offsetErr] = $.num.optional().min(0).get(params.offset);
|
2017-03-25 08:43:55 +01:00
|
|
|
if (offsetErr) return rej('invalid offset param');
|
|
|
|
|
|
|
|
// Get votes
|
|
|
|
const votes = await Vote.find({
|
2018-03-29 07:48:47 +02:00
|
|
|
userId: user._id
|
2017-03-25 08:43:55 +01:00
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
_id: false,
|
2018-04-07 19:30:37 +02:00
|
|
|
noteId: true
|
2017-03-25 08:43:55 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-04-07 19:30:37 +02:00
|
|
|
const nin = votes && votes.length != 0 ? votes.map(v => v.noteId) : [];
|
2017-03-25 08:43:55 +01:00
|
|
|
|
2018-04-07 19:30:37 +02:00
|
|
|
const notes = await Note
|
2017-03-25 08:43:55 +01:00
|
|
|
.find({
|
|
|
|
_id: {
|
|
|
|
$nin: nin
|
|
|
|
},
|
2018-03-29 07:48:47 +02:00
|
|
|
userId: {
|
2017-03-25 08:50:21 +01:00
|
|
|
$ne: user._id
|
|
|
|
},
|
2017-03-25 08:43:55 +01:00
|
|
|
poll: {
|
|
|
|
$exists: true,
|
|
|
|
$ne: null
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
limit: limit,
|
|
|
|
skip: offset,
|
|
|
|
sort: {
|
|
|
|
_id: -1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Serialize
|
2018-04-07 19:30:37 +02:00
|
|
|
res(await Promise.all(notes.map(async note =>
|
|
|
|
await pack(note, user, { detail: true }))));
|
2017-03-25 08:43:55 +01:00
|
|
|
});
|