From cc6e7bc6cab573936111d5e316b8caa6f367cc96 Mon Sep 17 00:00:00 2001 From: user Date: Fri, 29 May 2020 17:22:21 -0700 Subject: [PATCH] Implement Microsoft Teams Tenants list view --- src/App.js | 2 + src/components/blocks/TableContent.js | 2 +- .../pages/internal/MsTeamsTenantsList.js | 166 ++++++++++++++++++ 3 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 src/components/pages/internal/MsTeamsTenantsList.js diff --git a/src/App.js b/src/App.js index a6844fc..42f05f1 100644 --- a/src/App.js +++ b/src/App.js @@ -12,6 +12,7 @@ import AccountsList from './components/pages/internal/AccountsList'; import ApplicationsList from './components/pages/internal/ApplicationsList'; import SipTrunksList from './components/pages/internal/SipTrunksList'; import PhoneNumbersList from './components/pages/internal/PhoneNumbersList'; +import MsTeamsTenantsList from './components/pages/internal/MsTeamsTenantsList'; import AccountsAddEdit from './components/pages/internal/AccountsAddEdit'; import ApplicationsAddEdit from './components/pages/internal/ApplicationsAddEdit'; import SipTrunksAddEdit from './components/pages/internal/SipTrunksAddEdit'; @@ -44,6 +45,7 @@ function App() { + { //============================================================================= return ( - {contentToDelete && (contentToDelete.name || contentToDelete.number) && ( + {contentToDelete && (contentToDelete.name || contentToDelete.number || contentToDelete.fqdn) && ( { + let history = useHistory(); + const dispatch = useContext(NotificationDispatchContext); + useEffect(() => { + document.title = `Microsoft Teams Tenants | Jambonz | Open Source CPAAS`; + }); + + //============================================================================= + // Get data + //============================================================================= + const getMsTeamsTenants = async () => { + try { + if (!localStorage.getItem('token')) { + history.push('/'); + dispatch({ + type: 'ADD', + level: 'error', + message: 'You must log in to view that page.', + }); + return; + } + const msTeamsTenantsPromise = axios({ + method: 'get', + baseURL: process.env.REACT_APP_API_BASE_URL, + url: '/MicrosoftTeamsTenants', + headers: { + Authorization: `Bearer ${localStorage.getItem('token')}`, + }, + }); + const accountsPromise = axios({ + method: 'get', + baseURL: process.env.REACT_APP_API_BASE_URL, + url: '/Accounts', + headers: { + Authorization: `Bearer ${localStorage.getItem('token')}`, + }, + }); + const applicationsPromise = axios({ + method: 'get', + baseURL: process.env.REACT_APP_API_BASE_URL, + url: '/Applications', + headers: { + Authorization: `Bearer ${localStorage.getItem('token')}`, + }, + }); + const promiseAllValues = await Promise.all([ + msTeamsTenantsPromise, + accountsPromise, + applicationsPromise, + ]); + const msTeamsTenants = promiseAllValues[0].data; + const accounts = promiseAllValues[1].data; + const applications = promiseAllValues[2].data; + + const combinedData = msTeamsTenants.map(team => { + const account = accounts.filter(a => a.account_sid === team.account_sid ); + const application = applications.filter(a => a.application_sid === team.application_sid); + return { + sid: team.ms_teams_tenant_sid, + fqdn: team.tenant_fqdn, + account: account[0] && account[0].name, + application: application[0] && application[0].name, + }; + }); + return(combinedData); + } catch (err) { + if (err.response && err.response.status === 401) { + localStorage.removeItem('token'); + sessionStorage.clear(); + history.push('/'); + dispatch({ + type: 'ADD', + level: 'error', + message: 'Your session has expired. Please log in and try again.', + }); + } else { + dispatch({ + type: 'ADD', + level: 'error', + message: (err.response && err.response.data && err.response.data.msg) || 'Unable to get Microsoft Teams Tenant data', + }); + console.log(err.response || err); + } + } + }; + + //============================================================================= + // Delete Microsoft Teams Tenant + //============================================================================= + const formatTenantsToDelete = team => { + return [ + { name: 'FQDN:', content: team.fqdn || '[none]' }, + { name: 'Account:', content: team.account || '[none]' }, + { name: 'Application:', content: team.application || '[none]' }, + ]; + }; + const deleteTenant = async tenant => { + try { + if (!localStorage.getItem('token')) { + history.push('/'); + dispatch({ + type: 'ADD', + level: 'error', + message: 'You must log in to view that page.', + }); + return; + } + await axios({ + method: 'delete', + baseURL: process.env.REACT_APP_API_BASE_URL, + url: `/MicrosoftTeamsTenants/${tenant.sid}`, + headers: { + Authorization: `Bearer ${localStorage.getItem('token')}`, + }, + }); + return 'success'; + } catch (err) { + if (err.response && err.response.status === 401) { + localStorage.removeItem('token'); + sessionStorage.clear(); + history.push('/'); + dispatch({ + type: 'ADD', + level: 'error', + message: 'Your session has expired. Please log in and try again.', + }); + } else { + console.log(err.response || err); + return ((err.response && err.response.data && err.response.data.msg) || 'Unable to delete Microsoft Teams Tenant'); + } + } + }; + + //============================================================================= + // Render + //============================================================================= + return ( + + + + ); +}; + +export default MsTeamsTenantsList;