-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathusers.js
64 lines (59 loc) · 1.52 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const db = require('./db')
const github = require('./github')
function extractGitHubInfo (data) {
return {
_id: data.login,
login: data.login,
avatar_url: data.avatar_url,
name: data.name,
bio: data.bio,
html_url: data.html_url,
etag: data.etag
}
}
async function ensureExists (login, userData) {
const user = await db.users.findOne({ _id: login })
if (user) {
return
}
if (!userData) {
userData = await github.getUser(login)
}
const userRecord = extractGitHubInfo(userData)
userRecord.npm_verified = false
await db.users.insertOne(userRecord)
}
async function refreshUserGitHub (login) {
const user = await db.users.findOne({ _id: login })
if (user) {
const data = await github.getUser(login)
const userRecord = extractGitHubInfo(data)
return update(userRecord)
}
throw new Error('User not found')
}
async function get (login) {
return db.users.findOne({ _id: login })
}
function update (user) {
return db.users.updateOne(
{ _id: user._id },
{ $set: user }
)
}
async function checkAllExist (userList) {
const docs = await db.users.find({ _id: { $in: userList } }, { projection: { _id: 1 } }).toArray()
const matched = {}
userList.forEach(u => { matched[u] = true })
docs.forEach(d => {
delete matched[d._id]
})
return Object.keys(matched)
}
module.exports = {
get,
ensureExists,
update,
refreshUserGitHub,
checkAllExist
}