-
Notifications
You must be signed in to change notification settings - Fork 6
/
users.js
48 lines (39 loc) · 1.11 KB
/
users.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const express = require('express')
const db = require('../db')
const { celebrate } = require('celebrate')
const { schemas } = require('adex-models')
const router = express.Router()
router.post('/', celebrate({ body: schemas.user }), postUser)
router.get('/list', getUserList)
function postUser(req, res) {
const usersCol = db.getMongo().collection('users')
const user = req.body
// Assuming user has properties:
// role (to distinguish advertisers/publishers),
// channels, withdrawn (so we can get hasInteracted)
usersCol.insertOne(user).then(() => res.send({ success: true }))
}
function getUserList(req, res) {
const usersCol = db.getMongo().collection('users')
const hasInteracted = req.query.hasInteracted
let query = {}
if (hasInteracted === 'true') {
query = {
$or: [
{ channels: { $exists: true, $ne: null } },
{ withdrawn: { $exists: true, $gt: 0 } },
],
}
}
usersCol
.find(query)
.toArray()
.then(result => {
return res.send(result)
})
.catch(err => {
console.error('Error getting user list', err)
return res.status(500).send(err.toString())
})
}
module.exports = router