-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
445 lines (432 loc) · 11.2 KB
/
Copy pathsetup.js
File metadata and controls
445 lines (432 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#!/usr/bin/env node
'use strict';
/**
* Creates the four tables Sample Trail writes to, on your own n8n, and seeds the specification
* library with four worked examples.
*
* This exists because the store that needs no credential is otherwise the most tedious to set up:
* fifty three columns typed by hand across four tables, where one wrong name means a node writes
* into nothing. The spreadsheet paths have sheets/*.csv; this is the same thing for Data Tables.
*
* No dependencies, nothing to install. Node 20 or newer.
*
* N8N_URL=https://your-n8n N8N_API_KEY=... node setup.js
* node setup.js --no-seed create the tables, leave them empty
* node setup.js --list print the ids of tables that already exist
*
* Idempotent: it creates what is missing and leaves what exists alone. It never drops a column,
* because dropping one would take an audit record with it.
*
* The ids it prints go into the Settings node of each workflow.
*/
const TABLES = [
{
"key": "register",
"name": "sample_trail_register",
"columns": [
"sample_id",
"lot_ref",
"contract_ref",
"commodity",
"origin",
"shipper",
"spec_id",
"dispatch_ts",
"dispatch_photo",
"carrier_ref",
"receipt_ts",
"receipt_condition",
"receipt_photo",
"sla_due_ts",
"eval_ts",
"evaluator",
"input_type",
"input_url",
"transcript",
"extracted_json",
"verdict",
"verdict_reasons",
"decision",
"decision_by",
"decision_ts",
"decision_note",
"status"
]
},
{
"key": "spec",
"name": "sample_trail_spec",
"columns": [
"spec_id",
"contract_ref",
"commodity",
"param",
"label",
"unit",
"check",
"direction",
"threshold",
"values",
"tolerance_pct",
"required",
"notes"
]
},
{
"key": "log",
"name": "sample_trail_log",
"columns": [
"ts",
"sample_id",
"actor",
"action",
"from_status",
"to_status",
"note"
]
},
{
"key": "counterparty",
"name": "sample_trail_counterparty",
"columns": [
"contract_ref",
"shipper",
"shipper_email",
"customer",
"customer_email",
"notes"
]
}
];
/**
* Four specifications, one per commodity, as worked examples. Every counterparty here is invented.
* Delete them once you have added your own contracts, or leave them: a sample is only ever judged
* against the spec_id its own row names.
*/
const SPEC_SEED = [
{
"spec_id": "SP-COF-01",
"contract_ref": "CT-9902",
"commodity": "Coffee",
"param": "moisture_pct",
"label": "Moisture",
"unit": "%",
"check": "numeric",
"direction": "max",
"threshold": "12.0",
"values": "",
"tolerance_pct": "2",
"required": "yes",
"notes": ""
},
{
"spec_id": "SP-COF-01",
"contract_ref": "CT-9902",
"commodity": "Coffee",
"param": "defect_count",
"label": "Defect count",
"unit": "",
"check": "numeric",
"direction": "max",
"threshold": "5",
"values": "",
"tolerance_pct": "",
"required": "yes",
"notes": "full defects per 300g sample"
},
{
"spec_id": "SP-COF-01",
"contract_ref": "CT-9902",
"commodity": "Coffee",
"param": "grade_score",
"label": "Grade score",
"unit": "",
"check": "numeric",
"direction": "min",
"threshold": "82",
"values": "",
"tolerance_pct": "",
"required": "yes",
"notes": ""
},
{
"spec_id": "SP-COF-01",
"contract_ref": "CT-9902",
"commodity": "Coffee",
"param": "defects",
"label": "Excluded conditions",
"unit": "",
"check": "absent",
"direction": "",
"threshold": "",
"values": "mould, taint, live infestation",
"tolerance_pct": "",
"required": "yes",
"notes": "no tolerance applies to an exclusion"
},
{
"spec_id": "SP-GRN-01",
"contract_ref": "CT-7714",
"commodity": "Grain",
"param": "moisture_pct",
"label": "Moisture",
"unit": "%",
"check": "numeric",
"direction": "max",
"threshold": "14.0",
"values": "",
"tolerance_pct": "2",
"required": "yes",
"notes": ""
},
{
"spec_id": "SP-GRN-01",
"contract_ref": "CT-7714",
"commodity": "Grain",
"param": "foreign_matter_pct",
"label": "Foreign matter",
"unit": "%",
"check": "numeric",
"direction": "max",
"threshold": "2.0",
"values": "",
"tolerance_pct": "",
"required": "yes",
"notes": ""
},
{
"spec_id": "SP-GRN-01",
"contract_ref": "CT-7714",
"commodity": "Grain",
"param": "protein_pct",
"label": "Protein",
"unit": "%",
"check": "numeric",
"direction": "min",
"threshold": "11.5",
"values": "",
"tolerance_pct": "",
"required": "yes",
"notes": ""
},
{
"spec_id": "SP-GRN-01",
"contract_ref": "CT-7714",
"commodity": "Grain",
"param": "defects",
"label": "Excluded conditions",
"unit": "",
"check": "absent",
"direction": "",
"threshold": "",
"values": "mould, live infestation",
"tolerance_pct": "",
"required": "yes",
"notes": ""
},
{
"spec_id": "SP-COC-01",
"contract_ref": "CT-5521",
"commodity": "Cocoa",
"param": "moisture_pct",
"label": "Moisture",
"unit": "%",
"check": "numeric",
"direction": "max",
"threshold": "7.5",
"values": "",
"tolerance_pct": "3",
"required": "yes",
"notes": ""
},
{
"spec_id": "SP-COC-01",
"contract_ref": "CT-5521",
"commodity": "Cocoa",
"param": "bean_count_per_100g",
"label": "Bean count per 100g",
"unit": "",
"check": "numeric",
"direction": "max",
"threshold": "100",
"values": "",
"tolerance_pct": "",
"required": "yes",
"notes": ""
},
{
"spec_id": "SP-COC-01",
"contract_ref": "CT-5521",
"commodity": "Cocoa",
"param": "defects",
"label": "Excluded conditions",
"unit": "",
"check": "absent",
"direction": "",
"threshold": "",
"values": "mould, smoke taint, slaty beans",
"tolerance_pct": "",
"required": "yes",
"notes": ""
},
{
"spec_id": "SP-NUT-01",
"contract_ref": "CT-3308",
"commodity": "Nuts",
"param": "moisture_pct",
"label": "Moisture",
"unit": "%",
"check": "numeric",
"direction": "max",
"threshold": "6.0",
"values": "",
"tolerance_pct": "",
"required": "yes",
"notes": ""
},
{
"spec_id": "SP-NUT-01",
"contract_ref": "CT-3308",
"commodity": "Nuts",
"param": "oil_content_pct",
"label": "Oil content",
"unit": "%",
"check": "numeric",
"direction": "min",
"threshold": "60",
"values": "",
"tolerance_pct": "",
"required": "yes",
"notes": ""
},
{
"spec_id": "SP-NUT-01",
"contract_ref": "CT-3308",
"commodity": "Nuts",
"param": "trash_pct",
"label": "Trash content",
"unit": "%",
"check": "numeric",
"direction": "max",
"threshold": "3",
"values": "",
"tolerance_pct": "",
"required": "no",
"notes": "reported when a reading is taken"
},
{
"spec_id": "SP-NUT-01",
"contract_ref": "CT-3308",
"commodity": "Nuts",
"param": "defects",
"label": "Excluded conditions",
"unit": "",
"check": "absent",
"direction": "",
"threshold": "",
"values": "mould, rancidity, live infestation",
"tolerance_pct": "",
"required": "yes",
"notes": ""
}
];
const COUNTERPARTY_SEED = [
{
"contract_ref": "CT-9902",
"shipper": "Northwind Produce Ltd",
"shipper_email": "dispatch@northwind.example.invalid",
"customer": "Harbour Roasting Co",
"customer_email": "quality@harbour-roasting.example.invalid",
"notes": ""
},
{
"contract_ref": "CT-7714",
"shipper": "Meridian Agri Trading",
"shipper_email": "samples@meridian-agri.example.invalid",
"customer": "Inland Mills",
"customer_email": "intake@inland-mills.example.invalid",
"notes": ""
},
{
"contract_ref": "CT-5521",
"shipper": "Cape Fortuna Exports",
"shipper_email": "quality@cape-fortuna.example.invalid",
"customer": "Rivermouth Confectionery",
"customer_email": "buying@rivermouth.example.invalid",
"notes": ""
},
{
"contract_ref": "CT-3308",
"shipper": "Trade Winds Nut Co",
"shipper_email": "exports@tradewinds-nut.example.invalid",
"customer": "Bellweather Foods",
"customer_email": "procurement@bellweather.example.invalid",
"notes": ""
}
];
function config() {
let url = String(process.env.N8N_URL || '');
while (url.endsWith('/')) url = url.slice(0, -1);
const key = process.env.N8N_API_KEY || '';
if (!url || !key) {
console.error('Set N8N_URL and N8N_API_KEY first.');
console.error('An API key comes from Settings, n8n API, in your own instance.');
process.exit(1);
}
return { base: url + '/api/v1', key };
}
async function api(cfg, method, route, body) {
const res = await fetch(cfg.base + route, {
method,
headers: Object.assign({ 'X-N8N-API-KEY': cfg.key }, body ? { 'Content-Type': 'application/json' } : {}),
body: body ? JSON.stringify(body) : undefined,
});
const text = await res.text();
if (!res.ok) throw new Error(method + ' ' + route + ' returned ' + res.status + ': ' + text.slice(0, 300));
return text ? JSON.parse(text) : null;
}
async function main() {
const args = process.argv.slice(2);
const cfg = config();
const existing = {};
const found = await api(cfg, 'GET', '/data-tables?limit=200');
for (const table of found.data || []) existing[table.name] = table.id;
if (args.includes('--list')) {
for (const t of TABLES) console.log(t.key.padEnd(14) + t.name.padEnd(28) + (existing[t.name] || '(missing)'));
return;
}
const ids = {};
for (const table of TABLES) {
if (existing[table.name]) {
ids[table.key] = existing[table.name];
console.log(table.name + ' exists ' + existing[table.name]);
continue;
}
// Every column is text. The workflow coerces at its own boundary, and a store that guesses at
// types is a store that turns "12,5" into something nobody typed.
const created = await api(cfg, 'POST', '/data-tables', {
name: table.name,
columns: table.columns.map((name) => ({ name, type: 'string' })),
});
ids[table.key] = created.id;
console.log(table.name + ' created ' + created.id + ' ' + table.columns.length + ' columns');
}
if (!args.includes('--no-seed')) {
for (const [key, seed, label] of [['spec', SPEC_SEED, 'specification rows'], ['counterparty', COUNTERPARTY_SEED, 'counterparties']]) {
const rows = await api(cfg, 'GET', '/data-tables/' + ids[key] + '/rows?limit=1');
if ((rows.data || []).length > 0) {
console.log(label + ': already has rows, left alone');
continue;
}
await api(cfg, 'POST', '/data-tables/' + ids[key] + '/rows', { data: seed });
console.log(label + ': seeded ' + seed.length + ' rows');
}
}
console.log('');
console.log('Put these into the Settings node of each workflow:');
for (const [key, id] of Object.entries(ids)) console.log(' ' + key + '_table_id = ' + id);
}
main().catch((err) => {
console.error(err && err.message ? err.message : err);
process.exitCode = 1;
});