modifyPotentiallyEncryptedPatient method

  1. @override
Future<PotentiallyEncryptedPatient?> modifyPotentiallyEncryptedPatient(
  1. PotentiallyEncryptedPatient modifiedPatient
)
override

Modifies a potentially encrypted patient, ensuring that if the patient could not be decrypted there was no change to data which should be encrypted according to the current api configuration. Similarly to getPatientAndTryDecrypt this method is useful when a patient needs to update is own data before an hcp gave him access to his own encrypted data.

Implementation

@override
Future<PotentiallyEncryptedPatient?> modifyPotentiallyEncryptedPatient(PotentiallyEncryptedPatient modifiedPatient) async {
  final config = patientCryptoConfig(_api.crypto);
  if (!(modifiedPatient is EncryptedPatient) && !(modifiedPatient is Patient)) {
    throw ArgumentError('Unexpected type for patient ${modifiedPatient}');
  }
  final rawJson = modifiedPatient is EncryptedPatient
    ? EncryptedPatientMapper(modifiedPatient).toPatientDto().toJson()
    : PatientMapper(modifiedPatient as Patient).toPatientDto().toJson();
  final asDecrypted = DecryptedPatientDto.fromJson(rawJson);
  if (asDecrypted == null || (modifiedPatient is EncryptedPatient && (await config.marshaller(asDecrypted)).item2 != null)) {
    throw ArgumentError('Impossible to modify non-decryptable patient if new data requires encryption');
  }
  final modified = await _api.basePatientApi.rawModifyPatient(PatientDto.fromJson(rawJson)!);
  if (modified == null) throw StateError("Could not modify patient ${modifiedPatient.id}");
  return await _tryDecrypt(modified);
}