Last active
October 8, 2018 01:04
-
-
Save TaylorAckley/cc3fa3e096ec3e1c7f650ebcd266de04 to your computer and use it in GitHub Desktop.
MVC Store
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const User = require('../models/user.model'); | |
const moment = require('moment'); | |
class AccountStore { | |
static async register(req) { | |
let user = new User(req); | |
try { | |
return await user.save(); | |
} catch(exception) { | |
return exception; | |
} | |
} | |
/** Get all users in the system. */ | |
const PAGE_SIZE = 25 | |
const API_URL = 'https://....' | |
static async getAllUsers(req) { | |
const skip = Number(req.query.skip) || 0; | |
const limit = req.query.limit || PAGE_SIZE; | |
let query = {}; | |
const qs = []; | |
if (req.query.search) { | |
const search = new RegExp( | |
req.query.search, // needs to be unescaped | |
'gi' | |
); | |
query.$or = [{ 'profile.email': search }, { 'profile.lastName': search }]; | |
qs.push(`&search=${req.query.search}`); | |
} | |
if (req.query.confirmed) { | |
query['status.confirmed'] = true; | |
qs.push('&confirmed=true'); | |
} | |
if (req.query.marketing) { | |
query['status.optInEmail'] = req.query.marketing; | |
qs.push('&marketing=true'); | |
} | |
try { | |
return await User.find(query) | |
.sort('metadata.createdAt') | |
.limit(limit) | |
.skip(skip) | |
.exec(); | |
payload.payload = users; | |
payload.$records = users.length; | |
payload.$collectionSize = await User.find(query).count(); | |
// if the length of users is less than the size of skip, we can assume there are no more records. | |
payload.$next = | |
users.length < skip | |
? '' | |
: `${API_URL}/admin/accounts?skip=${skip + PAGE_SIZE}${qs.join()}`; | |
} catch(exception) { | |
return exception; | |
} | |
} | |
} | |
module.exports = AccountStore; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment