updateUser method

Future<UserModel> updateUser({
  1. required UserModel data,
})

Implementation

Future<UserModel> updateUser({
  required UserModel data,
}) async {
  if (data.uid == null) {
    throw UserDataServiceException(
        code: RdevCode.InvalidArgument, message: 'User uid is required');
  }

  final userRef = _db.collection('Users').doc(data.uid);

  try {
    await _db.runTransaction((tx) async {
      final userSnapshot = await tx.get(userRef);

      if (userSnapshot.exists) {
        final userJson = await data.toJson();
        tx.set(userRef, userJson, SetOptions(merge: true));
      } else {
        throw UserDataServiceException(
            code: RdevCode.NotFound,
            message: 'User with id:${data.uid} was not found');
      }
    });

    final snapshot = await userRef.get();
    final updatedModel = await UserModel.fromDocumentSnapshot(snapshot);
    return updatedModel;
  } catch (err) {
    if (err is UserDataServiceException) {
      rethrow;
    }
    throw UserDataServiceException(message: err.toString());
  }
}