0

I'm building a crowd-funding app and every campaign on my app has a target date. How do I write a function to automatically delete that campaign or shift it elsewhere once the target date is reached?

More specifically, I want to move the campaign from ref('/Fund/{Campaign_ID}') to ref('/Timed_out/{Campaign_ID}').

PS: Please don't suggest manual cron-jobs. It would be impossible for me to keep track of who starts a campaign if the number of users and volume of campaigns grows.

My database in JSON(The important bit):

{
  "Fund" : {
    "-LEEy7uxXEeI4AJuePoB" : {
      "amount_funded" : 1400000,
      "artist_name" : "Sean Roldan and Friends",
      "genre" : "Indian Blues and Jazz",
      "image_url" : "https://firebasestorage.googleapis.com/v0/b/artcall-f8f1a.appspot.com/o/profile_images%2Fthumbs%2Fseanroldan.jpg?alt=media&token=6414d8d6-e8a8-487e-baf3-0d36d017a205",
      "percent_funded" : 0,
      "perf_location" : "Chennai",
      "reward_amt" : 6000,
      "target_amount" : 1750000,
      "target_date" : 1566568176731,
      "time_left" : 1566568176731,
      "total_backers" : 0
    }
  },
  "Fund_Project_Request" : {
    "-LEEy7uxXEeI4AJuePoB" : {
      "as123132" : 2,
      "asd123132" : 2,
      "asdf123asdf21" : 2
    }
  },
  "Loyalty_Points" : {
    "value" : 3000
  },

Firebase Screenshot

Arjun Ram
  • 369
  • 2
  • 18
  • 1
    Since you already know that this takes [setting up a cron job](https://stackoverflow.com/questions/42790735/cloud-functions-for-firebase-trigger-on-time), what is keeping you from doing so? What becomes impossible? Maybe we'll be able to help better if you show what you've already tried. While you're at it, please also replace the screenshot of your data structure with the actual JSON as text. You can get this by clicking the "Export JSON" link in your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Jun 24 '18 at 14:20
  • Thing with cron-jobs is each time a campaign is approved, I'd have to manually set a cron-job right? This would become impractical for me as the number of campaigns grows. I'm asking if there's a programmatic way of triggering and setting cron-jobs. – Arjun Ram Jun 24 '18 at 15:50
  • You can use a single cron-job to trigger at an interval, and then scan for expired data. In fact, that's the most common way to implement time-to-live on Firebase at the moment. – Frank van Puffelen Jun 24 '18 at 15:54
  • That's an amazing solution(even if it's very obvious haha). Thank you so much! – Arjun Ram Jun 24 '18 at 15:55

1 Answers1

1

You can use a single cron-job to trigger at an interval, and then scan for expired data. In fact, that's the most common way to implement time-to-live on Firebase at the moment.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807