notifications: fix pagination of listByUserIdPaged

we have to filter in sql query, otherwise we don't get consistent per page count
This commit is contained in:
Girish Ramakrishnan
2021-04-20 13:49:05 -07:00
parent fbceb67df9
commit bb3f9744fb
3 changed files with 28 additions and 22 deletions

View File

@@ -103,21 +103,28 @@ function del(id, callback) {
});
}
function listByUserIdPaged(userId, page, perPage, callback) {
function listByUserIdPaged(userId, options, page, perPage, callback) {
assert.strictEqual(typeof userId, 'string');
assert.strictEqual(typeof options, 'object');
assert(options.acknowledged === null || typeof options.acknowledged === 'boolean');
assert.strictEqual(typeof page, 'number');
assert.strictEqual(typeof perPage, 'number');
assert.strictEqual(typeof callback, 'function');
var data = [ userId ];
var query = 'SELECT ' + NOTIFICATION_FIELDS + ' FROM notifications WHERE userId=?';
let args = [ userId ];
let query = `SELECT ${NOTIFICATION_FIELDS} FROM notifications WHERE userId=?`;
if ('acknowledged' in options) {
query += ' AND acknowledged=?';
args.push(options.acknowledged);
}
query += ' ORDER BY creationTime DESC LIMIT ?,?';
data.push((page-1)*perPage);
data.push(perPage);
args.push((page-1)*perPage);
args.push(perPage);
database.query(query, data, function (error, results) {
database.query(query, args, function (error, results) {
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
results.forEach(postProcess);