I need to use cloud function to query data from firestore based on the selected date. But when selecting parameta type I do not see DateTime listed
my cloud function code is like this
const functions = require('firebase-functions');
const admin = require('firebase-admin');
exports.calculateSalesByDate = functions.https.onCall(async (data, context) => {
try {
if (!context.auth.uid) {
return;
}
const shopId = data.shopId;
const selectedDate = data.selectedDate;
const firestore = admin.firestore();
// Query the productRegister collection to get all products and sort by name A-Z.
const productRegisterQuery = firestore.collection('productRegister')
.where('shopId', '==', shopId)
.orderBy('ProductName', 'asc'); // Sort by name A-Z
const productRegisterSnapshot = await productRegisterQuery.get();
const products = [];
// Use Promise.all to parallelize the fetching of shopStockSnapshot
await Promise.all(productRegisterSnapshot.docs.map(async (productDoc) => {
const productData = productDoc.data();
const productId = "productRegister/" + productDoc.id;
const productRef = productDoc.ref;
// Initialize quantities for each product with default values of 0 if null.
let purchasedQuantity = productData.purchasedQuantity || 0;
let grandTotalSales = productData.grandTotalSales || 0;
// Handle null values for blurHash and ProductImage.
const blurHash = productData.blurHash || 'LyPjx{v%~9P8m.WVb]oe?ETHIqv%';
const productImage = productData.ProductImage || 'https://firebasestorage.googleapis.com/v0/b/898798768.appspot.com/o/icons/default.png?';
// Query the "purchasedProducts" subcollection for the current product based on productRef.
const purchasedProductsQuery = firestore.collectionGroup('purchasedProducts')
.where('productRef', '==', productRef)
.where('date', '==', selectedDate)
.get();
const purchasedProductsSnapshot = await purchasedProductsQuery;
// Sum the quantities in purchasedProducts.
purchasedProductsSnapshot.forEach((productPurchasedDoc) => {
const productPurchasedData = productPurchasedDoc.data();
purchasedQuantity += productPurchasedData.quantity;
grandTotalSales += productPurchasedData.totalPrice;
});
// Add the product details along with PurchasePrice,SellingPrice, and availableStockValue to the result array.
products.push({
id: productId,
name: productData.ProductName,
unit: productData.ProductUnit,
productId: productData.ProductId,
blurHash,
productImage,
shopId: productData.shopId,
purchasedQuantity,
grandTotalSales,
});
}));
// Return the products array as the result.
return products;
} catch (error) {
// Handle any errors that occur during execution.
console.error('An error occurred:', error);
throw new functions.https.HttpsError('internal', 'An error occurred.');
}
});