-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
66 lines (56 loc) · 1.96 KB
/
database.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
const mongoose = require("mongoose");
require("dotenv").config();
exports.MongooseConnect = async () => {
await mongoose.connect(process.env.MONGODB_URI);
};
const fcmBatchSchema = new mongoose.Schema({
districtHash: String,
tokens: [[String]],
});
const FcmBatch = mongoose.model("FcmBatch", fcmBatchSchema);
exports.FcmBatch = FcmBatch;
// save district only in small case
exports.saveToken = async (districtHash, token) => {
// Find the latest batch
const consernedDistrict = await FcmBatch.findOne({ districtHash: districtHash });
if (consernedDistrict) {
//get latest tokens array and push new token
if (consernedDistrict.tokens[consernedDistrict.tokens.length - 1].length <=500) {
await consernedDistrict.tokens[consernedDistrict.tokens.length - 1].push(token);
consernedDistrict.save();
return {
indexOfBatch: consernedDistrict.tokens.length - 1,
// remb we are using push and not unshift
indexOfFcmInBatch: consernedDistrict.tokens[consernedDistrict.tokens.length - 1].length - 1
}
} else {
await consernedDistrict.tokens.push([token]);
consernedDistrict.save();
return {
indexOfBatch: consernedDistrict.tokens.length - 1,
indexOfFcmInBatch: 0
}
}
} else {
//create new batch
const newConsernedDistrict = new FcmBatch({
districtHash: districtHash,
tokens: [[token]],
});
// Save the batch to the database
await newConsernedDistrict.save();
return {
indexOfBatch: 0,
indexOfFcmInBatch: 0
}
}
};
// hash1 => Uri **value: 3
// hash2 => Baramulla // Tangmarg // Sopore **value: 1
// hash3 => Bandipora // Punch **value: 2
// hash4 => Teetwal // Karna **value: 4
// hash5 => Islamabad // Tral **value: -2
// hash6 => Pulwama // Kulgam // Harmukh **value: -1
// hash7 => Leh **value: -11
// hash8 => Pahalgam **value: -3
// hash9 => Srinagar // Budgam **value: 0