Ejemplo n.º 1
0
        public async Task <ScientificWorkWithReviewDto> GetWorkByIdAsync(uint userId, uint scientificWorkId)
        {
            var scientificWork = await _scientificWorkRepository.GetWorkByIdAsync(scientificWorkId);

            if (scientificWork is null)
            {
                return(null);
            }

            var mode = "";

            if (scientificWork.MainAuthor.Id == userId)
            {
                mode = "Author";
            }
            else if (await _reviewRepository.IsReviewerAsync(scientificWorkId, userId))
            {
                mode = "Reviewer";
            }
            else
            {
                mode = "Participant";
            }

            // participant can see this work only when it's approved
            if (scientificWork.Status != StatusEnum.Accepted && mode == "Participant")
            {
                throw new AuthenticationException();
            }

            var scientificWorkDto = new ScientificWorkDto()
            {
                Id             = scientificWork.Id,
                Title          = scientificWork.Name,
                Description    = scientificWork.Description,
                Specialization = scientificWork.Specialization,
                CreationDate   = scientificWork.CreationDate.ToString("g"),
                UpdateDate     = scientificWork.Versions.OrderBy(x => x.Version).Last().DateAdd.ToString("g"),
                Authors        = scientificWork.OtherAuthors,
            };

            string base64Photo = null;

            if (scientificWork.MainAuthor.Photo != null)
            {
                var authorPhoto = await _fileManager.GetBase64FileAsync(scientificWork.MainAuthor.Photo);

                var photoExtension = scientificWork.MainAuthor.Photo.Split(".")[^ 1];
Ejemplo n.º 2
0
        public async Task AddReviewAsync(uint reviewerId, string reviewMsg, IFormFile reviewFile, byte rating, uint scientificWorkId)
        {
            // check if user is a reviewer for given ScientificWork
            var isReviewerOfScientificWork = await _scientificWorkRepository.IsReviewerOfScientificWorkAsync(reviewerId, scientificWorkId);

            if (!isReviewerOfScientificWork)
            {
                return;
            }

            // check if review exists
            var newestVersion = await _scientificWorkFileRepository.GetNewestVersionWithReviewsAsync(scientificWorkId);

            var isReviewAdded = newestVersion.Reviews.Any(x => x.Reviewer.Id == reviewerId);

            if (isReviewAdded)
            {
                return;
            }

            var reviewer = await _userManager.FindByIdAsync(reviewerId.ToString());

            var review = new Review()
            {
                VersionOfScientificWork = newestVersion,
                Reviewer   = reviewer,
                DateReview = DateTime.Now,
                Rating     = rating,
            };

            // add file only when it's not null
            if (!(reviewFile is null))
            {
                review.File = await _fileManager.SaveFileAsync(reviewFile);
            }

            // when reviewer doesn't write message
            // it's just assign null (nothing change)
            review.Comment = reviewMsg;

            await _reviewRepository.AddReviewAsync(review);

            var reviewerCount = _reviewersWorkRepository.GetReviewersCount(scientificWorkId);
            var reviewsCount  = _scientificWorkFileRepository.GetReviewsCountInNewestVersion(scientificWorkId);

            var emailOfAuthor = await _scientificWorkRepository.GetEmailOfAuthorByWorkIdAsync(scientificWorkId);

            // all reviewers added their reviews
            if (reviewsCount == reviewerCount)
            {
                var sumOfRating = await _scientificWorkFileRepository.GetRatingSumFromVersion(newestVersion.Id);

                var ratingAvg = (float)sumOfRating / reviewerCount;

                var work = await _scientificWorkRepository.GetWorkByIdAsync(scientificWorkId);

                if (ratingAvg < 1.5)
                {
                    work.Status          = StatusEnum.Rejected;
                    newestVersion.Rating = 1;

                    await _emailSender.SendToAuthorWorkGotRejectedAsync(emailOfAuthor, scientificWorkId);
                }
                else if (ratingAvg <= 2.5)
                {
                    work.Status          = StatusEnum.Correcting;
                    newestVersion.Rating = 2;

                    await _emailSender.SendNewVersionEnabledEmailAsync(emailOfAuthor, scientificWorkId);
                }
                else
                {
                    work.Status          = StatusEnum.Accepted;
                    newestVersion.Rating = 3;

                    await _emailSender.SendToAuthorWorkGotAcceptedAsync(emailOfAuthor, scientificWorkId);
                }

                await _scientificWorkRepository.ChangeStatusAsync(work);

                await _scientificWorkFileRepository.AddRatingAsync(newestVersion);
            }
            else
            {
                await _emailSender.SendReceiveReviewEmailAsync(emailOfAuthor, scientificWorkId);
            }
        }
        public async Task <ScientificWorkWithReviewDto> GetWorkByIdAsync(uint userId, uint scientificWorkId)
        {
            var scientificWork = await _scientificWorkRepository.GetWorkByIdAsync(scientificWorkId);

            if (scientificWork is null)
            {
                return(null);
            }

            var mode = "";

            if (scientificWork.MainAuthor.Id == userId)
            {
                mode = "Author";
            }
            else if (await _reviewersWorkRepository.IsReviewerAsync(scientificWorkId, userId))
            {
                mode = nameof(UserTypeEnum.Reviewer);
            }
            else
            {
                mode = nameof(UserTypeEnum.Participant);
            }

            // participant can see this work only when it's approved
            if (scientificWork.Status != StatusEnum.Accepted && mode == nameof(UserTypeEnum.Participant))
            {
                throw new AuthenticationException();
            }

            var scientificWorkDto = _mapper.Map <ScientificWorkWithOtherAuthorsDto>(scientificWork);

            var mainAuthor = _mapper.Map <UserDto>(scientificWork.MainAuthor);

            mainAuthor.Photo = UserHelper.GetBase64Photo(_fileManager, mainAuthor.Photo);

            List <VersionDto> versionsDto = null;

            // normal user should not see reviews
            if (mode != nameof(UserTypeEnum.Participant))
            {
                var versions = await _scientificWorkFileRepository.GetVersionsWithReviews(scientificWorkId);

                // reviewer should see only own reviews and answers to these reviews
                if (mode == nameof(UserTypeEnum.Reviewer))
                {
                    versions.ToList().ForEach(x => x.Reviews = x.Reviews.Where(x => x.Reviewer.Id != userId));
                }

                versionsDto = _mapper.Map <List <VersionDto> >(versions);
            }

            return(new ScientificWorkWithReviewDto()
            {
                ScientificWork = scientificWorkDto,
                MainAuthor = mainAuthor,
                Mode = mode,
                Versions = versionsDto,
                Status = scientificWork.Status.ToString()
            });
        }