Skip to content

Commit

Permalink
make progress in risk categories
Browse files Browse the repository at this point in the history
  • Loading branch information
abrantesarthur committed Jan 17, 2025
1 parent 475c5ee commit 36691fc
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 17 deletions.
52 changes: 41 additions & 11 deletions src/oneTrust/helpers/flattenOneTrustAssessment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,20 @@ const flattenOneTrustRiskCategories = (
allCategories: OneTrustRiskCategories[],
prefix: string,
): any => {
const allCategoriesFlat = (allCategories ?? []).map((categories) =>
flattenObject({ obj: { categories }, prefix }),
const defaultRiskCategories = convertToEmptyStrings(
createDefaultCodec(OneTrustRiskCategories),
) as OneTrustRiskCategories;

const allCategoriesFlat = allCategories.map((categories) =>
!categories
? {}
: flattenObject({
obj: {
categories:
categories.length === 0 ? defaultRiskCategories : categories,
},
prefix,
}),
);
return aggregateObjects({ objs: allCategoriesFlat, wrap: true });
};
Expand All @@ -77,16 +89,25 @@ const flattenOneTrustRisks = (
prefix: string,
): any => {
const allRisksFlat = (allRisks ?? []).map((ars) => {
const { categories, rest: risks } = transposeObjectArray(ars ?? [], [
'categories',
]);
const { categories: allCategories, rest: risks } = transposeObjectArray(
ars ?? [],
['categories'],
);

// console.log({
// question: i,
// allCategories: JSON.stringify(allCategories, null, 2),
// });

return {
...(risks && flattenObject({ obj: { risks }, prefix })),
...(categories &&
flattenOneTrustRiskCategories(categories, `${prefix}_risks`)),
...(allCategories &&
flattenOneTrustRiskCategories(allCategories, `${prefix}_risks`)),
};
});

// console.log({ allRisksFlat });

return aggregateObjects({ objs: allRisksFlat, wrap: true });
};

Expand Down Expand Up @@ -140,13 +161,22 @@ export const flattenOneTrustQuestions = (
'risks',
]);

const defaultRisk = convertToEmptyStrings(
createDefaultCodec(OneTrustEnrichedRisk),
) as OneTrustEnrichedRisk;
const defaultRisk = {
...convertToEmptyStrings(createDefaultCodec(OneTrustEnrichedRisk)),
categories: null,
} as OneTrustEnrichedRisk;
// must differentiate zero risks and no risks when it com
/**
* FIXME: must differentiate empty risks and risk with empty categories
* right now we convert empty risks to risk with empty categories so both look the same
* ideally, when there is no risk we would behave like there is no category
*/
const allRisksDefault = allRisks.map((risks) =>
!risks || risks.length === 0 ? [defaultRisk] : risks,
);

// console.log({ allRisksDefault });

return {
...(questions && flattenObject({ obj: { questions }, prefix })),
...(nestedQuestions &&
Expand All @@ -161,7 +191,7 @@ export const flattenOneTrustQuestions = (
};
},
);

// console.log({ allSectionQuestionsFlat });
return aggregateObjects({
objs: allSectionQuestionsFlat,
wrap: true,
Expand Down
150 changes: 144 additions & 6 deletions src/oneTrust/helpers/tests/flattenOneTrustQuestionResponses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
OneTrustAssessmentQuestionOption,
OneTrustAssessmentQuestionResponses,
OneTrustAssessmentResponses,
OneTrustRiskCategories,
} from '@transcend-io/privacy-types';
import {
OneTrustEnrichedAssessmentQuestion,
Expand All @@ -29,8 +30,11 @@ describe('flattenOneTrustQuestions', () => {
createDefaultCodec(OneTrustAssessmentNestedQuestion);
const defaultQuestionOption: OneTrustAssessmentQuestionOption =
createDefaultCodec(OneTrustAssessmentQuestionOption);
const defaultQuestionRisk: OneTrustEnrichedRisk =
const defaultRisk: OneTrustEnrichedRisk =
createDefaultCodec(OneTrustEnrichedRisk);
const defaultRiskCategory: OneTrustRiskCategories = createDefaultCodec(
OneTrustRiskCategories,
);

it('should return an empty flat object on empty list input', () => {
const questions = [[]];
Expand Down Expand Up @@ -135,7 +139,7 @@ describe('flattenOneTrustQuestions', () => {
);
});

it('should correctly flatten questions risks', () => {
it.only('should correctly flatten questions risks', () => {
const allSectionQuestions: OneTrustEnrichedAssessmentQuestion[][] = [
// section 1
[
Expand All @@ -144,11 +148,11 @@ describe('flattenOneTrustQuestions', () => {
...defaultQuestion,
risks: [
{
...defaultQuestionRisk,
...defaultRisk,
name: 's1q1r1',
},
{
...defaultQuestionRisk,
...defaultRisk,
name: 's1q1r2',
},
],
Expand All @@ -158,7 +162,7 @@ describe('flattenOneTrustQuestions', () => {
...defaultQuestion,
risks: [
{
...defaultQuestionRisk,
...defaultRisk,
name: 's1q2r1',
},
],
Expand Down Expand Up @@ -186,7 +190,7 @@ describe('flattenOneTrustQuestions', () => {
...defaultQuestion,
risks: [
{
...defaultQuestionRisk,
...defaultRisk,
name: 's2q2r1',
},
],
Expand Down Expand Up @@ -238,6 +242,140 @@ describe('flattenOneTrustQuestions', () => {
);
});

it.only('should correctly flatten questions risks categories', () => {
const allSectionQuestions: OneTrustEnrichedAssessmentQuestion[][] = [
// section 0
[
// question 0
{
...defaultQuestion,
risks: [
// risk 0
{
...defaultRisk,
categories: [
// category 0
{
...defaultRiskCategory[0],
name: 's010r0c0',
},
// category 1
{
...defaultRiskCategory[0],
name: 's010r0c1',
},
],
},
// risk 1
{
...defaultRisk,
categories: [],
},
],
},
// question 1
{
...defaultQuestion,
risks: [
// risk 1
{
...defaultRisk,
// empty categories
categories: [],
},
// risk 2
{
...defaultRisk,
// empty categories
categories: [],
},
],
},
// question 2
{
...defaultQuestion,
risks: [
// risk 0
{
...defaultRisk,
// empty categories
categories: [],
},
// risk 1
{
...defaultRisk,
categories: [
// category 0
{
...defaultRiskCategory[0],
name: 's0q2r1c0',
},
],
},
// risk 2
{
...defaultRisk,
// empty categories
categories: [],
},
],
},
// question 3
{
...defaultQuestion,
// empty risks
risks: [],
},
// TODO: test with null risks
],
// section 1
[
// question 0
{
...defaultQuestion,
risks: [
// risk 0
{
...defaultRisk,
// empty categories
categories: [],
},
],
},
],
// section 2
[
// question 1
{
...defaultQuestion,
// empty risks
risks: [],
},
],
];

// section 0 question 0 has 2 risks, the first with 2 categories
const section0Question0 = '[[s010r0c0,s010r0c1],[]]';
// section 0 question 1 has 2 risks, none with categories
const section0Question1 = '[[],[]]';
// section 0 question 2 has 3 risks, the middle one with 1 category
const section0Question2 = '[[],[s0q2r1c0],[]]';
// section 0 question 3 has 0 risks
const section0Question3 = '[]';
const section0 = `[${section0Question0},${section0Question1},${section0Question2},${section0Question3}]`;
// section 1 question 0 has 1 risk with empty categories
const section1question0 = '[[]]';
const section1 = `[${section1question0}]`;
// section 2 question 0 has 0 risks
const section2question0 = '[]';
const section2 = `[${section2question0}]`;
const { sections_questions_risks_categories_name } =
flattenOneTrustQuestions(allSectionQuestions, 'sections');
expect(sections_questions_risks_categories_name).to.equal(
`${section0},${section1},${section2}`,
);
});

it('should correctly flatten nested questions', () => {
const allSectionQuestions: OneTrustEnrichedAssessmentQuestion[][] = [
// section 1 has 2 questions
Expand Down

0 comments on commit 36691fc

Please sign in to comment.