@openfn/language-varo 1.0.3 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,34 +1,26 @@
1
1
  # language-varo <img src='./assets/square.png' width="30" height="30"/>
2
2
 
3
- The Varo adaptor works in conjunction with the Gmail adaptor. It takes proprietary data formats found in `state.data` and attempts to convert them to EMS format.
3
+ The Varo adaptor is designed to work in conjunction with the Gmail adaptor.
4
4
 
5
- ## Setup the primary workflow and jobs
5
+ The following workflows have been illustrated:
6
+ - Gmail -> EMS report
7
+ Take proprietary data formats found in Gmail messages and convert them to EMS-compliant format.
8
+ - CCDX data collection -> Gmail
9
+ Retrieve data found in an OpenFn collection, convert it to a EMS report, assemble it as a Varo package and send it to a Varo inbox to be processed by Pogo LT.
10
+
11
+ # Gmail -> EMS report
6
12
 
7
13
  This will demonstrate how to pull messages from a Gmail account and pass them to the Varo adaptor which will convert them into EMS format to be used for downstream consumers in the workflow.
8
14
 
9
15
  The workflow requires some pre-configuration, namely the Gmail `access_token` and the OpenFn `collection_token`.
10
16
 
11
- ### Token configuration
12
-
13
- #### Gmail token
14
-
15
- Use Postman to retrieve an access token. This is a short-lived token will last 60 minutes and will have to be manually retrieved. See the documentation in the [Gmail adaptor readme](https://docs.openfn.org/adaptors/packages/gmail-readme#use-the-postman-application-to-query-the-oauth-enpoint-and-retrieve-an-access-token) for a guide on how to configure Postman to retrieve the access token.
16
-
17
- #### OpenFn collections token
17
+ ## Example workflow
18
18
 
19
- The workflow tracks the previously processed messages by storing the processed IDs in an OpenFn collection. OpenFn requires authorization to access a given collection.
20
-
21
- 1. Access the [Collections](http://localhost:4000/settings/collections) configuration in the administration area.
22
- 1. Ensure the collection named `gmail-processed-ids` exists.
23
- 1. Create a new token in the [Personal Access Tokens](http://localhost:4000/profile/tokens) configuration.
19
+ Place these files in the openfn/adaptors/workflows/readVaroPackages folder.
24
20
 
25
- ### Workflow
21
+ ### workflow.json
26
22
 
27
- Place these files in the openfn/adaptors/workflows folder
28
-
29
- #### workflow.json
30
-
31
- ```
23
+ ```js
32
24
  {
33
25
  "options": {
34
26
  "start": "getContentsFromMessages"
@@ -63,13 +55,11 @@ Place these files in the openfn/adaptors/workflows folder
63
55
  }
64
56
  ```
65
57
 
66
- ### Jobs
67
-
68
- #### jobGmail.js
58
+ ### jobGmail.js
69
59
 
70
60
  This job will define message parts of interest including: metadata, data and fridgeTag. Also important is a collection of previously-processed messageIds. This job will grab ids from the `gmail-processed-ids` collection and pass them into the request. Once the request is complete, the new, processed messageIds are placed into the collection for future retrieval.
71
61
 
72
- ```
62
+ ```js
73
63
  const contents = [
74
64
  { name: "metadata", file: /\d{12}_\d{8}T\d{6}Z\.json$/ },
75
65
  { name: "data", file: /_CURRENT_DATA_.*\.json$/ },
@@ -97,13 +87,13 @@ fnIf(
97
87
  );
98
88
  ```
99
89
 
100
- #### jobVaro.js
90
+ ### jobVaro.js
101
91
 
102
92
  The function `convertToEms` expects an array of message contents. This property contains text files in EMS-like Varo format or FridgeTag format and transforms them into EMS-structured data. Tip: This format is automatically provided by the Gmail adaptor.
103
93
 
104
94
  Expected data structure:
105
95
 
106
- ```
96
+ ```js
107
97
  [
108
98
  {
109
99
  metadata: {
@@ -119,7 +109,7 @@ Expected data structure:
119
109
  ```
120
110
 
121
111
 
122
- ```
112
+ ```js
123
113
  convertToEms($.data);
124
114
 
125
115
  fn((state) => {
@@ -128,12 +118,95 @@ fn((state) => {
128
118
  });
129
119
  ```
130
120
 
131
- ### Running the workflow
121
+ # CCDX data collection -> Gmail
122
+
123
+ Following is an example demonstrating how to retrieve data found in an OpenFn collection, convert it to a EMS report, assemble it as a Varo package and send it to a Varo inbox to be processed by Pogo LT.
124
+
125
+ ## Example workflow
126
+
127
+ Place these files in the openfn/adaptors/workflows/sendVaroPackage folder.
128
+
129
+ ### workflow.js
130
+ ```js
131
+ {
132
+ "workflow": {
133
+ "steps": [
134
+ {
135
+ "id": "convertRecordsToMessageContent",
136
+ "adaptor": "varo",
137
+ "expression": "jobVaro.js",
138
+ "next": {
139
+ "sendMessage": "state.data != null"
140
+ }
141
+ },
142
+ {
143
+ "id": "sendMessage",
144
+ "adaptor": "gmail",
145
+ "expression": "jobGmail.js",
146
+ "configuration": {
147
+ "access_token": "[redacted]"
148
+ }
149
+ }
150
+ ]
151
+ }
152
+ }
153
+ ```
154
+
155
+ ### jobVaro.js
156
+
157
+ ```js
158
+ // Pull the data from the collection into the state.
159
+ collections.get("ccdx-ems", "*");
160
+
161
+ // Convert the raw collection records into an EMS report.
162
+ convertRecordsToReport($.data);
163
+
164
+ // Convert the EMS report to message contents (subject, data file).
165
+ convertReportToMessageContent($.data, "ems");
166
+ ```
167
+
168
+ ### jobGmail.js
169
+
170
+ ```js
171
+ fn((state) => {
172
+ const { subject, data } = state.data;
173
+
174
+ /*
175
+ subject = "OpenFn | EMS";
176
+ data = {
177
+ filename: "data.json",
178
+ content: "{ ... EMS report converted to JSON string ... }",
179
+ };
180
+ */
181
+
182
+ // Compress the data.json file into a ZIP file.
183
+ const dataArchive = {
184
+ filename: "data.zip",
185
+ archive: [data],
186
+ };
187
+
188
+ state.data = {
189
+ to: "receiver@gmail.com",
190
+ subject,
191
+ attachments: [dataArchive],
192
+ };
193
+
194
+ return state;
195
+ });
196
+
197
+ sendMessage($.data);
198
+ ```
199
+
200
+ # Running workflow
201
+
202
+ We can look in more detail at the Gmail -> EMS report workflow and illustrate some advanced techniques to enhance the development experience and operation.
203
+
204
+ ## Basics
132
205
 
133
206
  The `-m` flag tells OpenFn to use the monorepo instead of its own adaptor cache.
134
207
 
135
208
  ```
136
- cd openfn/adaptors/workflows
209
+ cd openfn/adaptors/workflows/readVaroPackages
137
210
  openfn workflow.json -m
138
211
  ```
139
212
 
@@ -141,7 +214,7 @@ openfn workflow.json -m
141
214
 
142
215
  It's beneficial to isolate the Varo adaptor during development to avoid the redundant step of repeatedly querying Gmail which also requires refreshing the access token each hour. We can use advanced functionality of the OpenFn CLI to cache the output of the Gmail step which will enable us reuse its output while we are enhancing the Varo adaptor.
143
216
 
144
- ### Cache Gmail adaptor output
217
+ ### Cache the output from a step
145
218
 
146
219
  We can configure the workflow to retrieve this message content from a local file which will bypass the need to use the Gmail adaptor to retrieve this information.
147
220
 
@@ -153,7 +226,7 @@ We can configure the workflow to retrieve this message content from a local file
153
226
  openfn workflow.json -m --cache-steps --only getContentsFromMessages
154
227
  ```
155
228
 
156
- ### Running the workflow with cached Gmail adaptor output
229
+ ### Running with a cached step
157
230
 
158
231
  - `-m` Use the monorepo.
159
232
  - `--only convertToEms` Execute only the convertToEms step.
@@ -162,6 +235,22 @@ openfn workflow.json -m --cache-steps --only getContentsFromMessages
162
235
  openfn workflow.json -m --only convertToEms
163
236
  ```
164
237
 
238
+ # Token configuration
239
+
240
+ Some workflows required authorization to access the resources.
241
+
242
+ ## Gmail token
243
+
244
+ Use Postman to retrieve an access token. This is a short-lived token will last 60 minutes and will have to be manually retrieved. See the documentation in the [Gmail adaptor readme](https://docs.openfn.org/adaptors/packages/gmail-readme#use-the-postman-application-to-query-the-oauth-enpoint-and-retrieve-an-access-token) for a guide on how to configure Postman to retrieve the access token.
245
+
246
+ ## OpenFn collections token
247
+
248
+ A workflow may track the previously processed messages by storing the processed IDs in an OpenFn collection. OpenFn requires authorization to access a given collection.
249
+
250
+ 1. Access the [Collections](http://localhost:4000/settings/collections) configuration in the administration area.
251
+ 1. Ensure the collection named `gmail-processed-ids` exists.
252
+ 1. Create a new token in the [Personal Access Tokens](http://localhost:4000/profile/tokens) configuration.
253
+
165
254
  # Enhancing/developing the Varo adaptor
166
255
 
167
256
  These instructions will illustrate how to install OpenFn and the adaptor monorepo. The monorepo gives you access to in-development versions of the adaptors.
@@ -176,7 +265,6 @@ openfn
176
265
  └── workflows
177
266
  ```
178
267
 
179
-
180
268
  ## Install OpenFn
181
269
 
182
270
  Prerequisite is Docker in installed, up-to-date, and running on your computer.
package/ast.json CHANGED
@@ -47,6 +47,125 @@
47
47
  ]
48
48
  },
49
49
  "valid": true
50
+ },
51
+ {
52
+ "name": "convertItemsToReports",
53
+ "params": [
54
+ "items",
55
+ "reportType"
56
+ ],
57
+ "docs": {
58
+ "description": "Read a collection of EMS-like data records and convert them to a standard EMS report/record format.\nSystematically separates report properties from record properties.",
59
+ "tags": [
60
+ {
61
+ "title": "public",
62
+ "description": null,
63
+ "type": null
64
+ },
65
+ {
66
+ "title": "function",
67
+ "description": null,
68
+ "name": null
69
+ },
70
+ {
71
+ "title": "param",
72
+ "description": "Array of EMS-like JSON objects.",
73
+ "type": {
74
+ "type": "NameExpression",
75
+ "name": "Array"
76
+ },
77
+ "name": "items"
78
+ },
79
+ {
80
+ "title": "param",
81
+ "description": "Optional. Source of the report, e.g., \"ems\" or \"rtmd\".",
82
+ "type": {
83
+ "type": "OptionalType",
84
+ "expression": {
85
+ "type": "NameExpression",
86
+ "name": "string"
87
+ }
88
+ },
89
+ "name": "reportType",
90
+ "default": "'unknown'"
91
+ },
92
+ {
93
+ "title": "state",
94
+ "description": "{Array} data - The converted, EMS-compliant report with records."
95
+ },
96
+ {
97
+ "title": "returns",
98
+ "description": null,
99
+ "type": {
100
+ "type": "NameExpression",
101
+ "name": "Operation"
102
+ }
103
+ },
104
+ {
105
+ "title": "example",
106
+ "description": "convertItemsToReport(\n [\n { \"ASER\": \"BJBC 08 30\", \"ABST\": \"20241205T004440Z\", \"TVC\": 5.0 },\n { \"ASER\": \"BJBC 08 30\", \"ABST\": \"20241205T005440Z\", \"TVC\": 5.2 },\n ]\n);\n\nstate.data becomes:\n{\n \"ASER\": \"BJBC 08 30\",\n records: [\n { \"ABST\": \"20241205T004440Z\", \"TVC\": 5.0 },\n { \"ABST\": \"20241205T005440Z\", \"TVC\": 5.2 },\n ],\n}",
107
+ "caption": "Convert data to EMS-compliant data."
108
+ }
109
+ ]
110
+ },
111
+ "valid": true
112
+ },
113
+ {
114
+ "name": "convertReportsToMessageContents",
115
+ "params": [
116
+ "reports",
117
+ "reportType"
118
+ ],
119
+ "docs": {
120
+ "description": "Converts an EMS-compliant report into Varo-compatible message components.",
121
+ "tags": [
122
+ {
123
+ "title": "public",
124
+ "description": null,
125
+ "type": null
126
+ },
127
+ {
128
+ "title": "function",
129
+ "description": null,
130
+ "name": null
131
+ },
132
+ {
133
+ "title": "param",
134
+ "description": "EMS-compliant report objects.",
135
+ "type": {
136
+ "type": "NameExpression",
137
+ "name": "Object"
138
+ },
139
+ "name": "reports"
140
+ },
141
+ {
142
+ "title": "param",
143
+ "description": "Optional. Source of the report, e.g., \"ems\" or \"rtmd\".",
144
+ "type": {
145
+ "type": "OptionalType",
146
+ "expression": {
147
+ "type": "NameExpression",
148
+ "name": "string"
149
+ }
150
+ },
151
+ "name": "reportType",
152
+ "default": "'unknown'"
153
+ },
154
+ {
155
+ "title": "returns",
156
+ "description": "An operation function that receives `state` and returns updated message content.",
157
+ "type": {
158
+ "type": "NameExpression",
159
+ "name": "Function"
160
+ }
161
+ },
162
+ {
163
+ "title": "example",
164
+ "description": "// Convert EMS-compliant reports to Varo message components.\nconvertReportsToMessageContents(emsReports, \"ems\");"
165
+ }
166
+ ]
167
+ },
168
+ "valid": true
50
169
  }
51
170
  ],
52
171
  "exports": [],