-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.js
68 lines (54 loc) · 1.79 KB
/
data.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
import { Firestore } from '@google-cloud/firestore';
import { isDev } from './utils.js';
const config = {
projectId: process.env.FIRESTORE_PROJECT_ID,
};
if (isDev()) {
config.credentials = {
client_email: process.env.FIRESTORE_CLIENT_EMAIL,
private_key: process.env.FIRESTORE_PRIVATE_KEY,
};
}
const db = new Firestore(config);
export async function addProcessedAuctions(cars) {
console.log('::: adding processed auctions :::');
const batch = db.batch();
for (let car of cars) {
const carRef = db.collection('processedAuctions').doc(`${car.auctionId}`);
batch.set(carRef, { auctionId: car.auctionId });
}
return batch.commit();
}
export async function addPotentialCars(cars) {
console.log('::: adding processed potential cars :::');
const batch = db.batch();
for (let car of cars) {
const carRef = db.collection('potentialCars').doc(`${car.auctionId}`);
batch.set(carRef, car);
}
return batch.commit();
}
export async function getProcessedAuction(auctionId) {
console.log(`::: get processed auction ${auctionId} :::`);
const processedAuctionRef = db
.collection('processedAuctions')
.doc(`${auctionId}`);
const doc = await processedAuctionRef.get();
if (doc.exists) {
return doc.data();
} else {
return null;
}
}
export async function getAllProcessedAuctions() {
console.log('::: get all processed auctions :::');
const processedAuctionsRef = db.collection('processedAuctions');
const snapshot = await processedAuctionsRef.get();
return snapshot.docs.map((doc) => doc.data());
}
export async function getAllPotentialCars() {
console.log('::: get all potential cars :::');
const potentialCarsRef = db.collection('potentialCars');
const snapshot = await potentialCarsRef.get();
return snapshot.docs.map((doc) => doc.data());
}