Skip to content

Instantly share code, notes, and snippets.

@aleksa-krolls
aleksa-krolls / ValidateContractPaymentinSF_vAK.js
Created November 2, 2018 15:05
Changes to Validate Contract Payment in SF job. It'd be good to discuss the to-do you flag, as well as a more efficient way of writing this. I'm also wondering if alterstate() is just to log an output when testing or leveraged in other ways?
upsert('Payment__c', 'EE_Payment_ID__c', fields(
field("EE_Payment_ID__c", state => {
const { msisdn, District, reference_number, house_number, contract_number, payment_number } = state.data;
const countryCode = (msisdn.substr(0,3) == "250" ? 'RW' : 'Msisdn code not found' );
// =========================================================================
// TODO: Aleksa discuss with TD. Why are you catching this error?
// What will happen when we send 'District code not found' downstream?
function findDistrictCode(district) {
const districtFind = {
//Jobs are evaluated as Javascript...
//Here we are named functions from the OpenFn Mailgun language-pack: https://github.com/openfn/language-mailgun
send(
fields(
field('from', 'sebastian@gmail.com'),
field('to', 'recipient2@email.com'),
field('subject', 'Kobo: New Form Submitted'),
//Using Javascipt, you can write your own custom, anonymous functions to do whatever you desire - see function(state)
//Below I use the OpenFn dataValue() function to create a message & dynamically fill in the recipient name
//Jobs are evaluated as Javascript...
//Here we are named functions from the OpenFn Mailgun language-pack: https://github.com/openfn/language-mailgun
send(
fields(
field('from', 'sebastian@gmail.com'),
//You can send to multiple email addresses by separating with a comma
field('to', 'sebastian@gmail.com, recipient2@email.com'),
field('subject', 'Kobo: New Form Submitted'),
field('text', 'Log into Kobo to review the new form submitted.')
field('Name', (state) => {
return state.data.properties.patient_name + "(" +
state.data.properties.patient_id + ") (" +
state.data.properties.date_opened + ")"
//I used date_opened because i didn't see a visit_date property in the message body data
//you will want to confirm that this is the right date field to user
}),
alterState((state) => {
state.handlePhoto = function handlePhoto(state, photoField) {
const url1 = `https:`
const url2 = `/`
const baseUrl = `/www.commcarehq.org/a/${state.data.domain}/api/form/attachment/`;
const uuid = state.data.metadata.instanceID;
const image = (state.data.form.photos ? state.data.form.photos[`${photoField}`] : null);
return ( image ? `${url1}${url2}${baseUrl}${uuid}/${image}` : "" )
};
//************
field('gciclubfoot__Brace_Count__c', dataValue('form.subcase_0.case.update.brace_count')),
//in below line, try changing humanProper() to datavalue() --> this is line 34 in the job
field('gciclubfoot__Brace_Problems__c', humanProper('form.subcase_0.case.update.brace_problems')), // picklist
field('gciclubfoot__Brace_Problems_Notes__c', dataValue('form.subcase_0.case.update.brace_problems_specified')),
field('gciclubfoot__Brace_Problems_Type__c', (state) => {
return state.handleMultiSelect(state, "brace_problems_type")
}),
field('gciclubfoot__Brace_Type__c', (state) => {
const ref = state.data.form.subcase_0.case.update
field('Bracing_Stage__c', dataValue('properties.bracing_stage')),
/* (<-- add this to comment out a set of lines)
field('gciclubfoot__Diagnosis_Idiopathic_Specified__c', (state) => {
return state.handleMultiSelect(state, "diagnosis_idiopathic_specified")
}),
field('gciclubfoot__Diagnosis_Secondary_Specified__c', (state) => {
return state.handleMultiSelect(state, "diagnosis_secondary_specified")
@aleksa-krolls
aleksa-krolls / createANDenrollTEI.json
Last active April 19, 2019 19:33
Example payload for creating & enrolling a TEI in the Farmer Tracker program
{
"trackedEntity": "UgB6oOE7jYb",
"orgUnit": "dVtKvuWFMaP",
"attributes": [
{
"attribute": "kKbVP14xj1A",
"value": "A35789235"
},
{
"attribute": "mrRDbHRkOSV",
{
"trackedEntityType": "UgB6oOE7jYb",
"orgUnit": "dVtKvuWFMaP",
"attributes": [
{
"attribute": "kKbVP14xj1A",
"value": "A357892346"
},
{
"attribute": "mrRDbHRkOSV",
{
"trackedEntityType": "MCPQUTHX1Ze",
"orgUnit": "nKdtlnZztYd",
"attributes": [
{
"attribute": "TLjYqcKwhxP",
"value": "Alison Newbie"
},
{
"attribute": "ScSWoiqvdp5",