-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfirebase.js
More file actions
137 lines (124 loc) · 3.96 KB
/
firebase.js
File metadata and controls
137 lines (124 loc) · 3.96 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import * as firebase from 'firebase';
import '@firebase/firestore';
import { InviteStatus, PayTypes, SessionStatuses } from './constants/Enums';
var config = {
apiKey: "AIzaSyBVKzhG8Mi7-k9bbePV71YPTnhBsWG6-L0",
authDomain: "splitzease-d106e.firebaseapp.com",
databaseURL: "https://splitzease-d106e.firebaseio.com",
projectId: "splitzease-d106e",
storageBucket: "splitzease-d106e.appspot.com",
messagingSenderId: "699584016788"
};
firebase.initializeApp(config);
const db = firebase.firestore();
export const invites = db.collection('invites')
export const checks = db.collection('checks')
export const friends = db.collection('friends')
export const users = db.collection('users')
export const sessions = db.collection('sessions')
export const createSession = (uid, inviteList, restaurant, paytype = PayTypes.unknown) => {
let seshRef = sessions.doc(uid).collection('sessions').doc()
let sessionInviteList = []
inviteList.forEach((guest, index) => {
let iniviteRef = invites.doc(guest).collection('invites').doc()
sessionInviteList.push({
id: iniviteRef.id,
slot: index,
guest: guest,
})
iniviteRef.set({
restaurant,
host: uid,
status: InviteStatus.pending,
slot: index,
sessionId: seshRef.id,
sent: Date.now(),
paytype,
})
.catch(error => console.error(`UID: ${guest}, did not receive and invitation`, error));
});
seshRef.set({
sessionId: seshRef.id,
status: SessionStatuses.started,
inviteList: sessionInviteList,
restaurant,
host: uid,
paytype: paytype,
lastChanged: Date.now(),
}).then(() => {
console.log("Document written with ID: ", seshRef.id);
}).catch(err => console.error("Unable to create a check " + err))
return seshRef.id;
}
const fakeBill = {
cost: 120.00,
sessionId: "",
host: "123",
isBuying: false,
restaurant: "Amer. Test Kitchen",
perPerson: [
{
cost: 20.00,
uid: "user-1",
},
{
cost: 12.00,
uid: "user-3",
},
],
sharedCost: ["user-0", "user-2"]
}
export const sendChecks = (bill) => {
let shareTotal = bill.cost || 0
// send individual checks
bill.perPerson.map(check => {
let checkRef = checks.doc(check.uid).collection('checks').doc()
shareTotal -= check.cost
let chk = {
restaurant: bill.restaurant,
cost: parseFloat(check.cost).toFixed(2),
id: checkRef.id,
ts: Date.now(),
}
checkRef.set(chk).then(() => {
console.log("Document written with ID: ", checkRef.id);
}).catch(err => { console.error("Unable to create a check " + err) })
})
if (bill.isBuying) {
let checkRef = checks.doc(bill.host).collection('checks').doc()
let chk = {
restaurant: bill.restaurant,
cost: parseFloat(bill.cost).toFixed(2),
id: checkRef.id,
ts: Date.now(),
}
checkRef.set(chk).then(() => {
console.log("Document written with ID: ", checkRef.id);
}).catch(err => { console.error("Unable to create a check " + err) })
} else { // send check shares
bill.sharedCost.map(check => {
let checkRef = checks.doc(check).collection('checks').doc()
let chk = {
restaurant: bill.restaurant,
cost: parseFloat(shareTotal / bill.sharedCost.length).toFixed(2),
id: checkRef.id,
ts: Date.now(),
}
checkRef.set(chk).then(() => {
console.log("Document written with ID: ", checkRef.id);
}).catch(err => { console.error("Unable to create a check " + err) })
})
}
}
export const changeSessionState = (uid, sessionId, state) => {
const sessionRef = sessions.doc(uid).collection('sessions').doc(sessionId)
sessionRef.get().then(doc => {
let session = doc.data()
session.status = state
console.log(session)
sessionRef.set(session)
})
}
// changeSessionState('LHtoZbLQcIgjHfnvpaVATU5j2AF3', 'zOm4DnbkFRiR4Gda4uhz', SessionStatuses.done)
// sendChecks(fakeBill)
// createSession("123", ['user-0', 'user-1', 'user-2', 'user-'], "Test Kitchen")