addReview function

Future<String?> addReview(
  1. int rating,
  2. String text,
  3. List<Uint8List> images,
  4. OrderData jobInfo,
)

Implementation

Future<String?> addReview(int rating, String text, List<Uint8List> images, OrderData jobInfo) async{  // addRating
  User? user = FirebaseAuth.instance.currentUser;
  if (user == null)
    return "addRating user == null";
  List<ImageData> _images = [];
  try{
    for (var item in images){
      var ret = await uploadImage("rating", item);
      if (ret != null)
        return ret;
      _images.add(ImageData(localFile: localFile, serverPath: serverPath));
    }
    //
    if (jobInfo.ver4){
      for (var item in jobInfo.products){
        if (item.thisIsArticle){
          dbSetDocumentInTable("article", item.id, {
            "timeModify": DateTime.now().toUtc(),
          });
          dbIncrementCounter("article", item.id, "rating$rating", 1);
        }else{
          dbSetDocumentInTable("service", item.id, {
            "timeModify": DateTime.now().toUtc(),
          });
          dbIncrementCounter("service", item.id, "rating$rating", 1);
        }
      }
    }else{
      await dbSetDocumentInTable("service", jobInfo.serviceId, {
        "timeModify": DateTime.now().toUtc(),
      });
      await dbIncrementCounter("service", jobInfo.serviceId, "rating$rating", 1);
      // await FirebaseFirestore.instance.collection("service").doc(jobInfo.serviceId).set({
      //   "rating$rating": FieldValue.increment(1),
      //   "timeModify": DateTime.now().toUtc(),
      // }, SetOptions(merge:true));
    }
    await dbAddDocumentInTable("reviews", {
      "rating": rating,
      "text": text,
      "images": _images.map((i) => i.toJson()).toList(),
      "user": user.uid,
      "service": jobInfo.serviceId,
      'serviceName': getTextByLocale(jobInfo.service, locale),
      "provider": jobInfo.providerId,
      "userName": userAccountData.userName,
      "userAvatar": userAccountData.userAvatar,
      'time': DateTime.now().toUtc(),
      "timeModify": DateTime.now().toUtc(),
    });
    // await FirebaseFirestore.instance.collection("reviews").add({
    //   "rating": rating,
    //   "text": text,
    //   "images": _images.map((i) => i.toJson()).toList(),
    //   "user": user.uid,
    //   "service": jobInfo.serviceId,
    //   'serviceName': getTextByLocale(jobInfo.service, locale),
    //   "provider": jobInfo.providerId,
    //   "userName": userAccountData.userName,
    //   "userAvatar": userAccountData.userAvatar,
    //   'time': FieldValue.serverTimestamp(),
    //   "timeModify": FieldValue.serverTimestamp(),
    // });
    await dbSetDocumentInTable("booking", jobInfo.id, {
      "rated": true,
      "timeModify": DateTime.now().toUtc(),
    });
    // await FirebaseFirestore.instance.collection("booking").doc(jobInfo.id).set({
    //   "rated": true,
    //   "timeModify": FieldValue.serverTimestamp(),
    // }, SetOptions(merge:true));
    await dbIncrementCounter("settings", "main", "service_reviews", 1);
    // await FirebaseFirestore.instance.collection("settings").doc("main")
    //     .set({"service_reviews": FieldValue.increment(1)}, SetOptions(merge:true));

    await bookingSaveInCache(jobInfo);
  } catch (e) {
    return "addReview " + e.toString();
  }
  jobInfo.rated = true;
  return null;
}