createOrModifyMedicalDevices method

  1. @override
Future<List<MedicalDevice>?> createOrModifyMedicalDevices(
  1. List<MedicalDevice> medicalDevice
)
override

Create or update a batch of MedicalDevice

When modifying a device, you must ensure that the rev obtained when getting or creating the device is present as the rev is used to guarantee that the device has not been modified by a third party.

Parameters:

Implementation

@override
Future<List<MedicalDevice>?> createOrModifyMedicalDevices(List<MedicalDevice> medicalDevice) async {
  final medicalDevicesToCreate = medicalDevice.where((device) => device.rev == null).toSet();
  final medicalDevicesToUpdate = medicalDevice.toSet().difference(medicalDevicesToCreate);

  if (medicalDevicesToUpdate.any((element) => element.id == null || !Uuid.isValidUUID(fromString: element.id!))) {
    throw FormatException("The id of the device to update should be a valid UUID");
  }

  final deviceToCreate = medicalDevicesToCreate.map((e) => MedicalDeviceMapper(e).toDeviceDto()).toList();
  final deviceToUpdate = medicalDevicesToUpdate.map((e) => MedicalDeviceMapper(e).toDeviceDto()).toList();

  final deviceCreated = await api.baseDeviceApi.createDevices(deviceToCreate);
  final deviceUpdated = await api.baseDeviceApi.updateDevices(deviceToUpdate);
  final deviceIdsProcessed = [...?deviceCreated, ...?deviceUpdated].map((e) => e.id!).toList();

  return (await api.baseDeviceApi.getDevices(base_api.ListOfIdsDto(ids: deviceIdsProcessed)))!.map((e) => DeviceDtoMapper(e).toMedicalDevice()).toList();
}