-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathindex.js
More file actions
104 lines (89 loc) · 3.17 KB
/
index.js
File metadata and controls
104 lines (89 loc) · 3.17 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const { pick } = require('lodash')
const debug = require('debug')('dos')
const { User, UserChange } = require('../../models')
module.exports = {
/**
* Create or update one or more users.
*
* @param {Array} users The array of users.
* @return {Promise} A promise.
*/
createOrUpdateUsers(users = []) {
debug(`Create or update ${users.length} users...`)
return Promise.all(
users.map(async (user) => {
const exist = await User.findOne({ where: { originalId: user.originalId } })
if (exist) {
return await this.updateUser(exist.id, user)
} else {
return await User.create(user)
}
})
)
},
/**
* Find all users.
*
* @param {Array} attributes The list of attributes to return.
*
* @return {Promise} A promise.
*/
findAllUsers(attributes = []) {
debug('Find all users with:', { attributes })
if (attributes && !Array.isArray(attributes)) {
attributes = [attributes]
}
const opts = {}
if (attributes.length) {
opts.attributes = attributes
}
return User.findAll(opts)
},
/**
* Try to update a user in the database.
* The user will only be updated if a change was detected.
* If a change was detected a user change record will be also created.
* @param {Number} id The id of the user to update.
* @param {Object} user The user to update.
* @return {Promise}
*/
async updateUser(id, user) {
debug(`Update user with id: ${id}...`)
const original = await User.findByPk(id)
if (!original) {
throw new Error('No existing user.')
}
if (this.isDifferentUser(original, user)) {
await this.createUserChange(original, user)
const updates = pick(user, ['login', 'name', 'description', 'type', 'url', 'avatarUrl', 'company', 'location', 'followers', 'following', 'sources', 'forked', 'collaborations'])
return await User.update(updates, { where: { originalId: user.originalId } })
}
debug(`Nothing to update for user with login: ${user.login}`)
},
/**
* Indicates if two given users are different.
* @param {Object} a A user.
* @param {Object} b A user.
*/
isDifferentUser(a, b) {
const fields = ['login', 'name', 'description', 'type', 'url', 'company', 'location', 'followers', 'following', 'sources', 'forked', 'collaborations']
return (!a || !b) || fields.some((field) => a[field] !== b[field])
},
/**
* Create a user change based on the original and the changed user.
* @param {Object} original The original user.
* @param {Object} changed The changed user.
* @return {Promise}
*/
async createUserChange(original, changed) {
const change = pick(changed, ['login', 'name', 'description', 'type', 'url', 'avatarUrl', 'company', 'location'])
change.followers = changed.followers - original.followers
change.following = changed.following - original.following
change.sources = changed.sources - original.sources
change.forked = changed.forked - original.forked
change.collaborations = changed.collaborations - original.collaborations
change.userId = original.id
change.createdAt = new Date()
return await UserChange.create(change)
}
}