I have an app where I give users 30,000 credits at every monthly subscription or renewal. Now I want to introduce an yearly subscription but instead of giving 360,000 credits at the purchase event of a yearly subscription I want to give users only 30,000 credits at the start of every month from the date of purchase. But I am confused at how should I achieve that.
I am using Firebase cloudfunctions, Firestore and revenuecat sdk. The revenuecat is integrated to firebase and on every purchase event it adds new document in the events
collection. Whenever an new document with type INITIAL_PURCHASE
or RENEWAL
is created it adds 30,000 credits to the users account.
My problem is that on an yearly subscription it only creates new document once and not every month like in monthly subscription so I am only able to add 30,000 credits for yearly subscribers. Here is my cloud function that I use for listening to changes in events collection.
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.updateUserWordsOnNewEvent = functions.firestore
.document("events/{eventId}")
.onCreate((snapshot) => {
const db = admin.firestore();
const eventData = snapshot.data();
console.log(eventData);
if (eventData.type == "RENEWAL" || eventData.type == "INITIAL_PURCHASE") {
const userId = eventData.app_user_id;
return db
.collection("users")
.doc(userId)
.update({"credits": 30000})
.then(() => {
return {success: true};
})
.catch((error) => {
console.error(error);
return {error: "Failed to update data."};
});
}
});