0

I am making a shopping cart App. I have managed to do all the cart and order functions. One problem i am facing now is, I want to restrict users from ordering items from 8PM to 6AM everyday. So I made a document with a field value open/close. When the field value is open, the users can order the items. If the field value is close, user can't order the item. I can manually change that value from the admin App. But in case when the phone is power off or get errors, I can't use the admin app so I can't close the order with time. So i need a cloud function to automatically update that open/close document field value from Firestore Cloud functions. Is that anyway to do like that???? My firestore document is as in the image below....

enter image description here

I want to change that field value automatically with server time.... any ideas... please....

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
John Smith
  • 97
  • 1
  • 8
  • I think this **[answer](https://stackoverflow.com/questions/48474957/servertimestamp-is-allways-null-on-firebase-firestore/48475027)** might help. – Alex Mamo Dec 27 '20 at 15:05

2 Answers2

1

You can schedule a firebase cloud function to change the date every X hours/minutes/seconds

https://firebase.google.com/docs/functions/schedule-functions

exports.scheduledFunction = functions.pubsub.schedule('every 5 minutes').onRun((context) => {
  let doc = firestore().collection('shopopenstatus').doc('shopstatus').get(); 
  doc.update('closed')
});
Stevetro
  • 1,933
  • 3
  • 16
  • 29
  • I have read the documentation. They said you need to have Blaze Plan to use that cloud functions. Google doesn't support purchasing for my country. Is there any other way with free plan? Or any way to purchase Blaze Plan from my country. I am from Myanmar. Is there any other way to do that????? – John Smith Dec 29 '20 at 16:11
0

as @Stevetro mentioned you can schedule a firebase cloud function to change statuts [open <==> close] but instead of running this function many times actually you need to run it twice per day one at 8pm and other at 6am.

exports.scheduledFunctionClose = functions.pubsub.schedule('0 6 * * *')
  .timeZone('America/New_York') 
  .onRun((context) => {
    let doc = firestore().collection('shopopenstatus').doc('shopstatus').get(); 
  doc.update('close')
  return null;
});



exports.scheduledFunctionOpen = functions.pubsub.schedule('0 20 * * *')
  .timeZone('America/New_York') 
  .onRun((context) => {
    let doc = firestore().collection('shopopenstatus').doc('shopstatus').get(); 
  doc.update('open')
  return null;
});

these 2 scheduled functions will run every day one to open and other to close, hope that works with you.

  • I have read the documentation. They said you need to have Blaze Plan to use that cloud functions. Google doesn't support purchasing for my country. Is there any other way with free plan? Or any way to purchase Blaze Plan from my country. I am from Myanmar. – John Smith Dec 29 '20 at 16:11