This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 135
/
db.js
86 lines (67 loc) · 2 KB
/
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
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
import firebase from './firebase';
import getStripe from './stripe';
const firestore = firebase.firestore();
const app = firebase.app();
export function createUser(uid, data) {
return firestore
.collection('users')
.doc(uid)
.set({ uid, ...data }, { merge: true });
}
export function createSite(data) {
const site = firestore.collection('sites').doc();
site.set(data);
return site;
}
export async function deleteSite(id) {
firestore.collection('sites').doc(id).delete();
const snapshot = await firestore
.collection('feedback')
.where('siteId', '==', id)
.get();
const batch = firestore.batch();
snapshot.forEach((doc) => {
batch.delete(doc.ref);
});
return batch.commit();
}
export async function updateSite(id, newValues) {
return firestore.collection('sites').doc(id).update(newValues);
}
export function createFeedback(data) {
return firestore.collection('feedback').add(data);
}
export function deleteFeedback(id) {
return firestore.collection('feedback').doc(id).delete();
}
export function updateFeedback(id, newValues) {
return firestore.collection('feedback').doc(id).update(newValues);
}
export async function createCheckoutSession(uid) {
const checkoutSessionRef = await firestore
.collection('users')
.doc(uid)
.collection('checkout_sessions')
.add({
price: process.env.NEXT_PUBLIC_PRICE_ID,
allow_promotion_codes: true,
success_url: window.location.origin,
cancel_url: window.location.origin
});
checkoutSessionRef.onSnapshot(async (snap) => {
const { sessionId } = snap.data();
if (sessionId) {
const stripe = await getStripe();
stripe.redirectToCheckout({ sessionId });
}
});
}
export async function goToBillingPortal() {
const functionRef = app
.functions('us-central1')
.httpsCallable('ext-firestore-stripe-subscriptions-createPortalLink');
const { data } = await functionRef({
returnUrl: `${window.location.origin}/account`
});
window.location.assign(data.url);
}