-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload-to-firestore.mjs
125 lines (116 loc) · 3.59 KB
/
upload-to-firestore.mjs
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
// Import the necessary modules
import fs from "fs";
import admin from "firebase-admin";
// import serviceAccount
import serviceAccount from "./firebase-admin-private-key.json" assert { type: "json" };
// Initialize the Firebase Admin SDK with the credentials
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
// // Add the data to the "cities" collection
// import data from "./data/CitiesFromSQL.json" assert { type: "json" };
// for (let item of data) {
// let docData = {
// "name": item.name,
// "longitude": Number(item.longitude),
// "latitude": Number(item.latitude),
// }
// admin
// .firestore()
// .collection("citiesNew")
// .doc(docData, )
// }
// // Add the data to the "trips" collection
// import data from "./data/TripsFromSQL.json" assert { type: "json" };
// data.forEach((item) => {
// admin
// .firestore()
// .collection("trips")
// .add({
// id: Number(item.id),
// userId: item.userId,
// podSchedule: Number(item.podSchedule),
// statusId: Number(item.statusId),
// dateCreated: admin.firestore.Timestamp.fromDate(
// new Date(item.dateCreated)
// ),
// dateUpdated: admin.firestore.Timestamp.fromDate(
// new Date(item.dateUpdated)
// ),
// });
// });
// // Add the data to the "users" collection
// data.forEach((item) => {
// admin.firestore().collection("users").add({
// id: item.id,
// email: item.Email,
// password: item.Password,
// firstName: item.FirstName,
// lastName: item.LastName,
// addressLine1: item.AddressLine1,
// addressLine2: item.AddressLine2,
// city: item.City,
// state: item.State,
// zip: item.Zip,
// phone: item.Phone,
// });
// });
// // Add the data to the "users" collection
// data.forEach((item) => {
// admin.firestore().collection("podSchedules").add({
// id: Number(item.id),
// cityFrom: Number(item.CityFrom),
// cityTo: Number(item.CityTo),
// departureWindow: item.DepartureWindow,
// price: Number(item.Price),
// });
// });
// add the data to the "users" collection
import data from "./data/UserWithTrips.json" assert { type: "json" };
import podSchedules from "./data/PodScheduleFromSQL.json" assert { type: "json" };
import cities from "./data/CitiesFromSQL.json" assert { type: "json" };
for (let item of data) {
let docData = {
email: item.email,
password: item.password,
firstName: item.firstName,
lastName: item.lastName,
addressLine1: item.addressLine1,
addressLine2: item.addressLine2,
city: item.city,
state: item.state,
zip: item.zip,
phone: item.phone,
trips: item.trips.map((trip) => ({
tripId: Number(trip.tripId),
podSchedule: getPodSchedule(trip.podSchedule),
statusId: Number(trip.statusId),
dateCreated: admin.firestore.Timestamp.fromDate(
new Date(trip.dateCreated)
),
dateUpdated: admin.firestore.Timestamp.fromDate(
new Date(trip.dateUpdated)
),
})),
};
// set the document
await admin
.firestore()
.collection("users")
.doc(item.userId)
.set(docData);
}
function getPodSchedule(podScheduleId) {
let podSchedule = podSchedules.find(
(podSchedule) => podSchedule.id === podScheduleId
);
// get city names for CityFrom and CityTo from json and set those values
podSchedule.cityFrom = cities.find(
(city) => city.id === podSchedule.cityFrom
).name;
podSchedule.cityTo = cities.find(
(city) => city.id === podSchedule.cityTo
).name;
podSchedule.price = Number(podSchedule.price);
return podSchedule;
}