0

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."};
            });
      }
    });
Harsh Vardhan
  • 559
  • 1
  • 3
  • 12

1 Answers1

0

Here are two possible approaches:

1/ Record the 11 next monthly credit allocations in a collection and run a schedule Cloud Function once a day

When the yearly subscription is created you create 11 document in a specific (private) collection with the following fields: user uid and date of credit allocation (e.g. with YYYYMMDD format).

Then, every day you run a Scheduled Cloud Function which selects all the docs corresponding to the current date and allocate the credits by creating docs in the events collection.

2/ Schedule a Cloud Function to run in the future

You can implement the technique detailed in this article titled "How to schedule a Cloud Function to run in the future". When the yearly subscription is created you schedule 11 tasks to be executed in the future.

HOWEVER this technique is based on Cloud Tasks which has a limitation: You can't schedule a task to execute greater than 30 days in the future. So, actually, you'll not be able to schedule the 11 tasks but only one. And in each task (unless the last one), in addition to the credit allocation, you'll need to reschedule the subsequent monthly task...

A bit cumbersome... I therefore advise to go with the first approach.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121