Skip to content

Commit

Permalink
update writeOneTrustAssessment to write in csv format
Browse files Browse the repository at this point in the history
  • Loading branch information
abrantesarthur committed Jan 13, 2025
1 parent b26cb56 commit 3ac69be
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/oneTrust/codecs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable max-lines */
import * as t from 'io-ts';

// TODO: move all to privacy-types
// TODO: move to privacy-types

export const OneTrustAssessmentCodec = t.type({
/** ID of the assessment. */
Expand Down
7 changes: 4 additions & 3 deletions src/oneTrust/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ 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.
* valuable when converting assessments to CSV, as it contains all keys that
* make up the CSV header in the expected order
*/
const DEFAULT_ONE_TRUST_COMBINED_ASSESSMENT: OneTrustCombinedAssessmentCodec =
createDefaultCodec(OneTrustCombinedAssessmentCodec);

export const DEFAULT_ONE_TRUST_ASSESSMENT_CSV_KEYS = Object.keys(
/** The header of the OneTrust ASsessment CSV file */
export const DEFAULT_ONE_TRUST_ASSESSMENT_CSV_HEADER = Object.keys(
flattenOneTrustAssessment(DEFAULT_ONE_TRUST_COMBINED_ASSESSMENT),
);
63 changes: 28 additions & 35 deletions src/oneTrust/writeOneTrustAssessment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from './codecs';
import fs from 'fs';
import { flattenOneTrustAssessment } from './flattenOneTrustAssessment';
import { DEFAULT_ONE_TRUST_ASSESSMENT_CSV_KEYS } from './constants';
import { DEFAULT_ONE_TRUST_ASSESSMENT_CSV_HEADER } from './constants';

/**
* Write the assessment to disk at the specified file path.
Expand Down Expand Up @@ -96,23 +96,24 @@ export const writeOneTrustAssessment = ({
fs.writeFileSync(file, '[\n');
}

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

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

// // write to file
// fs.appendFileSync(file, stringifiedAssessment + comma);
// write to file
fs.appendFileSync(file, stringifiedAssessment + comma);

// // end with closing bracket
// if (index === total - 1) {
// fs.appendFileSync(file, ']');
// }
// end with closing bracket
if (index === total - 1) {
fs.appendFileSync(file, ']');
}
} else if (fileFormat === OneTrustFileFormat.Csv) {
// flatten the json object
// start with an opening bracket
const csvRows = [];

// write csv header at the beginning of the file
if (index === 0) {
fs.writeFileSync('./oneTrust.json', '[\n');
csvRows.push(DEFAULT_ONE_TRUST_ASSESSMENT_CSV_HEADER.join(','));
}

// flatten the assessment object so it does not have nested properties
Expand All @@ -122,29 +123,21 @@ export const writeOneTrustAssessment = ({
});

// 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 assessmentRow = DEFAULT_ONE_TRUST_ASSESSMENT_CSV_HEADER.map(
(header) => {
const value = flatAssessment[header] ?? '';
// Escape values containing commas or quotes
return typeof value === 'string' &&
(value.includes(',') || value.includes('"'))
? `"${value.replace(/"/g, '""')}"`
: value;
},
);

// const stringifiedAssessment = JSON.stringify(enrichedAssessment, null, 2);
// append the rows to the file
csvRows.push(`${assessmentRow.join(',')}\n`);
fs.appendFileSync('./oneTrust.csv', csvRows.join('\n'));

// 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', csvEntry + comma);

// end with closing bracket
if (index === total - 1) {
fs.appendFileSync('./oneTrust.json', ']');
}
// TODO: consider not to convert it to CSV at all! The importOneTrustAssessments does not actually accept CSV.
}
};

0 comments on commit 3ac69be

Please sign in to comment.