Customizing exported data with targetAction


#1

An example of using targetAction to customize a batch export output. It’s basically for each facility try to export the billing account that have a specific status:

var args = { spec: FacilityExportSpec.make({status: 'DEACTIVATED'}) };
var targetType = TypeRef.make({typeName: "Facility"});

// this will get all facilities and for each one will call the `targetAction` with it's ID
var spec = BatchExportSpec.make({
  filter: '1==1',
  targetAction: { typeName: "Facility", actionName: "generateDataForExport", args: args },
  numFiles: ids.size(),
  numObjPerFile: 1000,
  contentType: 'text/csv',
  csvHeader: 'result', // list of fields from the target function result type
  targetPath: 'some-path'
});

var ex = Export.startExport(spec);

You need to define the function that will be used by the export:

remix type Facility {
  generateDataForExport: function(spec: FacilityExportSpec): string js all
}
type FacilityExportSpec {
   // you need to have an ids or filter field
  ids: [string]
  status: string
}
type FacilityExportResult {
   // some fields
  result: string
}

Finally the implementation of the export function:

function generateDataForExport(spec) {
  var facility = Facility.fetch({filter: Filter.interests('id', spec.ids), include: 'billingAccounts.this'}).objs[0];
  var billingAccounts = facility.billingAccounts
      .filter(function(ba) {return ba.accountStatus===spec.status})
      .map(function(ba) {return ba.number})
      .join(',');
  return FacilityExportResult.make({
    result: [facility.id, facility.name, billingAccounts].join('I')
  })
}

BatchExportSpec creating pipe ('|') delimited files