예제 #1
0
        public ActionResult ChangeWorkorderStatus(StatusModel model)
        {
            _log.InfoFormat("Method: ChangeWorkorderStatus. Id: {0}, Status {1}", model.Id, model.Status);
            var workorder = repository.SearchFor <SageWorkOrder>(x => x.WorkOrder == model.Id).FirstOrDefault();

            if (workorder == null)
            {
                return(Error("Workorder not found", $"There is no workorders with id {model.Id}. workorder == null"));
            }

            var sageStatus = model.Status == WorkOrderStatus.Closed ?
                             WorkOrderStatus.ByStatus(WorkOrderStatus.Closed) :
                             WorkOrderStatus.ByStatus(WorkOrderStatus.Open);
            var result = sageApiProxy.EditWorkOrderStatus(model.Id, sageStatus.ToString());

            if (!result.IsSucceed)
            {
                return(Error("Was not able to save workorder to sage",
                             $"EditWorkOrderStatus method IsSucceed==false. {result.ErrorMassage}."));
            }

            workorder.Status = model.Status;
            repository.Update(workorder);
            _hub.UpdateSageWorkOrder(workorder);
            _log.InfoFormat("Workorder ({0}) status changed. Status: {1}. Repository updated", workorder.Name, model.Status);
            notification.SendNotification($"Workorder {workorder.Name} change status by {model.Status}");
            if (model.Status == WorkOrderStatus.WorkComplete)
            {
                _hub.ShowAlert(new SweetAlertModel()
                {
                    Message = $"Workorder #{workorder.WorkOrder} closed",
                    Title   = "Workorder completed",
                    Type    = "success"
                });
            }
            return(Success());
        }