Example #1
0
        public async Task <ServiceResult <Routine2ChangeStepResult> > ChangeStep(int id, Routine2Actions action, string description, int userId)
        {
            try
            {
                var entity = _context.Test.FirstOrDefault(q => q.Id == id);
                if (entity == null)
                {
                    throw new Exception($"داده‌ای با شناسه {id} یافت نشد");
                }

                var nextStep = GetNextStep(id, action);
                if (nextStep == -1)
                {
                    throw new Exception("Something went wrong, next dashboard couldn't be -1");
                }


                var fromStep = entity.RoutineStep;

                _routine2Repository.CreateLog(new CreateRoutine2LogViewModel
                {
                    RoutineId        = TestRoutine.RoutineId,
                    EntityId         = id,
                    Description      = description,
                    Action           = _routine2Repository.GetActionTitle(TestRoutine.RoutineId, fromStep, action),
                    Step             = fromStep,
                    ToStep           = nextStep,
                    UserId           = userId,
                    CreatorUserId    = userId,
                    RoutineRoleTitle = _routine2Repository.GetRoutineRoleTitle(TestRoutine.RoutineId, entity.RoutineStep),
                });

                var routineVm = new EditRoutine2ViewModel(nextStep, entity.RoutineFlownDate);
                // اگر در مرحله آخر باشیم، باید به روال برچست تمام شده بزنیم
                if (TestRoutine.DoneSteps.Contains(nextStep))
                {
                    routineVm.RoutineIsDone = true;
                }

                // به صورت پیش‌فرض طرح رد شده است، مگر اینکه در مراحل تایید باشیم
                if (TestRoutine.SucceededSteps.Contains(nextStep))
                {
                    routineVm.RoutineIsSucceeded = true;
                }

                Mapper.Map(routineVm, entity);
                _context.SaveChanges();

                var changeStepResult = new Routine2ChangeStepResult
                {
                    Action      = action,
                    Description = description,
                    UserId      = userId,
                    ToStep      = nextStep,
                    FromStep    = fromStep,
                    EntityId    = entity.Id,
                    RoutineId   = TestRoutine.RoutineId,
                };

                //try { await _routine2Repository.SendNoticeAsync(changeStepResult); } catch (Exception ex) { }

                return(ServiceResult <Routine2ChangeStepResult> .Okay(changeStepResult));
            }
            catch (Exception ex)
            {
                return(ServiceResult <Routine2ChangeStepResult> .Exception(ex));
            }
        }