Skip to content

Commit

Permalink
feat: show added and removed communities in history
Browse files Browse the repository at this point in the history
  • Loading branch information
RRanath committed Nov 14, 2024
1 parent 4ca0c77 commit 12c0188
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 71 deletions.
80 changes: 80 additions & 0 deletions app/components/Analyst/CBC/History/CbcHistoryCommunitiesTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import styled from 'styled-components';

interface Props {
heading: string;
communities: any[];
}

const StyledCommunitiesContainer = styled.div`
display: flex;
align-items: center;
`;

const StyledLeftContainer = styled.div`
padding-right: 2%;
width: 250px;
`;

const StyledTable = styled.table`
th {
border: none;
}
tbody > tr {
border-bottom: thin dashed;
border-color: ${(props) => props.theme.color.borderGrey};
td {
width: 200px;
max-width: 200px;
border: none;
}
}
`;

const StyledIdCell = styled.td`
width: 100px !important;
max-width: 100px;
`;

const CbcHistoryCommunitiesTable: React.FC<Props> = ({
heading,
communities,
}) => {
return (
<StyledCommunitiesContainer
style={{ display: 'flex', alignItems: 'center' }}
>
<StyledLeftContainer>{heading}</StyledLeftContainer>
<div>
<StyledTable>
<thead>
<tr>
<th>Economic Region</th>
<th>Regional District</th>
<th>Geographic Name</th>
<th>Type</th>
<th>ID</th>
</tr>
</thead>
<tbody>
{communities?.map((community, index) => (
<tr
// eslint-disable-next-line react/no-array-index-key
key={`${community.bc_geographic_name}-${index}`}
>
<td>{community.economic_region}</td>
<td>{community.regional_district}</td>
<td>{community.bc_geographic_name}</td>
<td>{community.geographic_type}</td>
<StyledIdCell>
{community.communities_source_data_id}
</StyledIdCell>
</tr>
))}
</tbody>
</StyledTable>
</div>
</StyledCommunitiesContainer>
);
};

export default CbcHistoryCommunitiesTable;
14 changes: 14 additions & 0 deletions app/components/Analyst/CBC/History/CbcHistoryContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import HistoryDetails from 'components/Analyst/History/HistoryDetails';
import cbcData from 'formSchema/uiSchema/history/cbcData';
import { DateTime } from 'luxon';
import styled from 'styled-components';
import CbcHistoryCommunitiesTable from './CbcHistoryCommunitiesTable';

const StyledContent = styled.span`
display: flex;
Expand Down Expand Up @@ -72,10 +73,23 @@ const HistoryContent = ({
'updated_at',
'change_reason',
'cbc_data_id',
'locations',
]}
diffSchema={cbcData}
overrideParent="cbcData"
/>
{json?.locations?.added?.length > 0 && (
<CbcHistoryCommunitiesTable
heading="Added Community location data"
communities={json?.locations?.added}
/>
)}
{json?.locations?.removed?.length > 0 && (
<CbcHistoryCommunitiesTable
heading="Deleted Community location data"
communities={json?.locations?.removed}
/>
)}
{op === 'UPDATE' && changeReason !== '' && (
<ChangeReason reason={changeReason} />
)}
Expand Down
4 changes: 4 additions & 0 deletions app/components/Analyst/CBC/History/CbcHistoryTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ const CbcHistoryTable: React.FC<Props> = ({ query }) => {
json={{
...historyItem.record?.json_data,
project_number: historyItem.record?.project_number,
locations: {
added: historyItem.record?.added_communities,
removed: historyItem.record?.deleted_communities,
},
}}
prevJson={{
...historyItem.oldRecord?.json_data,
Expand Down
48 changes: 14 additions & 34 deletions app/pages/analyst/cbc/[cbcId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import CbcForm from 'components/Analyst/CBC/CbcForm';
import { ChangeModal } from 'components/Analyst';
import styled from 'styled-components';
import ReviewTheme from 'components/Review/ReviewTheme';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useEffect, useMemo, useRef, useState } from 'react';
import { useUpdateCbcDataAndInsertChangeRequest } from 'schema/mutations/cbc/updateCbcDataAndInsertChangeReason';
import review from 'formSchema/analyst/cbc/review';
import reviewUiSchema from 'formSchema/uiSchema/cbc/reviewUiSchema';
Expand All @@ -25,7 +25,6 @@ import {
import customValidate, { CBC_WARN_COLOR } from 'utils/cbcCustomValidator';
import CbcRecordLock from 'components/Analyst/CBC/CbcRecordLock';
import useModal from 'lib/helpers/useModal';
import { useUpdateCbcCommunityDataMutationMutation } from 'schema/mutations/cbc/updateCbcCommunityData';

const getCbcQuery = graphql`
query CbcIdQuery($rowId: Int!) {
Expand Down Expand Up @@ -248,42 +247,12 @@ const Cbc = ({
}, [query, isCbcAdmin, editFeatureEnabled, cbcCommunitiesData]);

const [updateFormData] = useUpdateCbcDataAndInsertChangeRequest();
const [updateCbcCommunitySourceData] =
useUpdateCbcCommunityDataMutationMutation();

const handleChangeRequestModal = () => {
setChangeReason(null);
changeModal.open();
};

const handleUpdateCommunitySource = useCallback(() => {
updateCbcCommunitySourceData({
variables: {
input: {
_projectId: rowId,
_communityIdsToAdd: addedCommunities,
_communityIdsToArchive: removedCommunities,
},
},
debounceKey: 'cbc_update_community_source_data',
onCompleted: (response) => {
setAddedCommunities([]);
setRemovedCommunities([]);
setResponseCommunityData(
response.editCbcProjectCommunities.cbcProjectCommunities.map(
(proj) => proj.communitiesSourceDataByCommunitiesSourceDataId
)
);
},
});
}, [
addedCommunities,
removedCommunities,
updateCbcCommunitySourceData,
rowId,
setResponseCommunityData,
]);

const handleSubmit = () => {
const {
geographicNames,
Expand Down Expand Up @@ -317,12 +286,23 @@ const Cbc = ({
query?.cbcByRowId?.cbcDataByCbcId?.edges[0].node.rowId || null,
},
},
inputCbcProjectCommunities: {
_projectId: rowId,
_communityIdsToAdd: addedCommunities,
_communityIdsToArchive: removedCommunities,
},
},
debounceKey: 'cbc_update_form_data',
onCompleted: () => {
onCompleted: (response) => {
setEditMode(false);
changeModal.close();
handleUpdateCommunitySource();
setAddedCommunities([]);
setRemovedCommunities([]);
setResponseCommunityData(
response.editCbcProjectCommunities.cbcProjectCommunities.map(
(proj) => proj.communitiesSourceDataByCommunitiesSourceDataId
)
);
setAllowEdit(isCbcAdmin && editFeatureEnabled);
},
});
Expand Down
34 changes: 7 additions & 27 deletions app/pages/analyst/cbc/[cbcId]/edit/[section].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
getAllEconomicRegionNames,
} from 'utils/schemaUtils';
import ArrayLocationFieldTemplate from 'lib/theme/fields/ArrayLocationDataField';
import { useUpdateCbcCommunityDataMutationMutation } from 'schema/mutations/cbc/updateCbcCommunityData';
import customValidate, { CBC_WARN_COLOR } from 'utils/cbcCustomValidator';

const getCbcSectionQuery = graphql`
Expand Down Expand Up @@ -152,9 +151,6 @@ const EditCbcSection = ({
return formPayload;
};

const [updateCbcCommunitySourceData] =
useUpdateCbcCommunityDataMutationMutation();

const handleOnChange = (e) => {
if (section === 'locations') {
setFormData({
Expand All @@ -164,28 +160,6 @@ const EditCbcSection = ({
} else setFormData({ ...formData, [section]: e.formData });
};

const handleUpdateCommunitySource = useCallback(() => {
updateCbcCommunitySourceData({
variables: {
input: {
_projectId: rowId,
_communityIdsToAdd: addedCommunities,
_communityIdsToArchive: removedCommunities,
},
},
debounceKey: 'cbc_update_community_source_data',
onCompleted: () => {
setAddedCommunities([]);
setRemovedCommunities([]);
},
});
}, [
addedCommunities,
removedCommunities,
updateCbcCommunitySourceData,
rowId,
]);

const handleChangeRequestModal = (e) => {
changeModal.open();
setFormData({ ...formData, [section]: e.formData });
Expand Down Expand Up @@ -248,10 +222,16 @@ const EditCbcSection = ({
cbcDataId: cbcDataRowId,
},
},
inputCbcProjectCommunities: {
_projectId: rowId,
_communityIdsToAdd: addedCommunities,
_communityIdsToArchive: removedCommunities,
},
},
debounceKey: 'cbc_update_section_data',
onCompleted: () => {
handleUpdateCommunitySource();
setAddedCommunities([]);
setRemovedCommunities([]);
router.push(`/analyst/cbc/${rowId}`);
},
});
Expand Down
15 changes: 15 additions & 0 deletions app/schema/mutations/cbc/updateCbcDataAndInsertChangeReason.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const mutation = graphql`
mutation updateCbcDataAndInsertChangeReasonMutation(
$inputCbcData: UpdateCbcDataByRowIdInput!
$inputCbcChangeReason: CreateCbcDataChangeReasonInput!
$inputCbcProjectCommunities: EditCbcProjectCommunitiesInput!
) {
updateCbcDataByRowId(input: $inputCbcData) {
cbcData {
Expand All @@ -24,6 +25,20 @@ const mutation = graphql`
createdAt
}
}
editCbcProjectCommunities(input: $inputCbcProjectCommunities) {
cbcProjectCommunities {
communitiesSourceDataId
cbcId
communitiesSourceDataByCommunitiesSourceDataId {
geographicNameId
economicRegion
regionalDistrict
bcGeographicName
geographicType
rowId
}
}
}
}
`;

Expand Down
46 changes: 40 additions & 6 deletions db/deploy/computed_columns/cbc_history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,50 @@ begin;

create or replace function ccbc_public.cbc_history(_cbc_project ccbc_public.cbc) returns setof ccbc_public.record_version as $$

select id, record_id, old_record_id, op, ts, table_oid, table_schema, table_name,
select
r.id, r.record_id, r.old_record_id, r.op, r.ts, r.table_oid, r.table_schema, r.table_name,
-- if the operation is an update, use the updated_by field in the record
-- because created_by is listed as the person who initially created the record
case when op = 'UPDATE'::audit.operation then (record->>'updated_by')::int else created_by end as created_by,
created_at, record, old_record
from ccbc_public.record_version
where table_name = 'cbc_data' and record->>'cbc_id' = _cbc_project.id::text
case when r.op = 'UPDATE'::audit.operation then (r.record->>'updated_by')::int else r.created_by end as created_by,
r.created_at,
-- Add added_communities and deleted_communities to the record JSON field
r.record || jsonb_build_object(
'added_communities', (
select jsonb_agg(
community.record || (
select row_to_json(cs)::jsonb
from ccbc_public.communities_source_data as cs
where cs.geographic_name_id = (community.record->>'communities_source_data_id')::int
)
)
from ccbc_public.record_version as community
where community.ts = r.ts
and community.created_by = created_by
and community.table_name = 'cbc_project_communities'
and community.op = 'INSERT'
),
'deleted_communities', (
select jsonb_agg(
community.record || (
select row_to_json(cs)::jsonb
from ccbc_public.communities_source_data as cs
where cs.geographic_name_id = (community.record->>'communities_source_data_id')::int
)
)
from ccbc_public.record_version as community
where community.ts = r.ts
and community.created_by = created_by
and community.table_name = 'cbc_project_communities'
and community.op = 'UPDATE'
)
) as record,
r.old_record
from ccbc_public.record_version as r
where table_name = 'cbc_data' and r.record->>'cbc_id' = _cbc_project.id::text
-- order by record->>'updated_at' since created_at is filled with the initial record creation date
-- could be replaced by id desc, should give the same result
order by record->>'updated_at' desc;
order by r.record->>'updated_at' desc;



$$ language sql stable;
Expand Down
27 changes: 27 additions & 0 deletions db/deploy/computed_columns/cbc_history@1.203.3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- Deploy ccbc:computed_columns/cbc_history to pg

begin;

create or replace function ccbc_public.cbc_history(_cbc_project ccbc_public.cbc) returns setof ccbc_public.record_version as $$

select id, record_id, old_record_id, op, ts, table_oid, table_schema, table_name,
-- if the operation is an update, use the updated_by field in the record
-- because created_by is listed as the person who initially created the record
case when op = 'UPDATE'::audit.operation then (record->>'updated_by')::int else created_by end as created_by,
created_at, record, old_record
from ccbc_public.record_version
where table_name = 'cbc_data' and record->>'cbc_id' = _cbc_project.id::text
-- order by record->>'updated_at' since created_at is filled with the initial record creation date
-- could be replaced by id desc, should give the same result
order by record->>'updated_at' desc;


$$ language sql stable;

grant execute on function ccbc_public.cbc_history to ccbc_admin;
grant execute on function ccbc_public.cbc_history to cbc_admin;
grant execute on function ccbc_public.cbc_history to ccbc_analyst;

comment on function ccbc_public.cbc_history is 'Get the history of a cbc record';

commit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Deploy ccbc:tables/cbc_project_communities_001_enable_tracking to pg

BEGIN;

select audit.enable_tracking('ccbc_public.cbc_project_communities'::regclass);

COMMIT;
Loading

0 comments on commit 12c0188

Please sign in to comment.