Skip to content

Commit

Permalink
create oneTrustAssessmentToJson helper
Browse files Browse the repository at this point in the history
  • Loading branch information
abrantesarthur committed Jan 14, 2025
1 parent d11609e commit 7647c02
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
41 changes: 41 additions & 0 deletions src/oneTrust/helpers/oneTrustAssessmentToJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { OneTrustEnrichedAssessment } from '../codecs';

/**
* Converts the assessment into a json entry.
*
* @param param - information about the assessment and amount of entries
* @returns a stringified json entry ready to be appended to a file
*/
export const oneTrustAssessmentToJson = ({
assessment,
index,
total,
}: {
/** The assessment to convert */
assessment: OneTrustEnrichedAssessment;
/** The position of the assessment in the final Json object */
index: number;
/** The total amount of the assessments in the final Json object */
total: number;
}): string => {
let jsonEntry = '';
// start with an opening bracket
if (index === 0) {
jsonEntry = '[\n';
}

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

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

// write to file
jsonEntry = jsonEntry + stringifiedAssessment + comma;

// end with closing bracket
if (index === total - 1) {
jsonEntry += ']';
}

return jsonEntry;
};
1 change: 1 addition & 0 deletions src/oneTrust/helpers/parseCliSyncOtArguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const parseCliSyncOtArguments = (): OneTrustCliArguments => {
`The "fileFormat" parameter must equal ${OneTrustFileFormat.Csv} when "dryRun" is "false".`,
),
);
return process.exit(1);
}

// If trying to sync to disk, must specify a file path
Expand Down
1 change: 1 addition & 0 deletions src/oneTrust/helpers/syncOneTrustAssessments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const syncOneTrustAssessments = async ({
riskDetails,
});

// sync to file
if (dryRun && file && fileFormat) {
writeOneTrustAssessment({
assessment: enrichedAssessment,
Expand Down
24 changes: 7 additions & 17 deletions src/oneTrust/helpers/writeOneTrustAssessment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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';

/**
* Write the assessment to disk at the specified file path.
Expand Down Expand Up @@ -42,23 +43,12 @@ export const writeOneTrustAssessment = ({

// For json format
if (fileFormat === OneTrustFileFormat.Json) {
// start with an opening bracket
if (index === 0) {
fs.writeFileSync(file, '[\n');
}

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

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

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

// end with closing bracket
if (index === total - 1) {
fs.appendFileSync(file, ']');
}
const jsonEntry = oneTrustAssessmentToJson({
assessment,
index,
total,
});
fs.appendFileSync(file, jsonEntry);
} else if (fileFormat === OneTrustFileFormat.Csv) {
const csvRows = [];

Expand Down

0 comments on commit 7647c02

Please sign in to comment.