import React, { useEffect } from "react";
import { makeStyles } from "@mui/styles";
import theme from '../theme.jsx';
import { toast } from "react-toastify"
import AuthenticationData from "../components/AuthenticationWindow.jsx"
import {
Chip,
Modal,
Button,
Typography,
FormControl,
TextField,
Dialog,
DialogTitle,
DialogContent,
DialogActions,
Select,
MenuItem,
Box,
Divider,
InputLabel,
Stack,
} from '@mui/material';
const useStyles = makeStyles({
notchedOutline: {
borderColor: "#f85a3e !important",
},
})
const SubflowSuggestions = (props) => {
const {
type,
globalUrl,
workflows,
notificationWorkflow,
selectedOrganization,
} = props
const classes = useStyles();
const [notificationWorkflowModal, setNotificationWorkflowModal] = React.useState(false);
const [selectedAppDetails, setSelectedAppDetails] = React.useState({});
const [notificationWorkflowTestModal, setNotificationWorkflowTestModal] = React.useState(false);
const [selectedAuth, setSelectedAuth] = React.useState('');
const [emailData,setEmailData] = React.useState([]);
const [notificationAppDetails, setNotificationAppDetails] = React.useState([]);
const [generatedWorkflow, setGeneatedWorkflow] = React.useState({});
const [textFieldValue, setTextFieldValue] = React.useState("");
const [textFieldOneValue, setTextFieldOneValue] = React.useState("");
// getting comms & cases app from app framework
var notificationAppList = [];
if (selectedOrganization.security_framework.cases && selectedOrganization.security_framework.cases.name.length > 0) {
notificationAppList = notificationAppList.concat(selectedOrganization.security_framework.cases);
}
if (selectedOrganization.security_framework.communication && selectedOrganization.security_framework.communication.name.length > 0) {
notificationAppList = notificationAppList.concat(selectedOrganization.security_framework.communication);
}
useEffect(() => {
let nameList = notificationAppList.length > 0? notificationAppList.map(item => item.name): ["email"];
prepareNotificationAppList(nameList)
}, [notificationAppList,workflows]);
const executeTestWorkflow = (workflowid) => {
const data = { "execution_argument": '{"title":"THIS IS TEST ALERT","description":"TEST ALERT FROM SHUFFLE","reference_url": "shuffler.io"}' }
fetch(globalUrl + `/api/v1/workflows/${workflowid}/execute`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(data),
credentials: "include",
})
.then((response) => {
if (response.status !== 200) {
toast("Failed setting notification workflow: ", response.reason);
console.log("Status not 200 for workflows :O!");
return;
}
toast("Notification workflow ran successfully");
return response.json();
}).catch((error) => {
console.log("Error getting workflows: " + error);
})
}
const testWorkflowModal = notificationWorkflowTestModal ?
: null
// fixxxxxxxxxxxxxxxxxxxx
const checkIfAlreadyGenerated = (appList, workflows) => {
var workflowName = workflows.find(workflow => workflow.id === notificationWorkflow)
if (workflowName) {
workflowName = workflowName.name
}
else {
console.log("no workflow set")
return
}
if (workflowName) {
const parts = workflowName.split(' ');
console.log("parts", parts)
if (parts[0].toString() === "[GENERATED]" && parts.length > 1) {
console.log("parts1", parts[1])
if ((appList.includes(parts[1]))) {
console.log("workflow already generated")
setGeneatedWorkflow({"app_name": parts[1]})
}
}
}
else {
return
}
}
const mergeAuthData = (result, responseJson) => {
const updatedResult = result.map(item => {
const matches = responseJson.filter(authItem => authItem.app.name === item.name);
return {
...item,
authentication_data: matches.length > 0 ? matches : null
}
})
return updatedResult
}
const prepareNotificationAppList = (appList) => {
var result = []
fetch(globalUrl + "/api/v1/apps", {
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
credentials: "include",
}).then((response) => {
if (response.status !== 200) {
toast("Failed getting app ids: ", response.reason);
console.log("Status not 200 for app ids :O!");
return;
}
return response.json();
}).then((responseJson) => {
if (responseJson !== undefined) {
const filteredApps = responseJson.filter(app => appList.includes(app.name));
const emailData = responseJson.filter(app => app.name === "email")
setEmailData(emailData)
const appDetails = filteredApps.map(app => ({ name: app.name, id: app.id })); //mapped apps with IDs as sometime Ids were not correct in security framework
// console.log("appDetails: ", appDetails)
// result = appDetails
fetch(globalUrl + "/api/v1/apps/authentication", {
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
credentials: "include",
})
.then((response) => {
if (response.status !== 200) {
toast(`Failed getting auth for : `, response.reason);
console.log("Status not 200 for app auth :O!");
return;
}
return response.json();
}).then((responseJson) => {
if (!responseJson.success) {
console.log("Could not get app auth")
return;
}
// console.log("responseJson of auth: ", responseJson.data)
result = mergeAuthData(appDetails, responseJson.data)
// console.log("merged auth data: ", result)
// console.log("result", result)
result.map(item => {
fetch(globalUrl + `/api/v1/apps/${item.id}/config`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
credentials: "include",
}).then((response) => {
if (response.status !== 200) {
toast(`Failed getting config for ${item.id}: `, response.reason);
console.log("Status not 200 for app config :O!");
return;
}
return response.json();
}).then((responseJson) => {
if (!responseJson.success) {
console.log("Could not get app config")
return;
}
var decodedString = JSON.parse(atob(responseJson.app));
// console.log("dcodedString: ",decodedString)
item.auth_config = decodedString.authentication
item.large_image = decodedString.large_image
setNotificationAppDetails(result)
console.log("notificationAppDetails: ", notificationAppDetails)
}).then(()=>{
checkIfAlreadyGenerated(appList,workflows);
}).catch((error) => {
console.log("Error getting app config: " + error);
toast("Error getting app config: " + error);
})
})
})
}
}).catch((error) => {
console.log("Error getting app ids: " + error);
})
}
const renderChips = (apps) => {
return (
}
/>
))}