Example #1
0
        public void Save()
        {
            var correlationId = Guid.NewGuid();
            var auditDate     = DateTime.UtcNow;

            foreach (var item in _trackedItems)
            {
                var updated = item.Operation == AuditOperation.Delete ? null : _stateService.GetState(item.TrackedEntity);

                var initialState = item.InitialState == null ? null : JsonConvert.SerializeObject(item.InitialState);
                var updatedState = updated == null ? null : JsonConvert.SerializeObject(updated);

                var diff = _diffService.GenerateDiff(item.InitialState, updated);

                var audit = new Audit
                {
                    Id            = Guid.NewGuid(),
                    EntityType    = item.TrackedEntity.GetType().Name,
                    EntityId      = item.TrackedEntity.Id,
                    UserId        = _userId,
                    UserName      = _userName,
                    UserAction    = _userAction.ToString(),
                    AuditDate     = auditDate,
                    InitialState  = initialState,
                    UpdatedState  = updatedState,
                    Diff          = JsonConvert.SerializeObject(diff),
                    CorrelationId = correlationId
                };

                _auditRepository.Add(audit);
            }
        }
        public async Task Handle(EntityStateChangedEvent message, IMessageHandlerContext context)
        {
            var initialState = message.InitialState == null ? null : JsonConvert.DeserializeObject <Dictionary <string, object> >(message.InitialState);
            var updatedState = message.UpdatedState == null ? null : JsonConvert.DeserializeObject <Dictionary <string, object> >(message.UpdatedState);
            var diff         = _diffService.GenerateDiff(initialState, updatedState);

            if (diff.Count == 0)
            {
                return;
            }

            await _mediator.Send(new AddHistoryCommand
            {
                CorrelationId     = message.CorrelationId,
                StateChangeType   = message.StateChangeType,
                EntityId          = message.EntityId,
                InitialState      = message.InitialState,
                UpdatedState      = message.UpdatedState,
                UpdatedOn         = message.UpdatedOn,
                UpdatingUserName  = message.UpdatingUserName,
                UpdatingParty     = message.UpdatingParty,
                UpdatingUserId    = message.UpdatingUserId,
                EmployerAccountId = message.EmployerAccountId,
                ProviderId        = message.ProviderId,
                EntityType        = message.EntityType,
                Diff = JsonConvert.SerializeObject(diff)
            });
        }