- Extract calculateEffectiveGrants into n8n/shared/effective-grants.js (single source of truth), inlined at build time into W1/W2/W3 via gen_w*.js + inline_shared.js — removes the duplicated algorithm. - W1 now provisions with a status branch: suspended/deleted -> disable AList user + clear role scopes (soft delete) active -> create/update user+role, RECOMPUTE desired scopes (never restore stale), re-enable - Loop guard corrected: check AList user LIVE disabled state via GET /api/admin/user/get (sync_status alone breaks suspend->resume). - W2/W3 rebuilt on shared lib (identical behaviour). - Tests: test_effective_grants.js (15), test_w3_refactored.js (13), test_w1_lifecycle.js (23) — all green. - Drop superseded w2-compute.js / w3-compute.js / test_w3.js.
68 lines
3.2 KiB
JavaScript
68 lines
3.2 KiB
JavaScript
// Builds n8n/W1-customer-provision.json — Customer webhook with status lifecycle.
|
|
// Single source of truth: shared/effective-grants.js (inlined) + w1-body.js.
|
|
// Run: node n8n/gen_w1.js
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const { sharedSource } = require('./inline_shared');
|
|
|
|
let body = fs.readFileSync(path.join(__dirname, 'w1-body.js'), 'utf8');
|
|
body = body.replace('// __SHARED_INLINE__ (replaced by shared/effective-grants.js source at build)', sharedSource());
|
|
|
|
const workflow = {
|
|
name: 'W1 - Customer Provision & Status Lifecycle',
|
|
nodes: [
|
|
{
|
|
parameters: {
|
|
httpMethod: 'POST',
|
|
path: 'alist-customer-provision',
|
|
responseMode: 'onReceived',
|
|
options: {}
|
|
},
|
|
id: 'trigger-webhook',
|
|
name: 'NocoDB: Customer Webhook',
|
|
type: 'n8n-nodes-base.webhook',
|
|
typeVersion: 2,
|
|
position: [0, 0],
|
|
webhookId: 'customer-provision'
|
|
},
|
|
{
|
|
parameters: {
|
|
jsCode: "const body = $input.item.json.body ?? $input.item.json;\nconst data = body.data ?? {};\nconst rows = data.rows?.[0] ?? data.previous_rows?.[0] ?? data.rows_new?.[0] ?? data.rows_old?.[0] ?? {};\nconst id = rows.Id ?? data.data?.rows?.[0]?.Id;\nif (!id) throw new Error('no customer Id in webhook');\nreturn { json: { customerId: Number(id), eventType: body.type ?? '' } };\n"
|
|
},
|
|
id: 'extract-id',
|
|
name: 'Extract Customer Record Id',
|
|
type: 'n8n-nodes-base.code',
|
|
typeVersion: 2,
|
|
position: [220, 0]
|
|
},
|
|
{
|
|
parameters: {
|
|
jsCode: "// pass-through env + webhook fields (custom env vars readable ONLY via $env here)\nconst prev = $input.item.json;\nreturn { json: {\n customerId: prev.customerId,\n eventType: prev.eventType,\n NOCODB_URL: $env.NOCODB_URL,\n NOCODB_TOKEN: $env.NOCODB_TOKEN,\n NOCODB_TABLE_CUSTOMERS: $env.NOCODB_TABLE_CUSTOMERS,\n NOCODB_TABLE_CUSTOMERPRODUCTS: $env.NOCODB_TABLE_CUSTOMERPRODUCTS,\n NOCODB_TABLE_PRODUCTS: $env.NOCODB_TABLE_PRODUCTS,\n NOCODB_TABLE_RESOURCES: $env.NOCODB_TABLE_RESOURCES,\n NOCODB_TABLE_ACCESSGRANTS: $env.NOCODB_TABLE_ACCESSGRANTS,\n NOCODB_LINK_PROD_RESOURCES: $env.NOCODB_LINK_PROD_RESOURCES,\n ALIST_URL: $env.ALIST_URL,\n ALIST_ADMIN_USERNAME: $env.ALIST_ADMIN_USERNAME,\n ALIST_ADMIN_PASSWORD: $env.ALIST_ADMIN_PASSWORD,\n} };\n"
|
|
},
|
|
id: 'load-env',
|
|
name: 'Load Env',
|
|
type: 'n8n-nodes-base.code',
|
|
typeVersion: 2,
|
|
position: [220, 0]
|
|
},
|
|
{
|
|
parameters: { jsCode: body },
|
|
id: 'lifecycle',
|
|
name: 'Provision & Apply Lifecycle',
|
|
type: 'n8n-nodes-base.code',
|
|
typeVersion: 2,
|
|
position: [440, 0]
|
|
}
|
|
],
|
|
connections: {
|
|
'NocoDB: Customer Webhook': { main: [[{ node: 'Extract Customer Record Id', type: 'main', index: 0 }]] },
|
|
'Extract Customer Record Id': { main: [[{ node: 'Load Env', type: 'main', index: 0 }]] },
|
|
'Load Env': { main: [[{ node: 'Provision & Apply Lifecycle', type: 'main', index: 0 }]] }
|
|
},
|
|
settings: { executionOrder: 'v1' },
|
|
pinData: {}
|
|
};
|
|
|
|
fs.writeFileSync(path.join(__dirname, 'W1-customer-provision.json'), JSON.stringify(workflow, null, 2) + '\n');
|
|
console.log('wrote W1-customer-provision.json (' + body.length + ' chars body)');
|