createOrModifyDataSamplesFor method

  1. @override
Future<List<DataSample>?> createOrModifyDataSamplesFor(
  1. String patientId,
  2. List<DataSample> dataSample
)
override

Create or update a batch of DataSample for a patient

All the provided data samples will be created in the same batch. If you are trying to update some data samples, then those ones need to come from the same batch. When modifying a data sample, you can't update the patient of it : For this, you need to delete the faulty data sample and create a new one. When modifying the data sample, you also need to keep the same batchId : It is not possible to change the batch of a data sample.

Parameters:

  • String patientId (required):

  • List<DataSample> dataSample (required):

Implementation

@override
Future<List<DataSample>?> createOrModifyDataSamplesFor(String patientId, List<DataSample> dataSample) async {

  if (dataSample.distinctBy((e) => e.batchId).length > 1) {
    throw FormatException("Only data samples of a same batch (with the same batchId) can be processed together");
  }

  // Arbitrary : 1 service = 1K
  if (_countHierarchyOfDataSamples(0, 0, dataSample) > 1000) {
    throw FormatException("Too many data samples to process. Can't process more than 1000 data samples in the same batch");
  }

  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 contactTuple = await _getContactOfDataSample(localCrypto, currentUser, dataSample.first);
  final contactCached = contactTuple.item1;
  final existingContact = contactTuple.item2;

  final contactPatientId = await existingContact?.let((that) => _getPatientIdOfContact(localCrypto, currentUser, that));

  if (existingContact != null && contactPatientId == null) {
    throw FormatException("Can't update a batch of data samples that is not linked to any patient yet.");
  }

  if (contactPatientId != null && contactPatientId != patientId) {
    throw FormatException("Can't update the patient of a batch of data samples. Delete those samples and create new ones");
  }

  final existingPatient = await api.patientApi.getPatientAndTryDecrypt(patientId);
  final ccContact = contactCryptoConfig(currentUser, localCrypto);
  base_api.DecryptedContactDto? createdOrModifiedContact;

  if (contactCached && existingContact != null) {
    final serviceToModify = dataSample.map((e) => DataSampleMapper(e).toServiceDto(e.batchId));
    existingContact.services = serviceToModify.toSet();
    existingContact.openingDate =
        serviceToModify.where((element) => element.openingDate != null || element.valueDate != null).map((e) => e.openingDate ?? e.valueDate!).min;
    existingContact.closingDate =
        serviceToModify.where((element) => element.closingDate != null || element.valueDate != null).map((e) => e.closingDate ?? e.valueDate!).max;

    createdOrModifiedContact = await api.baseContactApi.modifyContact(currentUser, existingContact, ccContact);
  } else {
    final contactToCreate = _createContactDtoBasedOn(dataSample, existingContact);
    createdOrModifiedContact = await api.baseContactApi.createContactWithPatientInfo(
      currentUser,
      existingPatient!.id!,
      existingPatient.systemMetaData!.delegations.toDelegationMapDto(),
      contactToCreate,
      ccContact
    );
  }

  createdOrModifiedContact!.services.forEach((service) => contactsLinkedToDataSamplesCache.put(service.id, createdOrModifiedContact!));

  return createdOrModifiedContact.services.map((e) => e.toDataSample(createdOrModifiedContact!.id)).toList();
}