Skip to content

Commit

Permalink
enrich assessments with respondents information
Browse files Browse the repository at this point in the history
  • Loading branch information
abrantesarthur committed Jan 21, 2025
1 parent bb87789 commit 13e60f7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 13 deletions.
10 changes: 10 additions & 0 deletions src/oneTrust/codecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@ export const OneTrustEnrichedAssessmentResponse = t.type({
}),
}),
),
respondents: t.array(
t.intersection([
t.type({
...OneTrustGetAssessmentResponse.props.respondent.props,
}),
t.partial({
...OneTrustUserDetails.props,
}),
]),
),
createdBy: OneTrustEnrichedUser,
sections: t.array(OneTrustEnrichedAssessmentSection),
});
Expand Down
22 changes: 20 additions & 2 deletions src/oneTrust/helpers/enrichOneTrustAssessment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const enrichOneTrustAssessment = ({
riskDetails,
creatorDetails,
approversDetails,
respondentsDetails,
}: {
/** The OneTrust risk details */
riskDetails: OneTrustGetRiskResponse[];
Expand All @@ -30,6 +31,8 @@ export const enrichOneTrustAssessment = ({
creatorDetails: OneTrustGetUserResponse;
/** The OneTrust assessment approvers details */
approversDetails: OneTrustGetUserResponse[];
/** The OneTrust assessment internal respondents details */
respondentsDetails: OneTrustGetUserResponse[];
}): OneTrustEnrichedAssessment => {
const riskDetailsById = keyBy(riskDetails, 'id');
const { sections, createdBy, ...restAssessmentDetails } = assessmentDetails;
Expand All @@ -39,7 +42,6 @@ export const enrichOneTrustAssessment = ({
const { risks, ...restQuestion } = question;
const enrichedRisks = (risks ?? []).map((risk) => {
const details = riskDetailsById[risk.riskId];
// FIXME: missing the risk meta data and links to the assessment
return {
...risk,
description: details.description,
Expand All @@ -64,6 +66,7 @@ export const enrichOneTrustAssessment = ({
};
});

// grab creator details
const enrichedCreatedBy = {
...createdBy,
active: creatorDetails.active,
Expand All @@ -74,6 +77,7 @@ export const enrichOneTrustAssessment = ({
familyName: creatorDetails.name.familyName ?? null,
};

// grab approvers details
const approverDetailsById = keyBy(approversDetails, 'id');
const enrichedApprovers = assessmentDetails.approvers.map(
(originalApprover) => ({
Expand All @@ -92,12 +96,26 @@ export const enrichOneTrustAssessment = ({
}),
);

// combine the two assessments into a single enriched result
// grab respondents details
const respondentsDetailsById = keyBy(respondentsDetails, 'id');
const enrichedRespondents = assessmentDetails.respondents.map(
(respondent) => ({
...respondent,
active: respondentsDetailsById[respondent.id].active,
userType: respondentsDetailsById[respondent.id].userType,
emails: respondentsDetailsById[respondent.id].emails,
title: respondentsDetailsById[respondent.id].title,
givenName: respondentsDetailsById[respondent.id].name.givenName ?? null,
familyName: respondentsDetailsById[respondent.id].name.familyName ?? null,
}),
);

// combine everything into a single enriched assessment
return {
...assessment,
...restAssessmentDetails,
approvers: enrichedApprovers,
respondents: enrichedRespondents,
createdBy: enrichedCreatedBy,
sections: sectionsWithEnrichedRisk,
};
Expand Down
53 changes: 42 additions & 11 deletions src/oneTrust/helpers/syncOneTrustAssessments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,20 @@ export const syncOneTrustAssessments = async ({
});

// fetch assessment's creator information
logger.info(
`Fetching details about assessment ${index + 1} of ${
assessments.length
} creator...`,
);
const creatorId = assessmentDetails.createdBy.id;
const creator =
oneTrustCachedUsers[creatorId] ??
(await getOneTrustUser({
let creator = oneTrustCachedUsers[creatorId];
if (!creator) {
logger.info(
`Fetching details about assessment ${index + 1} of ${
assessments.length
} creator...`,
);
creator = await getOneTrustUser({
oneTrust,
userId: creatorId,
}));
oneTrustCachedUsers[creatorId] = creator;
});
oneTrustCachedUsers[creatorId] = creator;
}

// fetch assessment approvers information
const { approvers } = assessmentDetails;
Expand All @@ -105,6 +106,35 @@ export const syncOneTrustAssessments = async ({
);
}

// fetch assessment internal respondents information
const { respondents } = assessmentDetails;
// internal respondents names are not emails.
const internalRespondents = respondents.filter(
(r) => !r.name.includes('@'),
);
let respondentsDetails: OneTrustGetUserResponse[] = [];
if (internalRespondents.length > 0) {
logger.info(
`Fetching details about ${
internalRespondents.length
} internal responds for assessment ${index + 1} of ${
assessments.length
}...`,
);
respondentsDetails = await map(
internalRespondents.map(({ id }) => id),
async (userId) => {
let respondent = oneTrustCachedUsers[userId];
if (!respondent) {
respondent = await getOneTrustUser({ oneTrust, userId });
oneTrustCachedUsers[userId] = respondent;
}
return respondent;
},
{ concurrency: 5 },
);
}

// fetch assessment risk information
let riskDetails: OneTrustGetRiskResponse[] = [];
const riskIds = uniq(
Expand All @@ -129,13 +159,14 @@ export const syncOneTrustAssessments = async ({
);
}

// enrich the assessments with risk and details
// enrich the assessments with user and risk details
const enrichedAssessment = enrichOneTrustAssessment({
assessment,
assessmentDetails,
riskDetails,
creatorDetails: creator,
approversDetails,
respondentsDetails,
});

if (dryRun && file && fileFormat) {
Expand Down

0 comments on commit 13e60f7

Please sign in to comment.