-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnections.js
90 lines (78 loc) · 2.14 KB
/
connections.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
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
const exportsObj = {}
const Connection = require('../models').connection
const Op = require('sequelize').Op
exportsObj.getConnectionsForUser = (userId, config = []) => {
const fields = {
followees: 'usrFromId',
followers: 'usrToId'
}
const criteria = []
const compConfig = !!config.length ? config : Object.keys(fields)
compConfig.forEach((configKey) => {
const field = fields[configKey]
if (field) {
const fieldObj = {}
fieldObj[field] = userId
criteria.push(fieldObj)
}
})
const options = {
where: {
[Op.or]: criteria
}
}
return Connection.findAll(options)
.then(connections => connections.map(connection => connection.toJSON()))
.then((connections) => {
return connections.map((connection) => {
const connectionObj = {}
const followeeId = connection[fields.followees]
const followerId = connection[fields.followers]
if (userId === followeeId) {
connectionObj.type = 'followee'
connectionObj.userId = followerId
} else if (userId === followerId) {
connectionObj.type = 'follower'
connectionObj.userId = followeeId
} else return null
return connectionObj
})
})
.then(connections => connections.filter(connection => !!connection))
}
exportsObj.getConnectionById = (conId) => {
return Connection.findOne({ where: { id: conId }})
}
exportsObj.getConnection = (connection) => {
return Connection.findOne({
where: connection
})
}
exportsObj.insertConnection = (connection) => {
return Connection.create(connection)
}
exportsObj.deleteConnection = (conId) => {
return Connection.destroy({ where: { id: conId }})
.then(() => ({ id: conId }))
}
exportsObj.isFollowingMe = (meId, userIdVariant) => {
const options = {
where: {
usrFromId: userIdVariant,
usrToId: meId
}
}
return Connection.findAll(options)
.then(connections => connections.map(connection => connection.usrFromId))
}
exportsObj.followedByMe = (meId, userIdVariant) => {
const options = {
where: {
usrFromId: meId,
usrToId: userIdVariant
}
}
return Connection.findAll(options)
.then(connections => connections.map(connection => connection.usrToId))
}
module.exports = exportsObj