Exemplo n.º 1
0
            public async Task <Result <Exception, Unit> > Handle(Command request, CancellationToken cancellationToken)
            {
                var agentCallback = await _agentRepository.GetByIdAsync(request.CompanyId, request.AgentId);

                if (agentCallback.IsFailure)
                {
                    return(new NotFoundException("Não foi encontrado o agent informado na empresa do usuário"));
                }

                var profilesCallback = _repository.GetAllByProfileIdentifier(request.AgentId, request.ProfileIdentifier);

                if (profilesCallback.IsFailure)
                {
                    return(profilesCallback.Failure);
                }

                var profiles = profilesCallback.Success.ToList();

                foreach (var profile in profiles)
                {
                    profile.Removed = true;
                    await _repository.UpdateAsync(profile);
                }

                if (profiles.Count() < 0)
                {
                    return(Unit.Successful);
                }

                Log log = new Log
                {
                    UserId        = request.UserId,
                    UserCompanyId = request.CompanyId,
                    TargetId      = request.ProfileIdentifier,
                    NewValue      = "true",
                    OldValue      = "false",
                    EntityType    = ETypeEntity.AgentProfiles,
                    TypeLogMethod = ETypeLogMethod.Remove,
                    CreatedIn     = DateTime.Now
                };

                await _logRepository.CreateAsync(log);

                if (request.ProfileIdentifier != Guid.Empty &&
                    agentCallback.Success.ProfileIdentifier.Equals(request.ProfileIdentifier.ToString()))
                {
                    var logAgent = new Log
                    {
                        UserId        = request.UserId,
                        UserCompanyId = request.CompanyId,
                        TargetId      = request.AgentId,
                        NewValue      = $"{Guid.Empty}",
                        OldValue      = $"{agentCallback.Success.ProfileIdentifier}",
                        EntityType    = ETypeEntity.AgentProfiles,
                        TypeLogMethod = ETypeLogMethod.Remove,
                        CreatedIn     = DateTime.Now
                    };

                    agentCallback.Success.ProfileName = "Sem perfil";

                    agentCallback.Success.ProfileIdentifier = Guid.Empty;

                    await _agentRepository.UpdateAsync(agentCallback.Success);

                    await _logRepository.CreateAsync(logAgent);

                    var command = new ItemSolicitationHistoricCreate.Command(request.UserId, request.AgentId, request.CompanyId,
                                                                             profiles.FirstOrDefault().ItemId, SolicitationType.ChangeContainsProfile, "", "", "");

                    var handle = new ItemSolicitationHistoricCreate.Handler(_itemRepository, _agentRepository, _userRepository, _rabbitMQ);

                    await handle.Handle(command, cancellationToken);
                }

                return(Unit.Successful);
            }
Exemplo n.º 2
0
            public async Task <Result <Exception, Guid> > Handle(Command request, CancellationToken cancellationToken)
            {
                Result <Exception, Agent> agentVerify = await _agentRepository.GetByIdAsync(request.CompanyId, request.AgentId);

                if (agentVerify.IsFailure)
                {
                    return(new DuplicateException("Agent não encontrado na empresa do usuário."));
                }

                var userCallback = await _userRepository.GetByIdAsync(request.UserId);

                if (userCallback.IsFailure)
                {
                    return(new NotFoundException("Não foi encontrado um usúario com o id da requisição."));
                }

                var profileCallback = _repository.GetAllByProfileIdentifier(request.AgentId, request.ProfileIdentifier);

                if (profileCallback.IsFailure)
                {
                    return(profileCallback.Failure);
                }

                var items = _itemRepository.GetAll(request.AgentId);

                if (items.IsFailure)
                {
                    return(items.Failure);
                }

                #region Atualizando Profile no Agent
                var profiles = profileCallback.Success.ToList();

                agentVerify.Success.ProfileIdentifier = request.ProfileIdentifier;
                agentVerify.Success.ProfileName       = request.ProfileIdentifier != Guid.Empty ? profiles.FirstOrDefault().Name : "Sem perfil";

                var updatedAgent = await _agentRepository.UpdateAsync(agentVerify.Success);

                if (updatedAgent.IsFailure)
                {
                    return(updatedAgent.Failure);
                }

                var logProfile = new Log
                {
                    UserId        = request.UserId,
                    UserCompanyId = request.CompanyId,
                    TargetId      = agentVerify.Success.Id,
                    NewValue      = request.ProfileIdentifier != Guid.Empty ? $"{profiles.FirstOrDefault().ProfileIdentifier}" : "Sem perfil",
                    OldValue      = $"{agentVerify.Success.ProfileIdentifier}",
                    EntityType    = ETypeEntity.AgentProfiles,
                    TypeLogMethod = ETypeLogMethod.Apply,
                    CreatedIn     = DateTime.Now
                };

                await _logRepository.CreateAsync(logProfile);

                #endregion

                #region Atualizando Items

                Guid itemId = Guid.Empty;

                foreach (var item in items.Success.ToList())
                {
                    itemId = item.Id;

                    string value = null;

                    if (request.ProfileIdentifier != Guid.Empty)
                    {
                        value = profiles.FirstOrDefault(x => x.ItemId == item.Id).Value;
                    }

                    item.Default = value;

                    await _itemRepository.UpdateAsync(item);

                    Log log = new Log
                    {
                        UserId        = request.UserId,
                        UserCompanyId = request.CompanyId,
                        TargetId      = item.Id,
                        NewValue      = value,
                        OldValue      = item.Default,
                        EntityType    = ETypeEntity.AgentProfiles,
                        TypeLogMethod = ETypeLogMethod.Update,
                        CreatedIn     = DateTime.Now
                    };

                    await _logRepository.CreateAsync(log);
                }

                var command = new ItemSolicitationHistoricCreate.Command(request.UserId, request.AgentId, request.CompanyId,
                                                                         itemId, SolicitationType.ChangeContainsProfile, "", "", "");

                var handle = new ItemSolicitationHistoricCreate.Handler(_itemRepository, _agentRepository, _userRepository, _rabbitMQ);

                await handle.Handle(command, cancellationToken);

                #endregion


                return(request.ProfileIdentifier);
            }