From 418bca5387c54d03046a7a4324a26d7c49a26d73 Mon Sep 17 00:00:00 2001 From: Christopher Sunkel Date: Fri, 23 Feb 2024 16:32:06 +0000 Subject: [PATCH] Delete deprecated EditHistory component and other legacy code --- .../investments/controllers/edit-history.js | 25 ----- src/apps/investments/controllers/index.js | 2 - src/apps/investments/repos.js | 10 -- src/apps/investments/router-projects.js | 7 +- .../__test__/edit-history.test.js | 64 ------------- .../investments/transformers/edit-history.js | 40 -------- src/apps/investments/types.js | 21 ----- .../components/EditHistory/EditHistory.jsx | 93 ------------------- .../EditHistory/EditHistoryChangeList.jsx | 62 ------------- .../EditHistory/EditHistoryList.jsx | 67 ------------- test/selectors/edit-history.js | 20 ---- test/selectors/index.js | 1 - 12 files changed, 1 insertion(+), 411 deletions(-) delete mode 100644 src/apps/investments/controllers/edit-history.js delete mode 100644 src/apps/investments/transformers/__test__/edit-history.test.js delete mode 100644 src/apps/investments/transformers/edit-history.js delete mode 100644 src/apps/investments/types.js delete mode 100644 src/client/components/EditHistory/EditHistory.jsx delete mode 100644 src/client/components/EditHistory/EditHistoryChangeList.jsx delete mode 100644 src/client/components/EditHistory/EditHistoryList.jsx delete mode 100644 test/selectors/edit-history.js diff --git a/src/apps/investments/controllers/edit-history.js b/src/apps/investments/controllers/edit-history.js deleted file mode 100644 index adfdf8810a0..00000000000 --- a/src/apps/investments/controllers/edit-history.js +++ /dev/null @@ -1,25 +0,0 @@ -const { getInvestmentProjectAuditLog } = require('../repos') -const { transformAuditLog } = require('../transformers/edit-history') - -const fetchProjectsHistoryHandler = async (req, res, next) => { - try { - const { page } = req.query - - const { results, count } = await getInvestmentProjectAuditLog( - req, - req.params.investmentId, - page - ) - - res.json({ - results: transformAuditLog(results), - count, - }) - } catch (error) { - next(error) - } -} - -module.exports = { - fetchProjectsHistoryHandler, -} diff --git a/src/apps/investments/controllers/index.js b/src/apps/investments/controllers/index.js index 3b32fa949cd..92d76d847bc 100644 --- a/src/apps/investments/controllers/index.js +++ b/src/apps/investments/controllers/index.js @@ -1,7 +1,5 @@ const create = require('./create') -const editHistory = require('./edit-history') module.exports = { create, - editHistory, } diff --git a/src/apps/investments/repos.js b/src/apps/investments/repos.js index 3bce4b839df..61c3ff7532f 100644 --- a/src/apps/investments/repos.js +++ b/src/apps/investments/repos.js @@ -8,16 +8,6 @@ function getInvestment(req, investmentId) { ) } -function getInvestmentProjectAuditLog(req, investmentId, page = 1) { - const limit = 10 - const offset = limit * (page - 1) - return authorisedRequest(req, { - url: `${config.apiRoot}/v3/investment/${investmentId}/audit`, - qs: { limit, offset }, - }) -} - module.exports = { getInvestment, - getInvestmentProjectAuditLog, } diff --git a/src/apps/investments/router-projects.js b/src/apps/investments/router-projects.js index 0497e5abd04..07a2f878d79 100644 --- a/src/apps/investments/router-projects.js +++ b/src/apps/investments/router-projects.js @@ -11,7 +11,7 @@ const { const { shared } = require('./middleware') -const { create, editHistory } = require('./controllers') +const { create } = require('./controllers') const { renderAddEvidence } = require('./apps/evidence/controllers/create') const { postUpload } = require('../documents/middleware/upload') @@ -35,11 +35,6 @@ router.get( exportCollection('investment_project') ) -router.get( - urls.investments.editHistory.data.route, - editHistory.fetchProjectsHistoryHandler -) - // Add investment from Companies router.get('/create/:companyId?', create.start.renderCreatePage) diff --git a/src/apps/investments/transformers/__test__/edit-history.test.js b/src/apps/investments/transformers/__test__/edit-history.test.js deleted file mode 100644 index 757017b0c50..00000000000 --- a/src/apps/investments/transformers/__test__/edit-history.test.js +++ /dev/null @@ -1,64 +0,0 @@ -const { transformAuditLog } = require('../edit-history') - -describe('#transformAuditLog', () => { - const testUser = { - name: 'John Doe', - email: 'john@doe.doe', - } - - const testTimestamp = '2019-12-10T18:07:27.864590Z' - - const testChanges = { - a_change: [false, true], - } - - it('should return an empty array when there is no audit log', () => { - const actual = transformAuditLog([]) - expect(actual).to.deep.equal([]) - }) - - it('should return array of objects with timestamp, user and changes values, where there is an audit log with changes', () => { - const actual = transformAuditLog([ - { - user: testUser, - timestamp: testTimestamp, - changes: testChanges, - }, - ]) - expect(actual[0].timestamp).to.equal('2019-12-10T18:07:27.864590Z') - expect(actual[0].changedBy).to.equal('John Doe') - expect(actual[0].changes).to.deep.equal([ - { - fieldName: 'A change', - oldValue: false, - newValue: true, - }, - ]) - }) - - it('should return empty changes values objects, where there is an audit log without changes', () => { - const actual = transformAuditLog([ - { - user: testUser, - timestamp: testTimestamp, - changes: {}, - }, - ]) - - expect(actual[0].changes).to.deep.equal([]) - }) - - it('should return user email if there is no user name given', () => { - const actual = transformAuditLog([ - { - user: { - email: 'test@test.testtest', - }, - timestamp: testTimestamp, - changes: testChanges, - }, - ]) - - expect(actual[0].changedBy).to.equal('test@test.testtest') - }) -}) diff --git a/src/apps/investments/transformers/edit-history.js b/src/apps/investments/transformers/edit-history.js deleted file mode 100644 index b8f6dbb66c9..00000000000 --- a/src/apps/investments/transformers/edit-history.js +++ /dev/null @@ -1,40 +0,0 @@ -const { capitalize, lowerCase } = require('lodash') - -// TODO: when receive updated list of edit history labels from users, -// add EXCLUDED_FIELDS and FIELD_NAME_TO_LABEL_MAP -// see example in src/apps/companies/apps/edit-history/controller.js - -function mapFieldNameToLabel(fieldName) { - return capitalize(lowerCase(fieldName)) -} - -const unwrapFromArray = (change) => { - if (Array.isArray(change)) { - return change.length > 1 ? change : change[0] || null - } - return change -} - -const transformChanges = (changes) => { - return Object.keys(changes).map((fieldName) => ({ - fieldName: mapFieldNameToLabel(fieldName), - oldValue: unwrapFromArray(changes[fieldName][0]), - newValue: unwrapFromArray(changes[fieldName][1]), - })) -} - -const transformAuditLogItem = ({ timestamp, user, changes }) => { - const changedBy = user ? user.name || user.email : null - - return { - timestamp, - changedBy, - changes: transformChanges(changes), - } -} - -const transformAuditLog = (auditLog) => { - return auditLog.map(transformAuditLogItem) -} - -module.exports = { transformAuditLog } diff --git a/src/apps/investments/types.js b/src/apps/investments/types.js deleted file mode 100644 index f5cb5e19bc4..00000000000 --- a/src/apps/investments/types.js +++ /dev/null @@ -1,21 +0,0 @@ -const investmentTypes = { - FDI: 'FDI', - NON_FDI: 'Non-FDI', - CTI: 'Commitment to invest', -} - -const fdiTypes = { - MERGER: 'merger', - RETENTION: 'Retention', - ACQUISITION: 'Acquisition', - CAPITAL_ONLY: 'Capital only', - JOINT_VENTURE: 'Joint venture', - CREATION_OF_NEW_SITE_OR_ACTIVITY: 'Creation of new site or activity', - EXPANSION_OF_EXISTING_SITE_OR_ACTIVITY: - 'Expansion of existing site or activity', -} - -module.exports = { - investmentTypes, - fdiTypes, -} diff --git a/src/client/components/EditHistory/EditHistory.jsx b/src/client/components/EditHistory/EditHistory.jsx deleted file mode 100644 index adc2549304c..00000000000 --- a/src/client/components/EditHistory/EditHistory.jsx +++ /dev/null @@ -1,93 +0,0 @@ -import React, { useEffect, useState } from 'react' -import PropTypes from 'prop-types' -import { useSearchParam } from 'react-use' -import { H3 } from '@govuk-react/heading' -import { ErrorSummary, LoadingBox } from 'govuk-react' -import styled from 'styled-components' -import pluralize from 'pluralize' -import axios from 'axios' - -import EditHistoryList from './EditHistoryList' - -const StyledLoadingBox = styled(LoadingBox)` - ${({ loading, minHeight }) => loading && `min-height: ${minHeight}px;`} -` - -function EditHistory({ - dataEndpoint, - changeType, - getUpdatedBy, - getValue, - itemsPerPage, -}) { - const [editHistory, setEditHistory] = useState([]) - const [totalItems, setTotalItems] = useState(0) - const [isLoading, setIsLoading] = useState(true) - const [errorMessage, setErrorMessage] = useState(null) - - const totalPages = Math.ceil(totalItems / itemsPerPage) - const activePage = parseInt(useSearchParam('page'), 10) || 1 - const getPageUrl = (page) => `${window.location.pathname}?page=${page}` - const setActivePage = (page) => - window.history.pushState({}, '', getPageUrl(page)) - - const onPageClick = (page) => { - setActivePage(page) - } - - useEffect(() => { - async function fetchData() { - try { - const { data } = await axios.get(dataEndpoint, { - params: { - page: activePage, - }, - }) - setEditHistory(data.results) - setTotalItems(data.count) - setIsLoading(false) - } catch (err) { - setErrorMessage(err.message) - } - } - setIsLoading(true) - window.scrollTo(0, 0) - fetchData() - }, [activePage]) - - return errorMessage ? ( - - ) : ( - -

{pluralize('change', totalItems, true)}

- -
- ) -} - -EditHistory.propTypes = { - dataEndpoint: PropTypes.string.isRequired, - changeType: PropTypes.string.isRequired, - getUpdatedBy: PropTypes.func.isRequired, - getValue: PropTypes.func.isRequired, - itemsPerPage: PropTypes.number, -} - -EditHistory.defaultProps = { - itemsPerPage: 10, -} - -export default EditHistory diff --git a/src/client/components/EditHistory/EditHistoryChangeList.jsx b/src/client/components/EditHistory/EditHistoryChangeList.jsx deleted file mode 100644 index b3467eaf295..00000000000 --- a/src/client/components/EditHistory/EditHistoryChangeList.jsx +++ /dev/null @@ -1,62 +0,0 @@ -import React from 'react' -import PropTypes from 'prop-types' -import { FONT_SIZE } from '@govuk-react/constants' -import styled from 'styled-components' - -import SummaryTable from '../../components/SummaryTable' - -const StyledSummaryTable = styled(SummaryTable)` - caption { - font-size: ${FONT_SIZE.SIZE_16}; - } - - & > tbody > tr:first-child { - border-top: none; - } - - & > tbody > tr > th { - border: none; - font-weight: normal; - } - - & > tbody > tr > td { - border: none; - font-weight: bold; - } -` - -const StyledNoChanges = styled('div')` - font-size: 16px; - padding: 30px 0; -` - -function EditHistoryChangeList({ changes, changeType, getValue }) { - return ( - <> - {changes.map(({ fieldName, oldValue, newValue }) => ( - - - {getValue(oldValue, fieldName)} - - - {getValue(newValue, fieldName)} - - - ))} - {changes.length === 0 && ( - - No changes were made to {changeType} in this update - - )} - - ) -} - -EditHistoryChangeList.propTypes = { - changes: PropTypes.array.isRequired, - changedBy: PropTypes.string.isRequired, - changeType: PropTypes.string.isRequired, - getValue: PropTypes.func.isRequired, -} - -export default EditHistoryChangeList diff --git a/src/client/components/EditHistory/EditHistoryList.jsx b/src/client/components/EditHistory/EditHistoryList.jsx deleted file mode 100644 index 0350859678a..00000000000 --- a/src/client/components/EditHistory/EditHistoryList.jsx +++ /dev/null @@ -1,67 +0,0 @@ -import React from 'react' -import PropTypes from 'prop-types' -import { FONT_SIZE } from '@govuk-react/constants' -import styled from 'styled-components' - -import { GREY_1, GREY_2 } from '../../utils/colours' -import EditHistoryChangeList from './EditHistoryChangeList' -import Pagination from '../../components/Pagination' - -const StyledListContainer = styled('div')` - border-top: 2px solid ${GREY_2}; -` - -const StyledUpdatedOn = styled('p')` - color: ${GREY_1}; - margin-bottom: 0; - font-size: ${FONT_SIZE.SIZE_16}; - float: right; -` - -function EditHistoryList({ - items, - totalPages, - activePage, - onPageClick, - getPageUrl, - changeType, - getUpdatedBy, - getValue, -}) { - return ( -
- {items.map(({ timestamp, changes, changedBy }) => ( - - - {getUpdatedBy(timestamp, changedBy)} - - - - ))} - - -
- ) -} - -EditHistoryList.propTypes = { - items: PropTypes.array.isRequired, - totalPages: PropTypes.number.isRequired, - activePage: PropTypes.number.isRequired, - onPageClick: PropTypes.func.isRequired, - changeType: PropTypes.string.isRequired, - getUpdatedBy: PropTypes.func.isRequired, - getValue: PropTypes.func.isRequired, -} - -export default EditHistoryList diff --git a/test/selectors/edit-history.js b/test/selectors/edit-history.js deleted file mode 100644 index ff0d10dea31..00000000000 --- a/test/selectors/edit-history.js +++ /dev/null @@ -1,20 +0,0 @@ -module.exports = { - change: (changeIndex) => { - const change = `[data-test=editHistory] div:nth-child(${changeIndex})` - return { - change, - updated: `${change} > p`, - noChanges: `${change} > div`, - table: (tableIndex) => { - const table = `${change} table:nth-child(${tableIndex})` - return { - caption: `${table} caption`, - beforeChangeText: `${table} tr:nth-child(1) th`, - beforeChangeValue: `${table} tr:nth-child(1) td`, - afterChangeText: `${table} tr:nth-child(2) th`, - afterChangeValue: `${table} tr:nth-child(2) td`, - } - }, - } - }, -} diff --git a/test/selectors/index.js b/test/selectors/index.js index bd4d652ed01..d563f105fb0 100644 --- a/test/selectors/index.js +++ b/test/selectors/index.js @@ -24,7 +24,6 @@ exports.companyDnbHierarchy = { exports.eventCreate = require('./event/createOrEdit') -exports.editHistory = require('./edit-history') exports.entityCollection = require('./entity-collection') exports.interaction = {