public async Task <IHttpActionResult> GetEntityChanges([FromBody] GetChangesListModel model)
        {
            model = model ?? new GetChangesListModel();
            model.FilterByUserRole = UserManager.GetCurrentUserType();
            var result = await EntityChangeService.GetChangesAsync(model);

            return(Ok(result));
        }
 public Task <ApiCallDataResult <List <ChangeModel> > > GetEntityChanges(GetChangesListModel model = null)
 {
     return(Api.PostAsync <List <ChangeModel> >("entity-changes", model));
 }
        public async Task <List <ChangeModel> > GetChangesAsync(GetChangesListModel query)
        {
            query = query ?? new GetChangesListModel();
            var getEntityChangesDbQuery = Storage.TrackEntityChanges.AsQueryable();

            if (query.EntityNames.Any())
            {
                getEntityChangesDbQuery = getEntityChangesDbQuery.Where(e => query.EntityNames.Contains(e.EntityTable));
            }
            if (query.UserIds.Any())
            {
                getEntityChangesDbQuery = getEntityChangesDbQuery.Where(e => query.UserIds.Contains(e.ChangedByUserId));
            }
            if (query.TakeHistoryForLastNumberOfDays.HasValue)
            {
                var fromDate = DateTime.UtcNow.AddDays(-query.TakeHistoryForLastNumberOfDays.Value);
                getEntityChangesDbQuery = getEntityChangesDbQuery.Where(e => fromDate <= e.ChangeDateUtc);
            }

            if (query.EntityId.HasValue)
            {
                getEntityChangesDbQuery = getEntityChangesDbQuery.Where(e => e.EntityId == query.EntityId.Value);
            }

            var entityChanges = await getEntityChangesDbQuery

                                .Select(e => new ChangeModel
            {
                Id                    = e.Id,
                ChangeDate            = e.ChangeDateUtc,
                ChangeType            = e.ChangeType,
                PropertyChangesAsJson = e.PropertiesChangesWay1,
                EntityName            = e.EntityTable,
                EntityId              = e.EntityId,
                ChangedByUser         = new UserModel
                {
                    Name     = e.ChangedByUser.Name,
                    Email    = e.ChangedByUser.Email,
                    UserType = e.ChangedByUser.UserType
                }
            })
                                .OrderByDescending(x => x.ChangeDate)
                                .ToListAsync();

            entityChanges.ForEach(changeModel =>
            {
                var config = TrackingEntitiesConfiguration.GetConfigFor(changeModel.EntityName);
                if (config == null)
                {
                    return;
                }

                changeModel.PropertyChanges = JsonConvert.DeserializeObject <List <PropertyChangeDescription> >(changeModel.PropertyChangesAsJson)
                                              ?? new List <PropertyChangeDescription>();
                changeModel.EntityNameForDisplaying = changeModel.EntityName.SplitByCaps();


                foreach (var property in changeModel.PropertyChanges)
                {
                    var propertyConfig = config.PropertyList.FirstOrDefault(x => x.Name == property.PropertyName);
                    if (propertyConfig == null)
                    {
                        continue;
                    }

                    property.IsVisibleForUserRoles     = propertyConfig.IsVisibleForUserRoles;
                    property.PropertyNameForDisplaying = property.PropertyName.SplitByCaps();
                    property.OldValueForDisplaying     = propertyConfig.DisplayingPropertyFunction(property.OldValue);
                    property.NewValueForDisplaying     = propertyConfig.DisplayingPropertyFunction(property.NewValue);
                }

                if (query.FilterByUserRole.HasValue)
                {
                    changeModel.PropertyChanges = changeModel.PropertyChanges
                                                  .Where(x => x.IsVisibleForUserRoles.Contains(query.FilterByUserRole.Value))
                                                  .ToList();
                }
            });

            return(entityChanges);
        }