deleteAttachment method

  1. @override
Future<String?> deleteAttachment(
  1. String dataSampleId,
  2. String documentId
)
override

Delete an attachment of a DataSample

Deletes an attachment, using its corresponding documentId

Parameters:

  • String dataSampleId (required):

  • String documentId (required):

Implementation

@override
Future<String?> deleteAttachment(String dataSampleId, String documentId) 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, [dataSampleId])).firstOrNull ??
          (throw StateError("Could not find batch information of the data sample $dataSampleId"));

  final base_api.DecryptedServiceDto existingService =
      existingContact.services.findFirst((element) => element.id == dataSampleId) ?? (throw StateError("Could not find data sample $dataSampleId"));

  final contactPatientId = (await _getPatientIdOfContact(localCrypto, currentUser, existingContact)) ??
      (throw FormatException("Can not set an attachment to a data sample not linked to a patient"));

  final contentToDelete = existingService.content.entries.findFirst((input) => input.value.documentId == documentId)?.key ??
      (throw FormatException("Id $documentId does not reference any document in the data sample $dataSampleId"));

  existingService.content.removeWhere((key, value) => key != contentToDelete);
  createOrModifyDataSampleFor(contactPatientId, existingService.toDataSample(contactPatientId));

  return documentId;
}