Exemplo n.º 1
0
        public async Task <InviteMemberResult> InviteMember(string groupId, string userId)
        {
            var currentUser = await profileService.GetCurrentUser();

            var group = await database.GroupRepository.FindById(groupId) ??
                        throw new EntityNotFoundException("Group not found");

            if (currentUser.Id == userId)
            {
                throw new NoPermissionsException("You are not allowed to invite yourself to this group");
            }

            if (!InviteMemberPermissionSmartEnum.FromValue((int)group.InviteMemberPermission)
                .ValidatePermission(currentUser.Id, group))
            {
                throw new NoPermissionsException("You are not allowed to invite members in this group");
            }

            if (IsUserGroupMemberSpecification.Create(userId).IsSatisfied(group))
            {
                throw new DuplicateException("This user is currently member of this group");
            }

            var member       = GroupMember.Create(userId, groupId);
            var memberInvite = GroupInvite.Create(userId, groupId, isInvited: true);

            database.GroupMemberRepository.Add(member);
            group.GroupInvites.Add(memberInvite);

            if (await database.Complete())
            {
                await notifier.Push(NotificationMessages.GroupInvitedNotification(group.Name), userId,
                                    NotificationType.GroupInvited);

                return(new InviteMemberResult(member, memberInvite));
            }

            return(null);
        }