createOrModifyPatient method

  1. @override
Future<Patient?> createOrModifyPatient(
  1. Patient patient
)
override

Create or update a Patient

When modifying a patient, you must ensure that the rev obtained when getting or creating the patient is present as the rev is used to guarantee that the patient has not been modified by a third party.

Parameters:

Implementation

@override
Future<Patient?> createOrModifyPatient(Patient patient) async {
  final localCrypto = _api.crypto;
  final currentUser = (await _api.baseUserApi.getCurrentUser())
      ?? (throw StateError("There is no user currently logged in. You must call this method from an authenticated MedTechApi"));
  final ccPatient = patientCryptoConfig(localCrypto);

  if (patient.rev != null) {
    if (patient.id == null || !Uuid.isValidUUID(fromString: patient.id!)) {
      throw ArgumentError("The id of the Patient to modify should be a valid UUID");
    }
    final modifiedPatientDto =
        await base_api.PatientApiCrypto(_api.basePatientApi).modifyPatient(currentUser, PatientMapper(patient).toPatientDto(), ccPatient);
    return modifiedPatientDto != null
        ? PatientDtoMapper(modifiedPatientDto).toPatient()
        : throw StateError("Could not modify patient ${patient.id} with user ${currentUser.id}");
  }
  final createdPatientDto =
      await base_api.PatientApiCrypto(_api.basePatientApi).createPatient(currentUser, PatientMapper(patient).toPatientDto(), ccPatient);
  return createdPatientDto != null
      ? PatientDtoMapper(createdPatientDto).toPatient()
      : throw StateError("Could not modify patient ${patient.id} with user ${currentUser.id}");
}