@wallfar/ocd-studio-core-sdk
Version:
Helper SDK for our OneClick Studio modules
86 lines (85 loc) • 3.57 kB
JavaScript
import { collection, query, where, limit, getDocs, getDoc, doc, orderBy, startAfter } from 'firebase/firestore';
export async function fetchProductFirebase(config, slug) {
const { db, products_collection } = config;
const q = query(collection(db, products_collection), where('slug', '==', slug), limit(1));
const snapshot = await getDocs(q);
if (snapshot.empty)
return null;
const doc = snapshot.docs[0];
return {
id: doc.id,
...doc.data()
};
}
export async function fetchProductsFirebase(config, collectionId, sort, lastFetchedItemId, itemsPerPage) {
const { db, products_collection } = config;
let q = query(collection(db, products_collection), where('status', '==', 'published'), limit(itemsPerPage || 12));
if (collectionId) {
q = query(q, where('collections', 'array-contains', collectionId));
}
if (sort) {
switch (sort) {
case 'created-descending':
q = query(q, orderBy('createdAt', 'desc'));
break;
case 'created-ascending':
q = query(q, orderBy('createdAt', 'asc'));
break;
case 'price-descending':
q = query(q, orderBy('price', 'desc'));
break;
case 'price-ascending':
q = query(q, orderBy('price', 'asc'));
break;
case 'title-descending':
q = query(q, orderBy('title', 'desc'));
break;
case 'title-ascending':
q = query(q, orderBy('title', 'asc'));
break;
default:
break;
}
if (lastFetchedItemId) {
q = query(q, startAfter(lastFetchedItemId));
}
}
const snapshot = await getDocs(q);
if (snapshot.empty)
return null;
const docs = snapshot.docs;
return docs.map(doc => ({
id: doc.id,
...doc.data()
}));
}
export async function fetchCollectionsFirebase(config) {
const { db, collections_collection } = config;
if (!collections_collection)
return null;
if (collections_collection.includes(':')) {
const collectionParts = collections_collection.split(':');
const docSnap = await getDoc(doc(db, collectionParts[0]));
const collections = docSnap.exists() ? docSnap.data()?.[collectionParts[1]] : null;
return collections?.filter(collection => collection.status === 'published') || null;
}
else {
const snapshot = await getDocs(query(collection(db, collections_collection)));
return !snapshot.empty ? snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() })).filter((collection) => collection.status === 'published') : null;
}
}
export async function fetchShippingOptionsFirebase(config) {
const { db, shipping_options_collection } = config;
if (!shipping_options_collection)
return null;
if (shipping_options_collection.includes(':')) {
const collectionParts = shipping_options_collection.split(':');
const docSnap = await getDoc(doc(db, collectionParts[0]));
const options = docSnap.exists() ? docSnap.data()?.[collectionParts[1]] : null;
return options?.filter(opt => opt.status === 'published') || null;
}
else {
const snapshot = await getDocs(query(collection(db, shipping_options_collection)));
return !snapshot.empty ? snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() })).filter((opt) => opt.status === 'published') : null;
}
}