diff --git a/CoVAR-app/src/app/(pages)/dashboard/page.tsx b/CoVAR-app/src/app/(pages)/dashboard/page.tsx index b8ae5ba..d9e131a 100644 --- a/CoVAR-app/src/app/(pages)/dashboard/page.tsx +++ b/CoVAR-app/src/app/(pages)/dashboard/page.tsx @@ -7,7 +7,7 @@ import SeverityDistribution from './components/severityDistribution'; import VulnerabilityLineChart from './components/lineChart'; import ReportsList from './components/reportsList'; import TopVulnerabilities from './components/topVulnerabilities'; -import { fetchLastReportDates, getAllReports, getUserRole, fetchAssignedClients } from '@/functions/requests'; +import { fetchLastReportDates, getAllReports, getUserRole, fetchClientsAssigned, fetchOrganisationsAssigned } from '@/functions/requests'; import { useRouter } from 'next/navigation'; import axios from 'axios'; import { evaluateLaunchStyles } from '../../../styles/evaluateStyle'; @@ -118,14 +118,19 @@ const Dashboard: React.FC = () => { const fetchAssignedUsersAndOrgs = async () => { try { const token = localStorage.getItem('accessToken'); - const [usersResponse, orgsResponse, reportDates] = await Promise.all([ - axios.get('/api/users/assigned_clients', { headers: { Authorization: `Bearer ${token}` } }), - axios.get('/api/users/assigned_organizations', { headers: { Authorization: `Bearer ${token}` } }), + const userId = userID; + console.log(userId); + const [clients, orgs, reportDates] = await Promise.all([ + fetchClientsAssigned(token as string), + fetchOrganisationsAssigned(token as string), fetchLastReportDates(token as string) ]); - - setUsers(usersResponse.data); - setOrganizations(orgsResponse.data); + console.log(clients); + console.log(orgs); + console.log('Report Dates:', reportDates); + + setUsers(clients); + setOrganizations(orgs); setLastReportDatesClients(reportDates.clients); setLastReportDatesOrgs(reportDates.organizations); setLoading(false); @@ -134,6 +139,8 @@ const Dashboard: React.FC = () => { setLoading(false); } }; + + const calculateSeverityDistribution = (reports: VulnerabilityReport[]) => { const distribution: { [key: string]: number } = { @@ -181,6 +188,26 @@ const Dashboard: React.FC = () => { router.push(`/evaluate/organization/${organization.name}`); }; + const formatDate = (dateString: string) => { + if (!dateString) return 'No report'; + const date = new Date(dateString); + + const formattedDate = date.toLocaleDateString('en-GB', { + day: '2-digit', + month: '2-digit', + year: 'numeric' + }); + + const formattedTime = date.toLocaleTimeString('en-GB', { + hour: '2-digit', + minute: '2-digit', + second: '2-digit' + }); + + return `${formattedDate} ${formattedTime}`; + }; + + const getReportsPerClient = async () => { try { const data: ClientReport[] = await fetchReportsPerClient(); @@ -277,34 +304,34 @@ const Dashboard: React.FC = () => { {users.length === 0 && organizations.length === 0 ? ( No assigned clients or organisations found. ) : ( - - {users.map((user) => ( - - c.client_name === user.username)?.last_report_date || 'No report'}`} - /> - - - - - ))} - {organizations.map((org) => ( - - o.organization_name === org.name)?.last_report_date || 'No report'}`} - /> - - - - - ))} - + + {users.map((user) => ( + + c.client_name === user.username)?.last_report_date as string) || 'No report'}`} + /> + + + + + ))} + {organizations.map((org) => ( + + o.organization_name === org.name)?.last_report_date as string) || 'No report' }`} + /> + + + + + ))} + )} diff --git a/CoVAR-app/src/functions/requests.tsx b/CoVAR-app/src/functions/requests.tsx index a79956b..9ef9644 100644 --- a/CoVAR-app/src/functions/requests.tsx +++ b/CoVAR-app/src/functions/requests.tsx @@ -167,6 +167,8 @@ export const fetchAssignedClients = async (userId: string, accessToken: string) return await handleRequest(request); }; + + export const fetchAssignedOrganisations = async (userId: string, accessToken: string) => { const request = { method: 'get', @@ -177,17 +179,30 @@ export const fetchAssignedOrganisations = async (userId: string, accessToken: st }; export const fetchLastReportDates = async (token: string) => { - try { - const response = await axios.get('/api/reports/last_report_dates', { - headers: { - Authorization: `Bearer ${token}`, - }, - }); - return response.data; - } catch (error) { - console.error('Error fetching last report dates:', error); - throw error; - } + const request = { + method: 'get', + url: '/api/reports/last_report_dates', + headers: { Authorization: `Bearer ${token}` }, + }; + return await handleRequest(request); +}; + +export const fetchClientsAssigned = async (token: string) => { + const request = { + method: 'get', + url: '/api/users/assigned_clients', + headers: { Authorization: `Bearer ${token}` }, + }; + return await handleRequest(request); +}; + +export const fetchOrganisationsAssigned = async (token: string) => { + const request = { + method: 'get', + url: '/api/users/assigned_organizations', + headers: { Authorization: `Bearer ${token}` }, + }; + return await handleRequest(request); }; export const assignClient = async (userId: string, clientUsername: string, accessToken: string) => { diff --git a/server/routes/dashboard.js b/server/routes/dashboard.js index 1b70bae..d06fc3d 100644 --- a/server/routes/dashboard.js +++ b/server/routes/dashboard.js @@ -20,7 +20,6 @@ router.get('/reports/all', authenticateToken, async (req, res) => { // Route to get the last report date for each client or organization router.get('/reports/last_report_dates', authenticateToken, async (req, res) => { - try { // Query to get last report date for assigned clients const clientReports = await pgClient.query( @@ -28,7 +27,7 @@ router.get('/reports/last_report_dates', authenticateToken, async (req, res) => FROM users u JOIN user_reports ur ON u.user_id = ur.user_id JOIN reports r ON ur.report_id = r.report_id - WHERE u.role = 'client' + WHERE u.role = 'client' OR u.role = 'admin' GROUP BY u.username` ); @@ -46,6 +45,10 @@ router.get('/reports/last_report_dates', authenticateToken, async (req, res) => organizations: organizationReports.rows }; + console.log('Client Reports:', clientReports.rows); + console.log('Organization Reports:', organizationReports.rows); + + res.send(result); } catch (err) { console.error('Error fetching last report dates:', err); diff --git a/server/routes/users.js b/server/routes/users.js index 7b78036..7601d2d 100644 --- a/server/routes/users.js +++ b/server/routes/users.js @@ -349,6 +349,26 @@ router.get('/users/assigned_clients', authenticateToken, async (req, res) => { } }); +// Get all organizations assigned to the logged-in VA +router.get('/users/assigned_organizations', authenticateToken, async (req, res) => { + const token = req.headers['authorization'].split(' ')[1]; + const decodedToken = verifyToken(token); + const id = decodedToken.user_id; + + try { + const organizations = await pgClient.query( + `SELECT * FROM organizations + WHERE organization_id IN (SELECT organization FROM assignment WHERE va = $1)`, + [id] + ); + res.send(organizations.rows); + } catch (err) { + console.error(err.message); + res.status(500).send('Server Error'); + } +}); + + // Check if email exists in the database router.post('/users/checkEmailExists', async (req, res) => { const { email } = req.body;