public HttpResponseMessage CancelRecurringShiftInstance(Guid Id, RecurringShiftChangeActionDTO shiftCancelDTO)
        {
            var shift = db.ShiftTemplates.Find(Id);

            if (shift == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Recurring shift not found"));
            }

            //Only the staff member assigned to the shift can request to cancel the shift
            if (ClaimsAuthorization.CheckAccess("Get", "BusinessId", shift.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.RecurringShiftChangeRequests.Where(sc => sc.Type == ShiftRequestType.Cancel &&
                                                                               sc.ShiftTemplate.Id == Id &&
                                                                               sc.Status == RequestStatus.Pending &&
                                                                               sc.OccurenceDate == shiftCancelDTO.OccurenceDate);
                if (shiftCancelRequest.Count() > 0)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Existing cancellation request already pending for recurring shift"));
                }

                RecurringShiftChangeRequest shiftChangeRequest = new RecurringShiftChangeRequest
                {
                    Id            = Guid.NewGuid(),
                    Reason        = shiftCancelDTO.Reason,
                    ShiftTemplate = shift,
                    Type          = ShiftRequestType.Cancel,
                    OccurenceDate = shiftCancelDTO.OccurenceDate,
                    Status        = RequestStatus.Pending,
                    CreatedDate   = WebUI.Common.Common.DateTimeNowLocal()
                };

                db.RecurringShiftChangeRequests.Add(shiftChangeRequest);
                db.SaveChanges();

                return(Request.CreateResponse(HttpStatusCode.Created, shiftChangeRequest.Id));
            }
            else
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Unauthorized));
            }
        }
Example #2
0
 public ActionResult CancelRecurringShiftRequest(string shiftId, string reason, string shiftDate)
 {
     //Current user has requested to be linked to a business
     using (HttpClientWrapper httpClient = new HttpClientWrapper(Session))
     {
         var cancelDTO = new RecurringShiftChangeActionDTO {
             Id = Guid.Parse(shiftId), Reason = reason, OccurenceDate = new DateTime(long.Parse(shiftDate))
         };
         HttpResponseMessage response = httpClient.PostAsJsonAsync("api/EmployeeShiftActionAPI/CancelRecurringShiftInstance/" + 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"));
         }
     }
 }