Пример #1
0
 private void SendNotificationToSupportTeam(Task taskInHand)
 {
     emailScenario = EmailScenarioFactory.CreateEmailScenario(taskInHand);
     emailScenario.ExecuteScenario(taskInHand);
     taskInHand.BotStatus = Configurations.SUCCEEDED;
 }
Пример #2
0
        public string GrantSvnAccess(ServiceRequest SnowDataObject)
        {
            _logger.LogInformation("Starting user request processing");

            Task taskInHand = XmlDataMappingToTaskObject(SnowDataObject);
            List <UserRequest> UserRequests = XmlDataMappingToUserRequestObjects(SnowDataObject);

            taskInHand.UserRequests = UserRequests;

            if (TaskExists(taskInHand.TaskId))
            {
                _logger.LogInformation("Task id already exists in DB");

                LoadTaskDataFromDataBase(ref taskInHand);
                LoadUserRequestDataFromDataBase(ref UserRequests, taskInHand.TaskId);
                //taskInHand = loadtask(SnowDataObject.header.transaction_id);

                _logger.LogInformation("Loaded User Tasks from DB");
            }
            else
            {
                _logger.LogInformation(string.Format("New Task with id {0}", taskInHand.TaskId));
                _logger.LogInformation(string.Format("Setting bot status to {0}", Configurations.IN_PROGRESS));

                taskInHand.BotStatus = Configurations.IN_PROGRESS;
                Savetask(taskInHand);
                SaveUserRequests(ref UserRequests);
            }

            if (taskInHand.BotStatus == Configurations.IN_PROGRESS)
            {
                _logger.LogInformation("Going through each user request");

                foreach (var request in UserRequests)
                {
                    _logger.LogInformation(string.Format("Processing user request with id {0}", request.ID));

                    if (request.UserStatus == Configurations.NEW)
                    {
                        string UserExistsResponse = UserExistsOnRepoAsync(request);
                        if (UserExistsResponse == "true")
                        {
                            _logger.LogInformation(string.Format("User already exists on svn repo request ID;{0} ", request.ID));
                            _logger.LogInformation(string.Format("Updating user request to {0} on user request with id :{0}", Configurations.NEW, request.ID));

                            request.UserStatus = Configurations.ALREADY_EXISTS;
                            UpdateUserRequestStatus(request);
                        }

                        else if (UserExistsResponse == "Error")
                        {
                            break;
                        }
                        else
                        {
                            _logger.LogInformation(string.Format("Attempting to add user to svn repo user request id :{0}", request.ID));

                            try
                            {
                                taskInHand.LinuxPassword = GiveUserAccessOnRepo(request); //needs better design
                                request.UserStatus       = Configurations.ADDED_TO_SVN;
                                UpdateUserRequestStatus(request);
                            }
                            catch (Exception)
                            {
                                _logger.LogError(string.Format("Failed to add user to svn repo , user request id :{0}", request.ID));
                            }
                        }
                    }

                    if (request.UserStatus == Configurations.ADDED_TO_SVN)
                    {
                        try
                        {
                            _logger.LogInformation(string.Format("Attempting to add user to excel sheet, user request with id {0}", request.ID));

                            AddToExcelSheet(request);
                            request.UserStatus = Configurations.SUCCEEDED;
                            UpdateUserRequestStatus(request);
                        }
                        catch
                        {
                            _logger.LogError("Failed adding user to excel sheet , user request with id :{0}");
                        }
                    }
                }

                bool checkAllUsersAddedToSvn = true;
                int  UserAlreadyExistsCount  = 0;
                for (int i = 0; i < UserRequests.Count && i < Configurations.maximumAllowedRepos; i++)  // issue here confirm
                {
                    if (UserRequests[i].UserStatus == Configurations.ALREADY_EXISTS)
                    {
                        UserAlreadyExistsCount++;
                    }

                    if (UserRequests[i].UserStatus != Configurations.SUCCEEDED && UserRequests[i].UserStatus != Configurations.ALREADY_EXISTS)
                    {
                        checkAllUsersAddedToSvn = false;
                        IncrementFailedAttempts(taskInHand.TaskId);
                        //SendNotificationToSupportTeam(taskInHand); // not yet implemented
                        break;
                    }
                }

                if (UserAlreadyExistsCount == UserRequests.Count)
                {
                    taskInHand.BotStatus = Configurations.ALREADY_EXISTS;
                }

                if (checkAllUsersAddedToSvn)
                {
                    _logger.LogInformation(string.Format("User requests have been processed , setting task state to {0}", Configurations.SVN_PROCESSED));

                    //this is a bit hacky needs to be properly refactored.
                    if (taskInHand.BotStatus != Configurations.ALREADY_EXISTS)
                    {
                        taskInHand.BotStatus = Configurations.SVN_PROCESSED;
                    }
                }

                if (taskInHand.BotStatus == Configurations.SVN_PROCESSED || taskInHand.BotStatus == Configurations.ALREADY_EXISTS)
                {
                    try
                    {
                        _logger.LogInformation(string.Format("Attempting to send success email to relevant users , task id :{0}", taskInHand.TaskId));
                        emailScenario = EmailScenarioFactory.CreateEmailScenario(taskInHand);
                        emailScenario.ExecuteScenario(taskInHand);
                        taskInHand.BotStatus = Configurations.SUCCEEDED;
                    }
                    catch (Exception)
                    {
                        _logger.LogError(string.Format("Attempting to send success email to relevant users , task id :{0}", taskInHand.TaskId));

                        IncrementFailedAttempts(taskInHand.TaskId);
                    }
                }
            }

            if (taskInHand.BotStatus == Configurations.SUCCEEDED)
            {
                UpdateTaskStatus(taskInHand);
                return("200");
            }

            else
            {
                SendNotificationToSupportTeam(taskInHand);
                UpdateTaskStatus(taskInHand);
                return("500");
            }
        }