updateServices method

Implementation

Future<List<DecryptedContactDto>> updateServices(
    UserDto user, DecryptedPatientDto patient, List<ServiceDto> services, CryptoConfig<DecryptedContactDto, ContactDto> config) async {
  final Uuid uuid = Uuid();
  final serviceIds = services.map((it) => it.id).toSet();
  final contactIds = (await this.rawMatchContactsBy(ContactByServiceIdsFilter(ids: serviceIds)) ?? []).toSet().toList();

  return (await this.createContactsWithPatient(
      user,
      patient,
      (await this.getContacts(user, ListOfIdsDto(ids: contactIds), config)).map((contactDto) {
        final contactServiceIds = contactDto.services.map((service) => service.id).toSet().intersection(serviceIds);
        final subContacts = contactDto.subContacts
            .where((subContact) => subContact.services.any((serviceLink) => contactServiceIds.contains(serviceLink.serviceId)));

        return DecryptedContactDto.fromJson({
          ...toJsonDeep(contactDto),
          'id': uuid.v4(options: {'rng': UuidUtil.cryptoRNG}),
          'subContacts': subContacts,
          'services': services,
          'modified': DateTime.now().millisecondsSinceEpoch
        })!;
      }).toList(),
      config))!;
}