Skip to main content

Risk and Vulnerability Assessment

This job was provided by an OpenFn.org user via the job library API.

Metadata

Key Functions

alterState, dataValue, field, fields, join, map, upsert

Expression

// Your job goes here. Testing github
alterState(state => {
function checked(key, str) {
if (!str) return false;
return str.toString().split(' ').indexOf(key) >= 0;
}

function capitalizeFirstLetter(str) {
if (!str) return;
let new_str = str.toString().toLowerCase();
return new_str.slice(0, 1).toUpperCase() + new_str.slice(1);
}

function transformDont(str) {
if (!str) return str;
return str
.toString()
.split('_')
.map(word => word.replace(/dont/i, "don't"))
.join('_')
.toString()
.trim();
}

function transformCant(str) {
if (!str) return str;
return str
.toString()
.split('_')
.map(word => word.replace(/cant/i, "can't"))
.join('_')
.toString()
.trim();
}

function splitAndJoin(str, splitDelimiter, joinDelimiter) {
if (!str) return;

return str.toString().split(splitDelimiter).join(joinDelimiter).toString().trim();
}

function transformHowOften(str) {
if (!str) return;
if (str === 'special_occasions') return 'Special Occasions';
return transformGenericText(str).toString().trim();
}

function transformHIVStatus(status) {
switch (status) {
case 'yes_negative':
return 'Yes,Negative';
case 'yes_positive':
return 'Yes,Positive';
case 'no_i_dont':
return "No,I don't know";
case 'yes_dont_want_to_disclose':
return "Yes, don't want to disclose";
default:
return;
}
}

function transformLastTested(str) {
if (!str) return;
switch (str.toString()) {
case 'yes_negative':
return 'Yes,Negative';
case 'yes_positive':
return 'Yes,Positive';
case 'no_i_dont':
return "No,I don't know";
case 'yes_dont_want_to_disclose':
return "Yes, don't want to disclose";
default:
return;
}
}

function transformMonthAgo(str) {
if (!str) return;
switch (str) {
case 'month_ago':
return '1 month ago';
case 'months_ago':
return '3 months ago';
case 'month_ago':
return 'copy-1-of-months_ago ';
case 'month_ago':
return '6 months ago';
case 'a_year_or_more_ago':
return 'A year or more';
default:
return;
}
}

function transformGenericText(str) {
if (!str) return;
return capitalizeFirstLetter(splitAndJoin(transformDont(str), '_', ' '))
.toString()
.trim();
}

function transformRange(range) {
if (!range) return;
return range
.toString()
.replace(/\d_\d/i, match => match.replace('_', '-'))
.toString()
.trim();
}

function transformAgeRange(range) {
if (!range) return;
if (range === '10_years') return '< 10 years';
if (range === 'greater_than_19') return '> 19 years';
return capitalizeFirstLetter(transformDont(transformCant(splitAndJoin(transformRange(range), '_', ' '))))
.toString()
.trim();
}

function transformAbuseExperienced(str) {
if (!str) return;
return str
.split(' ')
.map(word => capitalizeFirstLetter(word))
.join(' ')
.toString()
.trim();
}

function getAge(dateString) {
if (!dateString) return;

const today = new Date();
const birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}

state.data.perform_a_risk_assessment = state.data.form.do_you_want_to_perform_a_risk_assessment === 'yes';

state.data.complete_service_referrals = state.data.form.referral_services ? true : false;

state.data.form.hidden_properties.gender =
capitalizeFirstLetter(state.data.form.hidden_properties.gender);

state.helperFunctions = {
checked,
transformAgeRange,
transformHIVStatus,
transformLastTested,
transformMonthAgo,
transformGenericText,
transformDont,
splitAndJoin,
transformHowOften,
transformAbuseExperienced,
getAge,
};
return state;
});

upsert(
'Risk_Assessment__c',
'CommCare_Ext_ID__c',
fields(
field('CommCare_Ext_ID__c', dataValue('id')),
field('Date__c', dataValue('form.date')),
field('Participant_Name__c', state => {
var firstname = state.data.form.hidden_properties.praticipant_first_name;
var lastname = state.data.form.hidden_properties.participant_surname;
return firstname + ' ' + lastname;
}),
field('Participant_Age__c', state => {
const dob = dataValue('form.hidden_properties.date_of_birth')(state);

return state.helperFunctions.getAge(dob);
}),
field('Participant_Sex__c', dataValue('form.hidden_properties.gender')),
field('X1_0_Sexually_Active_past_12_month__c', state =>
state.helperFunctions.transformGenericText(
dataValue('form.sexual_risk_and_pregnancy.sexually_active_12_months')(state)
)
),
field('X1_1_Age_at_first_sex__c', state =>
state.helperFunctions.transformAgeRange(
dataValue('form.sexual_risk_and_pregnancy.age_at_first_sex')(state)
)
),
field('X1_2_Have_you_ever_had_unprotected_sex__c', state =>
state.helperFunctions.transformGenericText(
dataValue('form.sexual_risk_and_pregnancy.have_you_ever_had_unprotected_sex')(state)
)
),
field('X1_3_Sex_for_monetary_or_material_benefi__c', state =>
state.helperFunctions.transformGenericText(
dataValue('form.sexual_risk_and_pregnancy.sex_monetary_material')(state)
)
),
field('Do_you_know_your_HIV_status__c', state =>
state.helperFunctions.transformHIVStatus(
dataValue('form.sexual_risk_and_pregnancy.hiv_status.do_you_know_your_hiv_status')(state)
)
),
field('X2_1_When_last_did_you_test_for_HIV__c', state =>
state.helperFunctions.transformMonthAgo(
dataValue('form.sexual_risk_and_pregnancy.hiv_status.when_last_did_you_test_for_hiv')(state)
)
),
field('X2_2_Are_you_on_treatment_ART__c', state =>
state.helperFunctions.transformGenericText(
dataValue('form.sexual_risk_and_pregnancy.hiv_status.are_you_on_treatment_art')(state)
)
),
field('X3_0_Had_an_STI_in_the_last_12months_yea__c', state =>
state.helperFunctions.transformGenericText(
dataValue('form.sexual_risk_and_pregnancy.hiv_status.sti_and_pep.hiv_last_12_months')(state)
)
),
field('X3_1_Past_12_months_sex_HIV_status_part__c', state =>
state.helperFunctions.transformGenericText(
dataValue('form.sexual_risk_and_pregnancy.hiv_status.sti_and_pep.knowing_partners_hiv_status')(state)
)
),
//PENDING Donny change data type in SF
// field('X4_0_Forms_of_abuse_experienced_before__c', state =>
// state.helperFunctions.transformAbuseExperienced(
// dataValue('form.risk_assessment.gender_based_violence.abuse_experienced')(state)
// )
// ),
field('X4_1_Feel_unsafe_any1_interact_with_ofte__c', state =>
state.helperFunctions.transformGenericText(
dataValue('form.sexual_risk_and_pregnancy.hiv_status.sti_and_pep.gender_based_violence.unsafe_with_others')(state)
)
),
field('X4_2_Of_what_relationship_person_to_you__c', state =>
state.helperFunctions.transformGenericText(
dataValue('form.sexual_risk_and_pregnancy.hiv_status.sti_and_pep.gender_based_violence.relationship_of_abuser')(state)
)
),
field('X5_0_Do_you_take_alcohol_or_drugs__c', state =>
state.helperFunctions.transformGenericText(
dataValue('form.sexual_risk_and_pregnancy.hiv_status.sti_and_pep.gender_based_violence.drugs_and_alcohol_use.do_you_take_alcohol_andor_drugs')(state)
)
),
field('X5_1_On_average_how_often_drink_alcohol__c', state =>
state.helperFunctions.transformHowOften(
dataValue('form.sexual_risk_and_pregnancy.hiv_status.sti_and_pep.gender_based_violence.drugs_and_alcohol_use.how_often_alcohol')(state)
)
),
field('X5_2_On_average_how_often_take_drugs__c', state =>
state.helperFunctions.transformHowOften(
dataValue('form.sexual_risk_and_pregnancy.hiv_status.sti_and_pep.gender_based_violence.drugs_and_alcohol_use.how_often_drugs')(state)
)
),
field('X5_3_Easy_for_teens_to_obtain_alcohol__c', state =>
state.helperFunctions.transformGenericText(
dataValue('form.sexual_risk_and_pregnancy.hiv_status.sti_and_pep.gender_based_violence.drugs_and_alcohol_use.alcohol_19')(state)
)
),
field('X5_4__c', state =>
state.helperFunctions.transformGenericText(
dataValue('form.sexual_risk_and_pregnancy.hiv_status.sti_and_pep.gender_based_violence.drugs_and_alcohol_use.illegal_drugs_19')(state)
)
)
)
);