Is this the right way?

We are testing more the OpenSPP integration and I’m wondering if this is the right way to write the code or if there is something more elegant?

state.references = [];
state.data = {};
createIndividual({ name: "H1" });
createIndividual({ name: "H2" });
createIndividual({ name: "H3" });
createGroup({ name: "HHH" });
fn(async (state) => {
  const groupId = state.data;
  for (const item of state.references) {
    if (typeof item === "string" && item.includes("IND")) {
      await addToGroup(groupId, item)(state);
    }
  }
  return state;
});
1 Like

Welcome, @jeremi !!! And great to see you at the DPGA meeting!

@mtuchi and @jclark are our job/adaptor champions at OpenFn, but if it were up to me I’d write:

createGroup({ name: "HHH" }, state => {
  const groupId = state.data.newId // or wherever this shows up from the server
  return { ...state, groupId }
});

// assuming you have an array of people
each(
  dataPath('people[*]'),
  createIndividual({ name: dataValue("name") })
);

each(
  dataPath('people[*]'),
  addToGroup(
    state => state.groupId,
    dataValue('')
  )
);

whether or not you use each, and no matter how the openSPP helper functions are designed, i’d skip manually adding state.data and state.references at the top, and (because I’m no JS-whiz) I’d shy away from the async stuff at the bottom. Let OpenFn handle the ordering for you.

1 Like