createOrModifyHealthcareElement method

  1. @override
Future<HealthcareElement?> createOrModifyHealthcareElement(
  1. String patientId,
  2. HealthcareElement healthcareElement
)
override

Create a Healthcare Element

Parameters:

Implementation

@override
Future<HealthcareElement?> createOrModifyHealthcareElement(String patientId, HealthcareElement healthcareElement) 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 ccHealthElement = healthElementCryptoConfig(localCrypto);
  final ccPatient = patientCryptoConfig(localCrypto);

  if (healthcareElement.rev != null) {
    if (healthcareElement.id == null || !Uuid.isValidUUID(fromString: healthcareElement.id!)) {
      throw ArgumentError("Error while updating: HealthcareElement id should be provided as an UUID v4 (String)");
    }
    final modifiedHealthElementDto = await api.baseHealthElementApi
        .modifyHealthElement(currentUser, HealthcareElementMapper(healthcareElement).toHealthElementDto(), ccHealthElement);
    return modifiedHealthElementDto != null
        ? HealthElementDtoMapper(modifiedHealthElementDto).toHealthcareElement()
        : (throw StateError("Could not modify healthElement ${healthcareElement.id}"));
  }

  final patient = await api.patientApi.getPatientAndTryDecrypt(patientId) ?? (throw StateError("Error while getting patient with id $patientId"));
  final createdHealthElementDto = await api.baseHealthElementApi.createHealthElementWithPatientInfo(
    currentUser,
    patient.id!,
    (patient.systemMetaData?.delegations ?? {}).toDelegationMapDto(),
    HealthcareElementMapper(healthcareElement).toHealthElementDto(),
    ccHealthElement
  );
  return createdHealthElementDto != null ? HealthElementDtoMapper(createdHealthElementDto).toHealthcareElement() : (throw StateError("Could not create healthElement ${healthcareElement.id}"));;
}