public async Task <RequestWorkflowDefinitionMapping> Update(RequestWorkflowDefinitionMapping RequestWorkflowDefinitionMapping) { RequestWorkflowDefinitionMappingDAO RequestWorkflowDefinitionMappingDAO = await DataContext.RequestWorkflowDefinitionMapping .Where(q => q.RequestId == RequestWorkflowDefinitionMapping.RequestId).FirstOrDefaultAsync(); if (RequestWorkflowDefinitionMappingDAO == null) { RequestWorkflowDefinitionMappingDAO = new RequestWorkflowDefinitionMappingDAO { RequestId = RequestWorkflowDefinitionMapping.RequestId, RequestStateId = RequestWorkflowDefinitionMapping.RequestStateId, WorkflowDefinitionId = RequestWorkflowDefinitionMapping.WorkflowDefinitionId, }; DataContext.RequestWorkflowDefinitionMapping.Add(RequestWorkflowDefinitionMappingDAO); } else { RequestWorkflowDefinitionMappingDAO.RequestStateId = RequestWorkflowDefinitionMapping.RequestStateId; RequestWorkflowDefinitionMappingDAO.WorkflowDefinitionId = RequestWorkflowDefinitionMapping.WorkflowDefinitionId; } await DataContext.SaveChangesAsync(); RequestWorkflowDefinitionMapping.RequestId = RequestWorkflowDefinitionMappingDAO.RequestId; return(RequestWorkflowDefinitionMapping); }
private async Task <bool> RejectStep(Guid RequestId, Dictionary <string, string> Parameters) { RequestWorkflowDefinitionMapping RequestWorkflowDefinitionMapping = await UOW.RequestWorkflowDefinitionMappingRepository.Get(RequestId); WorkflowDefinition WorkflowDefinition = await UOW.WorkflowDefinitionRepository.Get(RequestWorkflowDefinitionMapping.WorkflowDefinitionId); List <RequestWorkflowStepMapping> RequestWorkflowStepMappings = await UOW.RequestWorkflowStepMappingRepository.List(RequestId); if (WorkflowDefinition != null && RequestWorkflowStepMappings.Count > 0) { List <WorkflowStep> ToSteps = new List <WorkflowStep>(); List <Mail> Mails = new List <Mail>(); foreach (WorkflowStep WorkflowStep in WorkflowDefinition.WorkflowSteps) { if (CurrentContext.RoleIds.Contains(WorkflowStep.RoleId)) { var StartRequestStep = RequestWorkflowStepMappings .Where(x => x.WorkflowStateId == WorkflowStateEnum.PENDING.Id) .Where(x => x.WorkflowStepId == WorkflowStep.Id).FirstOrDefault(); if (StartRequestStep == null) { continue; } StartRequestStep.WorkflowStateId = WorkflowStateEnum.REJECTED.Id; StartRequestStep.UpdatedAt = StaticParams.DateTimeNow; StartRequestStep.AppUserId = CurrentContext.UserId; await UOW.RequestWorkflowStepMappingRepository.BulkMerge(RequestId, RequestWorkflowStepMappings); AppUserFilter AppUserFilter = new AppUserFilter { Id = new IdFilter { Equal = RequestWorkflowDefinitionMapping.CreatorId }, Skip = 0, Take = int.MaxValue, Selects = AppUserSelect.Email }; List <AppUser> appUsers = await UOW.AppUserRepository.List(AppUserFilter); List <string> recipients = appUsers.Select(au => au.Email).Distinct().ToList(); Mail MailForReject = new Mail { Recipients = recipients, Subject = CreateMailContent(WorkflowStep.SubjectMailForReject, Parameters), Body = CreateMailContent(WorkflowStep.BodyMailForReject, Parameters), RowId = Guid.NewGuid() }; Mails.Add(MailForReject); return(true); } } List <EventMessage <Mail> > messages = Mails.Select(m => new EventMessage <Mail>(m, m.RowId)).ToList(); RabbitManager.PublishList(messages, RoutingKeyEnum.MailSend); } return(false); }
public async Task <RequestState> GetRequestState(Guid RequestId) { RequestWorkflowDefinitionMapping RequestWorkflowDefinitionMapping = await UOW.RequestWorkflowDefinitionMappingRepository.Get(RequestId); if (RequestWorkflowDefinitionMapping == null) { return(null); } return(RequestWorkflowDefinitionMapping.RequestState); }
private async Task <bool> IsStartedStep(Guid RequestId) { RequestWorkflowDefinitionMapping RequestWorkflowDefinitionMapping = await UOW.RequestWorkflowDefinitionMappingRepository.Get(RequestId); if (RequestWorkflowDefinitionMapping == null) { return(false); } if (RequestWorkflowDefinitionMapping.RequestStateId == RequestStateEnum.APPROVING.Id) { return(true); } return(false); }
private async Task <bool> IsInitializedStep(Guid RequestId) { RequestWorkflowDefinitionMapping RequestWorkflowDefinitionMapping = await UOW.RequestWorkflowDefinitionMappingRepository.Get(RequestId); if (RequestWorkflowDefinitionMapping == null) { return(false); } else { if (RequestWorkflowDefinitionMapping.RequestStateId == RequestStateEnum.REJECTED.Id) { return(false); } return(true); } }
private async Task <bool> StartStep(Guid RequestId, Dictionary <string, string> Parameters) { RequestWorkflowDefinitionMapping RequestWorkflowDefinitionMapping = await UOW.RequestWorkflowDefinitionMappingRepository.Get(RequestId); WorkflowDefinition WorkflowDefinition = await UOW.WorkflowDefinitionRepository.Get(RequestWorkflowDefinitionMapping.WorkflowDefinitionId); if (WorkflowDefinition == null) { return(false); } else { // khởi tạo workflow RequestWorkflowDefinitionMapping = new RequestWorkflowDefinitionMapping { RequestId = RequestId, WorkflowDefinitionId = WorkflowDefinition.Id, RequestStateId = RequestStateEnum.APPROVING.Id, CreatorId = CurrentContext.UserId, }; await UOW.RequestWorkflowDefinitionMappingRepository.Update(RequestWorkflowDefinitionMapping); List <RequestWorkflowStepMapping> RequestWorkflowStepMappings = await UOW.RequestWorkflowStepMappingRepository.List(RequestId); List <WorkflowStep> Starts = new List <WorkflowStep>(); // tìm điểm bắt đầu của workflow // nút không có ai trỏ đến là nút bắt đầu // trong trường hợp có nhiều nút bắt đầu thì xét có nút nào thuộc các role hiện tại không foreach (WorkflowStep WorkflowStep in WorkflowDefinition.WorkflowSteps) { if (!WorkflowDefinition.WorkflowDirections.Any(d => d.ToStepId == WorkflowStep.Id) && CurrentContext.RoleIds.Contains(WorkflowStep.RoleId)) { Starts.Add(WorkflowStep); RequestWorkflowStepMapping RequestWorkflowStepMapping = RequestWorkflowStepMappings.Where(r => r.WorkflowStepId == WorkflowStep.Id).FirstOrDefault(); RequestWorkflowStepMapping.WorkflowStateId = WorkflowStateEnum.PENDING.Id; } } await UOW.RequestWorkflowStepMappingRepository.BulkMerge(RequestId, RequestWorkflowStepMappings); await UpdateParameters(RequestId, WorkflowDefinition, Parameters); } return(true); }
public async Task <RequestWorkflowDefinitionMapping> Get(Guid RequestId) { RequestWorkflowDefinitionMapping RequestWorkflowDefinitionMapping = await DataContext.RequestWorkflowDefinitionMapping.AsNoTracking() .Where(q => q.RequestId == RequestId) .Select(q => new RequestWorkflowDefinitionMapping { RequestId = q.RequestId, RequestStateId = q.RequestStateId, WorkflowDefinitionId = q.WorkflowDefinitionId, RequestState = new RequestState { Id = q.RequestState.Id, Code = q.RequestState.Code, Name = q.RequestState.Name, }, }).FirstOrDefaultAsync(); return(RequestWorkflowDefinitionMapping); }
private async Task <GenericEnum> EndStep(Guid RequestId, Dictionary <string, string> Parameters) { RequestWorkflowDefinitionMapping RequestWorkflowDefinitionMapping = await UOW.RequestWorkflowDefinitionMappingRepository.Get(RequestId); if (RequestWorkflowDefinitionMapping == null) { return(null); } List <RequestWorkflowStepMapping> RequestWorkflowStepMapping = await UOW.RequestWorkflowStepMappingRepository.List(RequestId); GenericEnum RequestState = RequestStateEnum.APPROVING; if (RequestWorkflowStepMapping != null) { if (RequestWorkflowStepMapping.Any(x => x.WorkflowStateId == WorkflowStateEnum.REJECTED.Id)) { RequestState = RequestStateEnum.REJECTED; } else if (RequestWorkflowStepMapping.All(x => x.WorkflowStateId == WorkflowStateEnum.APPROVED.Id)) { RequestState = RequestStateEnum.APPROVED; } } if (RequestState == null) { return(null); } RequestWorkflowDefinitionMapping = new RequestWorkflowDefinitionMapping { RequestId = RequestId, WorkflowDefinitionId = RequestWorkflowDefinitionMapping.WorkflowDefinitionId, RequestStateId = RequestState.Id, }; await UOW.RequestWorkflowDefinitionMappingRepository.Update(RequestWorkflowDefinitionMapping); return(RequestState); }
public async Task <bool> Initialize(Guid RequestId, long WorkflowTypeId, Dictionary <string, string> Parameters) { List <WorkflowDefinition> WorkflowDefinitions = await UOW.WorkflowDefinitionRepository.List(new WorkflowDefinitionFilter { StatusId = new IdFilter { Equal = StatusEnum.ACTIVE.Id }, WorkflowTypeId = new IdFilter { Equal = WorkflowTypeId }, Selects = WorkflowDefinitionSelect.Id, }); WorkflowDefinition WorkflowDefinition = WorkflowDefinitions.FirstOrDefault(); if (WorkflowDefinition == null) { return(false); } WorkflowDefinition = await UOW.WorkflowDefinitionRepository.Get(WorkflowDefinition.Id); if (WorkflowDefinition.WorkflowSteps == null) { bool ShouldInit = false; foreach (WorkflowStep WorkflowStep in WorkflowDefinition.WorkflowSteps) { if (!WorkflowDefinition.WorkflowDirections.Any(d => d.ToStepId == WorkflowStep.Id)) { if (CurrentContext.RoleIds.Contains(WorkflowStep.RoleId)) { ShouldInit = true; } } } if (ShouldInit == false) { return(false); } } await UOW.RequestWorkflowDefinitionMappingRepository.Delete(RequestId); // khởi tạo workflow RequestWorkflowDefinitionMapping RequestWorkflowDefinitionMapping = new RequestWorkflowDefinitionMapping { RequestId = RequestId, WorkflowDefinitionId = WorkflowDefinition.Id, RequestStateId = RequestStateEnum.NEW.Id, CreatorId = CurrentContext.UserId, }; await UOW.RequestWorkflowDefinitionMappingRepository.Update(RequestWorkflowDefinitionMapping); // khởi tạo trạng thái cho tất cả các nút List <RequestWorkflowStepMapping> RequestWorkflowStepMappings = new List <RequestWorkflowStepMapping>(); foreach (WorkflowStep WorkflowStep in WorkflowDefinition.WorkflowSteps) { RequestWorkflowStepMapping RequestWorkflowStepMapping = new RequestWorkflowStepMapping(); RequestWorkflowStepMappings.Add(RequestWorkflowStepMapping); RequestWorkflowStepMapping.RequestId = RequestId; RequestWorkflowStepMapping.WorkflowStepId = WorkflowStep.Id; RequestWorkflowStepMapping.WorkflowStateId = WorkflowStateEnum.NEW.Id; RequestWorkflowStepMapping.CreatedAt = StaticParams.DateTimeNow; RequestWorkflowStepMapping.UpdatedAt = null; RequestWorkflowStepMapping.AppUserId = null; } await UOW.RequestWorkflowStepMappingRepository.BulkMerge(RequestId, RequestWorkflowStepMappings); await UpdateParameters(RequestId, WorkflowDefinition, Parameters); return(true); }
private async Task <bool> ApproveStep(Guid RequestId, Dictionary <string, string> Parameters) { RequestWorkflowDefinitionMapping RequestWorkflowDefinitionMapping = await UOW.RequestWorkflowDefinitionMappingRepository.Get(RequestId); WorkflowDefinition WorkflowDefinition = await UOW.WorkflowDefinitionRepository.Get(RequestWorkflowDefinitionMapping.WorkflowDefinitionId); // tìm điểm bắt đầu // tìm điểm nhảy tiếp theo // chuyển trạng thái điểm nhảy // gửi mail cho các điểm nhảy có trạng thái thay đổi. List <RequestWorkflowStepMapping> RequestWorkflowStepMappings = await UOW.RequestWorkflowStepMappingRepository.List(RequestId); if (WorkflowDefinition != null && RequestWorkflowStepMappings.Count > 0) { // Tìm điểm đích List <WorkflowStep> ToSteps = new List <WorkflowStep>(); foreach (WorkflowStep WorkflowStep in WorkflowDefinition.WorkflowSteps) { if (CurrentContext.RoleIds.Contains(WorkflowStep.RoleId)) { var StartRequestStep = RequestWorkflowStepMappings .Where(x => x.WorkflowStateId == WorkflowStateEnum.PENDING.Id) .Where(x => x.WorkflowStepId == WorkflowStep.Id).FirstOrDefault(); if (StartRequestStep != null) { StartRequestStep.WorkflowStateId = WorkflowStateEnum.APPROVED.Id; StartRequestStep.UpdatedAt = StaticParams.DateTimeNow; StartRequestStep.AppUserId = CurrentContext.UserId; var NextSteps = WorkflowDefinition.WorkflowDirections.Where(d => d.FromStepId == WorkflowStep.Id) .Select(x => x.ToStep) ?? new List <WorkflowStep>(); ToSteps.AddRange(NextSteps); } } } ToSteps = ToSteps.Distinct().ToList(); List <Mail> Mails = new List <Mail>(); var NextStepIds = new List <long>(); var PreviousStepIds = new List <long>(); // tìm điểm bắt đầu cho những điểm đích tìm được foreach (WorkflowStep WorkflowStep in ToSteps) { var FromSteps = WorkflowDefinition.WorkflowDirections.Where(d => d.ToStepId == WorkflowStep.Id).Select(x => x.FromStep) ?? new List <WorkflowStep>(); var ApprovedFromRequestSteps = new List <RequestWorkflowStepMapping>(); foreach (var FromStep in FromSteps) { var FromRequestStep = RequestWorkflowStepMappings.Where(x => x.WorkflowStepId == FromStep.Id).FirstOrDefault(); ApprovedFromRequestSteps.Add(FromRequestStep); PreviousStepIds.Add(FromStep.Id); } // Nếu tất cả các điểm bắt đầu của điểm đích đang xét đều là APPROVED thì điểm đích sẽ là PENDING if (ApprovedFromRequestSteps.All(x => x.WorkflowStateId == WorkflowStateEnum.APPROVED.Id)) { var RequestWorkflowStepMapping = RequestWorkflowStepMappings.Where(x => x.WorkflowStepId == WorkflowStep.Id).FirstOrDefault(); RequestWorkflowStepMapping.WorkflowStateId = WorkflowStateEnum.PENDING.Id; RequestWorkflowStepMapping.UpdatedAt = null; RequestWorkflowStepMapping.AppUserId = null; NextStepIds.Add(WorkflowStep.Id); } } await UOW.RequestWorkflowStepMappingRepository.BulkMerge(RequestId, RequestWorkflowStepMappings); // Gửi mail cho những direction mà điểm nguồn và đích được tìm ra bên trên List <WorkflowDirection> WorkflowDirections = WorkflowDefinition.WorkflowDirections .Where(x => PreviousStepIds.Contains(x.FromStepId) && NextStepIds.Contains(x.ToStepId)).ToList(); foreach (var WorkflowDirection in WorkflowDirections) { //gửi mail AppUserFilter AppUserFilter = new AppUserFilter { RoleId = new IdFilter { Equal = WorkflowDirection.ToStep.RoleId }, Skip = 0, Take = int.MaxValue, Selects = AppUserSelect.Email }; List <AppUser> appUsers = await UOW.AppUserRepository.List(AppUserFilter); List <string> recipients = appUsers.Select(au => au.Email).Distinct().ToList(); Mail MailForCreator = new Mail { Recipients = recipients, Subject = CreateMailContent(WorkflowDirection.SubjectMailForCreator, Parameters), Body = CreateMailContent(WorkflowDirection.BodyMailForCreator, Parameters), RowId = Guid.NewGuid(), }; Mail MailForNextStep = new Mail { Recipients = recipients, Subject = CreateMailContent(WorkflowDirection.SubjectMailForNextStep, Parameters), Body = CreateMailContent(WorkflowDirection.BodyMailForNextStep, Parameters), RowId = Guid.NewGuid(), }; Mails.Add(MailForCreator); Mails.Add(MailForNextStep); } Mails = Mails.Distinct().ToList(); List <EventMessage <Mail> > messages = Mails.Select(m => new EventMessage <Mail>(m, m.RowId)).ToList(); RabbitManager.PublishList(messages, RoutingKeyEnum.MailSend); return(true); } return(false); }