Skip to content

About

Harambee connectors: KoboToolbox and CSV sources, and the Stellar Disbursement Platform client

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

@harambee-aid/connectors

The connectors behind Harambee: sources that read beneficiary submissions, a writer for the instruction file the Stellar Disbursement Platform (SDP) takes, and a client for the SDP itself.

The data types and the validation, dedupe and pricing logic live in @harambee-aid/core. The command line that ties it together is cli.

Install

pnpm add @harambee-aid/connectors @harambee-aid/core

Needs Node 22 or newer. The package is ESM only.

What is in it

Export What it does
CsvSource Reads submissions from a CSV file, such as a KoboToolbox export
KoboSource Reads submissions from the KoboToolbox API, page by page
KoboWriteback Marks submissions Approved, Not Approved or On Hold in Kobo
buildInstructionsCsv Turns transfers from core into the file the SDP expects
SdpClient Creates a disbursement, uploads the file, lists payments

Example

This reads a CSV, builds a batch, stages it in the SDP and lists the payments.

import { buildBatch } from '@harambee-aid/core';
import { CsvSource, SdpClient, buildInstructionsCsv } from '@harambee-aid/connectors';

const submissions = [];
for await (const s of new CsvSource({ path: './registrations.csv' }).read()) submissions.push(s);

const batch = buildBatch(submissions, {
  name: 'November payout',
  mapping: { name: 'name', phone: 'phone', verification: 'dob', householdSize: 'members' },
  validation: { contact: 'PHONE_NUMBER', defaultCountry: 'KE', verification: 'DATE_OF_BIRTH' },
  amount: { rule: { type: 'per_household', base: '10', perMember: '5' } },
});

const sdp = new SdpClient({ baseUrl: process.env.SDP_URL, apiKey: process.env.SDP_API_KEY });

const disbursement = await sdp.createDisbursement({
  name: batch.name,
  assetId: process.env.SDP_ASSET_ID,
  walletId: process.env.SDP_WALLET_ID,
  contactType: 'PHONE_NUMBER',
  verificationField: 'DATE_OF_BIRTH',
});

const csv = buildInstructionsCsv(batch.transfers, { contact: 'PHONE_NUMBER', verification: true });
await sdp.uploadInstructions(disbursement.id, csv);

// the disbursement is now READY. Someone with approval rights starts it in the SDP.
for await (const p of sdp.listPayments({ type: 'DISBURSEMENT' })) {
  console.log(p.externalPaymentId, p.status);
}

Staging, never paying

SdpClient has no method to start, pause or approve a disbursement, and a test keeps it that way. Uploading the instruction file moves a disbursement from DRAFT to READY. Only a person in the SDP can start it, and starting is what begins payments.

How payments are matched back

buildInstructionsCsv writes each submission id into the paymentID column. The SDP returns it as externalPaymentId on every payment, so a payment can always be traced to the submission it came from. The SDP allows at most 64 characters in the id columns, and the writer checks that before anything is uploaded.

Notes on each connector

CsvSource

  • Needs an id column, _id by default (set idColumn to change it). Repeated ids are an error.
  • Reads _submission_time when it is there. Kobo writes it in UTC without a zone, and it is read that way.
  • delimiter defaults to a comma.
  • Set delimiter: 'auto' to detect commas, semicolons, or tabs from the header. Delimiters inside quoted header cells are ignored; ambiguous or single-column headers fall back to a comma. Explicit delimiters still take precedence.

KoboSource

  • Accepts fields: ['group/question', 'household_size'] to request only selected questions on every page. _id and _submission_time are always included in that selection, and duplicate field names are removed. Omitting fields keeps the full submission behavior; fields: [] requests metadata only.
  • Authenticates with your API token and asks for submissions in id order.
  • Follows the next link only when it stays on the configured server, so the token never goes to another host.
  • Repeat groups come through as arrays and are read with core's field mapping, for example members[0]/name.

KoboWriteback

  • Kobo only lets a client set the validation status, so that is all this does. It never edits the answers a respondent gave.
  • Sends submissions in batches and refuses ids that are not numbers before sending any.

SdpClient

  • Authenticates with Authorization: Bearer SDP_.... The key is never put in an error message.
  • The SDP is multi-tenant and picks the tenant from the subdomain, so baseUrl looks like http://acme.stellar.local:8000.
  • Failures raise SdpError with the HTTP status. When the SDP rejects the instruction file, the line-by-line reasons are in error.extras.

Development

pnpm install
pnpm lint
pnpm typecheck
pnpm test
pnpm build

See the contributing guide.

License

Apache-2.0

About

Harambee connectors: KoboToolbox and CSV sources, and the Stellar Disbursement Platform client

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages