Skip to content

Commit

Permalink
fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
abrantesarthur committed Jan 24, 2025
1 parent 2cfb1c3 commit 6bb845f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 42 deletions.
51 changes: 24 additions & 27 deletions src/cli-sync-ot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,37 @@ async function main(): Promise<void> {
transcendAuth,
transcendUrl,
resource,
debug,
// debug,
dryRun,
} = parseCliSyncOtArguments();

// use the hostname and auth token to instantiate a client to talk to OneTrust
const oneTrust = createOneTrustGotInstance({ hostname, auth: oneTrustAuth });

try {
if (resource === OneTrustPullResource.Assessments) {
await syncOneTrustAssessments({
oneTrust,
file,
fileFormat,
dryRun,
...(transcendAuth && transcendUrl
? {
transcend: buildTranscendGraphQLClient(
transcendUrl,
transcendAuth,
),
}
: {}),
});
}
} catch (err) {
logger.error(
colors.red(
`An error occurred syncing the resource ${resource} from OneTrust: ${
debug ? err.stack : err.message
}`,
),
);
process.exit(1);
// try {
if (resource === OneTrustPullResource.Assessments) {
await syncOneTrustAssessments({
oneTrust,
file,
fileFormat,
dryRun,
...(transcendAuth && transcendUrl
? {
transcend: buildTranscendGraphQLClient(transcendUrl, transcendAuth),
}
: {}),
});
}
// } catch (err) {
// logger.error(
// colors.red(
// `An error occurred syncing the resource ${resource} from OneTrust: ${
// debug ? err.stack : err.message
// }`,
// ),
// );
// process.exit(1);
// }

// Indicate success
logger.info(
Expand Down
4 changes: 3 additions & 1 deletion src/codecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2093,9 +2093,11 @@ const OneTrustAssessmentRowInput = t.type({
});

/** Input for importing multiple OneTrust assessment forms into Transcend */
export const ImportOnetrustAssessmentsInput = t.type({
export const ImportOnetrustAssessmentsInput = t.partial({
/** 'The rows of the CSV file.' */
rows: t.array(OneTrustAssessmentRowInput),
/** 'The json record representing the assessment.' */
json: t.string,
});
/** Type override */
export type ImportOnetrustAssessmentsInput = t.TypeOf<
Expand Down
7 changes: 5 additions & 2 deletions src/oneTrust/helpers/oneTrustAssessmentToJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ export const oneTrustAssessmentToJson = ({
assessment,
index,
total,
wrap = true,
}: {
/** 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;
/** Whether to wrap every entry in brackets */
wrap?: boolean;
}): string => {
let jsonEntry = '';
// start with an opening bracket
if (index === 0) {
if (index === 0 || wrap) {
jsonEntry = '[\n';
}

Expand All @@ -33,7 +36,7 @@ export const oneTrustAssessmentToJson = ({
jsonEntry = jsonEntry + stringifiedAssessment + comma;

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

Expand Down
31 changes: 20 additions & 11 deletions src/oneTrust/helpers/syncOneTrustAssessmentToTranscend.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { logger } from '../../logger';
import { oneTrustAssessmentToCsvRecord } from './oneTrustAssessmentToCsvRecord';
import colors from 'colors';
import { GraphQLClient } from 'graphql-request';
import {
IMPORT_ONE_TRUST_ASSESSMENT_FORMS,
makeGraphQLRequest,
} from '../../graphql';
import { ImportOnetrustAssessmentsInput } from '../../codecs';
import { OneTrustEnrichedAssessment } from '@transcend-io/privacy-types';
import { oneTrustAssessmentToJson } from './oneTrustAssessmentToJson';

export interface AssessmentForm {
/** ID of Assessment Form */
Expand All @@ -24,27 +25,35 @@ export interface AssessmentForm {
export const syncOneTrustAssessmentToTranscend = async ({
transcend,
assessment,
total,
index,
}: {
/** the Transcend client instance */
transcend: GraphQLClient;
/** the assessment to sync to Transcend */
assessment: OneTrustEnrichedAssessment;
/** The index of the assessment being written to the file */
index: number;
/** The total amount of assessments that we will write */
total: number;
}): Promise<AssessmentForm | undefined> => {
logger.info();
logger.info(
colors.magenta(
`Writing enriched assessment ${index + 1} of ${total} to Transcend...`,
),
);

// convert the OneTrust assessment object into a CSV Record (a map from the csv header to values)
const csvRecord = oneTrustAssessmentToCsvRecord(assessment);
const json = oneTrustAssessmentToJson({
assessment,
index,
total,
wrap: true,
});

// transform the csv record into a valid input to the mutation
const input: ImportOnetrustAssessmentsInput = {
rows: [
{
columns: Object.entries(csvRecord).map(([key, value]) => ({
title: key,
value: value.toString(),
})),
},
],
json,
};

const {
Expand Down
4 changes: 3 additions & 1 deletion src/oneTrust/helpers/syncOneTrustAssessments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,13 @@ export const syncOneTrustAssessments = async ({
file,
fileFormat,
});
} else if (fileFormat === OneTrustFileFormat.Csv && transcend) {
} else if (fileFormat === OneTrustFileFormat.Json && transcend) {
// sync to transcend
await syncOneTrustAssessmentToTranscend({
assessment: enrichedAssessment,
transcend,
total: assessments.length,
index: trueIndex,
});
}
},
Expand Down

0 comments on commit 6bb845f

Please sign in to comment.