deleteDataSamples method

  1. @override
Future<List<String>?> deleteDataSamples(
  1. List<String> requestBody
)
override

Delete a batch of Data Samples

Deletes the batch of data samples identified by the provided dataSampleIds. The data samples to delete need to be part of the same batch

Parameters:

  • List<String> requestBody (required):

Implementation

@override
Future<List<String>?> deleteDataSamples(List<String> requestBody) 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 base_api.DecryptedContactDto existingContact =
      (await _findContactsForDataSampleIds(currentUser, localCrypto, requestBody)).firstOrNull ??
      (throw StateError("Could not find batch information of the data sample $requestBody"));

  final existingServiceIds = existingContact.services.map((e) => e.id);
  if (requestBody.any((element) => !existingServiceIds.contains(element))) {
    throw StateError("Could not find all data samples in same batch ${existingContact.id}");
  }

  final contactPatient = (await _getPatientOfContact(localCrypto, currentUser, existingContact)) ??
      (throw StateError("Couldn't find patient related to batch of data samples ${existingContact.id}"));
  final servicesToDelete = existingContact.services.where((element) => requestBody.contains(element.id));

  return (
      await api.baseContactApi.deleteServicesWithPatientInfo(
        currentUser,
        contactPatient.id!,
        contactPatient.systemMetaData!.delegations.toDelegationMapDto(),
        servicesToDelete.toList(),
        contactCryptoConfig(currentUser, localCrypto)
      )
  )?.services
      .where((element) => requestBody.contains(element.id))
      .where((element) => element.endOfLife != null)
      .map((e) => e.id)
      .toList();
}