I have a Cloud Function in my Flutter app with the following code:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const database = admin.firestore();
const min = "* * * * *";
exports.scheduled = functions.pubsub.schedule(min).onRun(async (context) => {
const batch = database.batch();
const {docs} = await database.collection("ADSGHANA").get();
docs.map((doc) => {
const {
TimeToDelete, //this is a Timestamp in a main collection
TimestampArrival, //this is a Timestamp in a main collection
TimestampDeparture, //this is a Timestamp in a main collection
PostStatus, //This is a string in a main collection
} = doc.data();
const now = admin.firestore.Timestamp.now();
const starting = now > TimestampDeparture; //If this time is reached an update in its corresponding subCollection should be performed.
const ending = now > TimestampArrival;
let val = PostStatus;
if (starting) val = "Started";
if (ending) val = "Completed";
batch.set(
database.doc(`ADSGHANA/${doc.id}`), {
Timestamp: TimeToDelete? TimestampArrival : now,
PostStatus: val,
//the subCollection update should be here
}, {
merge: true,
},
);
});
await batch.commit();
});
I want that if const starting = now > TimestampDeparture;
is reached it should update a particular update field in its subcollection as in the flutter code below. Except that I don’t know how to write this in JS.
FirebaseFirestore.instance.collection("collectionName")
.doc(theDocumentID)
.collection("subCollectionName")
.where("AdStatus", whereNotIn: ["Passenger cancelled",
"Rejected"])
.get()
.then((value) =>
value.docs.forEach((element) {
FirebaseFirestore.instance
.collection(collectionName)
.doc(uid)
.collection(subCollectionName)
.doc(element.id)
.update({"AdStatus": "Started"});
}));
Any help please?