-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
1365 lines (1291 loc) · 74.4 KB
/
Copy pathserver.ts
File metadata and controls
1365 lines (1291 loc) · 74.4 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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import express from "express";
import type { Request, Response } from "express";
import path from "path";
import { fileURLToPath } from "url";
import { existsSync, unlinkSync } from "fs";
import { execSync } from "child_process";
import { randomBytes, timingSafeEqual } from "crypto";
import cookieParser from "cookie-parser";
import jwt from "jsonwebtoken";
import bcrypt from "bcryptjs";
import Database from "better-sqlite3";
import { parsePublicBookingDate } from "./src/server/publicBooking";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// SECURITY: JWT secret must be set via env var in production â no insecure fallback
if (!process.env.JWT_SECRET) {
if (process.env.NODE_ENV === "production") {
throw new Error("FATAL: JWT_SECRET environment variable is not set. Set it in Railway variables.");
}
console.warn("WARNING: JWT_SECRET not set. Using insecure default for development only.");
}
const JWT_SECRET = process.env.JWT_SECRET || "dev-local-only-not-for-production";
const PORT = Number(process.env.PORT || 8080);
const DB_PATH = path.join(process.cwd(), "crm.db");
const TEMP_PASSWORD_CHARS = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789!@#";
const TEMP_PASSWORD_LENGTH = 12;
const MIN_DURATION_MINUTES = 5;
type SessionUser = { id?: number; role?: string };
type AuthenticatedRequest = Request & { user?: SessionUser };
const DEFAULT_SERVICE_SET = [
{ name: "Brow Threading", duration: 30, price: 25, category: "Beauty" },
{ name: "Full Face Threading", duration: 45, price: 45, category: "Beauty" },
{ name: "Brow Tinting", duration: 20, price: 20, category: "Beauty" },
{ name: "Brow Lamination", duration: 50, price: 65, category: "Beauty" },
{ name: "Lash Lift", duration: 60, price: 80, category: "Lashes" },
{ name: "Lash Tinting", duration: 30, price: 30, category: "Lashes" },
{ name: "Upper Lip Threading", duration: 15, price: 12, category: "Beauty" },
{ name: "Chin Threading", duration: 15, price: 12, category: "Beauty" },
];
const QUOTE_SERVICE_CATALOG = [
{ id: "crm", name: "CRM Dashboard", category: "Core", free_option: "Built-in (no external API needed)" },
{ id: "onboarding", name: "Onboarding & Data Setup", category: "Core", free_option: "Built-in checklist/import flow" },
{ id: "website-basic", name: "Basic Website (5 pages)", category: "Website", free_option: "Use static hosting free tiers (Cloudflare Pages / Netlify)" },
{ id: "website-custom", name: "Custom Website", category: "Website", free_option: "Use static hosting free tiers (Cloudflare Pages / Netlify)" },
{ id: "seo", name: "Local SEO Setup", category: "Website", free_option: "No API required; use checklist + GBP manually" },
{ id: "booking", name: "Online Booking Calendar", category: "Bookings", free_option: "Built-in webhook ingestion (no paid API required)" },
{ id: "reminders", name: "Appointment Reminders", category: "Bookings", free_option: "Resend free tier for email; SMS can stay in test mode" },
{ id: "ai-phone", name: "AI Phone Agent", category: "AI", free_option: "Use test simulation mode; optional telephony trial credits for live calls" },
{ id: "ai-chat", name: "AI Website Chat Widget", category: "AI", free_option: "Use embedded local test widget endpoint" },
{ id: "ai-followup", name: "Auto Follow-up Sequences", category: "AI", free_option: "Run built-in follow-up simulation" },
{ id: "reviews", name: "Review Management", category: "Marketing", free_option: "Use direct review-link workflow (no API required)" },
{ id: "email-sms", name: "Email & SMS Marketing", category: "Marketing", free_option: "Resend free tier for email; SMS test simulation" },
{ id: "social", name: "Social Media Templates", category: "Marketing", free_option: "Use Canva free template workflow" },
{ id: "priority-support", name: "Priority Support", category: "Support", free_option: "Built-in support queue process" },
];
const MONTHLY_SERVICE_PRICING: Record<string, number> = {
crm: 25,
"website-basic": 15,
"website-custom": 25,
seo: 15,
booking: 10,
reminders: 10,
"ai-phone": 35,
"ai-chat": 15,
"ai-followup": 15,
reviews: 12,
"email-sms": 15,
"priority-support": 20,
};
const SERVICE_TEMPLATE_CATALOG: Record<string, { name: string; checklist: string[]; defaults: Record<string, any> }> = {
crm: {
name: "CRM Dashboard",
checklist: ["Import client list", "Set appointment defaults", "Confirm invoice branding", "Decide on client portal access (optional)"],
defaults: { portalEnabled: false, invoiceBranding: "default" },
},
onboarding: {
name: "Onboarding & Data Setup",
checklist: ["Collect business profile details", "Import existing customer data", "Review team workflow preferences"],
defaults: { kickoffCall: true },
},
"website-basic": {
name: "Basic Website (5 pages)",
checklist: ["Confirm brand colors and logo", "Set page copy placeholders", "Connect contact form routing"],
defaults: { pages: ["Home", "About", "Services", "Gallery", "Contact"] },
},
"website-custom": {
name: "Custom Website",
checklist: ["Collect custom layout preferences", "Map booking integration placement", "Prepare SEO metadata placeholders"],
defaults: { customDesign: true, seoReady: true },
},
seo: {
name: "Local SEO Setup",
checklist: ["Capture primary service keywords", "Set local citation checklist", "Prepare Google Business Profile prompts"],
defaults: { mapFocus: true },
},
booking: {
name: "Online Booking Calendar",
checklist: ["Enable booking webhook", "Map services to booking slots", "Confirm booking confirmation messaging"],
defaults: { autoSync: true },
},
reminders: {
name: "Appointment Reminders",
checklist: ["Set reminder timing windows", "Load SMS/email reminder templates", "Verify test-mode reminder flow"],
defaults: { smsHoursBefore: 24, emailHoursBefore: 24 },
},
"ai-phone": {
name: "AI Phone Agent",
checklist: ["Set business-hours fallback", "Load FAQ starter prompts", "Run no-cost simulation call test"],
defaults: { simulationMode: true },
},
"ai-chat": {
name: "AI Website Chat Widget",
checklist: ["Set lead capture fields", "Load FAQ starter prompts", "Confirm booking handoff behavior"],
defaults: { simulationMode: true, leadCapture: true },
},
"ai-followup": {
name: "Auto Follow-up Sequences",
checklist: ["Set review request cadence", "Set rebook prompt timing", "Set promo sequence defaults"],
defaults: { sequenceMode: "post-visit" },
},
reviews: {
name: "Review Management",
checklist: ["Set preferred review link", "Enable post-visit review request", "Define response SLA preference"],
defaults: { autoRequest: true },
},
"email-sms": {
name: "Email & SMS Marketing",
checklist: ["Set newsletter sender profile", "Set promo campaign defaults", "Prepare re-engagement segment rules"],
defaults: { emailEnabled: true, smsTestMode: true },
},
social: {
name: "Social Media Templates",
checklist: ["Select brand color palette", "Set posting cadence preference", "Link Canva template handoff notes"],
defaults: { templatePack: "starter-20" },
},
"priority-support": {
name: "Priority Support",
checklist: ["Set support contact preference", "Schedule monthly check-in reminder", "Enable proactive monitoring notes"],
defaults: { responseSla: "same-day" },
},
};
const EMPTY_SETTINGS_JSON = "{}";
function generateTempPassword() {
return Array.from({ length: TEMP_PASSWORD_LENGTH }, () => TEMP_PASSWORD_CHARS[Math.floor(Math.random() * TEMP_PASSWORD_CHARS.length)]).join("");
}
function toISODateTime(value: any) {
if (value === null || value === undefined || value === "") return null;
if (typeof value === "number") {
// Excel serial date conversion: 25569 is the day offset between Excel's 1900 epoch and Unix epoch (1970-01-01).
// Note: this simple conversion targets normal modern business dates and does not special-case pre-1900 edge cases.
const date = new Date(Math.round((value - 25569) * 86400 * 1000));
return Number.isNaN(date.getTime()) ? null : date.toISOString();
}
const date = new Date(value);
return Number.isNaN(date.getTime()) ? null : date.toISOString();
}
function parseBusinessSettings(raw: any) {
if (!raw) return {};
try { return JSON.parse(raw); } catch { return {}; }
}
function parsePlanServices(raw: any): string[] {
if (!raw) return [];
if (Array.isArray(raw)) return raw.map(String);
try {
const parsed = JSON.parse(raw);
return Array.isArray(parsed) ? parsed.map(String) : [];
} catch {
return [];
}
}
function hasPlanService(planServices: string[], serviceId: string) {
return planServices.includes(serviceId);
}
function normalizePlanServices(raw: any): string[] {
const allowed = new Set(QUOTE_SERVICE_CATALOG.map((svc) => svc.id));
const incoming = parsePlanServices(raw);
const normalized = Array.from(new Set(incoming.filter((id) => allowed.has(id))));
return normalized.includes("crm") ? normalized : ["crm", ...normalized];
}
function buildServiceTemplate(serviceId: string, business: { name?: string; industry?: string }) {
const base = SERVICE_TEMPLATE_CATALOG[serviceId];
if (!base) return null;
return {
service_id: serviceId,
name: base.name,
status: "ready",
applied_at: new Date().toISOString(),
checklist: base.checklist.map((label) => ({ label, done: false })),
preferences: {
businessName: business?.name || "",
industry: business?.industry || "general",
...base.defaults,
},
};
}
function applyServiceTemplates(rawSettings: any, business: { name?: string; industry?: string }, serviceIds: string[], force = false) {
const settings = parseBusinessSettings(rawSettings);
const existingTemplates = settings.serviceTemplates && typeof settings.serviceTemplates === "object" ? settings.serviceTemplates : {};
const nextTemplates = { ...existingTemplates };
for (const serviceId of serviceIds) {
if (!SERVICE_TEMPLATE_CATALOG[serviceId]) continue;
if (!force && nextTemplates[serviceId]) continue;
const template = buildServiceTemplate(serviceId, business);
if (template) nextTemplates[serviceId] = template;
}
settings.serviceTemplates = nextTemplates;
settings.templatePresetVersion = "quote-builder-v1";
return settings;
}
// Rate limiting store (in-memory, per IP)
const loginAttempts = new Map<string, { count: number; resetAt: number }>();
const integrationAttempts = new Map<string, { count: number; resetAt: number }>();
const routeAttempts = new Map<string, { count: number; resetAt: number }>();
function checkRateLimit(ip: string): { allowed: boolean; retryAfter?: number } {
const now = Date.now();
const entry = loginAttempts.get(ip);
if (!entry || now > entry.resetAt) {
loginAttempts.set(ip, { count: 1, resetAt: now + 15 * 60 * 1000 });
return { allowed: true };
}
if (entry.count >= 10) {
return { allowed: false, retryAfter: Math.ceil((entry.resetAt - now) / 1000) };
}
entry.count++;
return { allowed: true };
}
function resetRateLimit(ip: string) { loginAttempts.delete(ip); }
function createRouteRateLimiter(prefix: string, maxRequests: number, windowMs: number) {
return (req: any, res: any, next: any) => {
const now = Date.now();
const ip = req.ip || "unknown";
const userPart = req.user?.id ? `u${req.user.id}` : "anon";
const key = `${prefix}:${userPart}:${ip}`;
const entry = routeAttempts.get(key);
if (!entry || now > entry.resetAt) {
routeAttempts.set(key, { count: 1, resetAt: now + windowMs });
return next();
}
if (entry.count >= maxRequests) {
const retryAfter = Math.ceil((entry.resetAt - now) / 1000);
return res.status(429).json({ error: `Too many requests. Try again in ${retryAfter}s` });
}
entry.count++;
next();
};
}
function checkIntegrationRateLimit(key: string): { allowed: boolean; retryAfter?: number } {
const now = Date.now();
const entry = integrationAttempts.get(key);
if (!entry || now > entry.resetAt) {
integrationAttempts.set(key, { count: 1, resetAt: now + 60 * 1000 });
return { allowed: true };
}
if (entry.count >= 60) {
return { allowed: false, retryAfter: Math.ceil((entry.resetAt - now) / 1000) };
}
entry.count++;
return { allowed: true };
}
if (process.env.RESET_DB === "true" && existsSync(DB_PATH)) {
unlinkSync(DB_PATH);
console.log("RESET_DB=true â deleted existing DB");
} else if (existsSync(DB_PATH)) {
try { const t = new Database(DB_PATH); t.prepare("SELECT 1").get(); t.close(); }
catch (e) { console.log("Corrupt DB â deleting"); unlinkSync(DB_PATH); }
}
if (!existsSync(DB_PATH)) {
console.log("No DB found â running seed...");
try { execSync("npx tsx seed.ts", { stdio: "inherit", cwd: process.cwd() }); }
catch (e) { console.error("Seed failed:", e); }
}
// ââ Always sync credentials on every boot ââââââââââââââââââââââââââââââââââââ
// This runs regardless of whether DB existed, ensuring passwords always match env vars
setTimeout(() => {
try {
const _adminEmail = process.env.SUPERADMIN_EMAIL || 'admin@peachstack.dev';
const _adminPass = process.env.SUPERADMIN_PASSWORD || 'PeachStack$105';
const _adminHash = bcrypt.hashSync(_adminPass, 10);
const _demoHash = bcrypt.hashSync('demo1234', 10);
db.prepare("UPDATE users SET password=?, email=? WHERE role='superadmin'").run(_adminHash, _adminEmail);
db.prepare("UPDATE users SET password=? WHERE email='priya@luxethreading.com'").run(_demoHash);
db.prepare("UPDATE users SET password=? WHERE email='marcus@metroauto.com'").run(_demoHash);
db.prepare("UPDATE users SET password=? WHERE email='amara@peachtreebites.com'").run(_demoHash);
console.log('Credentials synced for', _adminEmail, 'and demo accounts');
} catch(e) { console.error('Credential sync failed:', e); }
}, 2000); // 2s delay to ensure DB is ready
const db = new Database(DB_PATH);
db.pragma("journal_mode = WAL");
db.pragma("foreign_keys = ON");
db.exec(`
CREATE TABLE IF NOT EXISTS businesses (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
industry TEXT NOT NULL DEFAULT 'general',
owner_name TEXT,
owner_email TEXT,
phone TEXT,
address TEXT,
plan TEXT DEFAULT 'starter',
mrr REAL DEFAULT 0,
plan_services TEXT DEFAULT '[]',
status TEXT DEFAULT 'active',
settings TEXT DEFAULT '{}',
booking_webhook_key TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
last_login DATETIME
);
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
role TEXT NOT NULL DEFAULT 'business_admin',
business_id INTEGER REFERENCES businesses(id) ON DELETE CASCADE,
client_id INTEGER REFERENCES clients(id) ON DELETE SET NULL,
name TEXT,
must_change_password INTEGER DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
last_login DATETIME
);
CREATE TABLE IF NOT EXISTS clients (
id INTEGER PRIMARY KEY AUTOINCREMENT,
business_id INTEGER NOT NULL REFERENCES businesses(id) ON DELETE CASCADE,
name TEXT NOT NULL, email TEXT, phone TEXT, notes TEXT,
status TEXT DEFAULT 'New',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS appointments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
business_id INTEGER NOT NULL REFERENCES businesses(id) ON DELETE CASCADE,
client_id INTEGER REFERENCES clients(id) ON DELETE SET NULL,
service TEXT, staff TEXT, date DATETIME, duration INTEGER,
price REAL DEFAULT 0, tip REAL DEFAULT 0,
is_walkin INTEGER DEFAULT 0, status TEXT DEFAULT 'Confirmed', notes TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS services (
id INTEGER PRIMARY KEY AUTOINCREMENT,
business_id INTEGER NOT NULL REFERENCES businesses(id) ON DELETE CASCADE,
name TEXT NOT NULL,
duration INTEGER DEFAULT 60,
price REAL DEFAULT 0,
category TEXT DEFAULT '',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS expenses (
id INTEGER PRIMARY KEY AUTOINCREMENT,
business_id INTEGER NOT NULL REFERENCES businesses(id) ON DELETE CASCADE,
date TEXT NOT NULL, category TEXT NOT NULL, amount REAL NOT NULL, note TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS manual_revenue (
id INTEGER PRIMARY KEY AUTOINCREMENT,
business_id INTEGER NOT NULL REFERENCES businesses(id) ON DELETE CASCADE,
date TEXT NOT NULL, source TEXT NOT NULL, amount REAL NOT NULL, note TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS invoices (
id INTEGER PRIMARY KEY AUTOINCREMENT,
business_id INTEGER NOT NULL REFERENCES businesses(id) ON DELETE CASCADE,
client_id INTEGER REFERENCES clients(id) ON DELETE SET NULL,
client_name TEXT, client_email TEXT,
amount REAL NOT NULL, status TEXT DEFAULT 'Unpaid',
due_date TEXT, items TEXT DEFAULT '[]', notes TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
`);
const badAppointmentsCleanupResult = db.prepare(
`DELETE FROM appointments WHERE date LIKE '% %' OR date LIKE '%st%' OR date LIKE '%nd%' OR date LIKE '%rd%' OR date LIKE '%th%'`
).run();
if (badAppointmentsCleanupResult.changes > 0) {
console.log(`Removed ${badAppointmentsCleanupResult.changes} malformed appointments during startup cleanup`);
}
// Migrations â keep existing data, add new columns safely
const migrations = [
"ALTER TABLE users ADD COLUMN business_id INTEGER REFERENCES businesses(id)",
"ALTER TABLE users ADD COLUMN name TEXT",
"ALTER TABLE users ADD COLUMN role TEXT NOT NULL DEFAULT 'business_admin'",
"ALTER TABLE users ADD COLUMN client_id INTEGER REFERENCES clients(id) ON DELETE SET NULL",
"ALTER TABLE users ADD COLUMN last_login DATETIME",
"ALTER TABLE users ADD COLUMN must_change_password INTEGER DEFAULT 0",
"ALTER TABLE businesses ADD COLUMN booking_webhook_key TEXT",
"ALTER TABLE clients ADD COLUMN business_id INTEGER",
"ALTER TABLE appointments ADD COLUMN business_id INTEGER",
"ALTER TABLE appointments ADD COLUMN staff TEXT",
"ALTER TABLE appointments ADD COLUMN tip REAL DEFAULT 0",
"ALTER TABLE appointments ADD COLUMN is_walkin INTEGER DEFAULT 0",
"ALTER TABLE appointments ADD COLUMN notes TEXT",
"ALTER TABLE services ADD COLUMN category TEXT DEFAULT ''",
"ALTER TABLE expenses ADD COLUMN business_id INTEGER",
"ALTER TABLE manual_revenue ADD COLUMN business_id INTEGER",
"ALTER TABLE invoices ADD COLUMN business_id INTEGER",
"ALTER TABLE invoices ADD COLUMN client_id INTEGER",
];
for (const m of migrations) { try { db.exec(m); } catch(e) {} }
try { db.exec("CREATE UNIQUE INDEX IF NOT EXISTS idx_businesses_booking_webhook_key ON businesses(booking_webhook_key)"); } catch (e) { console.warn("Could not ensure booking webhook index; continuing without index may reduce lookup performance:", e); }
try {
const businesses = db.prepare("SELECT id, settings, booking_webhook_key FROM businesses").all() as any[];
const updateKey = db.prepare("UPDATE businesses SET booking_webhook_key=? WHERE id=?");
const backfill = db.transaction((rows: any[]) => {
let updated = 0;
for (const row of rows) {
if (row.booking_webhook_key) continue;
const settings = parseBusinessSettings(row.settings);
if (settings.bookingWebhookKey) {
updateKey.run(String(settings.bookingWebhookKey), row.id);
updated++;
}
}
return updated;
});
backfill(businesses);
} catch (e) {}
async function startServer() {
const app = express();
// Health check endpoint â must respond immediately for Railway
app.get("/health", (_req, res) => {
res.status(200).json({ status: "ok", timestamp: new Date().toISOString() });
});
app.use(express.json({ limit: "10mb" }));
app.use(cookieParser());
// Security headers
app.use((_req, res, next) => {
res.setHeader("X-Content-Type-Options", "nosniff");
res.setHeader("X-Frame-Options", "DENY");
res.setHeader("X-XSS-Protection", "1; mode=block");
res.setHeader("X-Robots-Tag", "noindex, nofollow");
res.setHeader("Referrer-Policy", "strict-origin-when-cross-origin");
next();
});
// --- MIDDLEWARE ---
const auth = (req: any, res: any, next: any) => {
const token = req.cookies?.token;
if (!token) return res.status(401).json({ error: "Unauthorized" });
try {
req.user = jwt.verify(token, JWT_SECRET);
next();
} catch { res.status(403).json({ error: "Invalid or expired session" }); }
};
const superadminOnly = (req: any, res: any, next: any) => {
if (req.user?.role !== "superadmin") return res.status(403).json({ error: "Forbidden" });
next();
};
const businessOnly = (req: any, res: any, next: any) => {
if (!["business_admin", "business_staff"].includes(req.user?.role)) return res.status(403).json({ error: "Forbidden" });
if (!req.user?.business_id) return res.status(403).json({ error: "No business context" });
next();
};
// Critical: ensure every business query is scoped to req.user.business_id
const scopeCheck = (req: any, res: any, next: any) => {
if (req.user?.role === "superadmin") return res.status(403).json({ error: "Superadmin cannot access business data directly" });
next();
};
const businessReadRateLimit = createRouteRateLimiter("business_read", 120, 60 * 1000);
const businessWriteRateLimit = createRouteRateLimiter("business_write", 60, 60 * 1000);
const integrationIngestRateLimit = (req: any, res: any, next: any) => {
const ip = req.ip || "unknown";
const source = req.path.includes("/website/") ? "website" : "booking";
const rl = checkIntegrationRateLimit(`${source}:${req.params.webhookKey}:${ip}`);
if (!rl.allowed) return res.status(429).json({ error: `Too many requests. Try again in ${rl.retryAfter}s` });
next();
};
const ensureServiceForBusiness = (businessId: number, serviceName: string, fallbackDuration = 60, fallbackPrice = 0, category = "General") => {
const name = String(serviceName || "").trim();
if (!name) return;
const exists = db.prepare("SELECT id FROM services WHERE business_id=? AND lower(name)=lower(?) LIMIT 1").get(businessId, name) as any;
if (exists) return;
db.prepare("INSERT INTO services (business_id,name,duration,price,category) VALUES (?,?,?,?,?)")
.run(businessId, name, Math.max(MIN_DURATION_MINUTES, fallbackDuration || 60), Math.max(0, fallbackPrice || 0), category || "General");
};
const importIntegrationAppointment = (webhookKey: string, payload: any, source: "booking" | "website") => {
const biz = db.prepare("SELECT id, settings, plan_services FROM businesses WHERE booking_webhook_key=? LIMIT 1").get(webhookKey) as any;
if (!biz) return { status: 404, body: { error: "Invalid key" } };
const planServices = parsePlanServices(biz.plan_services);
if (!hasPlanService(planServices, "booking")) {
return { status: 403, body: { error: "Booking integration is not enabled for this plan" } };
}
const p: any = payload || {};
const mapped = {
client_name: p.client_name || p.name || p.customer_name || p.invitee_name || p.contact?.name || p.event?.invitee_name || "",
client_email: p.client_email || p.email || p.customer_email || p.invitee_email || p.contact?.email || p.event?.invitee_email || "",
client_phone: p.client_phone || p.phone || p.contact?.phone || "",
service: p.service || p.service_name || p.event?.name || p.appointment_type || "Booked Service",
staff: p.staff || p.provider || p.assignee || "",
date: p.date || p.start_time || p.starts_at || p.event?.start_time || p.start || "",
duration: p.duration || p.duration_minutes || p.event?.duration || 60,
price: p.price || p.amount || 0,
tip: p.tip || 0,
status: p.status || "Confirmed",
notes: p.notes || p.source || `Imported from ${source} integration`,
};
const clientEmail = String(mapped.client_email || "").trim().toLowerCase();
const clientName = String(mapped.client_name || "").trim();
const service = String(mapped.service || "").trim();
const date = toISODateTime(mapped.date);
if (!service || !date) return { status: 400, body: { error: "service and date are required" } };
let clientId: number | null = null;
if (clientEmail) {
const existing = db.prepare("SELECT id FROM clients WHERE business_id=? AND lower(email)=lower(?) LIMIT 1").get(biz.id, clientEmail) as any;
if (existing) {
clientId = existing.id;
} else {
const created = db.prepare("INSERT INTO clients (business_id,name,email,phone,notes,status) VALUES (?,?,?,?,?,?)")
.run(biz.id, clientName || clientEmail, clientEmail, String(mapped.client_phone || ""), `Imported from ${source} integration`, "New");
clientId = Number(created.lastInsertRowid);
}
} else if (clientName) {
const existingByName = db.prepare("SELECT id FROM clients WHERE business_id=? AND lower(name)=lower(?) LIMIT 1").get(biz.id, clientName) as any;
if (existingByName) clientId = existingByName.id;
}
const duration = Math.max(MIN_DURATION_MINUTES, parseInt(mapped.duration, 10) || 60);
const price = Math.max(0, Number(mapped.price) || 0);
ensureServiceForBusiness(biz.id, service, duration, price, "Imported");
const is_walkin = clientId ? 0 : 1;
const inserted = db.prepare("INSERT INTO appointments (business_id,client_id,service,staff,date,duration,price,tip,is_walkin,status,notes) VALUES (?,?,?,?,?,?,?,?,?,?,?)")
.run(
biz.id,
clientId,
service,
String(mapped.staff || ""),
date,
duration,
price,
Math.max(0, Number(mapped.tip) || 0),
is_walkin,
String(mapped.status || "Confirmed"),
String(mapped.notes || ""),
);
return { status: 200, body: { ok: true, appointment_id: inserted.lastInsertRowid } };
};
// --- AUTH ROUTES ---
// Business login â /api/auth/login
app.post("/api/auth/login", (req, res) => {
const ip = req.ip || "unknown";
const rl = checkRateLimit(ip);
if (!rl.allowed) return res.status(429).json({ error: "Too many attempts. Try again in " + rl.retryAfter + "s" });
const { email, password } = req.body;
if (!email || !password) return res.status(400).json({ error: "Email and password required" });
const user = db.prepare("SELECT u.*, b.name as business_name, b.industry FROM users u LEFT JOIN businesses b ON u.business_id = b.id WHERE lower(u.email) = ? AND u.role != 'superadmin'").get(email.toLowerCase().trim()) as any;
if (!user || !bcrypt.compareSync(password, user.password)) return res.status(401).json({ error: "Invalid email or password" });
resetRateLimit(ip);
db.prepare("UPDATE users SET last_login=datetime('now') WHERE id=?").run(user.id);
if (user.business_id) db.prepare("UPDATE businesses SET last_login=datetime('now') WHERE id=?").run(user.business_id);
const token = jwt.sign({ id: user.id, email: user.email, role: user.role, business_id: user.business_id, name: user.name }, JWT_SECRET, { expiresIn: "12h" });
res.cookie("token", token, { httpOnly: true, secure: process.env.NODE_ENV === "production", sameSite: "strict" });
res.json({ user: { id: user.id, email: user.email, role: user.role, business_id: user.business_id, name: user.name, business_name: user.business_name, industry: user.industry, must_change_password: user.must_change_password } });
});
// Superadmin login â /api/admin/login (separate endpoint, separate cookie name conceptually)
app.post("/api/admin/login", (req, res) => {
const ip = req.ip || "unknown";
const rl = checkRateLimit("admin_" + ip);
if (!rl.allowed) return res.status(429).json({ error: "Too many attempts" });
const { email, password } = req.body;
if (!email || !password) return res.status(400).json({ error: "Required" });
const user = db.prepare("SELECT * FROM users WHERE lower(email) = ? AND role = 'superadmin'").get(email.toLowerCase().trim()) as any;
if (!user || !bcrypt.compareSync(password, user.password)) return res.status(401).json({ error: "Invalid credentials" });
resetRateLimit("admin_" + ip);
db.prepare("UPDATE users SET last_login=datetime('now') WHERE id=?").run(user.id);
const token = jwt.sign({ id: user.id, email: user.email, role: "superadmin" }, JWT_SECRET, { expiresIn: "8h" });
res.cookie("token", token, { httpOnly: true, secure: process.env.NODE_ENV === "production", sameSite: "strict" });
res.json({ user: { id: user.id, email: user.email, role: "superadmin", name: user.name } });
});
app.post("/api/auth/logout", (_req, res) => {
res.clearCookie("token"); res.json({ ok: true });
});
app.get("/api/auth/me", auth, (req: any, res) => {
const u = db.prepare("SELECT u.id,u.email,u.role,u.business_id,u.name,u.must_change_password,b.name as business_name,b.industry,b.settings FROM users u LEFT JOIN businesses b ON u.business_id=b.id WHERE u.id=?").get(req.user.id) as any;
if (!u) return res.status(401).json({ error: "Not found" });
res.json({ user: { ...u, settings: u.settings ? JSON.parse(u.settings) : {} } });
});
app.put("/api/auth/password", auth, (req: any, res) => {
const { current_password, new_password } = req.body;
if (!new_password || new_password.length < 8) return res.status(400).json({ error: "Password must be at least 8 characters" });
const user = db.prepare("SELECT * FROM users WHERE id=?").get(req.user.id) as any;
if (!bcrypt.compareSync(current_password, user.password)) return res.status(401).json({ error: "Current password incorrect" });
db.prepare("UPDATE users SET password=?, must_change_password=0 WHERE id=?").run(bcrypt.hashSync(new_password, 12), req.user.id);
res.json({ ok: true });
});
// --- SUPERADMIN ROUTES ---
app.get("/api/super/businesses", auth, superadminOnly, (_req, res) => {
const businesses = db.prepare(`
SELECT b.*,
(SELECT COUNT(*) FROM users WHERE business_id=b.id AND role='business_admin') as admin_count,
(SELECT COUNT(*) FROM clients WHERE business_id=b.id) as client_count,
(SELECT COUNT(*) FROM appointments WHERE business_id=b.id AND date>datetime('now')) as upcoming_appts,
(SELECT COALESCE(SUM(price+tip),0) FROM appointments WHERE business_id=b.id AND status='Completed') as total_revenue,
(SELECT email FROM users WHERE business_id=b.id AND role='business_admin' LIMIT 1) as admin_email
FROM businesses b ORDER BY b.created_at DESC
`).all();
res.json(businesses);
});
app.post("/api/super/businesses", auth, superadminOnly, async (req, res) => {
const { name, industry, owner_name, owner_email, phone, plan, mrr, plan_services } = req.body;
if (!name || !owner_email || !industry) return res.status(400).json({ error: "name, industry, owner_email required" });
if (db.prepare("SELECT id FROM users WHERE email=?").get(owner_email.toLowerCase())) return res.status(409).json({ error: "Email already in use" });
const tempPw = generateTempPassword();
const services = normalizePlanServices(plan_services);
const calculatedMrr = services.reduce((sum: number, id: string) => sum + (MONTHLY_SERVICE_PRICING[id] || 0), 0);
const initialSettings = applyServiceTemplates(EMPTY_SETTINGS_JSON, { name, industry }, services, true);
const tx = db.transaction(() => {
const insertBusiness = db.prepare(
"INSERT INTO businesses (name, industry, owner_name, owner_email, phone, plan, mrr, plan_services, status, settings) VALUES (@name, @industry, @owner_name, @owner_email, @phone, @plan, @mrr, @plan_services, @status, @settings)"
);
const biz = insertBusiness.run({
name,
industry,
owner_name: owner_name || "",
owner_email: owner_email.toLowerCase(),
phone: phone || "",
plan: plan || "starter",
mrr: Number(mrr) > 0 ? Number(mrr) : calculatedMrr,
plan_services: JSON.stringify(services),
status: "active",
settings: JSON.stringify(initialSettings),
});
db.prepare("INSERT INTO users (email,password,role,business_id,name,must_change_password) VALUES (?,?,'business_admin',?,?,1)").run(owner_email.toLowerCase(), bcrypt.hashSync(tempPw, 12), biz.lastInsertRowid, owner_name||"");
return { id: biz.lastInsertRowid, tempPw };
});
const result = tx();
res.json({ id: result.id, tempPassword: result.tempPw, message: "Business created. Share temp password with client." });
});
app.put("/api/super/businesses/:id", auth, superadminOnly, (req, res) => {
const { name, industry, owner_name, phone, plan, mrr, status } = req.body;
db.prepare("UPDATE businesses SET name=?,industry=?,owner_name=?,phone=?,plan=?,mrr=?,status=? WHERE id=?").run(name,industry,owner_name||"",phone||"",plan||"starter",mrr||0,status||"active",req.params.id);
res.json({ ok: true });
});
app.delete("/api/super/businesses/:id", auth, superadminOnly, (req, res) => {
// Cascade deletes all business data via FK
db.prepare("DELETE FROM businesses WHERE id=?").run(req.params.id);
res.json({ ok: true });
});
app.get("/api/super/stats", auth, superadminOnly, (_req, res) => {
const totalBiz = (db.prepare("SELECT COUNT(*) as c FROM businesses WHERE status='active'").get() as any).c;
const totalMrr = (db.prepare("SELECT COALESCE(SUM(mrr),0) as t FROM businesses WHERE status='active'").get() as any).t;
const newThisMonth = (db.prepare("SELECT COUNT(*) as c FROM businesses WHERE created_at>=date('now','start of month')").get() as any).c;
const recentLogins = db.prepare("SELECT b.name, b.industry, u.last_login, u.email FROM users u JOIN businesses b ON u.business_id=b.id WHERE u.role='business_admin' AND u.last_login IS NOT NULL ORDER BY u.last_login DESC LIMIT 8").all();
const byIndustry = db.prepare("SELECT industry, COUNT(*) as count FROM businesses GROUP BY industry ORDER BY count DESC").all();
const mrrByMonth = db.prepare("SELECT strftime('%Y-%m',created_at) as month, COUNT(*) as new_clients, SUM(mrr) as mrr FROM businesses WHERE status='active' GROUP BY month ORDER BY month DESC LIMIT 6").all();
res.json({ totalBiz, totalMrr, newThisMonth, recentLogins, byIndustry, mrrByMonth });
});
app.post("/api/super/businesses/:id/reset-password", auth, superadminOnly, (req, res) => {
const tempPw = generateTempPassword();
db.prepare("UPDATE users SET password=?,must_change_password=1 WHERE business_id=? AND role='business_admin'").run(bcrypt.hashSync(tempPw, 12), req.params.id);
res.json({ tempPassword: tempPw });
});
app.get("/api/super/businesses/:id", auth, superadminOnly, (req, res) => {
const business = db.prepare(`
SELECT b.*,
(SELECT email FROM users WHERE business_id=b.id AND role='business_admin' LIMIT 1) as admin_email
FROM businesses b
WHERE b.id=?
`).get(req.params.id);
if (!business) return res.status(404).json({ error: "Business not found" });
res.json(business);
});
// --- BUSINESS ROUTES (all scoped to req.user.business_id) ---
// Business profile
app.get("/api/business/profile", auth, businessOnly, (req: any, res) => {
const biz = db.prepare("SELECT * FROM businesses WHERE id=?").get(req.user.business_id) as any;
if (!biz) return res.status(404).json({ error: "Not found" });
res.json({ ...biz, settings: biz.settings ? JSON.parse(biz.settings) : {} });
});
app.put("/api/business/profile", auth, businessOnly, (req: any, res) => {
const { name, owner_name, phone, address, settings } = req.body;
db.prepare("UPDATE businesses SET name=?,owner_name=?,phone=?,address=?,settings=? WHERE id=?")
.run(name, owner_name||"", phone||"", address||"", JSON.stringify(settings||{}), req.user.business_id);
res.json({ ok: true });
});
// Settings / password
app.put("/api/business/settings", auth, businessOnly, (req: any, res) => {
const { name } = req.body;
db.prepare("UPDATE users SET name=? WHERE id=?").run(name, req.user.id);
res.json({ ok: true });
});
// Integrations/settings helper for import/webhook flows
const getIntegrationConfig = (req: any, res: any) => {
const biz = db.prepare("SELECT id, settings, plan_services FROM businesses WHERE id=?").get(req.user.business_id) as any;
if (!biz) return res.status(404).json({ error: "Not found" });
const planServices = parsePlanServices(biz.plan_services);
if (!hasPlanService(planServices, "booking")) return res.status(403).json({ error: "Booking integration is not enabled for this plan" });
const settings = parseBusinessSettings(biz.settings);
let webhookKey = biz.booking_webhook_key || settings.bookingWebhookKey || "";
if (!webhookKey) {
webhookKey = randomBytes(16).toString("hex");
}
if (settings.bookingWebhookKey !== webhookKey || biz.booking_webhook_key !== webhookKey) {
settings.bookingWebhookKey = webhookKey;
db.prepare("UPDATE businesses SET settings=?, booking_webhook_key=? WHERE id=?").run(JSON.stringify(settings), webhookKey, req.user.business_id);
}
const baseUrl = `${req.protocol}://${req.get("host")}`;
res.json({
bookingWebhookKey: webhookKey,
bookingWebhookUrl: `${baseUrl}/api/integrations/booking/${webhookKey}`,
websiteImportUrl: `${baseUrl}/api/integrations/website/${webhookKey}`,
});
};
app.get("/api/business/import-config", auth, businessOnly, scopeCheck, businessReadRateLimit, getIntegrationConfig); // backward compatible
app.get("/api/business/integration-config", auth, businessOnly, scopeCheck, businessReadRateLimit, getIntegrationConfig);
// Stats â scoped
app.get("/api/stats", auth, businessOnly, scopeCheck, (req: any, res) => {
const bid = req.user.business_id;
const { startDate, endDate } = req.query;
let af = "WHERE a.business_id=? AND a.status='Completed'", ap: any[] = [bid];
let ef = "WHERE business_id=?", ep: any[] = [bid];
let mf = "WHERE business_id=?", mp: any[] = [bid];
if (startDate && endDate) {
af += " AND a.date BETWEEN ? AND ?"; ap.push(startDate, endDate);
ef += " AND date BETWEEN ? AND ?"; ep.push(startDate, endDate);
mf += " AND date BETWEEN ? AND ?"; mp.push(startDate, endDate);
}
const tc = (db.prepare("SELECT count(*) as c FROM clients WHERE business_id=?").get(bid) as any).c;
const nm = (db.prepare("SELECT count(*) as c FROM clients WHERE business_id=? AND created_at>=date('now','start of month')").get(bid) as any).c;
const ar = (db.prepare(`SELECT COALESCE(sum(a.price+a.tip),0) as t FROM appointments a ${af}`).get(...ap) as any).t;
const mr = (db.prepare(`SELECT COALESCE(sum(amount),0) as t FROM manual_revenue ${mf}`).get(...mp) as any).t;
const totalRevenue = (ar||0) + (mr||0);
const te = (db.prepare(`SELECT COALESCE(sum(amount),0) as t FROM expenses ${ef}`).get(...ep) as any).t;
const ua = (db.prepare("SELECT count(*) as c FROM appointments WHERE business_id=? AND date>datetime('now')").get(bid) as any).c;
const ui = (db.prepare("SELECT count(*) as c, COALESCE(sum(amount),0) as t FROM invoices WHERE business_id=? AND status='Unpaid'").get(bid) as any);
const rbm = db.prepare(`
SELECT month, SUM(total) as total
FROM (
SELECT strftime('%Y-%m', a.date) as month, SUM(a.price+a.tip) as total
FROM appointments a ${af}
GROUP BY month
UNION ALL
SELECT strftime('%Y-%m', m.date) as month, SUM(m.amount) as total
FROM manual_revenue m ${mf}
GROUP BY month
)
GROUP BY month
ORDER BY month DESC
LIMIT 12
`).all(...ap, ...mp);
const ebm = db.prepare(`SELECT strftime('%Y-%m',date) as month, sum(amount) as total FROM expenses ${ef} GROUP BY month ORDER BY month DESC LIMIT 12`).all(...ep);
const rbs = db.prepare(`
SELECT service, SUM(total) as total, SUM(count) as count
FROM (
SELECT a.service as service, SUM(a.price+a.tip) as total, COUNT(*) as count
FROM appointments a ${af}
GROUP BY a.service
UNION ALL
SELECT m.source as service, SUM(m.amount) as total, COUNT(*) as count
FROM manual_revenue m ${mf}
GROUP BY m.source
)
GROUP BY service
ORDER BY total DESC
`).all(...ap, ...mp);
const ebc = db.prepare(`SELECT category as name, sum(amount) as value FROM expenses ${ef} GROUP BY category ORDER BY value DESC`).all(...ep);
const na = db.prepare("SELECT a.*, COALESCE(c.name,'Walk-in') as client_name FROM appointments a LEFT JOIN clients c ON a.client_id=c.id WHERE a.business_id=? AND a.date>datetime('now') ORDER BY a.date ASC LIMIT 5").all(bid);
const top = db.prepare("SELECT c.id,c.name,c.email,c.status, COALESCE(sum(a.price+a.tip),0) as total_revenue, count(a.id) as visit_count FROM clients c LEFT JOIN appointments a ON a.client_id=c.id AND a.status='Completed' WHERE c.business_id=? GROUP BY c.id ORDER BY total_revenue DESC LIMIT 6").all(bid);
res.json({ totalClients:tc, newClientsThisMonth:nm, totalRevenue, totalExpenses:te||0, netProfit:totalRevenue-(te||0), upcomingAppointments:ua, unpaidInvoices:ui.c, unpaidInvoicesTotal:ui.t, revenueByMonth:rbm, expensesByMonth:ebm, revenueByService:rbs, expensesByCategory:ebc, nextAppointments:na, topClients:top });
});
// CLIENTS â scoped
app.get("/api/clients", auth, businessOnly, scopeCheck, (req: any, res) => {
const rows = (db.prepare("SELECT * FROM clients WHERE business_id=? ORDER BY name ASC").all(req.user.business_id) as any[]).map((c: any) => {
const pu = db.prepare("SELECT id,email FROM users WHERE business_id=? AND role='customer' AND (client_id=? OR email=?)").get(req.user.business_id, c.id, c.email);
return { ...c, portal_user: pu || null };
});
res.json(rows);
});
app.post("/api/clients", auth, businessOnly, scopeCheck, (req: any, res) => {
const { name, email, phone, notes, status } = req.body;
if (!name) return res.status(400).json({ error: "Name required" });
const r = db.prepare("INSERT INTO clients (business_id,name,email,phone,notes,status) VALUES (?,?,?,?,?,?)").run(req.user.business_id, name, email||"", phone||"", notes||"", status||"New");
res.json({ id: r.lastInsertRowid });
});
app.get("/api/clients/:id", auth, businessOnly, scopeCheck, (req: any, res) => {
const client = db.prepare("SELECT * FROM clients WHERE id=? AND business_id=?").get(req.params.id, req.user.business_id);
if (!client) return res.status(404).json({ error: "Not found" });
const appointments = db.prepare("SELECT * FROM appointments WHERE client_id=? AND business_id=? ORDER BY date DESC").all(req.params.id, req.user.business_id);
const invoices = db.prepare("SELECT * FROM invoices WHERE client_id=? AND business_id=? ORDER BY created_at DESC").all(req.params.id, req.user.business_id);
const portal_user = db.prepare("SELECT id,email,must_change_password FROM users WHERE business_id=? AND role='customer' AND (client_id=? OR email=(SELECT email FROM clients WHERE id=?))").get(req.user.business_id, req.params.id, req.params.id);
res.json({ ...client as any, appointments, invoices, portal_user: portal_user || null });
});
app.put("/api/clients/:id", auth, businessOnly, scopeCheck, (req: any, res) => {
const existing = db.prepare("SELECT id FROM clients WHERE id=? AND business_id=?").get(req.params.id, req.user.business_id);
if (!existing) return res.status(404).json({ error: "Not found" });
const { name, email, phone, notes, status } = req.body;
db.prepare("UPDATE clients SET name=?,email=?,phone=?,notes=?,status=? WHERE id=? AND business_id=?").run(name, email||"", phone||"", notes||"", status||"New", req.params.id, req.user.business_id);
res.json({ ok: true });
});
app.delete("/api/clients/:id", auth, businessOnly, scopeCheck, (req: any, res) => {
const existing = db.prepare("SELECT id FROM clients WHERE id=? AND business_id=?").get(req.params.id, req.user.business_id);
if (!existing) return res.status(404).json({ error: "Not found" });
db.prepare("DELETE FROM appointments WHERE client_id=? AND business_id=?").run(req.params.id, req.user.business_id);
db.prepare("DELETE FROM invoices WHERE client_id=? AND business_id=?").run(req.params.id, req.user.business_id);
db.prepare("DELETE FROM clients WHERE id=? AND business_id=?").run(req.params.id, req.user.business_id);
res.json({ ok: true });
});
app.post("/api/clients/import", auth, businessOnly, scopeCheck, businessWriteRateLimit, (req: any, res) => {
const biz = db.prepare("SELECT plan_services FROM businesses WHERE id=?").get(req.user.business_id) as any;
const planServices = parsePlanServices(biz?.plan_services);
if (!hasPlanService(planServices, "crm")) return res.status(403).json({ error: "Client import requires CRM service in your plan" });
const rows = Array.isArray(req.body?.rows) ? req.body.rows : [];
const ins = db.prepare("INSERT INTO clients (business_id,name,email,phone,notes,status) VALUES (?,?,?,?,?,?)");
const tx = db.transaction((items: any[]) => {
let imported = 0;
for (const row of items) {
const name = String(row?.name || "").trim();
if (!name) continue;
ins.run(
req.user.business_id,
name,
String(row?.email || "").trim().toLowerCase(),
String(row?.phone || "").trim(),
String(row?.notes || "").trim(),
String(row?.status || "New").trim() || "New",
);
imported++;
}
return imported;
});
res.json({ imported: tx(rows) });
});
app.get("/api/services", auth, businessOnly, scopeCheck, businessReadRateLimit, (req: any, res) => {
const biz = db.prepare("SELECT plan_services FROM businesses WHERE id=?").get(req.user.business_id) as any;
const planServices = parsePlanServices(biz?.plan_services);
if (!hasPlanService(planServices, "crm")) return res.status(403).json({ error: "Services require CRM service in your plan" });
let rows = db.prepare("SELECT * FROM services WHERE business_id=? ORDER BY name ASC").all(req.user.business_id) as any[];
if (!rows.length) {
const ins = db.prepare("INSERT INTO services (business_id,name,duration,price,category) VALUES (?,?,?,?,?)");
const seed = db.transaction(() => {
for (const s of DEFAULT_SERVICE_SET) ins.run(req.user.business_id, s.name, s.duration, s.price, s.category);
return DEFAULT_SERVICE_SET.length;
});
seed();
rows = db.prepare("SELECT * FROM services WHERE business_id=? ORDER BY name ASC").all(req.user.business_id) as any[];
}
res.json(rows);
});
app.post("/api/services", auth, businessOnly, scopeCheck, businessWriteRateLimit, (req: any, res) => {
const biz = db.prepare("SELECT plan_services FROM businesses WHERE id=?").get(req.user.business_id) as any;
const planServices = parsePlanServices(biz?.plan_services);
if (!hasPlanService(planServices, "crm")) return res.status(403).json({ error: "Services require CRM service in your plan" });
const name = String(req.body?.name || "").trim();
if (!name) return res.status(400).json({ error: "Name required" });
const duration = Math.max(MIN_DURATION_MINUTES, parseInt(req.body?.duration, 10) || 60);
const price = Math.max(0, Number(req.body?.price) || 0);
const category = String(req.body?.category || "").trim();
const existing = db.prepare("SELECT id FROM services WHERE business_id=? AND lower(name)=lower(?)").get(req.user.business_id, name) as any;
if (existing) return res.status(409).json({ error: "Service already exists" });
const r = db.prepare("INSERT INTO services (business_id,name,duration,price,category) VALUES (?,?,?,?,?)").run(req.user.business_id, name, duration, price, category);
res.json({ id: r.lastInsertRowid });
});
// APPOINTMENTS â scoped
app.get("/api/appointments", auth, businessOnly, scopeCheck, (req: any, res) => {
res.json(db.prepare("SELECT a.*, COALESCE(c.name,'Walk-in') as client_name FROM appointments a LEFT JOIN clients c ON a.client_id=c.id WHERE a.business_id=? ORDER BY a.date DESC").all(req.user.business_id));
});
app.post("/api/appointments", auth, businessOnly, scopeCheck, (req: any, res) => {
const { client_id, service, staff, date, duration, price, tip, is_walkin, status, notes } = req.body;
if (client_id) {
const owns = db.prepare("SELECT id FROM clients WHERE id=? AND business_id=?").get(client_id, req.user.business_id);
if (!owns) return res.status(403).json({ error: "Client not in your business" });
}
const r = db.prepare("INSERT INTO appointments (business_id,client_id,service,staff,date,duration,price,tip,is_walkin,status,notes) VALUES (?,?,?,?,?,?,?,?,?,?,?)").run(req.user.business_id, client_id||null, service, staff||"", date, duration||60, price||0, tip||0, is_walkin?1:0, status||"Confirmed", notes||"");
res.json({ id: r.lastInsertRowid });
});
app.patch("/api/appointments/:id/status", auth, businessOnly, scopeCheck, (req: any, res) => {
const existing = db.prepare("SELECT id FROM appointments WHERE id=? AND business_id=?").get(req.params.id, req.user.business_id);
if (!existing) return res.status(404).json({ error: "Not found" });
db.prepare("UPDATE appointments SET status=? WHERE id=? AND business_id=?").run(req.body.status, req.params.id, req.user.business_id);
res.json({ ok: true });
});
app.delete("/api/appointments/:id", auth, businessOnly, scopeCheck, (req: any, res) => {
db.prepare("DELETE FROM appointments WHERE id=? AND business_id=?").run(req.params.id, req.user.business_id);
res.json({ ok: true });
});
app.post("/api/appointments/import", auth, businessOnly, scopeCheck, businessWriteRateLimit, (req: any, res) => {
const biz = db.prepare("SELECT plan_services FROM businesses WHERE id=?").get(req.user.business_id) as any;
const planServices = parsePlanServices(biz?.plan_services);
if (!hasPlanService(planServices, "crm")) return res.status(403).json({ error: "Appointment import requires CRM service in your plan" });
const rows = Array.isArray(req.body?.rows) ? req.body.rows : [];
const findClientByName = db.prepare("SELECT id FROM clients WHERE business_id=? AND lower(name)=lower(?) LIMIT 1");
const findClientByEmail = db.prepare("SELECT id,name FROM clients WHERE business_id=? AND lower(email)=lower(?) LIMIT 1");
const addClient = db.prepare("INSERT INTO clients (business_id,name,email,phone,notes,status) VALUES (?,?,?,?,?,?)");
const addAppointment = db.prepare("INSERT INTO appointments (business_id,client_id,service,staff,date,duration,price,tip,is_walkin,status,notes) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
const tx = db.transaction((items: any[]) => {
let imported = 0;
for (const row of items) {
const service = String(row?.service || "").trim();
const date = toISODateTime(row?.date);
if (!service || !date) continue;
const clientName = String(row?.client_name || "").trim();
const clientEmail = String(row?.client_email || "").trim().toLowerCase();
const clientPhone = String(row?.client_phone || "").trim();
let clientId: number | null = null;
let isWalkin = 0;
if (clientEmail) {
let client = findClientByEmail.get(req.user.business_id, clientEmail) as any;
if (!client) {
const newName = clientName || clientEmail;
const inserted = addClient.run(req.user.business_id, newName, clientEmail, clientPhone, "Imported from appointments import", "New");
clientId = Number(inserted.lastInsertRowid);
} else {
clientId = client.id;
}
} else if (clientName) {
const byName = findClientByName.get(req.user.business_id, clientName) as any;
if (byName) clientId = byName.id;
} else {
isWalkin = 1;
}
addAppointment.run(
req.user.business_id,
clientId,
service,
String(row?.staff || "").trim(),
date,
Math.max(MIN_DURATION_MINUTES, parseInt(row?.duration, 10) || 60),
Math.max(0, Number(row?.price) || 0),
Math.max(0, Number(row?.tip) || 0),
isWalkin,
String(row?.status || "Confirmed").trim() || "Confirmed",
String(row?.notes || "").trim(),
);
ensureServiceForBusiness(req.user.business_id, service, Math.max(MIN_DURATION_MINUTES, parseInt(row?.duration, 10) || 60), Math.max(0, Number(row?.price) || 0), "Imported");
imported++;
}
return imported;
});
res.json({ imported: tx(rows) });
});
app.post("/api/integrations/booking/:webhookKey", integrationIngestRateLimit, (req, res) => {
const result = importIntegrationAppointment(req.params.webhookKey, req.body, "booking");
res.status(result.status).json(result.body);
});
app.post("/api/integrations/website/:webhookKey", integrationIngestRateLimit, (req, res) => {
const result = importIntegrationAppointment(req.params.webhookKey, req.body, "website");
res.status(result.status).json(result.body);
});
// EXPENSES â scoped
app.get("/api/expenses", auth, businessOnly, scopeCheck, (req: any, res) => {
res.json(db.prepare("SELECT * FROM expenses WHERE business_id=? ORDER BY date DESC").all(req.user.business_id));
});
app.post("/api/expenses", auth, businessOnly, scopeCheck, (req: any, res) => {
const { date, category, amount, note } = req.body;
const r = db.prepare("INSERT INTO expenses (business_id,date,category,amount,note) VALUES (?,?,?,?,?)").run(req.user.business_id, date, category, amount, note||"");
res.json({ id: r.lastInsertRowid });
});
app.put("/api/expenses/:id", auth, businessOnly, scopeCheck, (req: any, res) => {
const { date, category, amount, note } = req.body;
db.prepare("UPDATE expenses SET date=?,category=?,amount=?,note=? WHERE id=? AND business_id=?").run(date, category, amount, note||"", req.params.id, req.user.business_id);
res.json({ ok: true });
});
app.delete("/api/expenses/:id", auth, businessOnly, scopeCheck, (req: any, res) => {
db.prepare("DELETE FROM expenses WHERE id=? AND business_id=?").run(req.params.id, req.user.business_id);
res.json({ ok: true });
});
app.post("/api/expenses/import", auth, businessOnly, scopeCheck, (req: any, res) => {
const { rows } = req.body;
const ins = db.prepare("INSERT INTO expenses (business_id,date,category,amount,note) VALUES (?,?,?,?,?)");
const tx = db.transaction((es: any[]) => { let n=0; for (const e of es) { if (!e.date||!e.amount) continue; ins.run(req.user.business_id,e.date,e.category||"Other",e.amount,e.note||""); n++; } return n; });
res.json({ imported: tx(rows) });
});
// MANUAL REVENUE â scoped
app.get("/api/revenue/manual", auth, businessOnly, scopeCheck, (req: any, res) => {
res.json(db.prepare("SELECT * FROM manual_revenue WHERE business_id=? ORDER BY date DESC").all(req.user.business_id));