createContactWithPatientInfo method

Future<DecryptedContactDto?> createContactWithPatientInfo(
  1. UserDto user,
  2. String patientId,
  3. Map<String, Set<DelegationDto>> patientDelegations,
  4. DecryptedContactDto contact,
  5. CryptoConfig<DecryptedContactDto, ContactDto> config,
)

Implementation

Future<DecryptedContactDto?> createContactWithPatientInfo(
    UserDto user,
    String patientId,
    Map<String, Set<DelegationDto>> patientDelegations,
    DecryptedContactDto contact,
    CryptoConfig<DecryptedContactDto, ContactDto> config
) async {
  var delegations = <String>{...(user.autoDelegations["all"] ?? {}), ...(user.autoDelegations["medicalInformation"] ?? {})};
  var encContact = await config.encryptContact(user.dataOwnerId()!, delegations, (await contact.initDelegations(user, config)));
  final secret = (await config.crypto.decryptEncryptionKeys(user.dataOwnerId()!, patientDelegations)).firstOrNull;
  if (secret == null) {
    throw FormatException("Cannot get delegation key for ${patientId} and hcp ${user.dataOwnerId()}");
  }

  final secretForDelegates = await Future.wait((<String>{...delegations, user.dataOwnerId()!})
      .map((String d) async => Tuple2(d, await config.crypto.encryptValueForHcp(user.dataOwnerId()!, d, contact.id, patientId))));
  encContact.cryptedForeignKeys = {
    ...encContact.cryptedForeignKeys,
    ...Map.fromEntries(secretForDelegates
        .map((t) => MapEntry(t.item1, <DelegationDto>{DelegationDto(owner: user.dataOwnerId()!, delegatedTo: t.item1, key: t.item2.item1)})))
  };
  encContact.secretForeignKeys = <String>{secret};

  var newContact = await this.rawCreateContact(encContact);
  return newContact != null ? await config.decryptContact(user.dataOwnerId()!, newContact) : null;
}