//2. User should not receive notification if they already have a Shift that overlaps with the newly created Shift. private bool IsUserAlreadyBooked(PendingShift pshift, User user) { //Is pending shifts, shifts in the future. //Is Shifts then always in the past? foreach (Shift usershift in user.Shifts) { if (pshift.Shift.Date == usershift.Date) { if (pshift.Shift.TimeStart.TimeOfDay > usershift.TimeStart.TimeOfDay & pshift.Shift.TimeStart.TimeOfDay < usershift.TimeEnd.TimeOfDay) { return(true); } if (pshift.Shift.TimeEnd.TimeOfDay > usershift.TimeStart.TimeOfDay & pshift.Shift.TimeEnd.TimeOfDay < usershift.TimeEnd.TimeOfDay) { return(true); } } // Can you have shifts that spans over multiple days? } return(false); }
public void isAdmin() { //arrange PendingShift pendingShift = new PendingShift() { Shift = new Shift() { ActiveRoute = true, ShiftQualificationNumber = 0, Date = DateTime.Today, TimeStart = new DateTime(2021, 1, 2, 12, 0, 0), TimeEnd = new DateTime(2021, 1, 2, 13, 0, 0) } }; List <User> users = new List <User> { new User() { IsAdmin = true } }; //act NotificationService notificationService = new NotificationService(); List <User> validUsers = notificationService.GetValidUsers(pendingShift, users, 10); //Assert Assert.IsTrue(validUsers.Count == 0); }
public ActionResult <PendingShift> Put(int id, [FromBody] PendingShift pendingShift) { try { return(Ok(_pendingShiftService.UpdatePendingShift(pendingShift))); } catch (Exception e) { return(BadRequest(e.Message)); } }
public PendingShift UpdatePendingShift(PendingShift pendingShift) { var newUserList = new List <UserPendingShift>(pendingShift.Users); _context.Attach(pendingShift).State = EntityState.Modified; _context.Entry(pendingShift).Reference(ps => ps.Shift).IsModified = true; _context.UserPendingShifts.RemoveRange( _context.UserPendingShifts.Where(ups => ups.PendingShiftId == pendingShift.Id)); _context.SaveChanges(); return(pendingShift); }
public PendingShift CreatePendingShift(PendingShift pendingShift) { pendingShift.ShiftId = pendingShift.Shift.Id; _context.Attach(pendingShift).State = EntityState.Added; _context.SaveChanges(); Shift shift = _context.Shifts.FirstOrDefault(s => s.Id == pendingShift.ShiftId); shift.PShift = pendingShift; _context.Attach(pendingShift).State = EntityState.Modified; _context.SaveChanges(); return(pendingShift); }
public void SpaceForOneShift() { //shift to cover PendingShift pendingShift = new PendingShift() { Shift = new Shift() { ActiveRoute = true, ShiftQualificationNumber = 0, Date = new DateTime(2021, 4, 7, 0, 0, 0), TimeStart = new DateTime(2021, 1, 2, 12, 0, 0), TimeEnd = new DateTime(2021, 1, 2, 13, 0, 0) } }; //user to assign to List <User> users = new List <User> { new User() { Group = new Group() { QualificationNumber = 1 }, Shifts = new List <Shift> { new Shift() { ActiveRoute = true, ShiftQualificationNumber = 0, Date = new DateTime(2021, 4, 5, 0, 0, 0), TimeStart = new DateTime(2021, 6, 5, 10, 0, 0), TimeEnd = new DateTime(2021, 1, 1, 18, 0, 0) }, new Shift() { ActiveRoute = true, ShiftQualificationNumber = 0, Date = new DateTime(2021, 4, 6, 0, 0, 0), TimeStart = new DateTime(2021, 1, 1, 10, 0, 0), TimeEnd = new DateTime(2021, 1, 1, 18, 0, 0) } } } }; //act NotificationService notificationService = new NotificationService(); List <User> validUsers = notificationService.GetValidUsers(pendingShift, users, 3); //Assert Assert.IsTrue(validUsers.Count == 1); }
public void sameWeekDifferentYear() { //shift to cover PendingShift pendingShift = new PendingShift() { Shift = new Shift() { ActiveRoute = true, ShiftQualificationNumber = 0, Date = new DateTime(2021, 1, 5, 0, 0, 0), TimeStart = new DateTime(2021, 1, 2, 9, 0, 0), TimeEnd = new DateTime(2021, 1, 2, 9, 30, 0) } }; //user to assign to List <User> users = new List <User> { new User() { Group = new Group() { QualificationNumber = 1 }, Shifts = new List <Shift> { new Shift() { ActiveRoute = true, ShiftQualificationNumber = 0, Date = new DateTime(2021, 1, 5, 0, 0, 0), TimeStart = new DateTime(2021, 6, 5, 10, 0, 0), TimeEnd = new DateTime(2021, 1, 1, 18, 0, 0) }, new Shift() { ActiveRoute = true, ShiftQualificationNumber = 0, Date = new DateTime(2020, 1, 5, 0, 0, 0), //Different year, should not count TimeStart = new DateTime(2021, 1, 1, 10, 0, 0), TimeEnd = new DateTime(2021, 1, 1, 18, 0, 0) } } } }; //act NotificationService notificationService = new NotificationService(); List <User> validUsers = notificationService.GetValidUsers(pendingShift, users, 2); //Assert Assert.IsTrue(validUsers.Count == 1); }
public void QualifiedUser() { //arrange List <User> users = new List <User> { testdata.userNormal // Has qualification Number 3 }; PendingShift pendingShift = testdata.pShift1; // Has Qualification Number 3 //act NotificationService notificationService = new NotificationService(); List <User> validUsers = notificationService.GetValidUsers(pendingShift, users, 10); //Assert Assert.IsTrue(validUsers.Count == 1); }
public void ShiftOnDifferentDay() { //arrange PendingShift pendingShift = new PendingShift() { Shift = new Shift() { ActiveRoute = true, ShiftQualificationNumber = 0, Date = new DateTime(2022, 2, 2, 0, 0, 0), TimeStart = new DateTime(2021, 1, 2, 12, 0, 0), TimeEnd = new DateTime(2021, 1, 2, 13, 0, 0) } }; List <User> users = new List <User> { new User() { Group = new Group() { QualificationNumber = 0 }, Shifts = new List <Shift> { new Shift() { ActiveRoute = true, ShiftQualificationNumber = 0, Date = new DateTime(2021, 2, 1, 0, 0, 0), TimeStart = new DateTime(2021, 1, 1, 10, 0, 0), TimeEnd = new DateTime(2021, 1, 1, 18, 0, 0) } } } }; //act NotificationService notificationService = new NotificationService(); List <User> validUsers = notificationService.GetValidUsers(pendingShift, users, 10); //Assert Assert.IsTrue(validUsers.Count == 1); }
// 1. User qualification number >= shift qualification number private bool IsUserQualified(PendingShift shift, User user) { if (user.Group is null) { throw new ArgumentNullException("The user must be assigned a group"); } int shiftQualityLevel = shift.Shift.ShiftQualificationNumber; int userQualityLevel = user.Group.QualificationNumber; if (shiftQualityLevel > userQualityLevel) { return(false); } else { return(true); } }
/** * The method must check all these requirements * 1. User qualification number >= shift qualification number * 2. User should not receive notification if they already have a Shift that overlaps with the newly created Shift. * 3. A user should not receive a notification if they already have X shifts or more in the same week as the new shift that was created. * 4. A user should not receive a notification if they are an admin. * We recommend splitting them up into smaller private methods */ public List <User> GetValidUsers(PendingShift shift, List <User> allUsers, int x) { if (shift.Shift is null) { throw new ArgumentNullException("the Pending shift are requried to have a valid shift"); } List <User> validUsers = new List <User>(); foreach (User user in allUsers) { if (user.IsAdmin) { break; } if (!IsUserQualified(shift, user)) { break; } if (IsUserAlreadyBooked(shift, user)) { break; } if (HasTooManyShifts(user, shift.Shift.Date, x)) { break; } validUsers.Add(user); } return(validUsers); }
public PendingShift UpdatePendingShift(PendingShift pendingShift) { return(_pendingShiftRepo.UpdatePendingShift(pendingShift)); }
public PendingShift CreatePendingShift(PendingShift pendingShift) { return(_pendingShiftRepo.CreatePendingShift(pendingShift)); }
public DummyData() { CreatePasswordHash(password, out passwordHashUserOne, out passwordSaltUserOne); CreatePasswordHash(password, out passwordHashUserTwo, out passwordSaltUserTwo); var SSA = new Group() { Type = "SSA", QualificationNumber = 3, }; var SSH = new Group() { Type = "SSH", QualificationNumber = 2 }; var UDD = new Group() { Type = "UDD", QualificationNumber = 1 }; userNormal = new User() { IsAdmin = false, Name = "Christian", PasswordSalt = passwordSaltUserOne, PasswordHash = passwordHashUserOne, Email = "*****@*****.**", Group = SSA, ProfilePicture = "https://scontent-ams4-1.xx.fbcdn.net/v/t1.0-9/68397042_2373050946108579_1355476049231609856_n.jpg?_nc_cat=111&_nc_ohc=L-VxFCgmIOEAQkExKADS7GkFanYn-wlS1DtritGMIDMaz-F2F47jDBqdg&_nc_ht=scontent-ams4-1.xx&oh=d64bb5c81845f4af32142583c4d47f87&oe=5E89A221" }; userNormal2 = new User() { IsAdmin = false, Name = "Richart", Email = "*****@*****.**", ProfilePicture = "https://scontent-ams4-1.xx.fbcdn.net/v/t1.0-9/13906839_10206396239896911_6785032534256698008_n.jpg?_nc_cat=100&_nc_ohc=NWUTbJTLB7oAQkf5171xXvT0S_v4pdlAB1wHR_kbXfXiZXd0kzXCiLjuA&_nc_ht=scontent-ams4-1.xx&oh=d38d30e968f89eff9eb1fe39ee3287c3&oe=5E68167D", PasswordSalt = passwordSaltUserOne, PasswordHash = passwordHashUserOne, Group = UDD }; userNormal3 = new User() { IsAdmin = false, Name = "Casper", Email = "*****@*****.**", ProfilePicture = "https://scontent-ams4-1.xx.fbcdn.net/v/t1.0-9/12524001_1549641258661056_8462488784370177517_n.png?_nc_cat=109&_nc_ohc=_uulefV7bhEAQmqusa2XXfvuZdh72xF92JjafAdGC2NjGXJZ0RGgy2z2w&_nc_ht=scontent-ams4-1.xx&oh=1c4928765d11ea4940a2a00321425a65&oe=5E6815A3" , PasswordSalt = passwordSaltUserOne, PasswordHash = passwordHashUserOne, Group = SSA }; userAdmin = new User() { IsAdmin = true, Name = "Simon", Email = "*****@*****.**", ProfilePicture = "https://imgix.bustle.com/elite-daily/2017/07/28103007/joffrey-game-of-thrones-choking.jpg?w=1020&h=574&fit=crop&crop=faces&auto=format&q=70", PasswordSalt = passwordSaltUserTwo, PasswordHash = passwordHashUserTwo, Group = SSH }; var route1 = new Route() { Name = "MA01", }; var route2 = new Route() { Name = "MA02", }; var ar = new ActiveRoute() { Name = "MA01" }; var ar2 = new ActiveRoute() { Name = "MA02" }; var ar3 = new ActiveRoute() { Name = "MA03" }; var ar4 = new ActiveRoute() { Name = "MA10" }; var ts = new TimeStart() { timeStart = "15:00" }; var ts2 = new TimeStart() { timeStart = "15:30" }; var ts3 = new TimeStart() { timeStart = "16:00" }; var ts4 = new TimeStart() { timeStart = "16:30" }; var te = new TimeEnd() { timeEnd = "23:00" }; var shift = new Shift() { ActiveRoute = true, ShiftQualificationNumber = 3, TimeStart = new DateTime(2021, 1, 1, 10, 0, 0), TimeEnd = new DateTime(2021, 1, 1, 18, 0, 0) }; var shift2 = new Shift() { ActiveRoute = true, ShiftQualificationNumber = 2, TimeStart = new DateTime(2021, 1, 2, 10, 0, 0), TimeEnd = new DateTime(2021, 1, 2, 18, 0, 0) }; var shift3 = new Shift() { ActiveRoute = true, ShiftQualificationNumber = 1, TimeStart = new DateTime(2021, 1, 3, 10, 0, 0), TimeEnd = new DateTime(2021, 1, 3, 18, 0, 0) }; pShift1 = new PendingShift() { Shift = shift }; userNormal.Shifts = new List <Shift> { shift2, shift3 }; }