New-client onboarding: provision resources/services/owner login, drop EA (#24)
Test backoffice (smb-crm) / test (push) Has been cancelled

n8n/onboarding.json now provisions a default resource (with Mon-Sat 09:00-18:00
hours so the public booking page has slots immediately), a starter service, and
an owner-login user for every new client, recording the temp password via the
existing credentials CRM entity -- gated behind an If check so a failed user
creation can't leave a stale credentials row. The EA-provisioning chain
(service/provider creation against Easy!Appointments) is removed entirely.

Adds POST /api/resources, /api/services, /api/owner_users to the backoffice API
for n8n to call, backed by booking_db.py's existing tenancy-safe create_*
helpers. Also adds "slug" to db.py's clients column list -- it was already a DB
column (#17) but the generic /api/clients POST silently dropped it, so
onboarding could never actually set a client's public-facing slug.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-04 13:11:38 +02:00
parent a27ee59125
commit aabd1d56c4
4 changed files with 315 additions and 126 deletions
+115 -124
View File
@@ -22,7 +22,7 @@
},
{
"parameters": {
"jsCode": "const w=$('Onboard webhook').first().json; const body=w.body||w;\nconst s=v=>{v=(v==null?'':String(v));return /^[=+\\-@\\t\\r]/.test(v)?\"'\"+v:v;};\nconst now=new Date();\nfunction pd(v){if(!v)return null;const m=String(v).match(/^(\\d{4})-(\\d{2})-(\\d{2})/);\n if(m)return new Date(Date.UTC(+m[1],+m[2]-1,+m[3]));const d=new Date(v);return isNaN(d)?null:d;}\nconst cycle=String(body.billing_cycle||'monthly').toLowerCase();\nconst start=pd(body.start_date)||new Date(Date.UTC(now.getUTCFullYear(),now.getUTCMonth(),now.getUTCDate()));\nconst startStr=start.toISOString().slice(0,10);\nconst tier=String(body.tier||'A').toUpperCase().includes('B')?'B':'A';\nconst services=String(body.services||'site, booking, leads');\nconst client={business_name:s(body.business_name),owner_name:s(body.owner_name),\n email:s(body.email),phone:s(body.phone),niche:s(body.niche),tier,status:'onboarding',\n domain:s(body.domain),stack_notes:s(body.stack_notes),vault_ref:s(body.vault_ref),\n services:s(services),billing_cycle:cycle,monthly_fee_eur:(body.monthly_fee_eur||''),\n start_date:startStr,notes:s(body.notes)};\nconst checklist=services.split(',').map(x=>x.trim()).filter(Boolean).map(x=>x+'\\u2610').join(' ');\nconst gl=new Date(start);gl.setUTCDate(gl.getUTCDate()+14);const goLive=gl.toISOString().slice(0,10);\nconst project={project_id:'P-'+Date.now(),deliverable:'Onboarding & Setup',tier,checklist,\n go_live_date:goLive,status:'todo'};\nreturn [{json:{client,project,tier,services,cycle,monthly_fee_eur:(body.monthly_fee_eur||''),\n business_name:client.business_name}}];"
"jsCode": "const w=$('Onboard webhook').first().json; const body=w.body||w;\nconst s=v=>{v=(v==null?'':String(v));return /^[=+\\-@\\t\\r]/.test(v)?\"'\"+v:v;};\nconst now=new Date();\nfunction pd(v){if(!v)return null;const m=String(v).match(/^(\\d{4})-(\\d{2})-(\\d{2})/);\n if(m)return new Date(Date.UTC(+m[1],+m[2]-1,+m[3]));const d=new Date(v);return isNaN(d)?null:d;}\nfunction slugify(v){return String(v||'').toLowerCase().normalize('NFKD').replace(/[\\u0300-\\u036f]/g,'')\n .replace(/[^a-z0-9]+/g,'-').replace(/^-+|-+$/g,'')||'client';}\nconst cycle=String(body.billing_cycle||'monthly').toLowerCase();\nconst start=pd(body.start_date)||new Date(Date.UTC(now.getUTCFullYear(),now.getUTCMonth(),now.getUTCDate()));\nconst startStr=start.toISOString().slice(0,10);\nconst tier=String(body.tier||'A').toUpperCase().includes('B')?'B':'A';\nconst services=String(body.services||'site, booking, leads');\nconst slug=slugify(body.business_name)+'-'+Math.random().toString(36).slice(2,6);\nconst client={business_name:s(body.business_name),owner_name:s(body.owner_name),\n email:s(body.email),phone:s(body.phone),niche:s(body.niche),tier,status:'onboarding',\n domain:s(body.domain),stack_notes:s(body.stack_notes),vault_ref:s(body.vault_ref),\n services:s(services),billing_cycle:cycle,monthly_fee_eur:(body.monthly_fee_eur||''),\n start_date:startStr,notes:s(body.notes),slug};\nconst checklist=services.split(',').map(x=>x.trim()).filter(Boolean).map(x=>x+'\\u2610').join(' ');\nconst gl=new Date(start);gl.setUTCDate(gl.getUTCDate()+14);const goLive=gl.toISOString().slice(0,10);\nconst project={project_id:'P-'+Date.now(),deliverable:'Onboarding & Setup',tier,checklist,\n go_live_date:goLive,status:'todo'};\nreturn [{json:{client,project,tier,services,cycle,monthly_fee_eur:(body.monthly_fee_eur||''),\n business_name:client.business_name}}];"
},
"id": "fe9a0f5d-c6f8-4328-a98e-edc022582ec3",
"name": "Compute",
@@ -133,10 +133,10 @@
},
{
"parameters": {
"jsCode": "const cid=$('Save client').first().json.added;\nconst biz=$('Compute').first().json.business_name||'Kunde';\nconst slug=String(cid).toLowerCase().replace(/[^a-z0-9]/g,'');\nconst password=Math.random().toString(36).slice(2,10)+Math.random().toString(36).slice(2,6);\nconst serviceBody={name:'Termin',duration:30,price:0,currency:'EUR',availabilitiesType:'flexible',attendantsNumber:1,isPrivate:false};\nreturn [{json:{cid,biz,username:slug,password,serviceBody}}];"
"jsCode": "const cid=$('Save client').first().json.added;\nconst c=($('Compute').first().json.client)||{};\nconst password=Math.random().toString(36).slice(2,10)+Math.random().toString(36).slice(2,6);\nconst hours=[0,1,2,3,4,5].map(weekday=>({weekday,opens_at:'09:00',closes_at:'18:00'}));\nconst resourceBody={client_id:cid,name:'Hauptressource',hours};\nconst serviceBody={client_id:cid,name:'Termin',duration_minutes:30,price:0};\nreturn [{json:{cid,resourceBody,serviceBody,userEmail:c.email,userPassword:password}}];"
},
"id": "ea-ea-build-service",
"name": "EA build service",
"id": "prov-build-provisioning",
"name": "Build provisioning",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
@@ -148,13 +148,44 @@
"parameters": {
"options": {},
"method": "POST",
"url": "http://easyappointments/index.php/api/v1/services",
"url": "http://smb-crm:8080/api/resources",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "Authorization",
"value": "Basic __EA_AUTH__"
"name": "X-CRM-Token",
"value": "__CRM_TOKEN__"
}
]
},
"sendBody": true,
"specifyBody": "json",
"jsonBody": "={{ JSON.stringify($json.resourceBody) }}"
},
"id": "prov-create-resource",
"name": "Create resource",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
900,
420
],
"retryOnFail": true,
"maxTries": 3,
"waitBetweenTries": 2000,
"continueOnFail": true
},
{
"parameters": {
"options": {},
"method": "POST",
"url": "http://smb-crm:8080/api/services",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "X-CRM-Token",
"value": "__CRM_TOKEN__"
}
]
},
@@ -162,8 +193,8 @@
"specifyBody": "json",
"jsonBody": "={{ JSON.stringify($json.serviceBody) }}"
},
"id": "ea-ea-create-service",
"name": "EA create service",
"id": "prov-create-service",
"name": "Create service",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
@@ -175,68 +206,11 @@
"waitBetweenTries": 2000,
"continueOnFail": true
},
{
"parameters": {
"jsCode": "const prep=$('EA build service').first().json;\nconst svcId=$json.id;\nconst c=($('Compute').first().json.client)||{};\nconst parts=String(prep.biz).trim().split(/\\s+/);\nconst firstName=parts[0]||prep.biz;\nconst lastName=parts.slice(1).join(' ')||'Studio';\nconst wp={start:'09:00',end:'18:00',breaks:[]};\nconst providerBody={firstName,lastName,email:'provider+'+prep.username+'@booking.mivanchenko.de',\n phone:c.phone||'',services:[svcId],isPrivate:false,timezone:'Europe/Berlin',notes:prep.cid,\n settings:{username:prep.username,password:prep.password,\n workingPlan:{monday:wp,tuesday:wp,wednesday:wp,thursday:wp,friday:wp,saturday:wp,sunday:null}}};\nreturn [{json:{providerBody,svcId,cid:prep.cid,username:prep.username,biz:prep.biz}}];"
},
"id": "ea-ea-build-provider",
"name": "EA build provider",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
1120,
520
]
},
{
"parameters": {
"options": {},
"method": "POST",
"url": "http://easyappointments/index.php/api/v1/providers",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "Authorization",
"value": "Basic __EA_AUTH__"
}
]
},
"sendBody": true,
"specifyBody": "json",
"jsonBody": "={{ JSON.stringify($('EA build provider').item.json.providerBody) }}"
},
"id": "ea-ea-create-provider",
"name": "EA create provider",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
1340,
520
],
"retryOnFail": true,
"maxTries": 3,
"waitBetweenTries": 2000,
"continueOnFail": true
},
{
"parameters": {
"jsCode": "const bp=$('EA build provider').first().json;\nconst provId=$json.id;\nconst embed='https://booking.mivanchenko.de/index.php/booking?service='+bp.svcId+'&provider='+provId;\nconst stack_notes='Buchung-Embed: '+embed+' | EA provider '+provId+' / svc '+bp.svcId+' / login '+bp.username;\nconst password=bp.providerBody.settings.password;\nreturn [{json:{cid:bp.cid,embed,provId,username:bp.username,password,patch:{stack_notes}}}];"
},
"id": "ea-ea-booking-info",
"name": "EA booking info",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
1560,
520
]
},
{
"parameters": {
"options": {},
"method": "PATCH",
"url": "=http://smb-crm:8080/api/clients/{{ $json.cid }}",
"url": "http://smb-crm:8080/api/owner_users",
"sendHeaders": true,
"headerParameters": {
"parameters": [
@@ -248,21 +222,54 @@
},
"sendBody": true,
"specifyBody": "json",
"jsonBody": "={{ JSON.stringify($json.patch) }}"
"jsonBody": "={{ JSON.stringify({ client_id: $json.cid, email: $json.userEmail, password: $json.userPassword }) }}"
},
"id": "ea-ea-update-client",
"name": "EA update client",
"id": "prov-create-owner-user",
"name": "Create owner user",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
1780,
520
900,
620
],
"retryOnFail": true,
"maxTries": 3,
"waitBetweenTries": 2000,
"continueOnFail": true
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict"
},
"conditions": [
{
"id": "c0000000-1111-2222-3333-aaaaaaaaaaaa",
"leftValue": "={{ $json.user_id }}",
"rightValue": "",
"operator": {
"type": "string",
"operation": "notEmpty",
"singleValue": true
}
}
],
"combinator": "and"
},
"options": {}
},
"id": "prov-user-created-check",
"name": "User created?",
"type": "n8n-nodes-base.if",
"typeVersion": 2,
"position": [
1120,
620
]
},
{
"parameters": {
"options": {},
@@ -279,15 +286,15 @@
},
"sendBody": true,
"specifyBody": "json",
"jsonBody": "={{ JSON.stringify({ client_id: $json.cid, label: 'Easy!Appointments Provider-Login', username: $json.username, secret: $json.password }) }}"
"jsonBody": "={{ JSON.stringify({ client_id: $('Build provisioning').item.json.cid, label: 'Owner-Login', username: $('Build provisioning').item.json.userEmail, secret: $('Build provisioning').item.json.userPassword }) }}"
},
"id": "ea-save-credential",
"id": "prov-save-credential",
"name": "Save credential",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
1780,
680
1340,
620
],
"retryOnFail": true,
"maxTries": 3,
@@ -327,7 +334,7 @@
"index": 0
},
{
"node": "EA build service",
"node": "Build provisioning",
"type": "main",
"index": 0
}
@@ -356,64 +363,48 @@
]
]
},
"EA build service": {
"Build provisioning": {
"main": [
[
{
"node": "EA create service",
"type": "main",
"index": 0
}
]
]
},
"EA create service": {
"main": [
[
{
"node": "EA build provider",
"type": "main",
"index": 0
}
]
]
},
"EA build provider": {
"main": [
[
{
"node": "EA create provider",
"type": "main",
"index": 0
}
]
]
},
"EA create provider": {
"main": [
[
{
"node": "EA booking info",
"type": "main",
"index": 0
}
]
]
},
"EA booking info": {
"main": [
[
{
"node": "EA update client",
"node": "Create resource",
"type": "main",
"index": 0
},
{
"node": "Create service",
"type": "main",
"index": 0
},
{
"node": "Create owner user",
"type": "main",
"index": 0
}
]
]
},
"Create owner user": {
"main": [
[
{
"node": "User created?",
"type": "main",
"index": 0
}
]
]
},
"User created?": {
"main": [
[
{
"node": "Save credential",
"type": "main",
"index": 0
}
]
],
[]
]
}
},