Esempio n. 1
0
        public void Should_Not_Award_If_Organization_Is_Invalid()
        {
            // Setup
            const int yearOfEmployment       = 1;
            var       kudosYearlyMultipliers = new[] { 1, 2, 3, 4 };

            var employee = new ApplicationUser
            {
                Id             = "user",
                OrganizationId = 2,
                RemainingKudos = 0,
                TotalKudos     = 10
            };

            var loyaltyKudosType = new KudosType
            {
                Id    = 1,
                Value = 10
            };

            // Act
            void Action() => _loyaltyKudosCalculator.CreateLoyaltyKudosLog(employee, loyaltyKudosType, 0, kudosYearlyMultipliers, yearOfEmployment);

            // Assert
            var e = Assert.Throws <ArgumentException>(Action);

            Assert.AreEqual("organizationId", e.ParamName);
        }
Esempio n. 2
0
        public void Should_Not_Award_Employee_With_Loyalty_Kudos_When_Multiplier_Is_Zero()
        {
            // Setup
            const int organizationId         = 2;
            const int yearOfEmployment       = 2;
            var       kudosYearlyMultipliers = new[] { 0, 0, 3, 4 };
            var       twoYearEmployee        = new ApplicationUser
            {
                Id             = "user",
                OrganizationId = 2,
                RemainingKudos = 0,
                TotalKudos     = 10
            };

            var loyaltyKudosType = new KudosType
            {
                Id    = 1,
                Value = 10
            };

            // Act
            var loyaltyKudosLog = _loyaltyKudosCalculator.CreateLoyaltyKudosLog(twoYearEmployee, loyaltyKudosType, organizationId,
                                                                                kudosYearlyMultipliers, yearOfEmployment);

            // Assert
            Assert.AreEqual(null, loyaltyKudosLog);
            Assert.AreEqual(0, twoYearEmployee.RemainingKudos);
            Assert.AreEqual(10, twoYearEmployee.TotalKudos);
        }
Esempio n. 3
0
        public void Should_Not_Award_If_Kudos_Yearly_Multipliers_Is_Empty_Array()
        {
            // Setup
            const int organizationId         = 2;
            const int yearOfEmployment       = 3;
            var       kudosYearlyMultipliers = new int[] { };
            var       employee = new ApplicationUser
            {
                Id             = "user",
                OrganizationId = 2,
                RemainingKudos = 0,
                TotalKudos     = 10
            };

            var loyaltyKudosType = new KudosType
            {
                Id    = 1,
                Value = 10,
                Name  = "Loyalty"
            };

            // Act
            void Action() => _loyaltyKudosCalculator.CreateLoyaltyKudosLog(employee, loyaltyKudosType, organizationId, kudosYearlyMultipliers, yearOfEmployment);

            // Assert
            var e = Assert.Throws <ArgumentException>(Action);

            Assert.AreEqual("kudosYearlyMultipliers", e.ParamName);
        }
Esempio n. 4
0
        public void Should_Not_Award_Employee_With_Loyalty_Kudos_Who_Is_Working_0_Years()
        {
            // Setup
            const int organizationId         = 2;
            const int yearOfEmployment       = 0;
            var       kudosYearlyMultipliers = new[] { 1, 2, 3, 4 };
            var       oneYearEmployee        = new ApplicationUser
            {
                Id             = "user",
                OrganizationId = 2,
                RemainingKudos = 0,
                TotalKudos     = 10
            };

            var loyaltyKudosType = new KudosType
            {
                Id    = 1,
                Value = 10
            };

            // Act
            void Action() => _loyaltyKudosCalculator.CreateLoyaltyKudosLog(oneYearEmployee, loyaltyKudosType, organizationId, kudosYearlyMultipliers, yearOfEmployment);

            // Assert
            var e = Assert.Throws <ArgumentException>(Action);

            Assert.AreEqual("yearOfEmployment", e.ParamName);
        }
 public void ValidateKudosType(KudosType kudosType)
 {
     if (kudosType == null)
     {
         throw new ValidationException(ErrorCodes.KudosTypeNotFound, "Kudos type not found");
     }
 }
        public List <KudosLog> GetEmployeeLoyaltyKudosLog(EmployeeLoyaltyKudosDto employeeLoyaltyKudos,
                                                          KudosType loyaltyType,
                                                          int organizationId,
                                                          int[] kudosYearlyMultipliers)
        {
            var receivedLoyaltiesCount = employeeLoyaltyKudos.AwardedLoyaltyKudosCount;
            var yearsToAwardFor        = CalculateYearsToAwardFor(employeeLoyaltyKudos.Employee.YearsEmployed, receivedLoyaltiesCount);
            var loyaltyKudosLogList    = new List <KudosLog>();

            foreach (var year in yearsToAwardFor)
            {
                if (employeeLoyaltyKudos.AwardedEmploymentYears.Any(y => y == year))
                {
                    continue;
                }

                var loyaltyKudosLog = CreateLoyaltyKudosLog(employeeLoyaltyKudos.Employee, loyaltyType, organizationId, kudosYearlyMultipliers, year);
                if (loyaltyKudosLog != null)
                {
                    loyaltyKudosLogList.Add(loyaltyKudosLog);
                }
            }

            return(loyaltyKudosLogList);
        }
Esempio n. 7
0
        public async Task Should_Map_Retrieved_Users_KudosLogs_Organization_And_KudosType_Correctly()
        {
            var user1 = new ApplicationUser {
                Id = "user1", OrganizationId = 2, EmploymentDate = DateTime.UtcNow.AddYears(-2).AddDays(-2)
            };
            var user2 = new ApplicationUser {
                Id = "user2", OrganizationId = 2, EmploymentDate = DateTime.UtcNow.AddYears(-2).AddDays(-2)
            };
            var user3 = new ApplicationUser {
                Id = "user3", OrganizationId = 2, EmploymentDate = DateTime.UtcNow.AddDays(-100)
            };
            var user4 = new ApplicationUser {
                Id = "user4", OrganizationId = 1, EmploymentDate = null
            };

            var employees = new List <ApplicationUser>
            {
                user1, user2, user3, user4
            };

            var organizations = new List <Organization>
            {
                new Organization {
                    Id = 1, ShortName = "tenant1", KudosYearlyMultipliers = "1;2;3;4"
                },
                new Organization {
                    Id = 2, ShortName = "tenant2", KudosYearlyMultipliers = "1;2;3;4"
                }
            };

            var kudosLogs = new List <KudosLog>
            {
                new KudosLog {
                    Id = 1, EmployeeId = "user2", KudosTypeName = "Loyalty", OrganizationId = 2, Status = KudosStatus.Approved, Employee = user2
                },
                new KudosLog {
                    Id = 2, EmployeeId = "user2", KudosTypeName = "Loyalty", OrganizationId = 2, Status = KudosStatus.Approved, Employee = user2
                },
                new KudosLog {
                    Id = 3, EmployeeId = "user1", KudosTypeName = "Loyalty", OrganizationId = 2, Status = KudosStatus.Approved, Employee = user1
                },
                new KudosLog {
                    Id = 4, EmployeeId = "user4", KudosTypeName = "Loyalty", OrganizationId = 1, Status = KudosStatus.Approved, Employee = user4
                }
            };

            var loyaltyKudosType = new KudosType {
                Id = 1, Value = 1, Name = "Loyalty"
            };

            _usersDbSet.SetDbSetDataForAsync(employees.AsQueryable());
            _kudosLogsDbSet.SetDbSetDataForAsync(kudosLogs.AsQueryable());
            _kudosTypeDbSet.SetDbSetDataForAsync(new[] { loyaltyKudosType }.AsQueryable());
            _organizationsDbSet.SetDbSetDataForAsync(organizations.AsQueryable());

            await _loyaltyKudosService.AwardEmployeesWithKudosAsync("tenant2");

            _kudosLogsDbSet.Received(1).Add(Arg.Is <KudosLog>(x => x.EmployeeId == "user1"));
        }
Esempio n. 8
0
        public async Task Should_Award_Employee_With_Loyalty_Kudos_OnlyAfter2And3Years()
        {
            var user1 = new ApplicationUser {
                Id = "user1", OrganizationId = 2, EmploymentDate = DateTime.UtcNow.AddYears(-3).AddDays(-10)
            };
            var user2 = new ApplicationUser {
                Id = "user2", OrganizationId = 2, EmploymentDate = DateTime.UtcNow.AddYears(-3).AddDays(-10)
            };
            var user3 = new ApplicationUser {
                Id = "user3", OrganizationId = 2, EmploymentDate = DateTime.UtcNow.AddDays(-100)
            };

            var employees = new List <ApplicationUser>
            {
                user1, user2, user3
            };

            var organizations = new List <Organization>
            {
                new Organization {
                    Id = 2, ShortName = "tenant2", KudosYearlyMultipliers = "0;2;4"
                }
            };

            var kudosLogs = new List <KudosLog>
            {
                new KudosLog {
                    Id = 1, EmployeeId = "user1", Created = DateTime.UtcNow.AddYears(-2), KudosTypeName = "Loyalty", OrganizationId = 2, Status = KudosStatus.Approved, Points = 20, Employee = user1
                },
                new KudosLog {
                    Id = 1, EmployeeId = "user1", Created = DateTime.UtcNow.AddYears(-1), KudosTypeName = "Other", OrganizationId = 2, Status = KudosStatus.Approved, Points = 20, Employee = user1
                }
            };

            var loyaltyKudosType = new KudosType {
                Id = 1, Value = 1, Name = "Loyalty"
            };

            _usersDbSet.SetDbSetDataForAsync(employees.AsQueryable());
            _kudosLogsDbSet.SetDbSetDataForAsync(kudosLogs.AsQueryable());
            _kudosTypeDbSet.SetDbSetDataForAsync(new[] { loyaltyKudosType }.AsQueryable());
            _organizationsDbSet.SetDbSetDataForAsync(organizations.AsQueryable());

            await _loyaltyKudosService.AwardEmployeesWithKudosAsync("tenant2");

            // Let's fake that we execute bgr job the second day.
            kudosLogs.Add(new KudosLog {
                Id = 2, EmployeeId = "user1", Created = DateTime.UtcNow.AddMinutes(-5), KudosTypeName = "Loyalty", OrganizationId = 2, Status = KudosStatus.Approved, Points = 40, Employee = user1
            });
            kudosLogs.Add(new KudosLog {
                Id = 2, EmployeeId = "user2", Created = DateTime.UtcNow.AddMinutes(-5), KudosTypeName = "Loyalty", OrganizationId = 2, Status = KudosStatus.Approved, Points = 40, Employee = user2
            });
            await _loyaltyKudosService.AwardEmployeesWithKudosAsync("tenant2");

            _kudosLogsDbSet.Received(2).Add(Arg.Is <KudosLog>(x => x.EmployeeId == "user1"));
            _kudosLogsDbSet.Received(3).Add(Arg.Is <KudosLog>(x => x.EmployeeId == "user2"));
            _kudosLogsDbSet.Received(5).Add(Arg.Any <KudosLog>());
        }
        public KudosLog CreateLoyaltyKudosLog(ApplicationUser recipient, KudosType loyaltyKudosType, int organizationId, int[] kudosYearlyMultipliers, int yearOfEmployment)
        {
            if (yearOfEmployment <= 0)
            {
                throw new ArgumentException("Invalid argument", nameof(yearOfEmployment));
            }

            if (organizationId <= 0)
            {
                throw new ArgumentException("Invalid argument", nameof(organizationId));
            }

            if (recipient == null)
            {
                throw new ArgumentNullException(nameof(recipient));
            }

            if (loyaltyKudosType == null)
            {
                throw new ArgumentNullException(nameof(loyaltyKudosType));
            }

            if (kudosYearlyMultipliers == null || kudosYearlyMultipliers.Length == 0)
            {
                throw new ArgumentException("Invalid argument", nameof(kudosYearlyMultipliers));
            }

            var timestamp = DateTime.UtcNow;
            var yearlyLoyaltyKudosMultiplier = CalculateYearlyMultiplier(yearOfEmployment, kudosYearlyMultipliers);

            if (yearlyLoyaltyKudosMultiplier == 0)
            {
                return(null);
            }

            var loyaltyLog = new KudosLog
            {
                Modified       = timestamp,
                CreatedBy      = LoyaltyKudosBotName,
                Comments       = CreateLoyaltyKudosComment(yearOfEmployment),
                EmployeeId     = recipient.Id,
                KudosTypeName  = loyaltyKudosType.Name,
                KudosTypeValue = loyaltyKudosType.Value,
                MultiplyBy     = yearlyLoyaltyKudosMultiplier,
                Points         = yearlyLoyaltyKudosMultiplier * loyaltyKudosType.Value,
                Status         = KudosStatus.Approved,
                OrganizationId = organizationId
            };

            if (recipient.EmploymentDate.HasValue)
            {
                loyaltyLog.Created = recipient.EmploymentDate.Value.AddYears(yearOfEmployment);
            }

            recipient.ReceiveKudos(loyaltyLog);
            return(loyaltyLog);
        }
Esempio n. 10
0
 private KudosTypeDTO MapKudosTypesToDTO(KudosType kudosType)
 {
     return(new KudosTypeDTO()
     {
         Id = kudosType.Id,
         Name = kudosType.Name,
         Value = kudosType.Value,
         Type = kudosType.Type,
         Description = kudosType.Description,
         IsNecessary = kudosType.Type == ConstBusinessLayer.KudosTypeEnum.Send ||
                       kudosType.Type == ConstBusinessLayer.KudosTypeEnum.Minus ||
                       kudosType.Type == ConstBusinessLayer.KudosTypeEnum.Other
     });
 }
Esempio n. 11
0
 private KudosTypeDTO MapKudosTypesToDTO(KudosType kudosType)
 {
     return(new KudosTypeDTO
     {
         Id = kudosType.Id,
         Name = kudosType.Name,
         Value = kudosType.Value,
         Type = kudosType.Type,
         Description = kudosType.Description,
         IsNecessary = kudosType.Type == KudosTypeEnum.Send ||
                       kudosType.Type == KudosTypeEnum.Minus ||
                       kudosType.Type == KudosTypeEnum.Other,
         IsActive = kudosType.IsActive
     });
 }
Esempio n. 12
0
        public void Should_Not_Award_If_Employee_Is_Null()
        {
            // Setup
            const int organizationId         = 2;
            const int yearOfEmployment       = 1;
            var       kudosYearlyMultipliers = new[] { 1, 2, 3, 4 };
            var       loyaltyKudosType       = new KudosType
            {
                Id    = 1,
                Value = 10
            };

            // Act
            void Action() => _loyaltyKudosCalculator.CreateLoyaltyKudosLog(null, loyaltyKudosType, organizationId, kudosYearlyMultipliers, yearOfEmployment);

            // Assert
            var e = Assert.Throws <ArgumentNullException>(Action);

            Assert.AreEqual("recipient", e.ParamName);
        }
Esempio n. 13
0
        private KudosLog CreateKudosLogForBasket(KudosBasketDonationDTO donation, KudosType kudosType, string logComment, string userId)
        {
            var timestamp = DateTime.UtcNow;
            var kudosLog  = new KudosLog
            {
                Created         = timestamp,
                Modified        = timestamp,
                CreatedBy       = donation.UserId,
                Points          = donation.DonationAmount,
                EmployeeId      = userId,
                MultiplyBy      = KudosMultiplier,
                Status          = KudosStatus.Approved,
                Comments        = logComment,
                OrganizationId  = donation.OrganizationId,
                KudosTypeName   = kudosType.Name,
                KudosTypeValue  = kudosType.Value,
                KudosSystemType = kudosType.Type
            };

            return(kudosLog);
        }
Esempio n. 14
0
        public async Task CreateKudosType(NewKudosTypeDto dto)
        {
            var alreadyExists = await _kudosTypesDbSet
                                .AnyAsync(t => t.Name == dto.Name);

            if (alreadyExists)
            {
                throw new ValidationException(444, "Kudos type already exists");
            }

            var newType = new KudosType
            {
                Name        = dto.Name,
                Value       = dto.Multiplier,
                Type        = ConstBusinessLayer.KudosTypeEnum.Ordinary,
                Description = dto.Description
            };

            _kudosTypesDbSet.Add(newType);

            await _uow.SaveChangesAsync(dto.UserId);
        }
Esempio n. 15
0
        public void Should_Award_2_Year_Employee_With_Loyalty_Kudos()
        {
            // Setup
            const int organizationId         = 2;
            const int yearOfEmployment       = 2;
            var       kudosYearlyMultipliers = new[] { 1, 2, 3, 4 };
            var       twoYearEmployee        = new ApplicationUser
            {
                Id             = "user",
                OrganizationId = 2,
                RemainingKudos = 0,
                TotalKudos     = 10,
                EmploymentDate = DateTime.Now.AddYears(-2)
            };

            var loyaltyKudosType = new KudosType
            {
                Id    = 1,
                Value = 10,
                Name  = "Loyalty"
            };

            // Act
            var loyaltyKudosLog = _loyaltyKudosCalculator.CreateLoyaltyKudosLog(twoYearEmployee, loyaltyKudosType, organizationId,
                                                                                kudosYearlyMultipliers, yearOfEmployment);

            // Assert
            Assert.AreEqual(twoYearEmployee.Id, loyaltyKudosLog.EmployeeId);
            Assert.AreEqual(20, loyaltyKudosLog.Points);
            Assert.AreEqual(2, loyaltyKudosLog.MultiplyBy);
            Assert.AreEqual(KudosStatus.Approved, loyaltyKudosLog.Status);
            Assert.AreEqual(loyaltyKudosType.Value, loyaltyKudosLog.KudosTypeValue);
            Assert.AreEqual(loyaltyKudosType.Name, loyaltyKudosLog.KudosTypeName);
            Assert.AreEqual("KudosLoyaltyBot", loyaltyKudosLog.CreatedBy);
            Assert.AreEqual("Kudos for 2 year loyalty", loyaltyKudosLog.Comments);
            Assert.AreEqual(20, twoYearEmployee.RemainingKudos);
            Assert.AreEqual(30, twoYearEmployee.TotalKudos);
        }