Skip to content

Commit

Permalink
update const flattenOneTrustSectionHeaders = (
Browse files Browse the repository at this point in the history
  • Loading branch information
abrantesarthur committed Jan 11, 2025
1 parent 460f25c commit ccd64ae
Showing 1 changed file with 11 additions and 25 deletions.
36 changes: 11 additions & 25 deletions src/oneTrust/flattenOneTrustAssessment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import {
// OneTrustAssessmentQuestionResponseCodec,
// OneTrustAssessmentQuestionRiskCodec,
OneTrustAssessmentSectionCodec,
OneTrustAssessmentSectionFlatHeaderCodec,
// OneTrustAssessmentSectionFlatHeaderCodec,
OneTrustAssessmentSectionHeaderCodec,
OneTrustAssessmentSectionHeaderRiskStatisticsCodec,
// OneTrustAssessmentSectionHeaderRiskStatisticsCodec,
OneTrustEnrichedRiskCodec,
OneTrustGetAssessmentResponseCodec,
} from './codecs';
Expand Down Expand Up @@ -165,40 +166,25 @@ const flattenOneTrustSectionHeaders = (
headers: OneTrustAssessmentSectionHeaderCodec[],
prefix: string,
): any => {
// TODO: do this for EVERY nested object that may be null
// // TODO: do this for EVERY nested object that may be null
const defaultRiskStatistics: OneTrustAssessmentSectionHeaderRiskStatisticsCodec =
{
maxRiskLevel: null,
riskCount: null,
sectionId: null,
};

const { riskStatistics, flatHeaders } = headers.reduce<{
/** The risk statistics of all headers */
riskStatistics: OneTrustAssessmentSectionHeaderRiskStatisticsCodec[];
/** The headers without risk statistics */
flatHeaders: OneTrustAssessmentSectionFlatHeaderCodec[];
}>(
(acc, header) => {
const { riskStatistics, ...rest } = header;
return {
riskStatistics: [
...acc.riskStatistics,
riskStatistics ?? defaultRiskStatistics,
],
flatHeaders: [...acc.flatHeaders, rest],
};
},
{
riskStatistics: [],
flatHeaders: [],
},
);
const { riskStatistics, rest: restHeaders } = extractProperties(headers, [
'riskStatistics',
]);

const flatFlatHeaders = flatHeaders.map((h) => flattenObject(h, prefix));
const flatFlatHeaders = restHeaders.map((h) => flattenObject(h, prefix));
const flatRiskStatistics = riskStatistics.map((r) =>
flattenObject(r ?? defaultRiskStatistics, `${prefix}_riskStatistics`),
);
return {
...aggregateObjects({ objs: flatFlatHeaders }),
...flattenList(riskStatistics, `${prefix}_riskStatistics`),
...aggregateObjects({ objs: flatRiskStatistics }),
};
};

Expand Down

0 comments on commit ccd64ae

Please sign in to comment.