Skip to main content

surveycto@3.0.0

cursor(value, options)
fetchSubmissions(formId, options)
jsonToCSVBuffer(rows)
list(resource, options)
uploadCsvRecords(datasetId, rows, metadata)
upsertDataset(data)
upsertRecord(datasetId, data)

This adaptor exports the following namespaced functions:

http.delete(path, params)
http.get(path, params)
http.post(path, params)
http.request(path, params)

This adaptor exports the following from common:

alterState()
chunk()
dataPath()
dataValue()
dateFns
field()
fields()
fn()
fnIf()
lastReferenceValue()
merge()
parseCsv()
sourceValue()

Functions

cursor

cursor(value, options) ⇒ Operation

Sets state.cursor to a SurveyCTO timestamp string (MMM dd, yyy h:mm:ss a). This supports natural language dates like now, today, yesterday, n hours ago, n days ago, and start, which will be converted into timestamp strings. See the usage guide at https://docs.openfn.org/documentation/jobs/job-writing-guide#using-cursors

ParamTypeDescription
valueanythe cursor value. Usually an ISO date, natural language date, or page number
optionsobjectoptions to control the cursor.
options.keystringset the cursor key. Will persist through the whole run.
options.defaultValueanythe value to use if value is falsy
options.formatfunctioncustom formatter for the final cursor value

Example: Use a cursor from state if present, or else use the default value

cursor('today')
fetchSubmissions('test', { date: $.cursor });

fetchSubmissions

fetchSubmissions(formId, options) ⇒ Operation

Fetch form submissions.

If a date filter is provided, it will be converted internally to the surveyCTO MMM dd, yyy h:mm:ss format (in UTC time).

ParamTypeDescription
formIdstringForm id
optionsFetchSubmissionOptionsForm submission date, format, status parameters

This operation writes the following keys to state:

State KeyDescription
datathe parsed response body
responsethe response from the SurveyCTO server, including headers, statusCode etc
referencesan array of all previous data objects used in the Job.

Example: Fetch all form submissions

fetchSubmissions('test');

Example: With SurveyCTO date format (UTC)

fetchSubmissions('test', { date: 'Apr 18, 2024 6:26:21 AM' });

Example: Using a rolling cursor

cursor((state) => state.cursor, { defaultValue: 'today' });
fetchSubmissions('test', { date: (state) => state.cursor, format: 'csv' });
cursor('now');

Example: Formatting the results to CSV String

fetchSubmissions('test', { format: 'csv' });

Example: With reviewStatus filter

fetchSubmissions('test', { status: 'approved|rejected' });

jsonToCSVBuffer

jsonToCSVBuffer(rows) ⇒ Operation

Converts an array of objects to a CSV buffer.

ParamTypeDescription
rows*An array of JSON objects.

Example

jsonToCSVBuffer([
{
lastName: 'Rothfuss',
firstName: 'Patrick',
book: 'The Name of the Wind'
},
{
lastName: 'Martin',
firstName: 'George',
book: 'A Game of Thrones'
},
])

list

list(resource, options) ⇒ Operation

List resources from SurveyCTO

ParamTypeDescription
resourcestringResource to fetch
optionsobjectOptional request query options. See the API docs for details

This operation writes the following keys to state:

State KeyDescription
datathe parsed response body
responsethe response from the server with total and nextCursor
referencesan array of all previous data objects used in the Job.

Properties

NameTypeDescription
options.limitnumberMaximum number of items to return. Defaults to 20. Maximum is 1000.
options.cursorstringOptional string to specify the starting point of the next page of results.

Example: List all dataset records

list(`datasets/${$.datasetId}/records`)

Example: List all datasets

list('datasets')

Example: List dataset records with pagination options

list(`datasets/${$.datasetId}/records`,{
limit: 2,
});

Example: List datasets with pagination options

list('datasets',{
limit: 2,
});

uploadCsvRecords

uploadCsvRecords(datasetId, rows, metadata) ⇒ Operation

Upload CSV dataset records

ParamTypeDescription
datasetIdstringID of the dataset
rowsstringAn array of JSON objects to be uploaded as records. The data will be converted to CSV format before upload.
metadataobjectOptional metadata for configuring how the uploaded data should be processed

This operation writes the following keys to state:

State KeyDescription
datathe parsed response body
responsethe response from the SurveyCTO server, including headers, statusCode etc
referencesan array of all previous data objects used in the Job.

Properties

NameTypeDescription
joiningFieldstringOptional field name to use for merging records. Required when uploadMode is MERGE.
uploadModestringOptional upload mode. One of APPEND (default), MERGE and CLEAR.

Example: Upload records

uploadCsvRecords('enumerators_dataset', [
{
id: '4',
name: 'Trial update',
users: 'All users',
},
{
id: '5',
name: 'Trials',
users: 'All users here',
},
]);

Example: Upload records with metadata

uploadCsvRecords(
'enumerators_dataset',
[
{
id: '4',
name: 'Trial update',
users: 'All users',
},
{
id: '5',
name: 'Trials',
users: 'All users here',
},
],
{
uploadMode: 'MERGE',
joiningField: 'id',
}
);

upsertDataset

upsertDataset(data) ⇒ Operation

Update (if exist) or create a dataset in SurveyCTO

ParamTypeDescription
dataobjectThe dataset object to create or update

This operation writes the following keys to state:

State KeyDescription
datathe parsed response body
responsethe response from the SurveyCTO server, including headers, statusCode etc
referencesan array of all previous data objects used in the Job.

Example: Upsert a dataset

upsertDataset({
id: 'enum_dataset',
title: 'Enum Dataset',
discriminator: 'ENUMERATORS',
locationContext: {
parentGroupId: 1,
siblingAbove: {
id: 'new_dataset',
itemClass: 'DATASET',
},
},
allowOfflineUpdates: false,
idFormatOptions: {
prefix: 'enum',
suffix: '',
numberOfDigits: '8',
allowCapitalLetters: true,
},
});

upsertRecord

upsertRecord(datasetId, data) ⇒ Operation

Update (if exist) or create a dataset record in SurveyCTO

ParamTypeDescription
datasetIdstringID of the dataset
dataobjectThe record object to create or update

This operation writes the following keys to state:

State KeyDescription
datathe parsed response body
responsethe response from the SurveyCTO server, including headers, statusCode etc
referencesan array of all previous data objects used in the Job.

Example: Upsert a dataset record

upsertRecord('enumerators_dataset', {
id: '2',
name: 'Trial update',
users: 'All users',
});

http

These functions belong to the http namespace.

http.delete

delete(path, params) ⇒ Operation

Delete resources from SurveyCTO

ParamTypeDescription
pathstringPath to resource
paramsRequestOptionsQuery and headers parameters

This operation writes the following keys to state:

State KeyDescription
datathe parsed response body
responsethe response from the SurveyCTO server, including headers, statusCode, body, etc
referencesan array of all previous data objects used in the Job

Example: Delete a dataset

http.delete('/datasets/enumerators_dataset');

Example: Delete a dataset record

http.delete('/datasets/enumerators_dataset/record', {
query: {
recordId: 2,
},
});

http.get

get(path, params) ⇒ Operation

Get resources from SurveyCTO

ParamTypeDescription
pathstringPath to resource
paramsRequestOptionsQuery and headers parameters

This operation writes the following keys to state:

State KeyDescription
datathe parsed response body
responsethe response from the SurveyCTO server, including headers, statusCode, body, etc
referencesan array of all previous data objects used in the Job

Example: Get a record with id

http.get('/datasets/enumerators_dataset/record', {
query: {
recordId: '4',
},
});

Example: Get a dataset with id

http.get('/datasets/enumerators_dataset')

Example: Get a dataset in csv format

http.get('/datasets/data/csv/enumerators_dataset', {
query: {
asAttachment: true,
},
});

http.post

post(path, params) ⇒ Operation

Send a HTTP POST request to SurveyCTO

ParamTypeDescription
pathstringPath to resource
paramsRequestOptionsQuery, body, and headers parameters

This operation writes the following keys to state:

State KeyDescription
datathe parsed response body
responsethe response from the SurveyCTO server, including headers, statusCode, body, etc
referencesan array of all previous data objects used in the Job

Example: Purge a dataset

http.post('/datasets/enumeratorse_dataset/purge');

http.request

request(path, params) ⇒ Operation

Make a HTTP request to SurveyCTO

ParamTypeDescription
pathstringPath to resource
paramsRequestOptionsQuery, body, headers and method parameters

This operation writes the following keys to state:

State KeyDescription
datathe parsed response body
responsethe response from the SurveyCTO server, including headers, statusCode, body, etc
referencesan array of all previous data objects used in the Job

Example: Post JSON data to SurveyCTO

http.request("/anEndpoint", {
method: "POST",
contentType: "json",
body: $.data,
});

Example: Upload a CSV blob to a dataset

  http.request('datasets/library/records/upload', {
method: 'POST',
contentType: 'form',
body: {
file: {
blob: $.data,
type: 'text/csv',
filename: 'library.csv'
}
},
});

Interfaces

FetchSubmissionOptions

Options provided to fetchSubmissions()

Properties

NameTypeDefaultDescription
[date]string0Fetch only submissions from this timestamp. Acccepts SuvreyCTO date strings, unix and epoch timestamps, and ISO dates. By default, all submissions will be retrieved.
[format]string"json"Format the submission data type as csv or json.
[status]stringReview status. Can be either, approved, rejected, pending or combine eg `approved

HTTPState

State object

Properties

NameDescription
datathe parsed response body
responsethe response from the SurveyCTO server, including headers, statusCode, body, etc
referencesan array of all previous data objects used in the Job

RequestOptions

Options provided to request()

Properties

NameTypeDefaultDescription
[headers]objectAn object of headers parameters.
[body]objectBody data to append to the request.
[query]objectAn object of query parameters to be encoded into the URL.
[contentType]objectSet the content-type header to the appropriate format. Supported values: json and form
[method]string"GET"The HTTP method to use.

SurveyCTOListState

List State object

Properties

NameDescription
datathe parsed response body
responsethe response from the server with total and nextCursor
referencesan array of all previous data objects used in the Job.

SurveyCTOState

State object

Properties

NameDescription
datathe parsed response body
responsethe response from the SurveyCTO server, including headers, statusCode etc
referencesan array of all previous data objects used in the Job.