-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.js
98 lines (94 loc) · 3.43 KB
/
mongo.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
const MongoClient = require('mongodb').MongoClient;
const ObjectId = require('mongodb').ObjectID;
const atlasURL = process.env.ATLAS_URL;
async function getConnectedClient() {
const client = await new MongoClient(atlasURL, { useNewUrlParser: true });
await client.connect();
return client;
}
module.exports = {
getUserFromSessionKey: async function(sessionKey) {
const client = await new MongoClient(atlasURL, { useNewUrlParser: true });
await client.connect();
console.log('Connected to MongoDB');
const db = client.db('your-final-grade');
const sessionsCollection = db.collection('sessions');
const sessionDocument = await sessionsCollection.findOne({ _id: ObjectId(sessionKey) });
if (!sessionDocument) {
throw new Error('Invalid Session Key');
}
const usersCollection = db.collection('users');
const userDocument = await usersCollection.findOne({ _id: sessionDocument.user });
if (!userDocument) {
throw new Error('User Does Not Exist');
}
await client.close();
return userDocument;
},
createToken: async function() {
const client = await getConnectedClient();
const db = client.db('your-final-grade');
const tokensCollection = db.collection('tokens');
const ret = await tokensCollection.insertOne({ scanned: false, scanned_by: ObjectId() })
await client.close();
return { token: ret.insertedId.toString() };
},
createSession: async function(userId) {
const client = await new MongoClient(atlasURL, { useNewUrlParser: true });
await client.connect();
console.log('Connected to MongoDB');
const db = client.db('your-final-grade');
const sessionsCollection = db.collection('sessions');
const ret = await sessionsCollection.insertOne({ user: ObjectId(userId) })
await client.close();
return { sessionKey: ret.insertedId.toString(), userId: userId.toString() };
},
setUserDataStore: async function(userObjectId, newDataStore) {
const client = await getConnectedClient();
const db = client.db('your-final-grade');
const usersCollection = db.collection('users');
await usersCollection.updateOne(
{
_id: userObjectId,
},
{
'$set': {
dataStore: newDataStore
}
}
)
await client.close();
},
setTokenAsScanned: async function(token) {
const client = await getConnectedClient();
const db = client.db('your-final-grade');
const tokensCollection = db.collection('tokens');
await tokensCollection.updateOne(
{
_id: ObjectId(token),
},
{
'$set': {
scanned: true
}
}
);
await client.close();
},
setTokenAsRedeemed: async function(token, userId) {
const client = await getConnectedClient();
const db = client.db('your-final-grade');
const tokensCollection = db.collection('tokens');
await tokensCollection.updateOne(
{
_id: ObjectId(token),
},
{
'$set': {
scanned_by: userId,
}
}
);
await client.close();
}
}