示例#1
0
        private async Task <Either <ActionResult, Unit> > CreateExistingUserContributorInvite(List <Guid> releaseIds,
                                                                                              Guid userId, string email, string publicationTitle)
        {
            // check the user doesn't already have the user release roles
            var existingReleaseRoleReleaseIds = _contentDbContext.UserReleaseRoles
                                                .AsQueryable()
                                                .Where(urr =>
                                                       releaseIds.Contains(urr.ReleaseId) &&
                                                       urr.Role == Contributor &&
                                                       urr.UserId == userId)
                                                .Select(urr => urr.ReleaseId)
                                                .ToList();

            var missingReleaseRoleReleaseIds = releaseIds
                                               .Except(existingReleaseRoleReleaseIds)
                                               .ToList();

            if (!missingReleaseRoleReleaseIds.Any())
            {
                return(ValidationActionResult(UserAlreadyHasReleaseRoles));
            }

            var emailResult = await SendContributorInviteEmail(
                publicationTitle : publicationTitle,
                releaseIds : missingReleaseRoleReleaseIds,
                email : email);

            if (emailResult.IsLeft)
            {
                return(emailResult);
            }

            await _userReleaseRoleRepository.CreateManyIfNotExists(
                userId : userId,
                releaseIds : missingReleaseRoleReleaseIds,
                role : Contributor,
                createdById : _userService.GetUserId()
                );

            await _userReleaseInviteRepository.CreateManyIfNotExists(
                releaseIds : missingReleaseRoleReleaseIds,
                email : email,
                releaseRole : Contributor,
                emailSent : true,
                createdById : _userService.GetUserId(),
                accepted : true);

            return(Unit.Instance);
        }
示例#2
0
        public async Task <Either <ActionResult, Unit> > UpdateReleaseContributors(
            Guid releaseId, List <Guid> userIds)
        {
            return(await _persistenceHelper
                   .CheckEntityExists <Release>(releaseId,
                                                query =>
                                                query.Include(r => r.Publication))
                   .OnSuccessDo(release => _userService
                                .CheckCanUpdateReleaseRole(release.Publication, Contributor))
                   .OnSuccess(async release =>
            {
                var releaseContributorReleaseRoles = await _contentDbContext.UserReleaseRoles
                                                     .Include(releaseRole => releaseRole.User)
                                                     .Where(userReleaseRole =>
                                                            userReleaseRole.ReleaseId == release.Id &&
                                                            userReleaseRole.Role == Contributor)
                                                     .ToListAsync();

                var releaseRolesToBeRemoved = releaseContributorReleaseRoles
                                              .Where(urr => !userIds.Contains(urr.UserId))
                                              .ToList();

                await _userReleaseRoleRepository.RemoveMany(
                    userReleaseRoles: releaseRolesToBeRemoved,
                    deletedById: _userService.GetUserId());

                var usersToBeAdded = userIds
                                     .Where(userId =>
                {
                    var userIdsWithReleaseRole = releaseContributorReleaseRoles
                                                 .Select(urr => urr.UserId)
                                                 .ToList();

                    return !userIdsWithReleaseRole.Contains(userId);
                }).ToList();

                await _userReleaseRoleRepository.CreateManyIfNotExists(
                    userIds: usersToBeAdded,
                    releaseId: release.Id,
                    role: Contributor,
                    createdById: _userService.GetUserId());

                return Unit.Instance;
            }));
        }