createHealthElementWithPatientInfo method

Future<DecryptedHealthElementDto?> createHealthElementWithPatientInfo(
  1. UserDto user,
  2. String patientId,
  3. Map<String, Set<DelegationDto>> patientDelegations,
  4. DecryptedHealthElementDto healthElementDto,
  5. CryptoConfig<DecryptedHealthElementDto, HealthElementDto> config,
)

Implementation

Future<DecryptedHealthElementDto?> createHealthElementWithPatientInfo(
  UserDto user,
  String patientId,
  Map<String, Set<DelegationDto>> patientDelegations,
  DecryptedHealthElementDto healthElementDto,
  CryptoConfig<DecryptedHealthElementDto,
  HealthElementDto> config
) async {
  final String? key = (await config.crypto.decryptEncryptionKeys(user.dataOwnerId()!, patientDelegations)).firstOrNull;

  if (key == null) {
    throw Exception("No delegation for user");
  }

  var delegations = <String>{...(user.autoDelegations["all"] ?? {}), ...(user.autoDelegations["medicalInformation"] ?? {})};
  final HealthElementDto encryptedHealthElement =
  await config.encryptHealthElement(user.dataOwnerId()!, delegations, await healthElementDto.initDelegations(user, config));
  final Set<String> secretFK = [key].toSet();
  final Set<String> newDelegations = [...delegations, user.dataOwnerId()!].toSet();
  final secretForDelegates = await Future.wait((newDelegations)
      .map((String d) async => Tuple2(d, await config.crypto.encryptValueForHcp(user.dataOwnerId()!, d, healthElementDto.id, patientId))));

  encryptedHealthElement.secretForeignKeys = secretFK;
  encryptedHealthElement.cryptedForeignKeys = {
    ...healthElementDto.cryptedForeignKeys,
    ...Map.fromEntries(secretForDelegates
        .map((t) => MapEntry(t.item1, <DelegationDto>{DelegationDto(owner: user.dataOwnerId()!, delegatedTo: t.item1, key: t.item2.item1)})))
  };

  final HealthElementDto? createdHealthElement = await this.rawCreateHealthElement(encryptedHealthElement);
  return createdHealthElement != null ? await config.decryptHealthElement(user.dataOwnerId()!, createdHealthElement) : null;
}