-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
35 lines (30 loc) · 887 Bytes
/
db.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
const admin = require("firebase-admin");
const { initializeApp, cert } = require("firebase-admin/app");
const { getFirestore } = require("firebase-admin/firestore");
const serviceAccount = JSON.parse(process.env.firebaseCredential);
exports.handler = async function (event, context) {
let app;
if (admin.apps.length === 0) {
app = initializeApp({
credential: cert(serviceAccount),
});
}
const db = getFirestore(app);
try {
const body = JSON.parse(event.body);
const customerEmail = body.email;
const orders = body.orders;
const response = await db.collection("orders").add({
customerEmail,
orders,
});
return {
statusCode: 200,
body: JSON.stringify({
message: "Document added successfully - " + response.id,
}),
};
} catch (error) {
console.log("Error adding document ", error);
}
};