I want to create a functionality to reset the count
field in documents to 0 at the start of every month. I have written a cloud function that fetches the document from Firebase and updates its count
field to 0 at the start of every month. However, it's not getting deployed on Firebase, and it doesn't show the specific error where it's coming from. Please, I need guidance. I'm attaching the code below.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
// To avoid deployment errors, do not call admin.initializeApp() in your code
exports.resetCancelCountAtBeginningOfMonth = functions.https.onCall( (data, context) => { // Write your code below!
if (!context.auth) {
// Throwing an HttpsError so that the client gets the error details.
throw new functions.https.HttpsError('unauthenticated', 'The function must be called while authenticated.'); } // Get the current date and month
const currentDate = new Date();
const currentMonth = currentDate.getMonth(); // Access the Firestore collection where user data is stored
const usersRef = admin.firestore().collection('users'); // Query only partner users
return usersRef.where('type', '==', 'provider').get() .then(snapshot => {
const batch = admin.firestore().batch(); // Iterate over partner users and reset their cancel count
snapshot.forEach(doc => { const userRef = usersRef.doc(doc.id); batch.update(userRef, { partnerCancelCount: 0 }); }); // Commit the batch update
return batch.commit(); }) .then(() => { console.log('Cancel count reset for all partners.'); return { success: true }; // Return success response to the client
}) .catch(error => { console.error('Error resetting cancel count:', error); throw new functions.https.HttpsError('internal', 'An error occurred while resetting cancel count.'); }); // Write your code above! } );