Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vuquangchien/5371bd44a516ab029a9dd44230315b13 to your computer and use it in GitHub Desktop.
Save vuquangchien/5371bd44a516ab029a9dd44230315b13 to your computer and use it in GitHub Desktop.
import { digest } from 'json-hash';
import mongose from 'mongoose';
import passportLocalMongoose from 'passport-local-mongoose';
import { fetchAndCache } from '../cache/redis';
const ObjectId = mongose.Schema.ObjectId;
const AdminUser = new mongose.Schema({
username: {
type: String,
unique: true,
require: true,
},
fullName: String,
email: {
type: String,
require: true,
unique: true,
index: true,
},
groups: [{ type: ObjectId, ref: 'AdminUserGroup', index: true }],
groupsFull: [
{
_id: String,
name: String,
},
],
permissions: [String],
isDeleted: Boolean,
createdDate: {
type: Date,
index: true,
defaultValue: new Date(),
},
questionPermissions: [
{
subjectSlug: {
type: String,
index: true,
},
gradeNumber: {
type: Number,
index: true,
},
roundNumber: {
type: Number,
index: true,
},
resource: {
type: String,
index: true,
},
action: {
type: String,
index: true,
},
detail: {
type: String,
index: true,
},
},
],
});
AdminUser.plugin(passportLocalMongoose, {
// Needed to set usernameUnique to true to avoid a mongodb index on the username column!
usernameUnique: false,
findByUsername(model, queryParameters) {
// Add additional query parameter - AND condition - active: true
// eslint-disable-next-line no-param-reassign
queryParameters.isDeleted = { $ne: true };
const find = {
exec: callback => {
const key = digest(queryParameters);
fetchAndCache({
key,
promise: model
.findOne(queryParameters)
// .select('-questionPermissions')
.populate('groups'),
})
.then(result => callback(null, result))
.catch(error => callback(error));
},
};
return find;
},
});
export default mongose.model('AdminUser', AdminUser);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment