-
Notifications
You must be signed in to change notification settings - Fork 0
/
Firestore.js
82 lines (67 loc) · 1.69 KB
/
Firestore.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
const database = require('./FirebaseSetup');
const ORDER_COLLECTION = 'orders';
const USERS_COLLECTION = 'users';
function addOrder(
details,
order_date,
orderer,
oderer_discord,
){
database.collection(ORDER_COLLECTION).add({
crafter:"",
current_status:"ordered",
details:details,
order_date:order_date,
orderer:orderer,
orderer_discord:oderer_discord,
}).then((docRef) => {
console.log("Document written with ID: ", docRef.id);
})
.catch((error) => {
console.error("Error adding document: ", error);
});
}
async function register(
discord_id,
discord_name,
createdTimestamp,
registrationID
){
const existsRef = await database.collection(USERS_COLLECTION).where("discord_id", "==",discord_id).get();
console.log(existsRef.empty);
if( existsRef.empty){
database.collection(USERS_COLLECTION).add({
active:true,
discord_id:discord_id,
discord_name:discord_name,
role:"user",
created:createdTimestamp,
auth_id:null,
registrationID:registrationID,
}).then((docRef) => {
console.log("Document written with ID: ", docRef.id);
})
.catch((error) => {
console.error("Error adding document: ", error);
});
return true;
}
else{
return false;
}
}
async function isUnique(registrationID){
const snapshot = database.collection(USERS_COLLECTION);
const query = snapshot
.where("registrationID", "==", registrationID);
const querySnapshot = await query.get();
//const data = querySnapshot.data();
//console.log(querySnapshot);
//console.log(snapshot);
return !querySnapshot.empty;
}
module.exports = {
addOrder,
register,
isUnique
};