updateUserProfile method Null safety

  1. @protected
Future<bool> updateUserProfile(
  1. User user
)

Updates the patient or supporter's profile data in the database, which is essentially all personal information except for linked data (such as supporters, interests, conditions). INTERNAL USE ONLY

Implementation

@protected
Future<bool> updateUserProfile(User user) async {
  // we can't work with an empty Firebase uid
  if (user.firebaseUid == null) {
    return false;
  }

  final patientGender =
      user.gender != null ? userGenderToGqlFormat(user.gender!) : null;

  final userId = await fetchUserId(user.firebaseUid!);

  if (userId == null) {
    return false;
  }

  final gqlQueryString = '''
    mutation UpdateUser {
      updateUser(
        input: {
          where: { id: "$userId" }
          data: {
            firstName: ${user.firstName?.gqlNullableString()}
            lastName: ${user.lastName?.gqlNullableString()}
            mobilePhone: ${user.mobilePhone?.gqlNullableString()}
            birthday: ${LocalDatePattern.iso.format(user.birthday!).gqlNullableString()}
            gender: ${patientGender?.gqlNullableString()}
            zipCode: ${user.zipCode?.gqlNullableString()}
            avatar: ${user.avatar?.id.gqlNullableString()}
            language: ${user.language?.defaultValue.id.gqlNullableString()}
            country: ${user.country?.defaultValue.id.gqlNullableString()}
            isCrashlyticsAllowed: ${user.isCrashlyticsAllowed}
            isMedicalProfileMiningAllowed: ${user.isMedicalProfileMiningAllowed}
            isOpenForThirdPartyContacts: ${user.isOpenForThirdPartyContacts}
          }
        }
      ) {
        user {
          ${UserGqlEntity.gqlQueryFragment}
        }
      }
    }
  ''';

  try {
    final queryResult = await graphQLClient.mutate<void>(MutationOptions(
      document: gql(gqlQueryString),
      fetchPolicy: FetchPolicy.networkOnly,
    ));

    if (queryResult.hasException) {
      logger?.e(queryResult.exception.toString());
      return false;
    }
  } catch (e, stackTrace) {
    logger?.e('Could not update user profile data', e, stackTrace);
  }

  return true;
}