Skip to content

Commit

Permalink
Merge pull request #1868 from IntersectMBO/fix/1601-junk-data-in-prot…
Browse files Browse the repository at this point in the history
…ocol-parameter-change-governance-actions
  • Loading branch information
MSzalowski authored Aug 29, 2024
2 parents ea386bb + 8ad1bb7 commit 51e039d
Show file tree
Hide file tree
Showing 13 changed files with 445 additions and 212 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ changes.
- Delete duplicate text on DRep registration form [Issue 1847](https://github.com/IntersectMBO/govtool/issues/1847)
- Fix modal content invisible on ios [Issue 1842](https://github.com/IntersectMBO/govtool/issues/1842)
- Fix counting votes by CC committee members and SPOs [Issue 1838](https://github.com/IntersectMBO/govtool/issues/1838)
- Fix displaying non relevant data in protocol parameter change Governance Action [Issue 1601](https://github.com/IntersectMBO/govtool/issues/1601)

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const GovernanceActionDetailsCardLinks = ({
overflow: "hidden",
textOverflow: "ellipsis",
whiteSpace: "nowrap",
mb: 2,
my: 2,
}}
data-testid="supporting-links"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import "react-diff-view/style/index.css";
import "./react-diff-view.overrides.css";

type Props = {
oldJson?: JSON | Record<string, unknown>;
newJson?: JSON | Record<string, unknown>;
oldJson?: JSON | Record<string, unknown> | null;
newJson?: JSON | Record<string, unknown> | null;
};

export const GovernanceActionDetailsDiffView = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import { useScreenDimension, useTranslation } from "@hooks";
import {
getProposalTypeNoEmptySpaces,
testIdFromLabel,
replaceNullValues,
getProposalTypeLabel,
filterUpdatableProtocolParams,
filterOutNullParams,
} from "@utils";
import { MetadataValidationStatus, ProposalData } from "@models";
import { GovernanceActionType } from "@/types/governanceAction";
Expand Down Expand Up @@ -90,7 +91,21 @@ export const GovernanceActionDetailsCardData = ({
const { screenWidth } = useScreenDimension();
const { isMobile } = useScreenDimension();

const nonNullProtocolParams = replaceNullValues(epochParams, protocolParams);
const updatableProtocolParams = useMemo(
() =>
filterUpdatableProtocolParams(epochParams, protocolParams, [
"id",
"registered_tx_id",
"key",
]),
[epochParams, protocolParams],
);

const nonNullProtocolParams = useMemo(
() =>
filterOutNullParams(protocolParams, ["id", "registered_tx_id", "key"]),
[updatableProtocolParams, protocolParams],
);

const isModifiedPadding =
(isDashboard && screenWidth < 1168) ?? screenWidth < 900;
Expand Down Expand Up @@ -136,7 +151,7 @@ export const GovernanceActionDetailsCardData = ({
dataTestId: "parameters-tab",
content: (
<GovernanceActionDetailsDiffView
oldJson={epochParams}
oldJson={updatableProtocolParams}
newJson={nonNullProtocolParams}
/>
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ const METADATA_API = axios.create({
timeout: TIMEOUT_IN_SECONDS,
});

export const postValidate = async <MetadataType>(body: MetadataValidationDTO) => {
const response = await METADATA_API.post<ValidateMetadataResult<MetadataType>>(
`/validate`,
body,
);
export const postValidate = async <MetadataType>(
body: MetadataValidationDTO,
) => {
const response = await METADATA_API.post<
ValidateMetadataResult<MetadataType>
>(`/validate`, body);

return response.data;
};
31 changes: 31 additions & 0 deletions govtool/frontend/src/utils/filterOutNullParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Filters out null parameters from the original object and returns a new object.
*
* @param originalObject - The original object to filter.
* @param filterOutKeys - An optional array of keys to exclude from the filtering process.
* @returns The filtered object without null parameters.
*/
export const filterOutNullParams = (
originalObject?: Record<string, unknown> | undefined | null,
filterOutKeys?: string[],
) => {
if (!originalObject) {
return null;
}

const finalObject = Object.entries(originalObject).reduce(
(acc: Record<string, unknown>, [key, value]) => {
if (
value !== null &&
value !== undefined &&
!filterOutKeys?.includes(key)
) {
acc[key] = value;
}
return acc;
},
{},
);

return finalObject;
};
34 changes: 34 additions & 0 deletions govtool/frontend/src/utils/filterUpdatableProtocolParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Filters the updatable protocol parameters based on the original object,
* reference object, and optional filter-out keys.
*
* @param originalObject - The original object containing the protocol parameters.
* @param referenceObject - The reference object containing the updated protocol parameters.
* @param filterOutKeys - An optional array of keys to be excluded from the final object.
* @returns The filtered object containing the updatable protocol parameters or null.
*/
export const filterUpdatableProtocolParams = (
originalObject?: Record<string, unknown> | null,
referenceObject?: Record<string, unknown> | null,
filterOutKeys?: string[],
): Record<string, unknown> | null => {
if (!originalObject || !referenceObject) {
return null;
}

const finalObject = Object.entries(referenceObject).reduce<
Record<string, unknown>
>((acc, [key, referenceValue]) => {
const isValid =
!filterOutKeys?.includes(key) &&
originalObject.hasOwnProperty(key) &&
referenceValue !== undefined &&
referenceValue !== null;

if (isValid) acc[key] = originalObject[key];

return acc;
}, {});

return finalObject;
};
3 changes: 2 additions & 1 deletion govtool/frontend/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export * from "./checkIsMaintenanceOn";
export * from "./checkIsWalletConnected";
export * from "./dRep";
export * from "./ellipsizeText";
export * from "./filterOutNullParams";
export * from "./filterUpdatableProtocolParams";
export * from "./formatDate";
export * from "./generateAnchor";
export * from "./generateJsonld";
Expand All @@ -25,7 +27,6 @@ export * from "./mapDtoToProposal";
export * from "./numberValidation";
export * from "./openInNewTab";
export * from "./removeDuplicatedProposals";
export * from "./replaceNullValues";
export * from "./setProtocolParameterUpdate";
export * from "./testIdFromLabel";
export * from "./wait";
23 changes: 0 additions & 23 deletions govtool/frontend/src/utils/replaceNullValues.ts

This file was deleted.

76 changes: 76 additions & 0 deletions govtool/frontend/src/utils/tests/filterOutNullParams.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { filterOutNullParams } from "../filterOutNullParams";

describe("filterOutNullParams", () => {
it("should filter out null parameters", () => {
const originalObject = {
key1: "value1",
key2: null,
key3: "value3",
key4: null,
};

const expectedObject = {
key1: "value1",
key3: "value3",
};

const result = filterOutNullParams(originalObject);

expect(result).toEqual(expectedObject);
});

it("should exclude keys from filtering process", () => {
const originalObject = {
key1: "value1",
key2: null,
key3: "value3",
key4: null,
};

const filterOutKeys = ["key2", "key4"];

const expectedObject = {
key1: "value1",
key3: "value3",
};

const result = filterOutNullParams(originalObject, filterOutKeys);

expect(result).toEqual(expectedObject);
});

it("should return null if originalObject is null", () => {
const originalObject = null;

const result = filterOutNullParams(originalObject);

expect(result).toBeNull();
});

it("should return null if originalObject is undefined", () => {
const originalObject = undefined;

const result = filterOutNullParams(originalObject);

expect(result).toBeNull();
});

it("shouldn't filter out 0", () => {
const originalObject = {
key1: "value1",
key2: 0,
key3: "value3",
key4: undefined,
};

const expectedObject = {
key1: "value1",
key2: 0,
key3: "value3",
};

const result = filterOutNullParams(originalObject);

expect(result).toEqual(expectedObject);
});
});
101 changes: 101 additions & 0 deletions govtool/frontend/src/utils/tests/filterUpdatableProtocolParams.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { filterUpdatableProtocolParams } from "../filterUpdatableProtocolParams";

describe("filterUpdatableProtocolParams", () => {
it("should return only existing in reference object properties", () => {
const originalParams = {
id: 1,
name: "John Doe",
age: 30,
email: "[email protected]",
nonce: "abc123",
};

const updateParams = {
name: "Jane Doe",
age: null,
address: undefined,
};

const expectedOutput = {
name: "John Doe",
};

const result = filterUpdatableProtocolParams(originalParams, updateParams);
expect(result).toEqual(expectedOutput);
});

it("should return only existing in reference object properties and filter out keys", () => {
const originalParams = {
id: 1,
name: "John Doe",
age: 30,
email: "",
nonce: "abc123",
};

const updateParams = {
id: 234,
name: "Jane Doe",
age: null,
address: undefined,
};

const expectedOutput = {
name: "John Doe",
};

const result = filterUpdatableProtocolParams(originalParams, updateParams, [
"id",
]);
expect(result).toEqual(expectedOutput);
});

it("should not filter out 0 value from the reference object", () => {
const originalParams = {
id: 1,
name: "John Doe",
age: 30,
email: "",
nonce: "abc123",
};

const updateParams = {
id: 234,
name: "Jane Doe",
age: 0,
address: undefined,
};

const expectedOutput = {
name: "John Doe",
age: 30,
};

const result = filterUpdatableProtocolParams(originalParams, updateParams, [
"id",
]);
expect(result).toEqual(expectedOutput);
});

it("should return null if original object is not provided", () => {
const updateParams = {
name: "Jane Doe",
age: null,
address: undefined,
};

const result = filterUpdatableProtocolParams(null, updateParams);
expect(result).toBeNull();
});

it("should return null if update object is not provided", () => {
const originalParams = {
id: 1,
name: "John Doe",
age: 30,
};

const result = filterUpdatableProtocolParams(originalParams, null);
expect(result).toBeNull();
});
});
3 changes: 2 additions & 1 deletion govtool/metadata-validation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
}
},
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
Loading

0 comments on commit 51e039d

Please sign in to comment.