Skip to content

Commit

Permalink
create oneTrustAssessmentToCsv helper
Browse files Browse the repository at this point in the history
  • Loading branch information
abrantesarthur committed Jan 14, 2025
1 parent 7647c02 commit 5dae37d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 59 deletions.
16 changes: 0 additions & 16 deletions src/oneTrust/constants.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/oneTrust/helpers/flattenOneTrustAssessment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
OneTrustEnrichedAssessmentSection,
OneTrustEnrichedRisk,
} from '../codecs';
// import { DEFAULT_ONE_TRUST_COMBINED_ASSESSMENT } from './constants';

// TODO: will have to use something like csv-stringify

Expand Down
55 changes: 55 additions & 0 deletions src/oneTrust/helpers/oneTrustAssessmentToCsv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { decodeCodec } from '@transcend-io/type-utils';

Check failure on line 1 in src/oneTrust/helpers/oneTrustAssessmentToCsv.ts

View workflow job for this annotation

GitHub Actions / build-and-upload-artifacts

Cannot find module './constants' or its corresponding type declarations.

Check failure on line 1 in src/oneTrust/helpers/oneTrustAssessmentToCsv.ts

View workflow job for this annotation

GitHub Actions / build-and-upload-artifacts

Parameter 'header' implicitly has an 'any' type.
import { OneTrustEnrichedAssessment } from '../codecs';
import { DEFAULT_ONE_TRUST_ASSESSMENT_CSV_HEADER } from './constants';
import { flattenOneTrustAssessment } from './flattenOneTrustAssessment';
import { OneTrustAssessmentCsvRecord } from '@transcend-io/privacy-types';

/**
* Converts the assessment into a csv entry.
*
* @param param - information about the assessment and amount of entries
* @returns a stringified csv entry ready to be appended to a file
*/
export const oneTrustAssessmentToCsv = ({
assessment,
index,
}: {
/** The assessment to convert */
assessment: OneTrustEnrichedAssessment;
/** The position of the assessment in the final Json object */
index: number;
}): string => {
const csvRows = [];

// write csv header at the beginning of the file
if (index === 0) {
csvRows.push(DEFAULT_ONE_TRUST_ASSESSMENT_CSV_HEADER.join(','));
}

// flatten the assessment object so it does not have nested properties
const flatAssessment = flattenOneTrustAssessment(assessment);

// comment
const flatAssessmentFull = Object.fromEntries(
DEFAULT_ONE_TRUST_ASSESSMENT_CSV_HEADER.map((header) => {
const value = flatAssessment[header] ?? '';
const escapedValue =
typeof value === 'string' &&
(value.includes(',') || value.includes('"'))
? `"${value.replace(/"/g, '""')}"`
: value;
return [header, escapedValue];
}),
);

// ensure the record has the expected type!
decodeCodec(OneTrustAssessmentCsvRecord, flatAssessmentFull);

// transform the flat assessment to have all CSV keys in the expected order
const assessmentRow = Object.values(flatAssessmentFull);

// append the rows to the file
csvRows.push(`${assessmentRow.join(',')}\n`);

return csvRows.join('\n');
};
51 changes: 9 additions & 42 deletions src/oneTrust/helpers/writeOneTrustAssessment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ import { logger } from '../../logger';
import colors from 'colors';
import { OneTrustFileFormat } from '../../enums';
import fs from 'fs';
import { flattenOneTrustAssessment } from './flattenOneTrustAssessment';
import { DEFAULT_ONE_TRUST_ASSESSMENT_CSV_HEADER } from '../constants';
import { decodeCodec } from '@transcend-io/type-utils';
import { OneTrustAssessmentCsvRecord } from '@transcend-io/privacy-types';
import { OneTrustEnrichedAssessment } from '../codecs';
import { oneTrustAssessmentToJson } from './oneTrustAssessmentToJson';
import { oneTrustAssessmentToCsv } from './oneTrustAssessmentToCsv';

/**
* Write the assessment to disk at the specified file path.
Expand Down Expand Up @@ -41,46 +38,16 @@ export const writeOneTrustAssessment = ({
),
);

// For json format
if (fileFormat === OneTrustFileFormat.Json) {
const jsonEntry = oneTrustAssessmentToJson({
assessment,
index,
total,
});
fs.appendFileSync(file, jsonEntry);
} else if (fileFormat === OneTrustFileFormat.Csv) {
const csvRows = [];

// write csv header at the beginning of the file
if (index === 0) {
csvRows.push(DEFAULT_ONE_TRUST_ASSESSMENT_CSV_HEADER.join(','));
}

// flatten the assessment object so it does not have nested properties
const flatAssessment = flattenOneTrustAssessment(assessment);

// comment
const flatAssessmentFull = Object.fromEntries(
DEFAULT_ONE_TRUST_ASSESSMENT_CSV_HEADER.map((header) => {
const value = flatAssessment[header] ?? '';
const escapedValue =
typeof value === 'string' &&
(value.includes(',') || value.includes('"'))
? `"${value.replace(/"/g, '""')}"`
: value;
return [header, escapedValue];
fs.appendFileSync(
file,
oneTrustAssessmentToJson({
assessment,
index,
total,
}),
);

// ensure the record has the expected type!
decodeCodec(OneTrustAssessmentCsvRecord, flatAssessmentFull);

// transform the flat assessment to have all CSV keys in the expected order
const assessmentRow = Object.values(flatAssessmentFull);

// append the rows to the file
csvRows.push(`${assessmentRow.join(',')}\n`);
fs.appendFileSync('./oneTrust.csv', csvRows.join('\n'));
} else if (fileFormat === OneTrustFileFormat.Csv) {
fs.appendFileSync(file, oneTrustAssessmentToCsv({ assessment, index }));
}
};

0 comments on commit 5dae37d

Please sign in to comment.