Skip to content

Commit

Permalink
create oneTrustAssessmentToCsvRecord helper
Browse files Browse the repository at this point in the history
  • Loading branch information
abrantesarthur committed Jan 14, 2025
1 parent 5dae37d commit 4256788
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 32 deletions.
38 changes: 8 additions & 30 deletions src/oneTrust/helpers/oneTrustAssessmentToCsv.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
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';
import { oneTrustAssessmentToCsvRecord } from './oneTrustAssessmentToCsvRecord';

/**
* Converts the assessment into a csv entry.
Expand All @@ -19,37 +16,18 @@ export const oneTrustAssessmentToCsv = ({
/** The position of the assessment in the final Json object */
index: number;
}): string => {
const csvRows = [];
const assessmentCsvRecord = oneTrustAssessmentToCsvRecord(assessment);

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

// 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`);
// append the row
const row = `${Object.values(assessmentCsvRecord).join(',')}\n`;
csvRows.push(row);

return csvRows.join('\n');
};
36 changes: 36 additions & 0 deletions src/oneTrust/helpers/oneTrustAssessmentToCsvRecord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { decodeCodec } from '@transcend-io/type-utils';

Check failure on line 1 in src/oneTrust/helpers/oneTrustAssessmentToCsvRecord.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/oneTrustAssessmentToCsvRecord.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 record (header + values). It always
* returns a record with every key in the same order.
*
* @param assessment - the assessment to convert to a csv record
* @returns a stringified csv entry ready to be appended to a file
*/
export const oneTrustAssessmentToCsvRecord = (
/** The assessment to convert */
assessment: OneTrustEnrichedAssessment,
): OneTrustAssessmentCsvRecord => {
// flatten the assessment object so it does not have nested properties
const flatAssessment = flattenOneTrustAssessment(assessment);

// transform the flat assessment to have all CSV keys in the expected order
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!
return decodeCodec(OneTrustAssessmentCsvRecord, flatAssessmentFull);
};
5 changes: 4 additions & 1 deletion src/oneTrust/helpers/syncOneTrustAssessments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,18 @@ export const syncOneTrustAssessments = async ({
riskDetails,
});

// sync to file
if (dryRun && file && fileFormat) {
// sync to file
writeOneTrustAssessment({
assessment: enrichedAssessment,
index,
total: assessments.length,
file,
fileFormat,
});
} else if (fileFormat === OneTrustFileFormat.Csv) {
// sync to transcend
// const csvEntry = oneTrustAssessmentToCsv({ assessment, index });
}
});
};
2 changes: 1 addition & 1 deletion src/oneTrust/helpers/writeOneTrustAssessment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const writeOneTrustAssessment = ({
}): void => {
logger.info(
colors.magenta(
`Syncing enriched assessment ${
`Writing enriched assessment ${
index + 1
} of ${total} to file "${file}"...`,
),
Expand Down

0 comments on commit 4256788

Please sign in to comment.