-
Notifications
You must be signed in to change notification settings - Fork 1
/
firestore.js
67 lines (60 loc) · 1.51 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
'use strict';
const {Firestore} = require('@google-cloud/firestore');
const db = new Firestore();
const collection = 'service-requests';
async function update(id, data) {
let ref;
if (id === null) {
ref = db.collection(collection).doc();
} else {
ref = db.collection(collection).doc(id);
}
data.id = ref.id;
data = {...data};
await ref.set(data);
return data;
}
async function create(data) {
return await update(null, data);
}
async function getSnapshot() {
const snapshot = await db
.collection(collection)
.orderBy('timestamp')
.get();
return snapshot;
}
async function getSnapshotForMonth(monthYear) {
let substrs = monthYear.split('-')
let nextMonthInt = parseInt(substrs[1]) + 1
let nextMonth
if (nextMonthInt === 13) {
nextMonth = '01'
substrs[0] = (parseInt(substrs[0]) + 1).toString()
} else {
nextMonth = nextMonthInt.toString()
if (nextMonth.length === 1) {
nextMonth = '0' + nextMonth
}
}
let endDate = substrs[0] + '-' + nextMonth + '-01T00:00:00'
let startD = new Date(monthYear + '-01T00:00:00').getTime()
let endD = new Date(endDate).getTime()
const snapshot = await db
.collection(collection)
.where("timestamp", ">=", startD)
.where("timestamp", "<", endD)
.orderBy('timestamp')
.get();
return snapshot;
}
async function deleteRec(id) {
let ref = db.collection(collection).doc(id);
return await db.recursiveDelete(ref);
}
module.exports = {
create,
getSnapshot,
getSnapshotForMonth,
deleteRec
};