Example #1
0
        public async Task AddKudosLog_UserIsNotAdminAndTotalPointsIsNotNull_AddKudosLogInvoked()
        {
            // Arrange
            const int explicitAmount = 123456;
            var       request        = new AddKudosLogViewModel
            {
                TotalPointsPerReceiver = explicitAmount
            };

            var mappedRequest = new AddKudosLogDto();

            _mapper.Map <AddKudosLogViewModel, AddKudosLogDto>(request).Returns(mappedRequest);
            _permissionService.UserHasPermissionAsync(Arg.Any <UserAndOrganizationDto>(), AdministrationPermissions.Kudos)
            .Returns(false);

            // Act
            var response = await _kudosController.AddKudosLog(request);

            var result = await response.ExecuteAsync(CancellationToken.None);

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            await _kudosService.DidNotReceive().AddKudosLogAsync(mappedRequest, explicitAmount);

            await _kudosService.Received(1).AddKudosLogAsync(mappedRequest);
        }
Example #2
0
        private async Task <AddKudosDto> GenerateLogForKudosMinusOperationAsync(AddKudosDto kudos)
        {
            var minusKudosType = await _kudosTypesDbSet.AsNoTracking().FirstAsync(n => n.Type == KudosTypeEnum.Minus);

            var kudosLogForMinusKudos = new AddKudosLogDto
            {
                PointsTypeId   = minusKudosType.Id,
                Comment        = kudos.KudosLog.Comment,
                MultiplyBy     = kudos.KudosLog.MultiplyBy,
                OrganizationId = kudos.KudosLog.OrganizationId,
                UserId         = kudos.KudosLog.UserId
            };

            var minusKudos = new AddKudosDto
            {
                KudosLog              = kudosLogForMinusKudos,
                ReceivingUser         = kudos.SendingUser,
                SendingUser           = kudos.ReceivingUser,
                TotalKudosPointsInLog = kudos.TotalKudosPointsInLog,
                KudosType             = MapKudosTypesToDto(minusKudosType),
                PictureId             = kudos.PictureId
            };

            return(minusKudos);
        }
Example #3
0
        private async Task <IList <AddKudosLogDto> > CreateKudosLogsAsync(Lottery lottery, UserAndOrganizationDto userOrg)
        {
            var kudosTypeId = await _kudosService.GetKudosTypeIdAsync(KudosTypeEnum.Refund);

            var usersToRefund = await _participantService.GetParticipantsCountedAsync(lottery.Id);

            var usersToSendKudos = new List <AddKudosLogDto>();

            foreach (var user in usersToRefund)
            {
                var totalReturn = user.Tickets * lottery.EntryFee;
                var kudosLog    = new AddKudosLogDto
                {
                    ReceivingUserIds = new List <string> {
                        user.UserId
                    },
                    PointsTypeId   = kudosTypeId,
                    MultiplyBy     = totalReturn,
                    Comment        = CreateComment(lottery, totalReturn),
                    UserId         = userOrg.UserId,
                    OrganizationId = userOrg.OrganizationId
                };

                usersToSendKudos.Add(kudosLog);
            }

            return(usersToSendKudos);
        }
Example #4
0
        private async Task AddKudosRequestAsync(AddKudosLogDto kudosLog, decimal?points = null)
        {
            var kudosDto = await MapInitialInfoToDtoAsync(kudosLog, points);

            var receivingUsers = await _usersDbSet
                                 .Where(x => kudosLog.ReceivingUserIds.Contains(x.Id) && x.OrganizationId == kudosLog.OrganizationId)
                                 .ToListAsync();

            var sendingUser = await _usersDbSet
                              .FirstOrDefaultAsync(x => x.Id == kudosDto.SendingUser.Id && x.OrganizationId == kudosLog.OrganizationId);

            _kudosServiceValidator.CheckForEmptyUserList(receivingUsers);

            foreach (var receivingUser in receivingUsers)
            {
                _kudosServiceValidator.ValidateUser(receivingUser);
                kudosDto.ReceivingUser = _mapper.Map <ApplicationUserDto>(receivingUser);
                await ChooseKudosifyTypeAsync(kudosDto);
            }

            await _uow.SaveChangesAsync(false);

            if (kudosDto.KudosType.Type != KudosTypeEnum.Send)
            {
                return;
            }

            foreach (var receivingUser in receivingUsers)
            {
                _asyncRunner.Run <IKudosNotificationService>(async notifier => await notifier.NotifyAboutKudosSentAsync(kudosDto), _uow.ConnectionName);
                await UpdateProfileKudosAsync(receivingUser, kudosLog);
            }

            await UpdateProfileKudosAsync(sendingUser, kudosLog);
        }
Example #5
0
        public async Task AddKudosLogAsync(AddKudosLogDto kudosDto, decimal?points = null)
        {
            if (!await UserHasPermissionAsync(kudosDto))
            {
                throw new ValidationException(ErrorCodes.KudosTypeNotFound);
            }

            await AddKudosRequestAsync(kudosDto, points);
        }
Example #6
0
        private async Task <bool> UserHasPermissionAsync(AddKudosLogDto kudosDto)
        {
            var kudosType = _kudosTypesDbSet.AsNoTracking().FirstOrDefault(p => p.Id == kudosDto.PointsTypeId);

            if (kudosType is null)
            {
                return(false);
            }

            if (kudosType.IsActive || kudosType.Type == KudosTypeEnum.Send)
            {
                return(true);
            }

            return(await HasKudosAdministratorPermissionAsync(kudosDto));
        }
Example #7
0
        private async Task <AddKudosDto> MapInitialInfoToDtoAsync(AddKudosLogDto kudosLog, decimal?overridenPoints = null)
        {
            var sendingUser = await _usersDbSet.FindAsync(kudosLog.UserId);

            _kudosServiceValidator.ValidateUser(sendingUser);

            var kudosType = await _kudosTypesDbSet.AsNoTracking().FirstOrDefaultAsync(p => p.Id == kudosLog.PointsTypeId);

            _kudosServiceValidator.ValidateKudosType(kudosType);

            return(new AddKudosDto
            {
                KudosLog = kudosLog,
                KudosType = MapKudosTypesToDto(kudosType),
                SendingUser = _mapper.Map <ApplicationUserDto>(sendingUser),
                TotalKudosPointsInLog = overridenPoints ?? kudosLog.MultiplyBy * kudosType?.Value ?? 0,
                PictureId = kudosLog.PictureId
            });
        }
Example #8
0
        private async Task AddKudosLogForCheatingAsync(UserAndOrganizationDto userOrg, int entryFee, ApplicationUser applicationUser)
        {
            var kudosLog = new AddKudosLogDto
            {
                ReceivingUserIds = new List <string> {
                    userOrg.UserId
                },
                PointsTypeId   = await _kudosService.GetKudosTypeIdAsync(KudosTypeEnum.Minus),
                MultiplyBy     = entryFee * 2,
                Comment        = "Cheating",
                UserId         = userOrg.UserId,
                OrganizationId = userOrg.OrganizationId
            };

            await _kudosService.AddLotteryKudosLogAsync(kudosLog, userOrg);

            await _uow.SaveChangesAsync(applicationUser.Id);

            await _kudosService.UpdateProfileKudosAsync(applicationUser, userOrg);
        }
Example #9
0
        public async Task AddLotteryKudosLogAsync(AddKudosLogDto kudosLogDto, UserAndOrganizationDto userOrg)
        {
            var kudosDto = await MapInitialInfoToDtoAsync(kudosLogDto);

            var receivingUser = await _usersDbSet
                                .FirstOrDefaultAsync(x => x.Id == kudosLogDto.UserId && x.OrganizationId == kudosLogDto.OrganizationId);

            if (receivingUser == null)
            {
                throw new ValidationException(ErrorCodes.ContentDoesNotExist, "User not found");
            }

            kudosDto.ReceivingUser = _mapper.Map <ApplicationUserDto>(receivingUser);

            InsertKudosLog(kudosDto, KudosStatus.Approved);

            await _uow.SaveChangesAsync(false);

            await UpdateProfileKudosAsync(_mapper.Map <ApplicationUser>(kudosDto.SendingUser), userOrg);
        }
Example #10
0
        public async Task BuyLotteryTicketAsync(BuyLotteryTicketDto lotteryTicketDto, UserAndOrganizationDto userOrg)
        {
            var applicationUser = await _userService.GetApplicationUserAsync(userOrg.UserId);

            var lotteryDetails = await GetLotteryDetailsAsync(lotteryTicketDto.LotteryId, userOrg);

            if (lotteryDetails is null || applicationUser is null)
            {
                return;
            }

            if (lotteryTicketDto.Tickets <= 0)
            {
                await AddKudosLogForCheatingAsync(userOrg, lotteryDetails.EntryFee, applicationUser);

                throw new LotteryException("Thanks for trying - you were charged double Kudos for this without getting a ticket.");
            }

            if (applicationUser.RemainingKudos < lotteryDetails.EntryFee * lotteryTicketDto.Tickets)
            {
                throw new LotteryException("User does not have enough kudos for the purchase.");
            }

            if (DateTime.UtcNow > lotteryDetails.EndDate)
            {
                throw new LotteryException("Lottery has already ended.");
            }

            var kudosLog = new AddKudosLogDto
            {
                ReceivingUserIds = new List <string> {
                    userOrg.UserId
                },
                PointsTypeId   = await _kudosService.GetKudosTypeIdAsync(KudosTypeEnum.Minus),
                MultiplyBy     = lotteryTicketDto.Tickets * lotteryDetails.EntryFee,
                Comment        = $"{lotteryTicketDto.Tickets} ticket(s) for lottery {lotteryDetails.Title}",
                UserId         = userOrg.UserId,
                OrganizationId = userOrg.OrganizationId
            };

            await _kudosService.AddLotteryKudosLogAsync(kudosLog, userOrg);

            if (applicationUser.RemainingKudos < 0)
            {
                kudosLog.PointsTypeId = await _kudosService.GetKudosTypeIdAsync(KudosTypeEnum.Refund);

                await _kudosService.AddRefundKudosLogsAsync(new List <AddKudosLogDto> {
                    kudosLog
                });
            }
            else
            {
                for (var i = 0; i < lotteryTicketDto.Tickets; i++)
                {
                    _participantsDbSet.Add(MapNewLotteryParticipant(lotteryTicketDto, userOrg));
                }
            }

            await _uow.SaveChangesAsync(applicationUser.Id);

            await _kudosService.UpdateProfileKudosAsync(applicationUser, userOrg);
        }