From 36691fce68b278e5d5d8864312b6f2854d8cc00e Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 17 Jan 2025 23:45:43 +0000 Subject: [PATCH] make progress in risk categories --- .../helpers/flattenOneTrustAssessment.ts | 52 ++++-- .../flattenOneTrustQuestionResponses.test.ts | 150 +++++++++++++++++- 2 files changed, 185 insertions(+), 17 deletions(-) diff --git a/src/oneTrust/helpers/flattenOneTrustAssessment.ts b/src/oneTrust/helpers/flattenOneTrustAssessment.ts index 468696fc..580859d4 100644 --- a/src/oneTrust/helpers/flattenOneTrustAssessment.ts +++ b/src/oneTrust/helpers/flattenOneTrustAssessment.ts @@ -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 }); }; @@ -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 }); }; @@ -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 && @@ -161,7 +191,7 @@ export const flattenOneTrustQuestions = ( }; }, ); - + // console.log({ allSectionQuestionsFlat }); return aggregateObjects({ objs: allSectionQuestionsFlat, wrap: true, diff --git a/src/oneTrust/helpers/tests/flattenOneTrustQuestionResponses.test.ts b/src/oneTrust/helpers/tests/flattenOneTrustQuestionResponses.test.ts index 14466ed0..b3112ca7 100644 --- a/src/oneTrust/helpers/tests/flattenOneTrustQuestionResponses.test.ts +++ b/src/oneTrust/helpers/tests/flattenOneTrustQuestionResponses.test.ts @@ -9,6 +9,7 @@ import { OneTrustAssessmentQuestionOption, OneTrustAssessmentQuestionResponses, OneTrustAssessmentResponses, + OneTrustRiskCategories, } from '@transcend-io/privacy-types'; import { OneTrustEnrichedAssessmentQuestion, @@ -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 = [[]]; @@ -135,7 +139,7 @@ describe('flattenOneTrustQuestions', () => { ); }); - it('should correctly flatten questions risks', () => { + it.only('should correctly flatten questions risks', () => { const allSectionQuestions: OneTrustEnrichedAssessmentQuestion[][] = [ // section 1 [ @@ -144,11 +148,11 @@ describe('flattenOneTrustQuestions', () => { ...defaultQuestion, risks: [ { - ...defaultQuestionRisk, + ...defaultRisk, name: 's1q1r1', }, { - ...defaultQuestionRisk, + ...defaultRisk, name: 's1q1r2', }, ], @@ -158,7 +162,7 @@ describe('flattenOneTrustQuestions', () => { ...defaultQuestion, risks: [ { - ...defaultQuestionRisk, + ...defaultRisk, name: 's1q2r1', }, ], @@ -186,7 +190,7 @@ describe('flattenOneTrustQuestions', () => { ...defaultQuestion, risks: [ { - ...defaultQuestionRisk, + ...defaultRisk, name: 's2q2r1', }, ], @@ -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