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)); } }
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)); } }