Skip to content

Commit 72b920b

Browse files
authored
Chore/Refactor login activity component (#5679)
chore(audit): refactor
1 parent 06b8126 commit 72b920b

6 files changed

Lines changed: 25 additions & 189 deletions

File tree

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NextFunction, Request, Response } from 'express'
2-
import auditService from '../../services/audit'
3-
import { InternalFlowiseError } from '../../../errors/internalFlowiseError'
42
import { StatusCodes } from 'http-status-codes'
3+
import { InternalFlowiseError } from '../../../errors/internalFlowiseError'
4+
import auditService from '../../services/audit'
55

66
const fetchLoginActivity = async (req: Request, res: Response, next: NextFunction) => {
77
try {
@@ -15,19 +15,6 @@ const fetchLoginActivity = async (req: Request, res: Response, next: NextFunctio
1515
}
1616
}
1717

18-
const deleteLoginActivity = async (req: Request, res: Response, next: NextFunction) => {
19-
try {
20-
if (typeof req.body === 'undefined') {
21-
throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, `Error: auditService.deleteLoginHistory - body not provided!`)
22-
}
23-
const apiResponse = await auditService.deleteLoginActivity(req.body)
24-
return res.json(apiResponse)
25-
} catch (error) {
26-
next(error)
27-
}
28-
}
29-
3018
export default {
31-
fetchLoginActivity,
32-
deleteLoginActivity
19+
fetchLoginActivity
3320
}

packages/server/src/enterprise/rbac/Permissions.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ export class Permissions {
138138

139139
const loginActivityCategory = new PermissionCategory('loginActivity')
140140
loginActivityCategory.addPermission(new Permission('loginActivity:view', 'View Login Activity', false, true, false))
141-
loginActivityCategory.addPermission(new Permission('loginActivity:delete', 'Delete Login Activity', false, true, false))
142141
this.categories.push(loginActivityCategory)
143142
}
144143

packages/server/src/enterprise/routes/audit/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ import { checkPermission } from '../../rbac/PermissionCheck'
44
const router = express.Router()
55

66
router.post(['/', '/login-activity'], checkPermission('loginActivity:view'), auditController.fetchLoginActivity)
7-
router.post(['/', '/login-activity/delete'], checkPermission('loginActivity:delete'), auditController.deleteLoginActivity)
87

98
export default router

packages/server/src/enterprise/services/audit/index.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { getRunningExpressApp } from '../../../utils/getRunningExpressApp'
2-
import { LoginActivity } from '../../database/entities/EnterpriseEntities'
3-
import { InternalFlowiseError } from '../../../errors/internalFlowiseError'
41
import { StatusCodes } from 'http-status-codes'
5-
import { getErrorMessage } from '../../../errors/utils'
62
import { Between, In } from 'typeorm'
7-
import { LoginActivityCode } from '../../Interface.Enterprise'
3+
import { InternalFlowiseError } from '../../../errors/internalFlowiseError'
4+
import { getErrorMessage } from '../../../errors/utils'
85
import { Platform } from '../../../Interface'
6+
import { getRunningExpressApp } from '../../../utils/getRunningExpressApp'
7+
import { LoginActivity } from '../../database/entities/EnterpriseEntities'
8+
import { LoginActivityCode } from '../../Interface.Enterprise'
99

1010
const PAGE_SIZE = 10
1111

@@ -89,21 +89,7 @@ const recordLoginActivity = async (username: string, activityCode: LoginActivity
8989
}
9090
}
9191

92-
const deleteLoginActivity = async (body: any) => {
93-
try {
94-
const appServer = getRunningExpressApp()
95-
96-
await appServer.AppDataSource.getRepository(LoginActivity).delete({
97-
id: In(body.selected)
98-
})
99-
return 'OK'
100-
} catch (error) {
101-
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: authService.loginActivity - ${getErrorMessage(error)}`)
102-
}
103-
}
104-
10592
export default {
10693
recordLoginActivity,
107-
deleteLoginActivity,
10894
fetchLoginActivity
10995
}

packages/ui/src/api/audit.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import client from './client'
22

33
const fetchLoginActivity = (body) => client.post(`/audit/login-activity`, body)
4-
const deleteLoginActivity = (body) => client.post(`/audit/login-activity/delete`, body)
54

65
export default {
7-
fetchLoginActivity,
8-
deleteLoginActivity
6+
fetchLoginActivity
97
}

packages/ui/src/views/auth/loginActivity.jsx

Lines changed: 16 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,35 @@
1-
import { forwardRef, useEffect, useState } from 'react'
2-
import { useDispatch, useSelector } from 'react-redux'
31
import moment from 'moment/moment'
42
import PropTypes from 'prop-types'
3+
import { forwardRef, useEffect, useState } from 'react'
4+
import { useSelector } from 'react-redux'
55

66
// material-ui
77
import {
88
Box,
9+
Checkbox,
10+
FormControl,
11+
IconButton,
12+
InputLabel,
13+
ListItemButton,
14+
ListItemText,
15+
MenuItem,
16+
OutlinedInput,
17+
Paper,
18+
Select,
919
Skeleton,
1020
Stack,
1121
Table,
1222
TableBody,
1323
TableContainer,
1424
TableHead,
1525
TableRow,
16-
Paper,
17-
IconButton,
18-
useTheme,
19-
Checkbox,
20-
Button,
21-
OutlinedInput,
22-
MenuItem,
23-
Select,
24-
InputLabel,
25-
FormControl,
26-
ListItemText,
27-
ListItemButton
26+
useTheme
2827
} from '@mui/material'
2928

3029
// project imports
31-
import MainCard from '@/ui-component/cards/MainCard'
32-
import ConfirmDialog from '@/ui-component/dialog/ConfirmDialog'
33-
import ViewHeader from '@/layout/MainLayout/ViewHeader'
3430
import ErrorBoundary from '@/ErrorBoundary'
31+
import ViewHeader from '@/layout/MainLayout/ViewHeader'
32+
import MainCard from '@/ui-component/cards/MainCard'
3533
import { StyledTableCell, StyledTableRow } from '@/ui-component/table/TableStyles'
3634
import DatePicker from 'react-datepicker'
3735
import 'react-datepicker/dist/react-datepicker.css'
@@ -41,18 +39,15 @@ import auditApi from '@/api/audit'
4139

4240
// Hooks
4341
import useApi from '@/hooks/useApi'
44-
import useConfirm from '@/hooks/useConfirm'
4542

4643
// utils
4744
import useNotifier from '@/utils/useNotifier'
4845

4946
// Icons
50-
import { IconCircleX, IconChevronLeft, IconChevronRight, IconTrash, IconX, IconLogin, IconLogout } from '@tabler/icons-react'
47+
import { IconChevronLeft, IconChevronRight, IconCircleX, IconLogin, IconLogout } from '@tabler/icons-react'
5148

5249
// store
5350
import { useError } from '@/store/context/ErrorContext'
54-
import { enqueueSnackbar as enqueueSnackbarAction, closeSnackbar as closeSnackbarAction } from '@/store/actions'
55-
import { PermissionButton } from '@/ui-component/button/RBACButtons'
5651

5752
const activityTypes = [
5853
'Login Success',
@@ -93,17 +88,11 @@ DatePickerCustomInput.propTypes = {
9388
const LoginActivity = () => {
9489
const theme = useTheme()
9590
const customization = useSelector((state) => state.customization)
96-
const dispatch = useDispatch()
9791
useNotifier()
9892
const { error, setError } = useError()
9993

100-
const enqueueSnackbar = (...args) => dispatch(enqueueSnackbarAction(...args))
101-
const closeSnackbar = (...args) => dispatch(closeSnackbarAction(...args))
102-
10394
const [isLoading, setLoading] = useState(true)
10495

105-
const { confirm } = useConfirm()
106-
10796
const getLoginActivityApi = useApi(auditApi.fetchLoginActivity)
10897
const [activity, setActivity] = useState([])
10998
const [typeFilter, setTypeFilter] = useState([])
@@ -114,8 +103,6 @@ const LoginActivity = () => {
114103
const [startDate, setStartDate] = useState(new Date(new Date().setMonth(new Date().getMonth() - 1)))
115104
const [endDate, setEndDate] = useState(new Date())
116105

117-
const [selected, setSelected] = useState([])
118-
119106
const onStartDateSelected = (date) => {
120107
setStartDate(date)
121108
refreshData(currentPage, date, endDate, typeFilter)
@@ -126,31 +113,6 @@ const LoginActivity = () => {
126113
refreshData(currentPage, startDate, date, typeFilter)
127114
}
128115

129-
const onSelectAllClick = (event) => {
130-
if (event.target.checked) {
131-
const newSelected = activity.map((n) => n.id)
132-
setSelected(newSelected)
133-
return
134-
}
135-
setSelected([])
136-
}
137-
138-
const handleSelect = (event, id) => {
139-
const selectedIndex = selected.indexOf(id)
140-
let newSelected = []
141-
142-
if (selectedIndex === -1) {
143-
newSelected = newSelected.concat(selected, id)
144-
} else if (selectedIndex === 0) {
145-
newSelected = newSelected.concat(selected.slice(1))
146-
} else if (selectedIndex === selected.length - 1) {
147-
newSelected = newSelected.concat(selected.slice(0, -1))
148-
} else if (selectedIndex > 0) {
149-
newSelected = newSelected.concat(selected.slice(0, selectedIndex), selected.slice(selectedIndex + 1))
150-
}
151-
setSelected(newSelected)
152-
}
153-
154116
const refreshData = (_page, _start, _end, _filter) => {
155117
const activityCodes = []
156118
if (_filter.length > 0) {
@@ -219,59 +181,6 @@ const LoginActivity = () => {
219181
}
220182
}
221183

222-
const deleteLoginActivity = async () => {
223-
const confirmPayload = {
224-
title: `Delete`,
225-
description: `Delete ${selected.length} ${selected.length > 1 ? 'records' : 'record'}? `,
226-
confirmButtonName: 'Delete',
227-
cancelButtonName: 'Cancel'
228-
}
229-
const isConfirmed = await confirm(confirmPayload)
230-
//
231-
if (isConfirmed) {
232-
try {
233-
const deleteResp = await auditApi.deleteLoginActivity({
234-
selected: selected
235-
})
236-
if (deleteResp.data) {
237-
enqueueSnackbar({
238-
message: selected.length + ' Login Activity Records Deleted Successfully',
239-
options: {
240-
key: new Date().getTime() + Math.random(),
241-
variant: 'success',
242-
action: (key) => (
243-
<Button style={{ color: 'white' }} onClick={() => closeSnackbar(key)}>
244-
<IconX />
245-
</Button>
246-
)
247-
}
248-
})
249-
onConfirm()
250-
}
251-
} catch (error) {
252-
enqueueSnackbar({
253-
message: `Failed to delete records: ${
254-
typeof error.response.data === 'object' ? error.response.data.message : error.response.data
255-
}`,
256-
options: {
257-
key: new Date().getTime() + Math.random(),
258-
variant: 'error',
259-
persist: true,
260-
action: (key) => (
261-
<Button style={{ color: 'white' }} onClick={() => closeSnackbar(key)}>
262-
<IconX />
263-
</Button>
264-
)
265-
}
266-
})
267-
}
268-
}
269-
}
270-
271-
const onConfirm = () => {
272-
getLoginActivityApi.request()
273-
}
274-
275184
useEffect(() => {
276185
getLoginActivityApi.request({
277186
pageNo: 1
@@ -298,7 +207,6 @@ const LoginActivity = () => {
298207
setStart(data.currentPage * data.pageSize - (data.pageSize - 1))
299208
setEnd(data.currentPage * data.pageSize > data.count ? data.count : data.currentPage * data.pageSize)
300209
setActivity(data.data)
301-
setSelected([])
302210
}
303211
}, [getLoginActivityApi.data])
304212

@@ -447,17 +355,6 @@ const LoginActivity = () => {
447355
/>
448356
</IconButton>
449357
</div>
450-
<PermissionButton
451-
permissionId={'loginActivity:delete'}
452-
sx={{ mt: 1, mb: 2 }}
453-
variant='outlined'
454-
disabled={selected.length === 0}
455-
onClick={deleteLoginActivity}
456-
color='error'
457-
startIcon={<IconTrash />}
458-
>
459-
{'Delete Selected'}
460-
</PermissionButton>
461358
</div>
462359
</div>
463360
<TableContainer
@@ -475,13 +372,6 @@ const LoginActivity = () => {
475372
}}
476373
>
477374
<TableRow>
478-
<StyledTableCell style={{ width: '5%' }}>
479-
<Checkbox
480-
color='primary'
481-
checked={selected.length === (activity || []).length}
482-
onChange={onSelectAllClick}
483-
/>
484-
</StyledTableCell>
485375
<StyledTableCell>Activity</StyledTableCell>
486376
<StyledTableCell>User</StyledTableCell>
487377
<StyledTableCell>Date</StyledTableCell>
@@ -508,15 +398,6 @@ const LoginActivity = () => {
508398
<StyledTableCell>
509399
<Skeleton variant='text' />
510400
</StyledTableCell>
511-
<StyledTableCell>
512-
<Skeleton variant='text' />
513-
</StyledTableCell>
514-
<StyledTableCell>
515-
<Skeleton variant='text' />
516-
</StyledTableCell>
517-
<StyledTableCell>
518-
<Skeleton variant='text' />
519-
</StyledTableCell>
520401
</StyledTableRow>
521402
<StyledTableRow>
522403
<StyledTableCell>
@@ -534,12 +415,6 @@ const LoginActivity = () => {
534415
<StyledTableCell>
535416
<Skeleton variant='text' />
536417
</StyledTableCell>
537-
<StyledTableCell>
538-
<Skeleton variant='text' />
539-
</StyledTableCell>
540-
<StyledTableCell>
541-
<Skeleton variant='text' />
542-
</StyledTableCell>
543418
</StyledTableRow>
544419
</>
545420
) : (
@@ -550,13 +425,6 @@ const LoginActivity = () => {
550425
key={index}
551426
sx={{ '&:last-child td, &:last-child th': { border: 0 } }}
552427
>
553-
<StyledTableCell component='th' scope='row' style={{ width: '5%' }}>
554-
<Checkbox
555-
color='primary'
556-
checked={selected.indexOf(item.id) !== -1}
557-
onChange={(event) => handleSelect(event, item.id)}
558-
/>
559-
</StyledTableCell>
560428
<StyledTableCell component='th' scope='row'>
561429
<div
562430
style={{
@@ -630,7 +498,6 @@ const LoginActivity = () => {
630498
</Stack>
631499
)}
632500
</MainCard>
633-
<ConfirmDialog />
634501
</>
635502
)
636503
}

0 commit comments

Comments
 (0)