setNextStep function

Future<String?> setNextStep(
  1. StatusData status,
  2. bool byCustomer,
  3. bool byProvider,
  4. bool byAdmin,
  5. String stringNowStatus,
  6. String stringBookingStatus,
)

Implementation

Future<String?> setNextStep(StatusData status,
    bool byCustomer, bool byProvider, bool byAdmin,
    String stringNowStatus, /// strings.get(186) /// "Now status:",
    String stringBookingStatus, /// strings.get(187) /// "Booking status was changed",
    ) async {
  User? user = FirebaseAuth.instance.currentUser;
  if (user == null)
    return "nextStep user == null";
  StatusData _status = StatusData.clone(status);

  try{
    StatusData _last = StatusData.createEmpty();
    for (var item in appSettings.statuses) {
      if (!item.cancel)
        _last = item;
    }
    currentOrder.history.add(StatusHistory(
        statusId: _status.id,
        time: DateTime.now().toUtc(),
        byCustomer: byCustomer,
        byProvider: byProvider,
        byAdmin: byAdmin,
        activateUserId : user.uid
    ));
    var _finished = currentOrder.finished;
    if (!_status.cancel)
      if (_status.id == _last.id)
        _finished = true;
    currentOrder.status = _status.id;
    currentOrder.finished = _finished;
    await bookingSaveInCache(currentOrder);
    await FirebaseFirestore.instance.collection("booking").doc(currentOrder.id).set({
      "status": currentOrder.status,
      "history": currentOrder.history.map((i) => i.toJson()).toList(),
      "finished" : currentOrder.finished,
      "timeModify": FieldValue.serverTimestamp(),
    }, SetOptions(merge:true));

    // notify
    if (byCustomer){  // customer app
      var _provider = getProviderById(currentOrder.providerId);
      if (_provider != null){
        UserData? _user = await getProviderUserByEmail(_provider.login);
        if (_user != null){
          sendMessage("$stringNowStatus ${getTextByLocale(status.name, locale)}",  /// "Now status:",
              stringBookingStatus,  /// "Booking status was changed",
              _user.id, true, appSettings.cloudKey);
        }
      }
    }
    if (byProvider){
      sendMessage("$stringNowStatus ${getTextByLocale(status.name, locale)}",  /// "Now status:",
          stringBookingStatus,  /// "Booking status was changed",
          currentOrder.customerId, true, appSettings.cloudKey);
    }

  }catch(ex){
    return "setNextStep " + ex.toString();
  }
  return null;
}