From 5dae37dc315c772ef325bc513351529202f1cdd4 Mon Sep 17 00:00:00 2001 From: Arthur Date: Tue, 14 Jan 2025 03:05:48 +0000 Subject: [PATCH] create oneTrustAssessmentToCsv helper --- src/oneTrust/constants.ts | 16 ------ .../helpers/flattenOneTrustAssessment.ts | 1 - .../helpers/oneTrustAssessmentToCsv.ts | 55 +++++++++++++++++++ .../helpers/writeOneTrustAssessment.ts | 51 +++-------------- 4 files changed, 64 insertions(+), 59 deletions(-) delete mode 100644 src/oneTrust/constants.ts create mode 100644 src/oneTrust/helpers/oneTrustAssessmentToCsv.ts diff --git a/src/oneTrust/constants.ts b/src/oneTrust/constants.ts deleted file mode 100644 index ad249a2a..00000000 --- a/src/oneTrust/constants.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { createDefaultCodec } from '@transcend-io/type-utils'; -import { OneTrustEnrichedAssessment } from './codecs'; -import { flattenOneTrustAssessment } from './helpers'; - -/** - * An object with default values of type OneTrustEnrichedAssessment. It's very - * 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: OneTrustEnrichedAssessment = - createDefaultCodec(OneTrustEnrichedAssessment); - -/** The header of the OneTrust ASsessment CSV file */ -export const DEFAULT_ONE_TRUST_ASSESSMENT_CSV_HEADER = Object.keys( - flattenOneTrustAssessment(DEFAULT_ONE_TRUST_COMBINED_ASSESSMENT), -); diff --git a/src/oneTrust/helpers/flattenOneTrustAssessment.ts b/src/oneTrust/helpers/flattenOneTrustAssessment.ts index 89f4743d..5609add9 100644 --- a/src/oneTrust/helpers/flattenOneTrustAssessment.ts +++ b/src/oneTrust/helpers/flattenOneTrustAssessment.ts @@ -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 diff --git a/src/oneTrust/helpers/oneTrustAssessmentToCsv.ts b/src/oneTrust/helpers/oneTrustAssessmentToCsv.ts new file mode 100644 index 00000000..05ac1176 --- /dev/null +++ b/src/oneTrust/helpers/oneTrustAssessmentToCsv.ts @@ -0,0 +1,55 @@ +import { decodeCodec } from '@transcend-io/type-utils'; +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'); +}; diff --git a/src/oneTrust/helpers/writeOneTrustAssessment.ts b/src/oneTrust/helpers/writeOneTrustAssessment.ts index 173ae381..af8e4ab7 100644 --- a/src/oneTrust/helpers/writeOneTrustAssessment.ts +++ b/src/oneTrust/helpers/writeOneTrustAssessment.ts @@ -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. @@ -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 })); } };