build method

  1. @override
Future<Filter<DataSample>> build()
override

Implementation

@override
Future<Filter<DataSample>> build() async {
  if (_dataOwnerId == null) {
    throw FormatException("Data Owner must be set for patient filter.");
  }
  final String dataOwnerId = _dataOwnerId!;

  final filters = [
    _byIds?.let((v) => DataSampleByIdsFilter(ids: v)),
    _byIdentifiers?.let((v) => DataSampleByHcPartyIdentifiersFilter(dataOwnerId: dataOwnerId, identifiers: v.toList())),
    _byTagCodeDateFilter?.let((v) {
      v.healthcarePartyId = dataOwnerId;
      return v;
    }),
    await _forPatients?.let((v) async {
      var localCrypto = v.item1;
      Set<String> secretForeignKeys = (await Future.wait(v.item2.map((p) {
        var delegations = (p.systemMetaData?.delegations ?? {}).toDelegationMapDto();
        return localCrypto.decryptEncryptionKeys(dataOwnerId, delegations);
      })))
          .toSet()
          .flatten();
      return DataSampleBySecretForeignKeys(healthcarePartyId: dataOwnerId, patientSecretForeignKeys: secretForeignKeys);
    }),
    await _union?.let((v) async => UnionFilter<DataSample>(
        filters: await Future.wait(v.map((f) async => await f.forDataOwner(f.dataOwnerId ?? dataOwnerId).build()).toList()))),
    await _intersection?.let((v) async => IntersectionFilter<DataSample>(
        filters: await Future.wait(v.map((f) async => await f.forDataOwner(f.dataOwnerId ?? dataOwnerId).build()).toList())))
  ].whereType<Filter<DataSample>>().toList();

  if (filters.isEmpty) {
    return DataSampleByHcPartyFilter(hcpId: dataOwnerId);
  } else if (filters.length == 1) {
    return filters[0];
  } else {
    return IntersectionFilter(filters: filters);
  }
}