diff --git a/salesbook.Maui/Core/Services/ManageDataService.cs b/salesbook.Maui/Core/Services/ManageDataService.cs index faa35d8..a9c240e 100644 --- a/salesbook.Maui/Core/Services/ManageDataService.cs +++ b/salesbook.Maui/Core/Services/ManageDataService.cs @@ -90,14 +90,22 @@ public class ManageDataService( public async Task> GetContact(WhereCondContact? whereCond, DateTime? lastSync) { - List? contactList; - List? prospectList; whereCond ??= new WhereCondContact(); + // Ottengo liste locali + var contactList = await localDb.Get(x => + (whereCond.FlagStato != null && x.FlagStato == whereCond.FlagStato) || + (whereCond.PartIva != null && x.PartIva == whereCond.PartIva) || + (whereCond.PartIva == null && whereCond.FlagStato == null) + ); + + var prospectList = await localDb.Get(x => + (whereCond.PartIva != null && x.PartIva == whereCond.PartIva) || + (whereCond.PartIva == null) + ); + if (networkService.ConnectionAvailable) { - var response = new UsersSyncResponseDTO(); - var clienti = await integryApiService.RetrieveAnagClie( new CRMAnagRequestDTO { @@ -109,10 +117,6 @@ public class ManageDataService( } ); - response.AnagClie = clienti.AnagClie; - response.VtbDest = clienti.VtbDest; - response.VtbCliePersRif = clienti.VtbCliePersRif; - var prospect = await integryApiService.RetrieveProspect( new CRMProspectRequestDTO { @@ -122,38 +126,62 @@ public class ManageDataService( FilterDate = lastSync } ); + + _ = UpdateDbUsers(new UsersSyncResponseDTO + { + AnagClie = clienti.AnagClie, + VtbDest = clienti.VtbDest, + VtbCliePersRif = clienti.VtbCliePersRif, + PtbPros = prospect.PtbPros, + PtbProsRif = prospect.PtbProsRif + }); - response.PtbPros = prospect.PtbPros; - response.PtbProsRif = prospect.PtbProsRif; + if (lastSync != null) + { + contactList = MergeLists( + contactList, + clienti.AnagClie, + x => x.CodAnag + ); - _ = UpdateDbUsers(response); - - contactList = clienti.AnagClie; - prospectList = prospect.PtbPros; - } - else - { - contactList = await localDb.Get(x => - (whereCond.FlagStato != null && x.FlagStato.Equals(whereCond.FlagStato)) || - (whereCond.PartIva != null && x.PartIva.Equals(whereCond.PartIva)) || - (whereCond.PartIva == null && whereCond.FlagStato == null) - ); - prospectList = await localDb.Get(x => - (whereCond.PartIva != null && x.PartIva.Equals(whereCond.PartIva)) || - (whereCond.PartIva == null) - ); + prospectList = MergeLists( + prospectList, + prospect.PtbPros, + x => x.CodPpro + ); + } + else + { + contactList = clienti.AnagClie; + prospectList = prospect.PtbPros; + } } // Mappa i contatti var contactMapper = mapper.Map>(contactList); - // Mappa i prospects + // Mappa i prospect var prospectMapper = mapper.Map>(prospectList); contactMapper.AddRange(prospectMapper); return contactMapper; } + + private static List? MergeLists(List? localList, List? apiList, Func keySelector) + { + if (localList == null || apiList == null) return null; + + var dictionary = localList.ToDictionary(keySelector); + + foreach (var apiItem in apiList) + { + var key = keySelector(apiItem); + dictionary[key] = apiItem; + } + + return dictionary.Values.ToList(); + } public async Task GetSpecificContact(string codAnag, bool isContact) { @@ -240,7 +268,7 @@ public class ManageDataService( .Distinct().ToList(); IDictionary? distinctUser = null; - + if (userListState.AllUsers != null) { distinctUser = userListState.AllUsers @@ -251,7 +279,8 @@ public class ManageDataService( var returnDto = activities .Select(activity => { - if (activity.CodJcom is "0000" && userSession.ProfileDb != null && userSession.ProfileDb.Equals("smetar", StringComparison.OrdinalIgnoreCase)) + if (activity.CodJcom is "0000" && userSession.ProfileDb != null && + userSession.ProfileDb.Equals("smetar", StringComparison.OrdinalIgnoreCase)) { activity.CodJcom = null; } @@ -263,8 +292,7 @@ public class ManageDataService( var minuteBefore = activity.EstimatedTime.Value - activity.AlarmTime.Value; dto.MinuteBefore = (int)Math.Abs(minuteBefore.TotalMinutes); - dto.NotificationDate = dto.MinuteBefore == 0 ? - activity.EstimatedTime : activity.AlarmTime; + dto.NotificationDate = dto.MinuteBefore == 0 ? activity.EstimatedTime : activity.AlarmTime; } if (activity.CodJcom != null) @@ -281,7 +309,7 @@ public class ManageDataService( string? ragSoc; if (distinctUser != null && (distinctUser.TryGetValue(activity.CodAnag, out ragSoc) || - distinctUser.TryGetValue(activity.CodAnag, out ragSoc))) + distinctUser.TryGetValue(activity.CodAnag, out ragSoc))) { dto.Cliente = ragSoc; } @@ -343,7 +371,7 @@ public class ManageDataService( public async Task DeleteProspect(string codPpro) { var persRifList = await GetTable(x => x.CodPpro!.Equals(codPpro)); - + if (!persRifList.IsNullOrEmpty()) { foreach (var persRif in persRifList) diff --git a/salesbook.Maui/Core/System/GenericSystemService.cs b/salesbook.Maui/Core/System/GenericSystemService.cs new file mode 100644 index 0000000..022f53d --- /dev/null +++ b/salesbook.Maui/Core/System/GenericSystemService.cs @@ -0,0 +1,8 @@ +using salesbook.Shared.Core.Interface.System; + +namespace salesbook.Maui.Core.System; + +public class GenericSystemService : IGenericSystemService +{ + public string GetCurrentAppVersion() => AppInfo.VersionString; +} \ No newline at end of file diff --git a/salesbook.Maui/MauiProgram.cs b/salesbook.Maui/MauiProgram.cs index e1bf355..7430936 100644 --- a/salesbook.Maui/MauiProgram.cs +++ b/salesbook.Maui/MauiProgram.cs @@ -7,6 +7,7 @@ using MudBlazor.Services; using MudExtensions.Services; using salesbook.Maui.Core.RestClient.IntegryApi; using salesbook.Maui.Core.Services; +using salesbook.Maui.Core.System; using salesbook.Maui.Core.System.Network; using salesbook.Maui.Core.System.Notification; using salesbook.Maui.Core.System.Notification.Push; @@ -16,6 +17,7 @@ using salesbook.Shared.Core.Dto.PageState; using salesbook.Shared.Core.Helpers; using salesbook.Shared.Core.Interface; using salesbook.Shared.Core.Interface.IntegryApi; +using salesbook.Shared.Core.Interface.System; using salesbook.Shared.Core.Interface.System.Network; using salesbook.Shared.Core.Interface.System.Notification; using salesbook.Shared.Core.Messages.Activity.Copy; @@ -102,6 +104,7 @@ namespace salesbook.Maui builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); + builder.Services.AddSingleton(); builder.Services.AddSingleton(); _ = typeof(System.Runtime.InteropServices.SafeHandle); diff --git a/salesbook.Maui/salesbook.Maui.csproj b/salesbook.Maui/salesbook.Maui.csproj index cd056de..aeadbe4 100644 --- a/salesbook.Maui/salesbook.Maui.csproj +++ b/salesbook.Maui/salesbook.Maui.csproj @@ -29,8 +29,8 @@ it.integry.salesbook - 2.1.5 - 23 + 2.2.0 + 24 14.2