forked from jamesnottidge/CUSC-donate-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
120 lines (105 loc) · 2.89 KB
/
index.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { config } from "dotenv";
import express from "express";
import axios from "axios";
import cors from "cors";
import { MongoClient } from "mongodb";
config();
const app = express();
app.use(
cors({
origin: [
"http://localhost:5173",
"http://127.0.0.1:5173",
"http://studentaffairs.cu.edu.ng",
"https://studentaffairs.cu.edu.ng",
],
})
);
app.use(express.json());
let mongoClient = await new MongoClient(process.env.MONGO_URI, {
useUnifiedTopology: true,
useNewUrlParser: true,
}).connect();
let transactions = mongoClient.db("Waffles").collection("Transactions");
const apiUrl = "https://api.flutterwave.com/v3/transactions";
const apiKey = process.env.FLUTIL_API_KEY;
app.get("/api/transactions", async (req, res) => {
let cachedTransactions = await readTransactions();
res.send(cachedTransactions);
});
app.get("/api/update", async (req, res) => {
let pageNumber = 1;
let total_pages = 0;
const allTransactions = [];
while (pageNumber) {
await fetch(
`${apiUrl}?from=2023-04-11&status=successful&page=${pageNumber.toString()}`,
{
headers: {
Authorization: `Bearer ${apiKey}`,
},
}
)
.then((response) => response.json())
.then(async (data) => {
let flutterwaveTransactionCount = data.meta.page_info.total;
let cacheIsCurrent = await checkCache(flutterwaveTransactionCount);
if (!cacheIsCurrent) {
total_pages = Math.ceil(data.meta.page_info.total / 10);
if (data.data.length < 11 && total_pages !== 0) {
allTransactions.push(...data.data);
if (pageNumber === total_pages) {
pageNumber = false;
await insertTransactions(allTransactions);
return res.json({ message: "Updated" });
}
pageNumber++;
} else if (
allTransactions.length < data.meta.page_info.total &&
data.meta.page_info.total_pages == total_pages
) {
allTransactions.push(...data.data);
pageNumber = false;
await insertTransactions(allTransactions);
return res.json({ message: "Updated" });
}
} else {
pageNumber = false;
res.json({ message: "Up to date" });
}
})
.catch(async (error) => {
console.log(error);
let cachedTransactions = await readTransactions();
pageNumber = false;
res.json({ message: "An error occured" });
});
}
});
app.listen(4000, () => {
console.log("server is running");
});
async function insertTransactions(data) {
// make sure is unique
await transactions.updateOne(
{ _id: process.env.DOC_ID },
{ $set: { data } },
{ upsert: true }
);
}
// need to add date field
async function readTransactions() {
let doc = await transactions.findOne({ _id: process.env.DOC_ID });
return doc.data;
}
async function checkCache(count) {
let doc = (await transactions.findOne({ _id: process.env.DOC_ID })) ?? {
data: [],
};
let cacheCount = doc.data.length;
if (cacheCount === count) {
return true;
} else {
return false;
}
}