addCalMessage function

Future addCalMessage(
  1. Map<String, dynamic> payload
)

Implementation

Future addCalMessage(Map<String, dynamic> payload) async {
  try{
    String id = await getUserID();
    final mobileMoney = await getRecords("calculated_messages");
    String? deviceId = await getPlatformIdentifier();
    print(deviceId);
    payload.addAll({"userID": id, "deviceID": deviceId!});
    if (mobileMoney.items.isNotEmpty) {
      for (var element in mobileMoney.items) {
        if (element.data['deviceID'] == deviceId) {
          pocketBase
              .collection('calculated_messages')
              .update(element.id, body: payload)
              .then((value) => {
            showMsgSuccess("Mobile Money","Encrypted and saved successfully"),
            print(value.data),
          }
          )
              .onError((error, stackTrace) => {
            showMsgFailed("Mobile Money","Failed to encrypt and save"),
          });
        }
      }
    } else {
      pocketBase
          .collection("calculated_messages")
          .create(body: payload)
          .then((value) => showMsgSuccess("Mobile Money","Encrypted and saved"))
          .onError((error, stackTrace) => showMsgFailed("Mobile Money","Failed to encrypt and save"));
    }
  }catch (e){
    showMsgFailed("Mobile Money","Something happened to prevent saving");
  }
}