public HttpResponseMessage CancelShift(Guid Id, ShiftChangeActionDTO shiftCancelDTO) { var shift = db.Shifts.Find(Id); if (shift == null) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Shift not found")); } //Only the staff member assigned to the shift can request to cancel the shift if (ClaimsAuthorization.CheckAccess("Get", "BusinessId", shift.Roster.BusinessLocation.Business.Id.ToString()) || shift.Employee.UserProfile.Email != User.Identity.Name) { if (shiftCancelDTO.Reason == String.Empty) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Reason cannot be blank")); } //Check to see if there is already a pending cancellation request var shiftCancelRequest = db.ShiftChangeRequests.Where(sc => sc.Type == ShiftRequestType.Cancel && sc.Shift.Id == Id && sc.Status == RequestStatus.Pending); if (shiftCancelRequest.Count() > 0) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Existing cancellation request already for shift id:" + Id.ToString())); } ShiftChangeRequest shiftChangeRequest = new ShiftChangeRequest { Id = Guid.NewGuid(), Reason = shiftCancelDTO.Reason, Shift = shift, Type = ShiftRequestType.Cancel, Status = RequestStatus.Pending, CreatedDate = WebUI.Common.Common.DateTimeNowLocal(), CreatedBy = shift.Employee }; //Get all managers for this busiess location var managers = db.Employees.Where(m => m.IsAdmin == true && m.BusinessLocation.Id == shift.Roster.BusinessLocation.Id); foreach (var mgr in managers) { if (mgr.UserProfile != null) { //Send notifications to managers of the affected business location information that the employee has requested to cancel a shift MessagingService.ShiftCancelRequest(mgr.UserProfile.Email, mgr.UserProfile.FirstName, shift.Employee.UserProfile.FirstName + ' ' + shift.Employee.UserProfile.LastName, shift.InternalLocation.BusinessLocation.Name, shift.StartTime, shift.FinishTime); } } db.ShiftChangeRequests.Add(shiftChangeRequest); db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.Created, shiftChangeRequest.Id)); } else { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Unauthorized)); } }
//[Authorize(Roles = Constants.RoleBusinessLocationManager + "," + Constants.RoleBusinessAdmin)] public ActionResult ApproveShiftChangeRequest(string reqId, string reason, bool isRecurring) { //Current user has requested to be linked to a business using (HttpClientWrapper httpClient = new HttpClientWrapper(Session)) { var acceptDTO = new ShiftChangeActionDTO { Id = Guid.Parse(reqId), Reason = reason }; if (!isRecurring) { HttpResponseMessage response = httpClient.PostAsJsonAsync("api/ManagerShiftActionAPI/ApproveShiftChangeRequest/" + reqId, acceptDTO).Result; Response.StatusCode = (int)response.StatusCode; if (!response.IsSuccessStatusCode) { Response.SuppressFormsAuthenticationRedirect = true; return(this.Content(JsonConvert.DeserializeObject <dynamic>(response.Content.ReadAsStringAsync().Result).Message.ToString())); } } else { HttpResponseMessage response = httpClient.PostAsJsonAsync("api/ManagerShiftActionAPI/ApproveRecurringShiftChangeRequest/" + reqId, acceptDTO).Result; Response.StatusCode = (int)response.StatusCode; if (!response.IsSuccessStatusCode) { Response.SuppressFormsAuthenticationRedirect = true; return(this.Content(JsonConvert.DeserializeObject <dynamic>(response.Content.ReadAsStringAsync().Result).Message.ToString())); } } } return(Content("Success")); }
public HttpResponseMessage ApproveShiftChangeRequest(string id, ShiftChangeActionDTO acceptDTO) { try { var requestId = Guid.Parse(id); var email = HttpContext.Current.User.Identity.Name; var scRequest = db.ShiftChangeRequests.FirstOrDefault(er => er.Id == requestId && er.Status == RequestStatus.Pending); if (scRequest == null) { return(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Unable to find pending shift change request.")); } //Ensure user has "Put" manager permissions for the business which the request corresponds to if (!ClaimsAuthorization.CheckAccess("Put", "BusinessLocationId", scRequest.Shift.Roster.BusinessLocation.Id.ToString())) { return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "You do not have permissions.")); } //Update Request object scRequest.Status = RequestStatus.Approved; scRequest.ActionedDate = WebUI.Common.Common.DateTimeNowLocal(); scRequest.ActionedComment = acceptDTO.Reason; //Attach the Employee record which matches the logged in user profile and business id. scRequest.ActionedBy = db.UserProfiles.FirstOrDefault(usr => usr.Email == email).Employees.FirstOrDefault(emp => emp.BusinessLocation.Id == scRequest.Shift.InternalLocation.BusinessLocation.Id); if (scRequest.Type == ShiftRequestType.Cancel) { //Update shift to be cancelled, ie set to not have an employee assigned and become and open shift scRequest.Shift.Employee.Shifts.Remove(scRequest.Shift); } //If this is an open shift Take request, then need to reject all other requests for same shift // and send notification if (scRequest.Type == ShiftRequestType.TakeOpenShift) { scRequest.Shift.Employee = scRequest.CreatedBy; //Assign the open shift to the employee foreach (var openShiftRequest in db.ShiftChangeRequests.Where(scr => scr.Shift.Id == scRequest.Shift.Id && scr.Id != scRequest.Id)) { openShiftRequest.Status = RequestStatus.Denied; openShiftRequest.ActionedDate = WebUI.Common.Common.DateTimeNowLocal(); openShiftRequest.ActionedComment = acceptDTO.Reason; } MessagingService.OpenShiftRequestAccept(scRequest.CreatedBy.Email, scRequest.CreatedBy.FirstName, scRequest.Shift.StartTime.ToString() + " - " + scRequest.Shift.FinishTime.ToString() + " @ " + scRequest.Shift.InternalLocation.BusinessLocation.Name); } db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } catch (Exception ex) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message)); } }
public HttpResponseMessage RequestOpenShift(Guid Id, ShiftChangeActionDTO shiftRequestDTO) { var shift = db.Shifts.Find(Id); if (shift == null) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Shift not found")); } //Only the staff member assigned to the shift can request to cancel the shift if (ClaimsAuthorization.CheckAccess("Get", "BusinessId", shift.Roster.BusinessLocation.Business.Id.ToString())) { var email = HttpContext.Current.User.Identity.Name; var employee = db.Employees.First(emp => emp.UserProfile.Email == email && emp.BusinessLocation.Id == shift.Roster.BusinessLocation.Id); //Check to see if there is already a pending shift request var shiftRequest = db.ShiftChangeRequests.Where(sc => sc.Type == ShiftRequestType.TakeOpenShift && sc.Shift.Id == Id && sc.Status == RequestStatus.Pending && sc.CreatedBy.Id == employee.Id); if (shiftRequest.Count() > 0) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Existing request already pending for shift id:" + Id.ToString())); } ShiftChangeRequest shiftChangeRequest = new ShiftChangeRequest { Id = Guid.NewGuid(), Shift = shift, Type = ShiftRequestType.TakeOpenShift, Status = RequestStatus.Pending, Reason = shiftRequestDTO.Reason, CreatedDate = WebUI.Common.Common.DateTimeNowLocal(), CreatedBy = employee }; db.ShiftChangeRequests.Add(shiftChangeRequest); db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.Created, shiftChangeRequest.Id)); } else { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Unauthorized)); } }
public HttpResponseMessage RejectRecurringShiftChangeRequest(string id, ShiftChangeActionDTO rejectDTO) { try { var requestId = Guid.Parse(id); var email = HttpContext.Current.User.Identity.Name; var scRequest = db.RecurringShiftChangeRequests.FirstOrDefault(er => er.Id == requestId && er.Status == RequestStatus.Pending); //Ensure user has "Put" manager permissions for the business location which the request corresponds to if (!ClaimsAuthorization.CheckAccess("Put", "BusinessLocationId", scRequest.ShiftTemplate.BusinessLocation.Id.ToString())) { return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "You do not have permissions.")); } if (scRequest == null) { return(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Unable to find pending recurring shift change request.")); } //Update Request object scRequest.Status = RequestStatus.Denied; scRequest.ActionedDate = WebUI.Common.Common.DateTimeNowLocal(); scRequest.ActionedComment = rejectDTO.Reason; //Attach the Employee record which matches the logged in user profile and business id.' var usrProfile = db.UserProfiles.FirstOrDefault(usr => usr.Email == email); var employee = usrProfile.Employees.FirstOrDefault(emp => emp.BusinessLocation.Id == scRequest.ShiftTemplate.BusinessLocation.Id); if (employee == null) { return(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Unable to find employee profile.")); } scRequest.ActionedBy = employee; db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } catch (Exception ex) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message)); } }
public ActionResult CancelShiftRequest(string shiftId, string reason) { //Current user has requested to be linked to a business using (HttpClientWrapper httpClient = new HttpClientWrapper(Session)) { var cancelDTO = new ShiftChangeActionDTO { Id = Guid.Parse(shiftId), Reason = reason }; HttpResponseMessage response = httpClient.PostAsJsonAsync("api/EmployeeShiftActionAPI/CancelShift/" + shiftId, cancelDTO).Result; Response.StatusCode = (int)response.StatusCode; if (!response.IsSuccessStatusCode) { Response.SuppressFormsAuthenticationRedirect = true; return(this.Content(JsonConvert.DeserializeObject <dynamic>(response.Content.ReadAsStringAsync().Result).Message.ToString())); } else { return(Content("Success")); } } }
public HttpResponseMessage ApproveRecurringShiftChangeRequest(string id, ShiftChangeActionDTO acceptDTO) { try { var requestId = Guid.Parse(id); var email = HttpContext.Current.User.Identity.Name; var scRequest = db.RecurringShiftChangeRequests.FirstOrDefault(er => er.Id == requestId && er.Status == RequestStatus.Pending); if (scRequest == null) { return(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Unable to find pending shift change request.")); } var weekStarting = WebUI.Common.Common.GetStartOfWeek(scRequest.OccurenceDate, DayOfWeek.Monday); //Ensure user has "Put" manager permissions for the business which the request corresponds to if (!ClaimsAuthorization.CheckAccess("Put", "BusinessLocationId", scRequest.ShiftTemplate.BusinessLocation.Id.ToString())) { return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "You do not have permissions.")); } //Ensure week starting date is in the future AND is not already rostered //if (weekStarting < WebUI.Common.Common.DateTimeNowLocal()) // return Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "This shift occurs in a week starting in the past"); //Ensure occurence date is after current time if (new DateTime(scRequest.OccurenceDate.Year, scRequest.OccurenceDate.Month, scRequest.OccurenceDate.Day, scRequest.ShiftTemplate.StartTime.Hours, scRequest.OccurenceDate.Minute, 0) < WebUI.Common.Common.DateTimeNowLocal()) { return(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "This shift occurs in the past")); } var roster = db.Rosters.FirstOrDefault(r => r.WeekStartDate == weekStarting && r.BusinessLocation.Id == scRequest.ShiftTemplate.BusinessLocation.Id); if (roster != null) { return(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Roster already created for week starting " + weekStarting.ToShortDateString())); } //Update Request object scRequest.Status = RequestStatus.Approved; scRequest.ActionedDate = WebUI.Common.Common.DateTimeNowLocal(); scRequest.ActionedComment = acceptDTO.Reason; //Attach the Employee record which matches the logged in user profile and business id. scRequest.ActionedBy = db.UserProfiles.FirstOrDefault(usr => usr.Email == email).Employees.FirstOrDefault(emp => emp.BusinessLocation.Id == scRequest.ShiftTemplate.BusinessLocation.Id); if (scRequest.Type == ShiftRequestType.Cancel) { var occurenceDayOfWeek = scRequest.OccurenceDate.DayOfWeek; //Create one off OPEN shift template to fill this shift ShiftTemplate sst = new ShiftTemplate { Id = Guid.NewGuid(), WeekStarting = weekStarting, StartTime = scRequest.ShiftTemplate.StartTime, FinishTime = scRequest.ShiftTemplate.FinishTime, Monday = (occurenceDayOfWeek == DayOfWeek.Monday ? true : false), Tuesday = (occurenceDayOfWeek == DayOfWeek.Tuesday ? true : false), Wednesday = (occurenceDayOfWeek == DayOfWeek.Wednesday ? true : false), Thursday = (occurenceDayOfWeek == DayOfWeek.Thursday ? true : false), Friday = (occurenceDayOfWeek == DayOfWeek.Friday ? true : false), Saturday = (occurenceDayOfWeek == DayOfWeek.Saturday ? true : false), Sunday = (occurenceDayOfWeek == DayOfWeek.Sunday ? true : false), Frequency = ShiftFrequency.OneOff, BusinessLocation = scRequest.ShiftTemplate.BusinessLocation, Employee = null, //set to an OPEN SHIFT InternalLocation = scRequest.ShiftTemplate.InternalLocation, Role = scRequest.ShiftTemplate.Role, Enabled = true }; db.ShiftTemplates.Add(sst); //TODO create an exception to recurring shift template to show that this have been cancelled??? } db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } catch (Exception ex) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message)); } }