private async void PushDataRelations() { bool completed = true; bool completedRelations = true; List <Guid> clientRelationsIds = new List <Guid>(); List <Guid> clientNoRelationsIds = new List <Guid>(); List <ClientToDeleteDTO> clientIdsDelete = new List <ClientToDeleteDTO>(); int n = 0; #region Initialize InitSend(); var practice = await GetPractice(); if (null == practice) { return; } bool serverOnline = await CheckConnection(); if (!serverOnline) { return; } var practicses = _practiceSetupService.GetAll(); #endregion #region LoadIds _clientReaderService.ResetState(); var allClientsIds = _clientReaderService.LoadClientIds(AppPracticeId); clientNoRelationsIds = allClientsIds; var totalCount = allClientsIds.Count; var clientIdsWithRelations = _clientReaderService.LoadClientIdsWithRelations(AppPracticeId); clientRelationsIds = clientIdsWithRelations.Select(x => x.IndexId).ToList(); if (clientRelationsIds.Any()) { _relsIds.AddRange(clientIdsWithRelations.Select(x => x.IndexId)); _relsIds.AddRange(clientIdsWithRelations.SelectMany(x => x.SecondaryIds)); } if (_relsIds.Any()) { clientNoRelationsIds = allClientsIds.Where(x => !_relsIds.Contains(x)).ToList(); } #endregion #region CLIENTSONLY foreach (var id in clientNoRelationsIds) { var clientToDeleteDto = new ClientToDeleteDTO(); n++; #region LoadClients Client client = null; try { client = _clientReaderService.LoadClient(id); } catch (Exception e) { } #endregion #region SendClients if (null != client) { var clientInfo = new SyncClientDTO(client); clientInfo.PracticeCode = GetCode(clientInfo.PracticeId, practicses); CurrentStatus = showPerc("Clients", n, totalCount); bool status = false; bool isCompleted = client.CanBeSynced(); try { if (isCompleted) { status = await _clientSyncService.AttempSendClients(Address, clientInfo); } else { } } catch (Exception e) { } if (status) { clientToDeleteDto = new ClientToDeleteDTO(client.Id, client.PersonId); } else { completed = false; } } #endregion #region DeleteClients if (!clientToDeleteDto.NotSent) { List <Encounter> encounters = new List <Encounter>(); try { encounters = _clientReaderService.LoadEncounters(id); } catch (Exception e) { } if (null != encounters && encounters.Count > 0) { var syncEncounters = SyncClientEncounterDTO.Create(encounters, client); bool status = false; try { status = await _clientSyncService.AttempSendClientEncounters(Address, syncEncounters); } catch (Exception e) { } if (status) { foreach (var encounter in encounters) { clientToDeleteDto.AddEnounter(new EnconterToDeleteDTO(encounter.Id, encounter.EncounterType)); } } else { completed = false; } } clientIdsDelete.Add(clientToDeleteDto); foreach (var toDeleteDto in clientIdsDelete) { //TODO: ALLOW DELETE [] if (deleteOnPush) { try { _clientReaderService.Purge(toDeleteDto); } catch (Exception e) { } } } } #endregion } #endregion #region CLIENTRELATIONS foreach (var id in clientRelationsIds) { var clientToDeleteDto = new ClientToDeleteDTO(); n++; Client client = null; List <Client> clientContacts = new List <Client>(); int contactCount = 0; #region LoadClientWithRelations try { client = _clientReaderService.LoadClient(id); var contact = clientIdsWithRelations.FirstOrDefault(x => x.IndexId == id); contactCount = contact.SecondaryIds.Count; if (null != contact && client.CanBeSynced()) { foreach (var secondaryId in contact.SecondaryIds) { var clientContact = _clientReaderService.LoadClientContact(secondaryId); if (null != clientContact) { clientContacts.Add(clientContact); } } } } catch (Exception e) { } #endregion #region SendClientWithRelations if (null != client) { var clientInfo = new SyncClientDTO(client); clientInfo.PracticeCode = GetCode(clientInfo.PracticeId, practicses); CurrentStatus = showPerc("Clients", n, totalCount); bool status = false; try { if (client.CanBeSynced()) { var contactsCannotBeSynced = clientContacts.Any() && clientContacts.Any(x => !x.CanBeSynced()); if (!contactsCannotBeSynced) { status = await _clientSyncService.AttempSendClients(Address, clientInfo); foreach (var clientContact in clientContacts) { var clientConactInfo = new SyncClientDTO(clientContact); clientConactInfo.PracticeCode = GetCode(clientConactInfo.PracticeId, practicses); n++; CurrentStatus = showPerc("Clients", n, totalCount); status = await _clientSyncService.AttempSendClients(Address, clientConactInfo); } } else { CurrentStatus = showPerc("Clients", n + contactCount, totalCount); } } else { CurrentStatus = showPerc("Clients", n + contactCount, totalCount); } } catch (Exception e) { } if (status) { clientToDeleteDto = new ClientToDeleteDTO(client.Id, client.PersonId); } else { completedRelations = false; } } #endregion #region DeleteSentClientWithRelations if (!clientToDeleteDto.NotSent) { List <Encounter> encounters = new List <Encounter>(); List <Encounter> contactEncounters = new List <Encounter>(); try { encounters = _clientReaderService.LoadEncounters(id); var contact = clientIdsWithRelations.FirstOrDefault(x => x.IndexId == id); foreach (var sid in contact.SecondaryIds) { var cencounters = _clientReaderService.LoadEncounters(sid); encounters.AddRange(cencounters); } } catch (Exception e) { } if (null != encounters && encounters.Count > 0) { var syncEncounters = SyncClientEncounterDTO.Create(encounters, client); bool status = false; try { status = await _clientSyncService.AttempSendClientEncounters(Address, syncEncounters); } catch (Exception e) { } if (status) { foreach (var encounter in encounters) { clientToDeleteDto.AddEnounter(new EnconterToDeleteDTO(encounter.Id, encounter.EncounterType)); } } else { completedRelations = false; } } clientIdsDelete.Add(clientToDeleteDto); foreach (var ct in clientContacts) { clientIdsDelete.Add(new ClientToDeleteDTO(ct.Id, ct.PersonId)); } foreach (var toDeleteDto in clientIdsDelete) { //TODO: ALLOW DELETE [] if (deleteOnPush) { try { _clientReaderService.Purge(toDeleteDto); _sentIds.Add(toDeleteDto.Id); } catch (Exception e) { } } } } #endregion } #endregion CurrentStatusTotal = $"Total Clients {totalCount}"; CurrentStatusSent = $"Total Sent {clientIdsDelete.Count}"; CurrentStatusFailed = $"Total Failed {totalCount - clientIdsDelete.Count}"; var messageVerdict = $"Check and resolve any Incomplete records and review server logs"; if (completed && completedRelations) { if (totalCount > clientIdsDelete.Count) { CurrentStatusSummary = messageVerdict; } _dialogService.ShowToast("process completed"); } else { CurrentStatusSummary = messageVerdict; _dialogService.ShowToast("process completed"); } IsBusy = false; }
private async void PushData() { IsBusy = true; CurrentStatus = $"connecting..."; var practice = await _activationService.GetLocal(Address); if (null != practice) { bool completed = true; try { var response = await _activationService.AttempCheckVersion(Address); if (string.IsNullOrWhiteSpace(response)) { _dialogService.Alert($"you are using an old version of LiveHAPI"); IsBusy = false; CurrentStatus = $"send failed! updated LiveHAPI "; return; } int.TryParse(response, out var version); if (version != _hapiVersion) { _dialogService.Alert($"you are using an old version of LiveHAPI"); IsBusy = false; CurrentStatus = $"send failed! update LiveHAPI to v{_hapiVersion}"; return; } var pracs = _practiceSetupService.GetAll(); await _activationService.AttemptEnrollPractice(Address, pracs); } catch (Exception e) { _dialogService.Alert($"connetion Error {e.Message}"); IsBusy = false; return; } var ids = _clientReaderService.LoadClientIds(); var count = ids.Count; int n = 0; var clientIdsDelete = new List <ClientToDeleteDTO>(); var encountersDelete = new List <EnconterToDeleteDTO>(); var practicses = _practiceSetupService.GetAll(); foreach (var id in ids) { var clientToDeleteDto = new ClientToDeleteDTO(); n++; var client = _clientReaderService.LoadClient(id); if (null != client) { var clientInfo = new SyncClientDTO(client); clientInfo.PracticeCode = GetCode(clientInfo.PracticeId, practicses); CurrentStatus = showPerc("Clients", n, count); var status = await _clientSyncService.AttempSendClients(Address, clientInfo); if (status) { clientToDeleteDto = new ClientToDeleteDTO(client.Id, client.PersonId); } else { completed = false; } } if (!clientToDeleteDto.NotSent) { var encounters = _clientReaderService.LoadEncounters(id); if (null != encounters && encounters.Count > 0) { var syncEncounters = SyncClientEncounterDTO.Create(encounters, client); var status = await _clientSyncService.AttempSendClientEncounters(Address, syncEncounters); if (status) { foreach (var encounter in encounters) { clientToDeleteDto.AddEnounter(new EnconterToDeleteDTO(encounter.Id, encounter.EncounterType)); } } else { completed = false; } } var shrs = _clientReaderService.LoadPSmartStores(id); if (null != shrs && shrs.Count > 0) { await _clientSyncService.SendClientShrs(Address, shrs); } clientIdsDelete.Add(clientToDeleteDto); foreach (var toDeleteDto in clientIdsDelete) { //TODO: ALLOW DELETE [] if (deleteOnPush) { _clientReaderService.Purge(toDeleteDto); } } } } // send CurrentStatus = $"done! sent {clientIdsDelete.Count} of {count} "; if (completed) { _dialogService.ShowToast("completed successfully"); } else { CurrentStatus = $"{CurrentStatus} with some Errors, Please check server logs"; _dialogService.Alert("send completed with errors"); } } else { IsBusy = false; CurrentStatus = $"connection failed !!"; _dialogService.Alert("Could not connect to server"); } IsBusy = false; }