Skip to content

Commit

Permalink
create getListOfAssessments helper
Browse files Browse the repository at this point in the history
  • Loading branch information
abrantesarthur committed Jan 7, 2025
1 parent 099a7e3 commit 70de777
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 85 deletions.
108 changes: 30 additions & 78 deletions src/cli-pull-ot.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#!/usr/bin/env node
import got from 'got';

import yargs from 'yargs-parser';
import { logger } from './logger';
import colors from 'colors';
import { DEFAULT_ONE_TRUST_PULL_RESOURCES } from './oneTrust';
import { getListOfAssessments } from './oneTrust';
import { OneTrustPullResource } from './enums';
import { createOneTrustGotInstance } from './oneTrust/createOneTrustGotInstance';
// import { mapSeries } from 'bluebird';

const VALID_RESOURCES = Object.values(OneTrustPullResource);
Expand All @@ -31,38 +30,30 @@ async function main(): Promise<void> {
// includeGuessedCategories = 'false',
// TODO: make this required!
hostname = 'app-eu.onetrust.com',
resources = DEFAULT_ONE_TRUST_PULL_RESOURCES.join(','),
resource = OneTrustPullResource.Assessments,
// pageSize = '',
debug = '',
// auth,
// TODO: remove hardcode
auth = 'ODgyOWYxZWYyYjExNDAwYjkyNDlkZWMzMzgzYTY5MTM6YnRIRkNHTUc4V0N3NEFvaGFEa3dHdGtUN0JLY2hHMkY=',
// trackerStatuses = Object.values(ConsentTrackerStatus).join(','),
} = yargs(process.argv.slice(2));

// Parse request actions
let splitResources: OneTrustPullResource[] = [];
if (!resources) {
if (!resource) {
logger.error(
colors.red(
`Missing required parameter "resources". e.g. --resources=${VALID_RESOURCES.join(
',',
)}`,
`Missing required parameter "resource". e.g. --resource=${OneTrustPullResource.Assessments}`,
),
);
process.exit(1);
}
splitResources =
resources === 'all'
? VALID_RESOURCES
: (resources.split(',') as OneTrustPullResource[]);
const invalidResources = splitResources.filter(
(resource) => !VALID_RESOURCES.includes(resource),
);
if (invalidResources.length > 0) {

if (!VALID_RESOURCES.includes(resource)) {
logger.error(
colors.red(
`Received invalid resources values: "${invalidResources.join(
`Received invalid resource value: "${resource}". Allowed: ${VALID_RESOURCES.join(
',',
)}". Allowed: ${VALID_RESOURCES.join(',')}`,
)}`,
),
);
process.exit(1);
Expand All @@ -73,66 +64,27 @@ async function main(): Promise<void> {

// Sync to Disk
try {
// TODO invoke a createOneTrustGotInstance helper inspired by createSombraGotInstance
const oneTrust = got.extend({
// TODO: create constant or env var
prefixUrl: `https://${hostname}`,
headers: {
accept: 'application/json',
'content-type': 'application/json',
// TODO: fetch from environment
authorization:
'Bearer ODgyOWYxZWYyYjExNDAwYjkyNDlkZWMzMzgzYTY5MTM6YnRIRkNHTUc4V0N3NEFvaGFEa3dHdGtUN0JLY2hHMkY=',
},
});

// TODO: get based on the resources
// TODO: export into its own function
let currentPage = 0;
let totalPages = 1;
let totalElements = 0;

const allAssessments = [];

logger.info('Getting list of OneTrust assessments...');
while (currentPage < totalPages) {
// eslint-disable-next-line no-await-in-loop
const { body } = await oneTrust.get(
`api/assessment/v2/assessments?page=${currentPage}&size=2000`,
);
const parsedBody = JSON.parse(body);
const assessments = parsedBody.content ?? [];
console.log({ assessments });
allAssessments.push(...assessments);
const page = parsedBody.page ?? { totalPages: 0, totalElements: 0 };
if (currentPage === 0) {
totalPages = page.totalPages;
totalElements = page.totalElements;
}
currentPage += 1;

// log progress
logger.info(
`Fetched ${allAssessments.length} of ${totalElements} assessments.`,
);
if (resource === OneTrustPullResource.Assessments) {
const oneTrust = createOneTrustGotInstance({ hostname, auth });
// const assessments =
await getListOfAssessments({ oneTrust });
// logger.info('Enriching the fetched OneTrust assessments with details');
// await mapSeries(allAssessments, async (assessment, index) => {
// logger.info(
// `Enriching assessment ${index + 1} of ${allAssessments.length}`,
// );

// const { body } = await oneTrust.get(
// `api/assessment/v2/assessments/${assessment.assessmentId}/export?ExcludeSkippedQuestions=false`,
// );
// const parsedBody = JSON.parse(body);

// console.log({ parsedBody });
// });

// const detailsBody = JSON.parse(details.body);
}

// logger.info('Enriching the fetched OneTrust assessments with details');
// await mapSeries(allAssessments, async (assessment, index) => {
// logger.info(
// `Enriching assessment ${index + 1} of ${allAssessments.length}`,
// );

// const { body } = await oneTrust.get(
// `api/assessment/v2/assessments/${assessment.assessmentId}/export?ExcludeSkippedQuestions=false`,
// );
// const parsedBody = JSON.parse(body);

// console.log({ parsedBody });
// });

// const detailsBody = JSON.parse(details.body);

// logger.info(colors.magenta('Writing configuration to file "file"...'));
// writeTranscendYaml(file, configuration);
} catch (err) {
Expand Down
25 changes: 25 additions & 0 deletions src/oneTrust/createOneTrustGotInstance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import got, { Got } from 'got';

/**
* Instantiate an instance of got that is capable of making requests to OneTrust
*
* @param param - information about the OneTrust URL
* @returns The instance of got that is capable of making requests to the customer ingress
*/
export const createOneTrustGotInstance = ({
hostname,
auth,
}: {
/** Hostname of the OneTrust API */
hostname: string;
/** The OAuth access token */
auth: string;
}): Got =>
got.extend({
prefixUrl: `https://${hostname}`,
headers: {
accept: 'application/json',
'content-type': 'application/json',
authorization: `Bearer ${auth}`,
},
});
49 changes: 49 additions & 0 deletions src/oneTrust/getListOfAssessments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Got } from 'got';
import { logger } from '../logger';
import {
OneTrustAssessment,
OneTrustGetListOfAssessmentsResponse,
} from './types';

/**
* Fetch a list of all assessments from the OneTrust client.
*
* @param param - the information about the OneTrust client
* @returns a list of OneTrustAssessment
*/
export const getListOfAssessments = async ({
oneTrust,
}: {
/** The OneTrust client instance */
oneTrust: Got;
}): Promise<OneTrustAssessment[]> => {
let currentPage = 0;
let totalPages = 1;
let totalElements = 0;

const allAssessments: OneTrustAssessment[] = [];

logger.info('Getting list of OneTrust assessments...');
while (currentPage < totalPages) {
// eslint-disable-next-line no-await-in-loop
const { body } = await oneTrust.get(
`api/assessment/v2/assessments?page=${currentPage}&size=2000`,
);
const parsedBody = JSON.parse(body) as OneTrustGetListOfAssessmentsResponse;
const assessments = parsedBody.content ?? [];
allAssessments.push(...assessments);
const page = parsedBody.page ?? { totalPages: 0, totalElements: 0 };
if (currentPage === 0) {
totalPages = page.totalPages;
totalElements = page.totalElements;
}
currentPage += 1;

// log progress
logger.info(
`Fetched ${allAssessments.length} of ${totalElements} assessments.`,
);
}

return allAssessments;
};
3 changes: 2 additions & 1 deletion src/oneTrust/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './pullOneTrustConfiguration';
export * from './getListOfAssessments';
export * from './createOneTrustGotInstance';
5 changes: 0 additions & 5 deletions src/oneTrust/pullOneTrustConfiguration.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/oneTrust/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable max-lines */
interface OneTrustAssessment {
export interface OneTrustAssessment {
/** ID of the assessment. */
assessmentId: string;
/** Date that the assessment was created. */
Expand Down

0 comments on commit 70de777

Please sign in to comment.