public AgreementParticipant[] Handle(ParticipantsByAgreementId query)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            // when agreement id is not known or valid, return user's default affiliation
            if (!query.AgreementId.HasValue || query.AgreementId == 0)
            {
                var noParticipants = new AgreementParticipant[0];

                // return nothing when there is no username
                if (string.IsNullOrWhiteSpace(query.Principal.Identity.Name))
                {
                    return(noParticipants);
                }

                // return nothing when user is not an agreement manager or supervisor
                if (!query.Principal.IsInAnyRole(RoleName.AgreementManagers))
                {
                    return(noParticipants);
                }

                var user = _queryProcessor.Execute(new UserByName(query.Principal.Identity.Name)
                {
                    EagerLoad = new Expression <Func <User, object> >[]
                    {
                        x => x.Tenant,
                    }
                });
                if (user == null)
                {
                    return(noParticipants);
                }

                var participant = new AgreementParticipant
                {
                    IsOwner       = true,
                    Agreement     = new Agreement(),
                    Establishment = user.Tenant,
                };
                return(new[] { participant });
            }

            var participants = _entities.Query <AgreementParticipant>()
                               .EagerLoad(_entities, query.EagerLoad)
                               .ByAgreementId(query.AgreementId.Value)
                               .VisibleTo(query.Principal, _queryProcessor)
                               .OrderBy(query.OrderBy)
            ;

            return(participants.ToArray());
        }
Beispiel #2
0
        internal static string ToJsonAudit(this AgreementParticipant entity)
        {
            var state = JsonConvert.SerializeObject(new
            {
                entity.Id,
                entity.AgreementId,
                entity.EstablishmentId,
                entity.IsOwner,
            });

            return(state);
        }
        public void Handle(CreateParticipant command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var ownedTenantIds = _queryProcessor.Execute(new MyOwnedTenantIds(command.Principal));

            var entity = new AgreementParticipant
            {
                EstablishmentId = command.EstablishmentId,
                IsOwner         = ownedTenantIds.Contains(command.EstablishmentId),
            };

            if (command.Agreement != null)
            {
                entity.Agreement = command.Agreement;
            }
            else
            {
                entity.AgreementId = command.AgreementId;
            }

            // log audit
            var audit = new CommandEvent
            {
                RaisedBy = command.Principal.Identity.Name,
                Name     = command.GetType().FullName,
                Value    = JsonConvert.SerializeObject(new
                {
                    command.AgreementId,
                    command.EstablishmentId,
                }),
                NewState = entity.ToJsonAudit(),
            };

            _entities.Create(audit);

            _entities.Create(entity);
            if (!command.NoCommit)
            {
                _unitOfWork.SaveChanges();
            }
        }