Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
abrantesarthur committed Jan 13, 2025
1 parent bd5842c commit b26cb56
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 65 deletions.
7 changes: 6 additions & 1 deletion src/oneTrust/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { createDefaultCodec } from '../helpers';
import { OneTrustCombinedAssessmentCodec } from './codecs';
import { flattenOneTrustAssessment } from './flattenOneTrustAssessment';

/**
* An object with default values of type OneTrustCombinedAssessmentCodec. It's very
* valuable when converting assessments to CSV. When we flatten it, the resulting
* value always contains all keys that eventually we add to the header.
*/
export const DEFAULT_ONE_TRUST_COMBINED_ASSESSMENT: OneTrustCombinedAssessmentCodec =
const DEFAULT_ONE_TRUST_COMBINED_ASSESSMENT: OneTrustCombinedAssessmentCodec =
createDefaultCodec(OneTrustCombinedAssessmentCodec);

export const DEFAULT_ONE_TRUST_ASSESSMENT_CSV_KEYS = Object.keys(
flattenOneTrustAssessment(DEFAULT_ONE_TRUST_COMBINED_ASSESSMENT),
);
87 changes: 27 additions & 60 deletions src/oneTrust/flattenOneTrustAssessment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
OneTrustEnrichedRiskCodec,
OneTrustRiskCategories,
} from './codecs';
import { DEFAULT_ONE_TRUST_COMBINED_ASSESSMENT } from './constants';
// import { DEFAULT_ONE_TRUST_COMBINED_ASSESSMENT } from './constants';

// TODO: will have to use something like csv-stringify
Expand Down Expand Up @@ -240,68 +239,36 @@ const flattenOneTrustSections = (
export const flattenOneTrustAssessment = (
combinedAssessment: OneTrustCombinedAssessmentCodec,
): any => {
/**
* TODO: experiment creating a default assessment with
* const result = createDefaultCodec(OneTrustGetAssessmentResponseCodec);
* Then, flatten it and aggregate it with the actual assessment. This way, every
* assessment will always have the same fields!
*/

const flatten = (assessment: OneTrustCombinedAssessmentCodec): any => {
const {
approvers,
primaryEntityDetails,
respondents,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
respondent,
sections,
...rest
} = assessment;

const flatApprovers = approvers.map((approver) =>
flattenObject(approver, 'approvers'),
);
const flatRespondents = respondents.map((respondent) =>
flattenObject(respondent, 'respondents'),
);
const flatPrimaryEntityDetails = primaryEntityDetails.map(
(primaryEntityDetail) =>
flattenObject(primaryEntityDetail, 'primaryEntityDetails'),
);

return {
...flattenObject(rest),
...aggregateObjects({ objs: flatApprovers }),
...aggregateObjects({ objs: flatRespondents }),
...aggregateObjects({ objs: flatPrimaryEntityDetails }),
...flattenOneTrustSections(sections, 'sections'),
};
};

// add default values to assessments
const combinedAssessmentWithDefaults =
enrichCombinedAssessmentWithDefaults(combinedAssessment);

const combinedAssessmentFlat = flatten(combinedAssessmentWithDefaults);
const defaultAssessmentFlat = flatten(DEFAULT_ONE_TRUST_COMBINED_ASSESSMENT);

// TODO: test that both have the same keys
const keysOne = Object.keys(combinedAssessmentFlat);
const keysTwo = Object.keys(defaultAssessmentFlat);

const keysInCombinedOnly = keysOne.filter((k) => !keysTwo.includes(k));
const {
approvers,
primaryEntityDetails,
respondents,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
respondent,
sections,
...rest
} = combinedAssessmentWithDefaults;

const flatApprovers = approvers.map((approver) =>
flattenObject(approver, 'approvers'),
);
const flatRespondents = respondents.map((respondent) =>
flattenObject(respondent, 'respondents'),
);
const flatPrimaryEntityDetails = primaryEntityDetails.map(
(primaryEntityDetail) =>
flattenObject(primaryEntityDetail, 'primaryEntityDetails'),
);

console.log({
keysInCombinedOnly,
});
return combinedAssessmentFlat;
return {
...flattenObject(rest),
...aggregateObjects({ objs: flatApprovers }),
...aggregateObjects({ objs: flatRespondents }),
...aggregateObjects({ objs: flatPrimaryEntityDetails }),
...flattenOneTrustSections(sections, 'sections'),
};
};
/**
*
*
* TODO: convert to camelCase -> Title Case
* TODO: section -> header is spread
* TODO: section -> questions -> question is spread
* TODO: section -> questions -> question -> questionOptions are aggregated
* TODO: section -> questions -> question -> questionResponses -> responses are spread
*/
20 changes: 16 additions & 4 deletions src/oneTrust/writeOneTrustAssessment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from './codecs';
import fs from 'fs';
import { flattenOneTrustAssessment } from './flattenOneTrustAssessment';
import { DEFAULT_ONE_TRUST_ASSESSMENT_CSV_KEYS } from './constants';

/**
* Write the assessment to disk at the specified file path.
Expand Down Expand Up @@ -114,21 +115,32 @@ export const writeOneTrustAssessment = ({
fs.writeFileSync('./oneTrust.json', '[\n');
}

const flattened = flattenOneTrustAssessment({
// flatten the assessment object so it does not have nested properties
const flatAssessment = flattenOneTrustAssessment({
...assessment,
...enrichedAssessment,
});
const stringifiedFlattened = JSON.stringify(flattened, null, 2);
// TODO: do not forget to ensure we have the same set of keys!!!

// transform the flat assessment to have all CSV keys in the expected order
const flatAssessmentWithCsvKeys =
DEFAULT_ONE_TRUST_ASSESSMENT_CSV_KEYS.reduce(
(acc, key) => ({
...acc,
[key]: flatAssessment[key] ?? '',
}),
{},
);
const csvEntry = JSON.stringify(flatAssessmentWithCsvKeys, null, 2);

// const stringifiedAssessment = JSON.stringify(enrichedAssessment, null, 2);

// Add comma for all items except the last one
const comma = index < total - 1 ? ',' : '';

// TODO: might be better not to convert it to CSV at all! The importOneTrustAssessments does not actually accept CSV.
// write to file
// fs.appendFileSync(file, stringifiedAssessment + comma);
fs.appendFileSync('./oneTrust.json', stringifiedFlattened + comma);
fs.appendFileSync('./oneTrust.json', csvEntry + comma);

// end with closing bracket
if (index === total - 1) {
Expand Down

0 comments on commit b26cb56

Please sign in to comment.