How to trigger a cloud function when a date stored in a firestore doc property arrives

848 views firebase

I'm building a multisided app where people can sell and buy food. Thats the shortest summarize possible.

When an user make a food order to a restaurant and this order has been marked as dispatched, the app generates a comission on the restaurant profile in firestore.

When the commission is created it triggers a background firebase cloud function that check if the restaurant has an active billing cycle, if not, it creates one like this:

billingCycle: {
  openDate: 'the moment where the fee was created',
  endDate: '4th day after' //

This object its created in the user profile (with correct date) and works ok!

Now I want to emit an invoice when the date of the endDate prop value arrives, here comes the question.

How can I trigger a function when the date of the endDate meet?

I was thinking in moving that decition to the app. That way the app detects when its time to trigger while is using it, but what if the user is not?

How can I trigger the firebase cloud function for emit the invoice independent the interaction of the user with the client app?

I've researched a lot to find an answer but I didn't find anything related so any kind of help is so much appreciated.

*This is the first time Im using FCF.

answered question

1 Answer


You can use an onUpdate trigger on documents that may get updated that way. Write code in the function that checks, on every update, if the dates match in the document. If the dates match, emit the invoice, then update the document again with a flag value that indicates the invoice is emitted.

You will also have to use that new flag to determine not to emit the invoice again on further updates (otherwise your function will send an invoice every time it's updated, when the dates match - this boolean will indicate that it already happened).

posted this

Have an answer?


Please login first before posting an answer.