Can we delete an attribute of a document in firestore automatically? - firebase

I am working on an android project using java where we have a subscription for pro users. Right now I am using firestore as my database, is there any way that I can automatically change the pro attribute in user from true to false in a specific time automatically as the subscription time end? In my user collection's document, there are attributes is_a_pro and pro_availability_time which is in json (YYYY-MM-DD). Thank you

There is nothing built into Firestore to automatically change documents at a given time.
The most common solution is to schedule a Cloud Function, that runs periodically, and then check which subscriptions have expired.
An alternative to running them periodically is to schedule a Cloud Task for when each subscription expires. For an example of how to do that, see Doug's blog post How to schedule a Cloud Function to run in the future with Cloud Tasks.

Related

how to schedule a Firebase Cloud function to run in 5 minutes on a specific firestore document upon creation

I would like to make a call to a payment provider's verification API to check the status of the transaction.
The problem.
The payment provider sends a webhook with the status of the transaction but sometimes this process fails. I typically update the status of the transaction in the firestore document upon receipt of the webhook but sometimes this fails leaving me with incoherent statuses in both the payment gateway's console and the firestore document.
Proposed solution.
Upon the creation of a document in firestore, trigger pubsub with a schedule that will run in 5mins.
When the scheduled time arrives, trigger cloud function to run the verification code and update the firestore document.
My request.
Any suggestions and code samples on how to accomplish this will be appreciated.
Thank you.
I tried a cloud function that runs periodically on ALL the documents to confirm the ones that have not been updated yet. this is rather an expensive approach seeing that the amount of documents to read and verify is massive. Also, the payment gateway's verification API only accepts a limited amount of request per second.
For this type of runAfter type functionality, have a look at enqueueing functions in Cloud Tasks in the documentation.
Also see:
Running a scheduler for each user in Firebase Cloud Functions?
run some code 1 hour after a document is updated with cloud function and firestore

Flutter firebase change a field after 1 year

I am building a feature where user subscribe to app by making payment, it sets a value in cloud Firestore like {"subscribed": true}. I want to set the value false after one year. Can you anyone show me an example how to do it ?
There's no way you can directly use Firestore to schedule any task in the future. If you're not running any custom backend service, then you probably have to look into doing it with Cloud Tasks (Cloud Functions to trigger them).
You can create a cloud function to schedule the Task and one function to handle the task (this will include the logic to update the subscribed flag to false again.

How to schedule an update/notification using firebase cloud functions with dynamic frequency?

I have an app, where I have an admin and a customer.
Admins can invite users that are in a line.
Admins can configure maximum waiting time for which a customer should show up after he gets invited.
If he does not show up in specified period of time I need to update the database to set the status of a customer's ticket to "no-appearance" and send notification to a customer.
How can I do that using firebase cloud functions?
It looks like I cannot setTimeout because functions are limited to 540 seconds and it is not applicable in my case.
Looks like Pub-sub functions can only be created with a specific period which you have to determine statically
You can use Google Cloud Task to send a scheduled job to trigger cloud functions. Here an article about that: https://medium.com/swlh/event-driven-scheduled-jobs-using-gcp-cloud-tasks-e712c760ab95
The difference of a Pub/Sub is that you can send a task with a dynamic schedule and that task runs only once.

How to trigger cloud function based on date stored in cloud firestore?

I'm building an app that allows users to schedule social media posts. The date (and time) when the post is supposed to go live is stored (along with the content of the post) in a cloud firestore document. Is there a way to trigger the cloud function based on the date set by the user?
The built-in type to trigger scheduled functions sets the schedule at deploy time. There is no built-in trigger type to trigger at a specific dynamic time. But you can use Cloud Scheduler to dynamically schedule a callback to a Cloud Function.
Doug Stevenson wrote a good blog post about that, so I recommend checking that out: How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL).
There is nowadays also a built-in trigger type for this: enqueue functions with Cloud Tasks

https.onRequest() vs onWrite() firebase using nodejs for Stripe

Currently the open sourced cloud function provided by Firebase/Stripe uses onwrite to Firebase database to trigger the charge to Stripe:
https://github.com/firebase/functions-samples/tree/master/stripe
It seems that it would be more direct and faster to just call Stripe using https trigger instead of writing to Firebase local database which syncs/writes to Firebase server database, which then triggers the cloud function call to Stripe.
Will there be any problems using Https call to trigger Stripe call? What advantages are there for using onwrite trigger instead of https?
Beginner to beginner, this is my understanding:
Let's say you have an app where a you need to
(1) sign users up for a subscription using Stripe, and then
(2) when users use the app, check to make sure their subscription is still valid.
For (1),
you'd only do this once(ish) per user, and you need to tell Stripe "make a new subscription for this user," so it makes sense to use an https.onRequest or https.onCall function.
For (2),
you'd be checking to see whether the user is subscribed many times, and you're not telling Stripe something, you're asking it about stored information: "is this user's subscription still valid?"
If you're asking about stored information, it's a lot faster to check your own database rather than to wait for a response from Stripe. You just need to make sure the information in your database (e.g. Firestore) is up to date with Stripe.
So what you can do is create a Stripe webhook that will trigger an https.onRequest function whenever there is a change to a user's subscription status. Then, your function writes the change to your database.
So, rather than ask Stripe over and over, "is this user subscribed," and wait for a slow response, you just check your own database, knowing that it's kept up to date by the Stripe webhook.

Resources