public async Task <IActionResult> AddScheduledShiftAssignment(int scheduledShiftId, int?userId) { var now = _clock.UtcNow.UtcDateTime; //ensure the shift exists and that the user is allowed to modify it var shift = await _shiftRepository.GetScheduledShift(scheduledShiftId); if (User.RoleInPatrol(shift.PatrolId).CanMaintainSchedule() && now < shift.StartsAt) { //ensure if the user is specified they are in the patrol PatrolUser patrolUser = null; if (userId.HasValue) { patrolUser = await _patrolRepository.GetPatrolUser(userId.Value, shift.PatrolId); } if (!userId.HasValue || patrolUser != null) { var assignment = await _scheduleService.AddScheduledShiftAssignment(scheduledShiftId, userId); var scheduledShift = await _shiftRepository.GetScheduledShiftAssignments(shift.PatrolId, scheduledShiftId : scheduledShiftId); return(MapScheduledShiftAssessments(scheduledShift)); } else { return(Forbid()); } } else { return(Forbid()); } }
public async Task <User> AddUserToPatrol(int patrolId, Role?role, string firstName, string lastName, string email) { var user = await _userRepository.GetUser(email); if (user == null) { //create and email user = new User() { Email = email, FirstName = firstName, LastName = lastName }; await _userRepository.InsertUser(user); await _emailService.SendNewUserWelcomeEmail(user, "", ""); } else { await _emailService.SendExistingUserWelcomeEmail(user, "", ""); } var patrolUser = new PatrolUser() { PatrolId = patrolId, UserId = user.Id, Role = role }; await _patrolRepository.InsertPatrolUser(patrolUser); return(user); }
public async Task CanGetPatrolUsersForUser() { await _patrolRepository.InsertPatrol(_patrol); var patrolUser = new PatrolUser() { PatrolId = _patrol.Id, UserId = _user.Id }; await _patrolRepository.InsertPatrolUser(patrolUser); var allPatrolUsers = await _patrolRepository.GetPatrolUsersForUser(_user.Id); Assert.AreEqual(1, allPatrolUsers.Count()); Assert.AreEqual(_patrol.Id, allPatrolUsers.First().PatrolId); }
public async Task CanInsertPatrolUser() { await _patrolRepository.InsertPatrol(_patrol); var patrolUser = new PatrolUser() { PatrolId = _patrol.Id, UserId = _user.Id }; await _patrolRepository.InsertPatrolUser(patrolUser); var selected = await _patrolRepository.GetPatrolUser(patrolUser.Id); Assert.AreEqual(patrolUser.Id, selected.Id); Assert.AreEqual(patrolUser.UserId, selected.UserId); Assert.AreEqual(patrolUser.PatrolId, selected.PatrolId); }
public async Task CanDeletePatrolUser() { await _patrolRepository.InsertPatrol(_patrol); var patrolUser = new PatrolUser() { PatrolId = _patrol.Id, UserId = _user.Id }; await _patrolRepository.InsertPatrolUser(patrolUser); var selected = await _patrolRepository.GetPatrolUser(patrolUser.Id); Assert.NotNull(selected); await _patrolRepository.DeletePatrolUser(patrolUser.PatrolId, patrolUser.UserId); var selectedAfterDelete = await _patrolRepository.GetPatrolUser(patrolUser.Id); Assert.IsNull(selectedAfterDelete); }
public async Task UpdatePatrolUser(PatrolUser patrolUser) { await _connection.UpdateAsync(patrolUser) .ConfigureAwait(false); }
public async Task InsertPatrolUser(PatrolUser patrolUser) { patrolUser.Id = (int)await _connection.InsertAsync(patrolUser) .ConfigureAwait(false); }