Firebase Cloud function not working well with runTransactions

I’ve got a little problem with my runTransactions using the Cloud Functions (JavaScript).
My app has a startTime, completeTime and deleteTime. So when the time reaches “completeTime”, it adds +1 to a field in the Firestore as shown in the CODE . This works well.

CODE

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const database = admin.firestore();
const min = "*/5 * * * *";

exports.scheduled = functions.pubsub.schedule(min).onRun(async (context) => {
  const batch = database.batch();
  const {docs} = await database.collection("ADSGHANA").get();
  docs.map((doc) => {
    const {
      AdsId,
      UID,
      TimestampArrival,
      TimestampDeparture,
      PostStatus,
      TimeToDelete,
    } = doc.data();
    const getDriveInfo = database.collection("USERS GHANA").doc(UID);
    const now = admin.firestore.Timestamp.now();
    const starting = now > TimestampDeparture;
    const ending = now > TimestampArrival;
    const deleting = now > TimeToDelete;
    let val = PostStatus;
    if (starting && PostStatus !== "Cancelled") val = "Started";
    if (ending && PostStatus !== "Cancelled") val = "Completed";

    batch.update(
        database.doc(`ADSGHANA/${doc.id}`), {
          PostStatus: val,
        },
    );
    if (starting && PostStatus !== "Cancelled") {
      database.collection("USERS GHANA")
          .doc(UID).collection("USER JOURNEY")
          .doc(UID).collection("AS A DRIVER")
          .doc(AdsId).update({"PostStatus": "Started"});
      database.doc(`ADSGHANA/${doc.id}`)
          .collection("GHANA REQUEST")
          .where("AdStatus", "not-in",
              ["Passenger cancelled", "Rejected", "Driver cancelled"])
          .get()
          .then((value) => {
            value.docs.forEach((element) => {
              database.doc(`ADSGHANA/${doc.id}`)
                  .collection("GHANA REQUEST").doc(element.id)
                  .update({"AdStatus": "Started"});
              database.collection("USERS GHANA")
                  .doc(UID)
                  .collection("USER JOURNEY")
                  .doc(UID)
                  .collection("AS A DRIVER")
                  .doc(AdsId)
                  .collection("USER REQUEST")
                  .where("AdStatus", "not-in",
                      ["Passenger cancelled", "Rejected", "Driver cancelled",
                        "Passenger Cancelled", "Driver Cancelled",
                      ])
                  .get()
                  .then((value) => {
                    value.docs.forEach(
                        (element) => {
                          database.collection("USERS GHANA")
                              .doc(UID)
                              .collection("USER JOURNEY")
                              .doc(UID)
                              .collection("AS A DRIVER")
                              .doc(AdsId)
                              .collection("USER REQUEST")
                              .doc(element.id)
                              .update({"AdStatus": "Started"});
                        });
                  });
              database.collection("USERS GHANA")
                  .doc(element.id)
                  .collection("USER JOURNEY")
                  .doc(element.id)
                  .collection("AS A PASSENGER")
                  .doc(AdsId)
                  .update({"AdStatus": "Started"});
            });
          });
    } null;
    if (ending && PostStatus !== "Cancelled") { //This is the completeTime
      database.runTransaction(async (transaction) => {//This is where it adds +1
        const driverInfo = await transaction.get(getDriveInfo);
        const driverCompleted = driverInfo.get("DrivRideCompleted") + 1;
        transaction.update(getDriveInfo,
            {"DrivRideCompleted": driverCompleted});
      });
      database.collection("USERS GHANA")
          .doc(UID).collection("USER JOURNEY")
          .doc(UID).collection("AS A DRIVER")
          .doc(AdsId).update({"PostStatus": "Completed"});
      database.collection("USERS GHANA")
          .doc(UID)
          .collection("USER JOURNEY")
          .doc(UID)
          .collection("AS A DRIVER")
          .doc(AdsId)
          .collection("USER REQUEST")
          .where("AdStatus", "not-in",
              ["Passenger cancelled",
                "Rejected",
                "Driver cancelled",
                "Driver Cancelled", "Passenger Cancelled"],
          )
          .get()
          .then((value) => {
            value.docs.forEach(
                (element) => {
                  database.collection("USERS GHANA")
                      .doc(UID)
                      .collection("USER JOURNEY")
                      .doc(UID)
                      .collection("AS A DRIVER")
                      .doc(AdsId)
                      .collection("USER REQUEST")
                      .doc(element.id)
                      .update(
                          {"AdStatus": "Completed"},
                      );
                },
            );
          });
      database.collection("ADSGHANA").doc(UID)
          .collection("GHANA REQUEST")
          .where("AdStatus", "not-in",
              ["Passenger cancelled", "Rejected", "Driver cancelled",
                "Passenger Cancelled"])
          .get().then((value) => {
            value.docs.forEach((element) => {
              const getPassInfo = database.collection("USERS GHANA")
                  .doc(element.id);
              database.runTransaction(async (transaction) => {
                const passInfo = await transaction.get(getPassInfo);
                const passCompleted = passInfo.get("PassRideCompleted") + 1;
                transaction.update(getPassInfo,
                    {"PassRideCompleted": passCompleted});
              });
              database.doc(`ADSGHANA/${doc.id}`)
                  .collection("GHANA REQUEST").doc(element.id)
                  .update({"AdStatus": "Completed"});
              database.collection("USERS GHANA").doc(element.id)
                  .collection("USER JOURNEY")
                  .doc(element.id)
                  .collection("AS A PASSENGER")
                  .doc(AdsId)
                  .update({"AdStatus": "Completed"});
            });
          });
    } null;
    if (deleting) { //When it gets to this time it adds another +1 which shouldn't happen
      database.collection("ADSGHANA").doc(UID)
          .collection("GHANA REQUEST")
          .get().then((value) => {
            value.forEach((element) => {
              database.collection("ADSGHANA").doc(UID)
                  .collection("GHANA REQUEST").doc(element.id).delete();
            });
          }).then(() => {
            database.collection("ADSGHANA").doc(UID).delete();
          });
    } null;
  });
  await batch.commit();
});

However, when it gets to “deleteTime”, it also add +1 to the same field making it a total of +2. I don’t understand why it’s happening

The whole app is like this (Using Flutter):

A user has a “UserCollection” called “USERS GHANA” with different fields including “DrivRideCompleted”. The user makes a post to create a new collection called “ADSGHANA”. This collection has “startTime”, “completeTime”, “deleteTime” and “Status.

So using cloud functions, when the time now = admin.firestore.Timestamp.now() is equal to the “startTime”, it updates the field “Status” of the “ADSGHANA” collection to “Started. Works as expected.

When “now” is equal to “completeTime”, it updates the field “Status” of the “ADSGHANA” collection to “Completed” and then updates the field “DrivRideCompleted” of the collection “USERS GHANA” to +1. This works as expected.

When “now” is equal to “deleteTime”, it deletes the collections “ADSGHANA”. This also works well. However, it also adds another +1 to the field “DrivRideCompleted” of collection “USERS GHANA” again making it a total of +2. This is not what I want it to do.

What’s wrong with my code. Please check and correct it for me. I would be grateful if this you did. Thanks.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật