Show data Table with pagination (startRow, endRow, filter, sorting)

This commit is contained in:
Paolo Arena
2019-10-14 20:31:57 +02:00
parent c5a19f2d70
commit 9120485939
8 changed files with 219 additions and 34 deletions

View File

@@ -6,6 +6,9 @@ const _ = require('lodash');
const tools = require('../tools/general');
const shared_consts = require('../tools/shared_nodejs');
const queryclass = require('../classes/queryclass');
mongoose.Promise = global.Promise;
mongoose.level = "F";
@@ -86,6 +89,9 @@ var UserSchema = new mongoose.Schema({
perm: {
type: Number
},
ipaddr: {
type: String,
},
date_reg: {
type: Date,
default: Date.now()
@@ -148,8 +154,7 @@ UserSchema.statics.setPermissionsById = function (id, perm) {
UserSchema.statics.isAdmin = function (user) {
try {
const ris = ((user.perm & tools.Permissions.Admin) === 1);
return ris;
return ((user.perm & shared_consts.Permissions.Admin) === shared_consts.Permissions.Admin);
}catch (e) {
return false
}
@@ -263,6 +268,8 @@ UserSchema.statics.findByEmail = function (idapp, email) {
UserSchema.pre('save', function (next) {
var user = this;
/*
if (user.isModified('password')) {
bcrypt.genSalt(10, (err, salt) => {
@@ -291,12 +298,73 @@ UserSchema.methods.removeToken = function (token) {
UserSchema.statics.getUsersList = function (idapp) {
const User = this;
return User.find({ 'idapp': idapp }, { username: 1, name: 1, surname: 1, verified_email: 1, perm:1, email: 1 })
return User.find({ 'idapp': idapp }, { username: 1, name: 1, surname: 1, verified_email: 1, perm:1, email: 1, date_reg: 1 })
};
var User = mongoose.model('User', UserSchema);
UserSchema.statics.getUsersListByParams = function (params) {
const User = this;
myclParamQuery = new queryclass.CParamsQuery(params);
const filterMatchBefore = `${ myclParamQuery.filter }`;
return User.find(
{ $match: filterMatchBefore },
{ 'idapp': idapp },
{ username: 1, name: 1, surname: 1, verified_email: 1, perm:1, email: 1, date_reg: 1 })
};
/**
* Query blog posts by user -> paginated results and a total count.
* @returns {Object} Object -> `{ rows, count }`
*/
UserSchema.statics.queryTable = function (idapp, params) {
const User = this;
if (typeof params.startRow !== 'number') {
throw new Error('startRow must be number')
} else if (typeof params.endRow !== 'number') {
throw new Error('endRow must be number')
}
const query = [
{ $match: Object.assign({ idapp }, params.filter) }
];
if (params.sortBy) {
// maybe we want to sort by blog title or something
const mysort = { $sort: params.sortBy };
// console.log('sortBy', params.sortBy);
// console.table(mysort);
query.push(mysort)
}
query.push(
{ $group: {
_id: null,
// get a count of every result that matches until now
count: { $sum: 1 },
// keep our results for the next operation
results: { $push: '$$ROOT' }
} },
// and finally trim the results to within the range given by start/endRow
{ $project: {
count: 1,
rows: { $slice: ['$results', params.startRow, params.endRow] }
} }
);
return User
.aggregate(query)
.then(([{ count, rows }]) => ({ count, rows }))
};
const User = mongoose.model('User', UserSchema);
class Hero {
constructor(name, level) {