-
Notifications
You must be signed in to change notification settings - Fork 21
/
server.js
99 lines (97 loc) · 3.23 KB
/
server.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
91
92
93
94
95
96
97
98
99
var dotenv = require('dotenv').config(),
express = require('express'),
mongoDB = require('mongodb'),
io = require('socket.io'),
bcrypt = require('bcrypt'),
withAs = (obj, cb) => cb(obj),
app = express()
.use(express.static(
process.env.production ?
'production' : 'development'
)).listen(process.env.PORT || 3000)
mongoDB.MongoClient.connect(
process.env.MONGO,
{useNewUrlParser: true, useUnifiedTopology: true},
(err, client) => err ? console.log(err)
: io(app).on('connection', socket => [
socket.on('datachange', (name, doc) =>
socket.broadcast.emit('datachange', name, doc)
),
socket.on('bcrypt', (text, cb) =>
bcrypt.hash(text, 10, (err, res) => res && cb(res))
),
socket.on('login', (creds, cb) => withAs(
client.db(process.env.dbname),
db => db.collection('users').findOne(
{ // hanya user aktif yang boleh login
username: creds.username,
keaktifan: 1
},
(err, res) => res && bcrypt.compare(
// tes kebenaran password
creds.password, res.password,
// kembalikan doc user yg ditemukan, jgn diubah
(err, result) => cb({res: result && res})
)
)
)),
socket.on('dbCall', (obj, cb) => withAs(
client.db(process.env.dbname).collection(obj.collection),
coll => ({
find: () =>
coll.find(obj.projection, obj.options)
.toArray((err, res) => res && cb(res))
,
findOne: () => coll.findOne(
{_id: obj._id}, (err, res) => res && cb(res)
),
insertOne: () => coll.insertOne(
obj.document, (err, res) => res && cb(res)
),
insertMany: () => coll.insertMany(
obj.documents, (err, res) => res && cb(res)
),
updateOne: () => coll.updateOne(
{_id: obj._id}, {$set: obj.document}, {upsert: true},
(err, res) => res && cb(res)
),
updateMany: () => (obj.documents || []).map(
doc => coll.updateOne(
{_id: doc._id}, {$set: doc}, {upsert: true},
(err, res) => res && cb(res)
)
),
deleteOne: () => coll.deleteOne(
{_id: obj._id}, (err, res) => res && cb(res)
),
getDifference: () => withAs(
{
ids: obj.clientColl.map(i => i._id),
latest: obj.clientColl.reduce(
(acc, inc) => inc.updated > acc ?
inc.updated : acc, 0
)
},
({ids, latest}) => coll.find({$or: [
// cari yg belum tersedia pada client
{_id: {$nin: ids}},
// cari yg lebih baru dari milik client
{updated: {$gt: latest}}
]}).toArray((err, res) => res && cb(res))
)
}[obj.method]())
)),
withAs( // buat user admin pertama jika masih kosong
client.db(process.env.dbname).collection('users'),
users => users.findOne({}, (err, res) =>
!res && users.insertOne({
_id: '050zjiki5pqoi0f2ua0xdm',
username: 'admin', nama: 'admin',
bidang: 5, peranan: 4, keaktifan: 1,
password: '$2b$10$xZ22.NIdyoSP65nPTRUf2uN9.Dd4gkCbChwD5fOCjTm4kSPHylS4a',
updated: 1590416308426 // password: admin
})
)
)
])
)