Skip to main content

Upsert Risk and Vulnerability Assessment - Service Referral

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

Metadata

  • Name: Upsert Risk and Vulnerability Assessment - Service Referral
  • Adaptor: @openfn/language-salesforce
  • Adaptor Version: v2.7.4
  • Created about 3 years ago
  • Updated 2 months ago
  • Score: 75 (an indicator of how useful this job may be)

Key Functions

alterState, dataValue, field, fields, join, map, relationship, upsertIf, Array

Expression

// Your job goes here.
alterState(state => {
function split(str, separator) {
if (!str) {
return [];
} else {
return str.toString().split(separator);
}
}
function checked(key, str) {
if (!str) return false;
return split(str.toString(), ' ').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 split(str.toString(), '_')
.map(word => word.replace(/dont/i, "don't"))
.join('_')
.toString()
.trim();
}

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

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

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

function transformHowOften(str) {
if (!str) return;
if (str === 'special_occasions') return 'Special Occasions';
if (str === 'no_dont_want_to_disclose') return "Don't want to disclose";
if (str === 'dont_want_to_disclose') return "Don't want to disclose";
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 split(str, ' ')
.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.client_information.gender = capitalizeFirstLetter(state.data.form.client_information.gender);

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

upsertIf(
dataValue('perform_a_risk_assessment'),
'Risk_Assessment__c',
'CommCare_Ext_ID__c',
fields(
field('CommCare_Ext_ID__c', dataValue('id')),
field('Date__c', dataValue('form.risk_assessment.date')),
field('Participant_Name__c', dataValue('form.client_information.participant_full_name')),
field('Participant_Age__c', state => {
const dob = dataValue('form.client_information.date_of_birth')(state);

return state.helperFunctions.getAge(dob);
}),
relationship('Site2__r', 'CommCare_Ext_ID__c', dataValue('form.client_information.site')),
relationship('EventLink__r', 'CommCare_Ext_ID__c', dataValue('form.client_information.intervention_name')),
relationship('Venue__r', 'CommCare_Ext_ID__c', dataValue('form.client_information.venue')),

field('Participant_Sex__c', dataValue('form.client_information.gender')),
field('X1_0_Sexually_Active_past_12_month__c', state =>
state.helperFunctions.transformGenericText(
dataValue('form.risk_assessment.sexual_risk_and_pregnancy.sexually_active_12_months')(state)
)
),
field('X1_1_Age_at_first_sex__c', state =>
state.helperFunctions.transformAgeRange(
dataValue('form.risk_assessment.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.risk_assessment.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.risk_assessment.sexual_risk_and_pregnancy.sex_monetary_material')(state)
)
),
field('Do_you_know_your_HIV_status__c', state =>
state.helperFunctions.transformHIVStatus(
dataValue('form.risk_assessment.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.risk_assessment.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.risk_assessment.hiv_status.are_you_on_treatment_art')(state)
)
),
field('X3_0_Had_an_STI_in_the_last_12months_yea__c', state =>
state.helperFunctions.transformHowOften(dataValue('form.risk_assessment.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.risk_assessment.sti_and_pep.knowing_partners_hiv_status')(state)
)
),
field('X4_0_Forms_of_abuse_experienced_before__c', state => {
const abusesArray = state.helperFunctions.split(
dataValue('form.risk_assessment.gender_based_violence.abuse_experienced')(state),
' '
);
const abuses = [];
abusesArray.forEach(ab => {
abuses.push(state.helperFunctions.transformGenericText(ab));
});
return abuses.join(';');
}),
field('X4_1_Feel_unsafe_any1_interact_with_ofte__c', state =>
state.helperFunctions.transformGenericText(
dataValue('form.risk_assessment.gender_based_violence.unsafe_with_others')(state)
)
),
field('X4_2_Of_what_relationship_person_to_you__c', state =>
state.helperFunctions.transformGenericText(
dataValue('form.risk_assessment.gender_based_violence.relationship_of_abuser')(state)
)
),
field('X5_0_Do_you_take_alcohol_or_drugs__c', state =>
state.helperFunctions.transformGenericText(
dataValue('form.risk_assessment.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.risk_assessment.drugs_and_alcohol_use.how_often_alcohol')(state)
)
),
field('X5_2_On_average_how_often_take_drugs__c', state =>
state.helperFunctions.transformHowOften(
dataValue('form.risk_assessment.drugs_and_alcohol_use.how_often_drugs')(state)
)
),
field('X5_3_Easy_for_teens_to_obtain_alcohol__c', state =>
state.helperFunctions.transformGenericText(
dataValue('form.risk_assessment.drugs_and_alcohol_use.alcohol_19')(state)
)
),
field('X5_4__c', state =>
state.helperFunctions.transformGenericText(
dataValue('form.risk_assessment.drugs_and_alcohol_use.illegal_drugs_19')(state)
)
)
)
);

fn(state => {
state.data.intervention_name = state.data.form.client_information.intervention_name
? [relationship('Event__r', 'CommCare_Ext_ID__c', dataValue('form.client_information.intervention_name'))]
: [];

return state;
});

upsertIf(dataValue('complete_service_referrals'), 'NewReferral__c', 'CommCare_Ext_ID__c', state => ({
...fields(
field('CommCare_Ext_ID__c', state => {
if (state.data.form.subcase_0.case) return state.data.form.subcase_0.case['@case_id'];
else if (state.data.form.referral_services) {
if (state.data.form.referral_services.art_support_services.skillz_plus_club_details)
return state.data.form.referral_services.art_support_services.skillz_plus_club_details
.create_skillz_plus_club_participant_case.case['@case_id'];
}
return undefined;
}),
field('Participants_Full_Name__c', dataValue('form.client_information.participant_full_name')),
field('Service_Provider_Name__c', dataValue('form.client_information.coach_name')),
field('Parent_Guardian_Full_Name__c', dataValue('form.parent__guardian_information.parent__guardian_full_name')),
field('Relationship__c', dataValue('form.parent__guardian_information.relationship_to_client')),
//field('', dataValue('form.parent__guardian_information.please_specify')),
field('Parent_Guardian_Contact_Number__c', dataValue('form.parent__guardian_information.contact_number')),
//field('', dataValue('form.parent__guardian_information.guardian_consent')),
field('Institution_Referred_To_1__c', dataValue('form.referral_information.institution_referred_to')),
field('Institution_Referred_To_2__c', dataValue('form.referral_information.copy-1-of-institution_referred_to')),
field('Institution_Referred_To_3__c', dataValue('form.referral_information.copy-2-of-institution_referred_to')),
field('HIV_Testing_Services_GRS_Staff__c', state =>
state.helperFunctions.checked(
'hiv_testing_services',
dataValue('form.referral_services.hiv_support__care.check_the_services_that_client_was_referred_to')(state)
)
),
field('HIV_Other__c', dataValue('form.referral_services.hiv_support__care.please_specify')),
field('ART_Initiation_GRS_Staff__c', state =>
state.helperFunctions.checked(
'art_initiationenrollment',
dataValue('form.referral_services.art_support_services.check_the_services_that_client_was_referred_to')(state)
)
),
field('SKILLZ_Plus_Club_Support_GRS_Staff__c', state =>
state.helperFunctions.checked(
'grs_skillz_plus_club',
dataValue('form.referral_services.art_support_services.skillz_plus_club_option')(state)
)
),
field('PMTCT_GRS_Staff__c', state =>
state.helperFunctions.checked(
'pmtct',
dataValue('form.referral_services.art_support_services.check_the_services_that_client_was_referred_to')(state)
)
),
field('VMMC_GRS_Staff__c', state =>
state.helperFunctions.checked(
'vmmc',
dataValue('form.referral_services.hiv_support__care.check_the_services_that_client_was_referred_to')(state)
)
),
field('TB_Screening_GRS_Staff__c', state =>
state.helperFunctions.checked(
'tb_screening_treatment_adherence',
dataValue('form.referral_services.hiv_support__care.check_the_services_that_client_was_referred_to')(state)
)
),
field('Legal_Other__c', dataValue('form.referral_services.legal_services.please_specify_other_legal')),
field('Post_Exposure_Prophylaxis_GRS_Staff__c', state =>
state.helperFunctions.checked(
'post_exposure_prophylaxis',
dataValue('form.referral_services.hiv_support__care.check_the_services_that_client_was_referred_to')(state)
)
),
field('PrEP_GRS_Staff__c', state =>
state.helperFunctions.checked(
'prep',
dataValue('form.referral_services.hiv_support__care.check_the_services_that_client_was_referred_to')(state)
)
),
field('HIV_STI_PREVENTION_Other_GRS_Staff__c', state =>
state.helperFunctions.checked(
'other',
dataValue('form.referral_services.hiv_support__care.check_the_services_that_client_was_referred_to')(state)
)
),
field('HIV_Other__c', dataValue('form.referral_services.hiv_support__care.please_specify')),
field(
'Victim_Friendly_Services_GRS_Staff__c',
state =>
state.helperFunctions.checked(
'suspected_abuse_reported_police_court_other_authority',
dataValue(
'form.referral_services.child_protection_support_services.check_the_services_that_client_was_referred_to'
)(state)
) ||
state.helperFunctions.checked(
'post_violence_care_medical_legal_counseling',
dataValue(
'form.referral_services.child_protection_support_services.check_the_services_that_client_was_referred_to'
)(state)
) ||
state.helperFunctions.checked(
'child_support_grant_social_foster_general_support',
dataValue(
'form.referral_services.child_protection_support_services.check_the_services_that_client_was_referred_to'
)(state)
) ||
state.helperFunctions.checked(
'other',
dataValue(
'form.referral_services.child_protection_support_services.check_the_services_that_client_was_referred_to'
)(state)
)
),

field('Sexual_and_GBV_Abuse_GRS_Staff__c', state =>
state.helperFunctions.checked(
'sexual__gender-based_violenceabus',
dataValue('form.referral_services.other_srhr_services.check_the_services_that_client_was_referred_to')(state)
)
),

// field('missing in SF', dataValue('form.referral_services.child_protection_support_services.type_of_post_violence_care')),
// field('missing in SF', dataValue('form.referral_services.child_protection_support_services.child_support_grant_for')),
field(
'Legal_Services_Other_GRS_Staff__c',
state =>
state.helperFunctions.checked(
'other',
dataValue('form.referral_services.legal_services.legal_services')(state)
) ||
state.helperFunctions.checked('id', dataValue('form.referral_services.legal_services.legal_services')(state)) ||
state.helperFunctions.checked(
'birth_bertificate',
dataValue('form.referral_services.legal_services.legal_services')(state)
)
),
field('Legal_Other__c', dataValue('form.referral_services.legal_services.please_specify_other_legal')),
field('IPTG_Health_Facility__c', state =>
state.helperFunctions.checked(
'iptg_to_health_facility',
dataValue('form.referral_services.mental_health_services.mental_health_select_services')
)
),
field('IPTG_Specialized_to_SMZ__c', state =>
state.helperFunctions.checked(
'iptg_specialised_to_smz',
dataValue('form.referral_services.mental_health_services.mental_health_select_services')
)
),
field('STI_Screen_Testing_GRS_Staff__c', state =>
state.helperFunctions.checked(
'sti_screening_diagnosis_treatment',
dataValue('form.referral_services.other_srhr_services.check_the_services_that_client_was_referred_to')(state)
)
),
field('Contraception_Family_Plan_GRS_Staff__c', state =>
state.helperFunctions.checked(
'contraceptivesfamily_planning',
dataValue('form.referral_services.other_srhr_services.check_the_services_that_client_was_referred_to')(state)
)
),
field('Cervical_Cancer_Screening_GRS_Staff__c', state =>
state.helperFunctions.checked(
'cervical_cancer_screening',
dataValue('form.referral_services.other_srhr_services.check_the_services_that_client_was_referred_to')(state)
)
),
field('HPV_vaccine_GRS_Staff__c', state =>
state.helperFunctions.checked(
'hpv',
dataValue('form.referral_services.other_srhr_services.check_the_services_that_client_was_referred_to')(state)
)
),
field('Antenatal_Care_ANC_GRS_Staff__c', state =>
state.helperFunctions.checked(
'antenatal_care_anc',
dataValue('form.referral_services.other_srhr_services.check_the_services_that_client_was_referred_to')(state)
)
),

field(
'Psycho_Social_Support_GRS_Staff__c',
state =>
state.helperFunctions.checked(
'drug_or_alcohol_abuse',
dataValue('form.referral_services.other_srhr_services.check_the_services_that_client_was_referred_to')(state)
) ||
state.helperFunctions.checked(
'psychiatric_careintensive_counseling',
dataValue('form.referral_services.other_srhr_services.check_the_services_that_client_was_referred_to')(state)
) ||
state.helperFunctions.checked(
'psychosocial_support',
dataValue('form.referral_services.other_srhr_services.check_the_services_that_client_was_referred_to')(state)
)
),
field('Psycho_Social_Services_Other_GRS_Staff__c', state =>
state.helperFunctions.checked(
'other',
dataValue('form.referral_services.other_srhr_services.check_the_services_that_client_was_referred_to')(state)
)
),
field('Psycho_Other__c', dataValue('form.referral_services.other_srhr_services.please_specify_srhr_referred')),

field('Date_Referred__c', dataValue('form.follow-up.date_referred')),
field('Expected_Visit_Date__c', dataValue('form.follow-up.expected_visit_date')),
field('Client_Follow_up_Consent_given__c', state =>
state.helperFunctions.checked('client_follow-up_consent_given', dataValue('form.follow-up.select')(state))
),
field('Client_Linked_to_care__c', state =>
state.helperFunctions.checked('client_linked_to_care', dataValue('form.follow-up.select')(state))
),

field('Business_Unit_Site__c', state => {
const bu = dataValue('form.client_information.business_unit')(state);
return bu === '65680f0c4c144b03ad0f86bdc46c1ebc'
? 'GRS Zambia'
: bu === '04d98397e28046118fade28ced6b65cb'
? 'GRS Zimbabwe'
: bu === 'ed125ab19ec34aacab79585e59eb76f4'
? 'GRS Partnerships'
: undefined;
})
),
...fields(...state.data.intervention_name),
}));