Skip to content

Commit

Permalink
feat(common): update firebase version
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-chaz committed Nov 29, 2021
1 parent 63f9dd9 commit cb3c5d1
Show file tree
Hide file tree
Showing 3 changed files with 1,788 additions and 856 deletions.
55 changes: 23 additions & 32 deletions lib/dbs/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
import firebase from 'firebase/app';
import 'firebase/firestore';
import { initializeApp } from 'firebase/app';
import { deleteDoc, doc, getDoc, getFirestore, setDoc, updateDoc } from 'firebase/firestore';
import { SessionProps, UserData } from '../../types';

// Firebase config and initialization
// Prod applications might use config file
const { FIRE_API_KEY, FIRE_DOMAIN, FIRE_PROJECT_ID } = process.env;

const firebaseConfig = {
apiKey: FIRE_API_KEY,
authDomain: FIRE_DOMAIN,
projectId: FIRE_PROJECT_ID,
};

if (!firebase.apps.length) {
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
} else {
firebase.app();
}

const db = firebase.firestore();
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);

// Firestore data management functions

Expand All @@ -28,14 +20,14 @@ export async function setUser({ user }: SessionProps) {
if (!user) return null;

const { email, id, username } = user;
const ref = db.collection('users').doc(String(id));
const ref = doc(db, 'users', String(id));
const data: UserData = { email };

if (username) {
data.username = username;
}

await ref.set(data, { merge: true });
await setDoc(ref, data, { merge: true });
}

export async function setStore(session: SessionProps) {
Expand All @@ -49,10 +41,10 @@ export async function setStore(session: SessionProps) {
if (!accessToken || !scope) return null;

const storeHash = context?.split('/')[1] || '';
const ref = db.collection('store').doc(storeHash);
const ref = doc(db, 'store', storeHash);
const data = { accessToken, adminId: id, scope };

await ref.set(data);
await setDoc(ref, data);
}

// User management for multi-user apps
Expand All @@ -69,24 +61,23 @@ export async function setStoreUser(session: SessionProps) {

const contextString = context ?? sub;
const storeHash = contextString?.split('/')[1] || '';
const collection = db.collection('storeUsers');
const documentId = `${userId}_${storeHash}`; // users can belong to multiple stores
const ref = collection.doc(documentId);
const storeUser = await ref.get();
const ref = doc(db, 'storeUsers', documentId);
const storeUser = await getDoc(ref);

// Set admin (store owner) if installing/ updating the app
// https://developer.bigcommerce.com/api-docs/apps/guide/users
if (accessToken) {
// Create a new admin user if none exists
if (!storeUser?.exists) {
await ref.set({ storeHash, isAdmin: true });
if (!storeUser.exists()) {
await setDoc(ref, { storeHash, isAdmin: true });
} else if (!storeUser.data()?.isAdmin) {
await ref.update({ isAdmin: true });
await updateDoc(ref, { isAdmin: true });
}
} else {
// Create a new user if it doesn't exist
if (!storeUser?.exists) {
await ref.set({ storeHash, isAdmin: owner.id === userId }); // isAdmin true if owner == user
if (!storeUser.exists()) {
await setDoc(ref, { storeHash, isAdmin: owner.id === userId }); // isAdmin true if owner == user
}
}
}
Expand All @@ -95,29 +86,29 @@ export async function deleteUser({ context, user, sub }: SessionProps) {
const contextString = context ?? sub;
const storeHash = contextString?.split('/')[1] || '';
const docId = `${user?.id}_${storeHash}`;
const storeUsersRef = db.collection('storeUsers').doc(docId);
const ref = doc(db, 'storeUsers', docId);

await storeUsersRef.delete();
await deleteDoc(ref);
}

export async function hasStoreUser(storeHash: string, userId: string) {
if (!storeHash || !userId) return false;

const docId = `${userId}_${storeHash}`;
const userDoc = await db.collection('storeUsers').doc(docId).get();
const userDoc = await getDoc(doc(db, 'storeUsers', docId));

return userDoc?.exists;
return userDoc.exists();
}

export async function getStoreToken(storeHash: string) {
if (!storeHash) return null;
const storeDoc = await db.collection('store').doc(storeHash).get();
const storeDoc = await getDoc(doc(db, 'store', storeHash));

return storeDoc?.exists ? storeDoc.data()?.accessToken : null;
return storeDoc.data()?.accessToken ?? null;
}

export async function deleteStore({ store_hash: storeHash }: SessionProps) {
const ref = db.collection('store').doc(storeHash);
const ref = doc(db, 'store', storeHash);

await ref.delete();
await deleteDoc(ref);
}
Loading

0 comments on commit cb3c5d1

Please sign in to comment.