@fayz-ai/plugin-agenda 0.2.1 → 0.3.0
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/dist/chunk-DRZF6CV2.js +421 -0
- package/dist/chunk-DRZF6CV2.js.map +1 -0
- package/dist/chunk-LFUELUIT.cjs +423 -0
- package/dist/chunk-LFUELUIT.cjs.map +1 -0
- package/dist/index.cjs +741 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +741 -10
- package/dist/index.js.map +1 -1
- package/dist/migrations/index.d.ts +2 -0
- package/dist/migrations/index.d.ts.map +1 -0
- package/dist/public/BookingWidget.d.ts +4 -2
- package/dist/public/BookingWidget.d.ts.map +1 -1
- package/dist/public/components/BrandHeader.d.ts +6 -0
- package/dist/public/components/BrandHeader.d.ts.map +1 -0
- package/dist/public/components/ServiceCard.d.ts +5 -0
- package/dist/public/components/ServiceCard.d.ts.map +1 -0
- package/dist/public/components/SidebarExtras.d.ts +4 -0
- package/dist/public/components/SidebarExtras.d.ts.map +1 -0
- package/dist/public/components/StepRailItem.d.ts +4 -0
- package/dist/public/components/StepRailItem.d.ts.map +1 -0
- package/dist/public/components/SuccessPanel.d.ts +4 -0
- package/dist/public/components/SuccessPanel.d.ts.map +1 -0
- package/dist/public/components/SummaryRail.d.ts +27 -0
- package/dist/public/components/SummaryRail.d.ts.map +1 -0
- package/dist/public/components/defaults.d.ts +3 -0
- package/dist/public/components/defaults.d.ts.map +1 -0
- package/dist/public/context.d.ts +22 -8
- package/dist/public/context.d.ts.map +1 -1
- package/dist/public/createPublicBookingPlugin.d.ts +27 -10
- package/dist/public/createPublicBookingPlugin.d.ts.map +1 -1
- package/dist/public/data.d.ts +37 -0
- package/dist/public/data.d.ts.map +1 -0
- package/dist/public/data.supabase.d.ts +7 -0
- package/dist/public/data.supabase.d.ts.map +1 -0
- package/dist/public/format.d.ts +18 -0
- package/dist/public/format.d.ts.map +1 -0
- package/dist/public/hooks.d.ts +13 -11
- package/dist/public/hooks.d.ts.map +1 -1
- package/dist/public/index.cjs +1016 -613
- package/dist/public/index.cjs.map +1 -1
- package/dist/public/index.d.ts +15 -4
- package/dist/public/index.d.ts.map +1 -1
- package/dist/public/index.js +1006 -616
- package/dist/public/index.js.map +1 -1
- package/dist/public/steps/ContactStep.d.ts +30 -0
- package/dist/public/steps/ContactStep.d.ts.map +1 -0
- package/dist/public/steps/DateTimeStep.d.ts +23 -0
- package/dist/public/steps/DateTimeStep.d.ts.map +1 -0
- package/dist/public/steps/OverviewStep.d.ts +16 -0
- package/dist/public/steps/OverviewStep.d.ts.map +1 -0
- package/dist/public/steps/PaymentStep.d.ts +20 -0
- package/dist/public/steps/PaymentStep.d.ts.map +1 -0
- package/dist/public/types.d.ts +134 -0
- package/dist/public/types.d.ts.map +1 -1
- package/package.json +12 -12
- package/src/index.ts +11 -0
- package/src/migrations/001_public_booking.sql +278 -0
- package/src/migrations/index.ts +282 -0
- package/src/public/BookingWidget.tsx +136 -713
- package/src/public/components/BrandHeader.tsx +27 -0
- package/src/public/components/ServiceCard.tsx +39 -0
- package/src/public/components/SidebarExtras.tsx +30 -0
- package/src/public/components/StepRailItem.tsx +27 -0
- package/src/public/components/SuccessPanel.tsx +27 -0
- package/src/public/components/SummaryRail.tsx +90 -0
- package/src/public/components/defaults.ts +14 -0
- package/src/public/context.tsx +64 -7
- package/src/public/createPublicBookingPlugin.ts +131 -21
- package/src/public/data.supabase.ts +94 -0
- package/src/public/data.ts +99 -0
- package/src/public/format.ts +61 -0
- package/src/public/hooks.ts +22 -34
- package/src/public/index.ts +57 -4
- package/src/public/steps/ContactStep.tsx +179 -0
- package/src/public/steps/DateTimeStep.tsx +138 -0
- package/src/public/steps/OverviewStep.tsx +81 -0
- package/src/public/steps/PaymentStep.tsx +180 -0
- package/src/public/types.ts +169 -3
- package/dist/chunk-GAJVIEXM.cjs +0 -870
- package/dist/chunk-GAJVIEXM.cjs.map +0 -1
- package/dist/chunk-U6VTLOHN.js +0 -867
- package/dist/chunk-U6VTLOHN.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkLFUELUIT_cjs = require('./chunk-LFUELUIT.cjs');
|
|
4
4
|
var chunkSYDBDI3Q_cjs = require('./chunk-SYDBDI3Q.cjs');
|
|
5
5
|
var React12 = require('react');
|
|
6
6
|
var core = require('@fayz-ai/core');
|
|
@@ -1726,8 +1726,8 @@ function AppointmentModal({ open, mode, bookingId, prefill, initialTab, external
|
|
|
1726
1726
|
onCancel: () => setClientEditing(false),
|
|
1727
1727
|
onSubmit: async (data) => {
|
|
1728
1728
|
try {
|
|
1729
|
-
const { getSupabaseClientOptional:
|
|
1730
|
-
const supabase =
|
|
1729
|
+
const { getSupabaseClientOptional: getSupabaseClientOptional4 } = await import('@fayz-ai/core');
|
|
1730
|
+
const supabase = getSupabaseClientOptional4();
|
|
1731
1731
|
if (!supabase) return;
|
|
1732
1732
|
const dbData = {};
|
|
1733
1733
|
for (const [k, v] of Object.entries(data)) {
|
|
@@ -1888,10 +1888,10 @@ function AppointmentModal({ open, mode, bookingId, prefill, initialTab, external
|
|
|
1888
1888
|
onClick: async () => {
|
|
1889
1889
|
setCreatingClient(true);
|
|
1890
1890
|
try {
|
|
1891
|
-
const { getSupabaseClientOptional:
|
|
1892
|
-
const { getAgendaTenantId } = await import('./tenant-222EHYUC.cjs');
|
|
1893
|
-
const supabase =
|
|
1894
|
-
const tenantId =
|
|
1891
|
+
const { getSupabaseClientOptional: getSupabaseClientOptional4 } = await import('@fayz-ai/core');
|
|
1892
|
+
const { getAgendaTenantId: getAgendaTenantId2 } = await import('./tenant-222EHYUC.cjs');
|
|
1893
|
+
const supabase = getSupabaseClientOptional4();
|
|
1894
|
+
const tenantId = getAgendaTenantId2();
|
|
1895
1895
|
if (!supabase || !tenantId) throw new Error("Not initialized");
|
|
1896
1896
|
const { data: personRow, error: personError } = await supabase.schema("saas_core").from("persons").insert({
|
|
1897
1897
|
tenant_id: tenantId,
|
|
@@ -3473,6 +3473,448 @@ function AgendaPage({ config, provider: provider2, store }) {
|
|
|
3473
3473
|
}, [tenantId]);
|
|
3474
3474
|
return /* @__PURE__ */ jsxRuntime.jsx(AgendaContextProvider, { config, provider: provider2, store, children: /* @__PURE__ */ jsxRuntime.jsx(CalendarView, {}) });
|
|
3475
3475
|
}
|
|
3476
|
+
function getTenantId() {
|
|
3477
|
+
return chunkSYDBDI3Q_cjs.getAgendaTenantId() ?? core.getActiveTenantId();
|
|
3478
|
+
}
|
|
3479
|
+
function snakeToCamel(obj) {
|
|
3480
|
+
const result = {};
|
|
3481
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
3482
|
+
result[key.replace(/_([a-z])/g, (_, c) => c.toUpperCase())] = value;
|
|
3483
|
+
}
|
|
3484
|
+
return result;
|
|
3485
|
+
}
|
|
3486
|
+
function getClients() {
|
|
3487
|
+
const supabase = core.getSupabaseClientOptional();
|
|
3488
|
+
if (!supabase) throw new Error("Supabase not initialized");
|
|
3489
|
+
return { core: supabase.schema("saas_core"), pub: supabase };
|
|
3490
|
+
}
|
|
3491
|
+
function mapBooking(row) {
|
|
3492
|
+
return snakeToCamel(row);
|
|
3493
|
+
}
|
|
3494
|
+
function mapSchedule(row) {
|
|
3495
|
+
return snakeToCamel(row);
|
|
3496
|
+
}
|
|
3497
|
+
function addMinutes(iso, minutes) {
|
|
3498
|
+
const d = new Date(iso);
|
|
3499
|
+
d.setMinutes(d.getMinutes() + minutes);
|
|
3500
|
+
return d.toISOString();
|
|
3501
|
+
}
|
|
3502
|
+
function unique(values) {
|
|
3503
|
+
return Array.from(new Set(values.filter((value) => !!value)));
|
|
3504
|
+
}
|
|
3505
|
+
function indexById(rows) {
|
|
3506
|
+
return new Map((rows ?? []).filter((row) => row?.id).map((row) => [row.id, row]));
|
|
3507
|
+
}
|
|
3508
|
+
function groupBy(rows, getKey) {
|
|
3509
|
+
const grouped = /* @__PURE__ */ new Map();
|
|
3510
|
+
for (const row of rows) {
|
|
3511
|
+
const key = getKey(row);
|
|
3512
|
+
if (!key) continue;
|
|
3513
|
+
grouped.set(key, [...grouped.get(key) ?? [], row]);
|
|
3514
|
+
}
|
|
3515
|
+
return grouped;
|
|
3516
|
+
}
|
|
3517
|
+
function getDurationMinutes(startsAt, endsAt) {
|
|
3518
|
+
if (!endsAt) return null;
|
|
3519
|
+
return Math.round((new Date(endsAt).getTime() - new Date(startsAt).getTime()) / 6e4);
|
|
3520
|
+
}
|
|
3521
|
+
function mergeBookings(viewBookings, canonicalBookings) {
|
|
3522
|
+
const canonicalIds = new Set(canonicalBookings.map((booking) => booking.id));
|
|
3523
|
+
const canonicalOrderIds = new Set(canonicalBookings.map((booking) => booking.orderId).filter(Boolean));
|
|
3524
|
+
const merged = [
|
|
3525
|
+
...canonicalBookings,
|
|
3526
|
+
...viewBookings.filter((booking) => {
|
|
3527
|
+
if (canonicalIds.has(booking.id)) return false;
|
|
3528
|
+
if (booking.orderId && canonicalOrderIds.has(booking.orderId)) return false;
|
|
3529
|
+
if (canonicalOrderIds.has(booking.id)) return false;
|
|
3530
|
+
return true;
|
|
3531
|
+
})
|
|
3532
|
+
];
|
|
3533
|
+
return merged.sort((a, b) => new Date(a.startsAt).getTime() - new Date(b.startsAt).getTime());
|
|
3534
|
+
}
|
|
3535
|
+
async function hydrateCanonicalBookings(core, bookings) {
|
|
3536
|
+
const personIds = unique(bookings.flatMap((booking) => [booking.party_id, booking.assignee_id]));
|
|
3537
|
+
const locationIds = unique(bookings.map((booking) => booking.location_id));
|
|
3538
|
+
const orderIds = unique(bookings.map((booking) => booking.order_id));
|
|
3539
|
+
unique(bookings.map((booking) => booking.id));
|
|
3540
|
+
const [
|
|
3541
|
+
{ data: persons, error: personsError },
|
|
3542
|
+
{ data: locations, error: locationsError },
|
|
3543
|
+
{ data: orders, error: ordersError },
|
|
3544
|
+
{ data: items, error: itemsError }
|
|
3545
|
+
] = await Promise.all([
|
|
3546
|
+
personIds.length ? core.from("persons").select("id, name, phone, email, avatar_url").in("id", personIds) : Promise.resolve({ data: [], error: null }),
|
|
3547
|
+
locationIds.length ? core.from("locations").select("id, name").in("id", locationIds) : Promise.resolve({ data: [], error: null }),
|
|
3548
|
+
orderIds.length ? core.from("orders").select("id, total, status").in("id", orderIds) : Promise.resolve({ data: [], error: null }),
|
|
3549
|
+
orderIds.length ? core.from("order_items").select("id, order_id, service_id, name, duration_minutes, unit_price, assignee_id, sort_order").in("order_id", orderIds) : Promise.resolve({ data: [], error: null })
|
|
3550
|
+
]);
|
|
3551
|
+
if (personsError) throw personsError;
|
|
3552
|
+
if (locationsError) throw locationsError;
|
|
3553
|
+
if (ordersError) throw ordersError;
|
|
3554
|
+
if (itemsError) throw itemsError;
|
|
3555
|
+
const personsById = indexById(persons);
|
|
3556
|
+
const locationsById = indexById(locations);
|
|
3557
|
+
const ordersById = indexById(orders);
|
|
3558
|
+
const itemsByOrderId = groupBy(items ?? [], (item) => item.order_id);
|
|
3559
|
+
return bookings.map((booking) => {
|
|
3560
|
+
const client = booking.party_id ? personsById.get(booking.party_id) : null;
|
|
3561
|
+
const professional = booking.assignee_id ? personsById.get(booking.assignee_id) : null;
|
|
3562
|
+
const location = booking.location_id ? locationsById.get(booking.location_id) : null;
|
|
3563
|
+
const order = booking.order_id ? ordersById.get(booking.order_id) : null;
|
|
3564
|
+
const bookingItems = ((booking.order_id ? itemsByOrderId.get(booking.order_id) : null) ?? []).sort((a, b) => (a.sort_order ?? 0) - (b.sort_order ?? 0)).map((item) => ({
|
|
3565
|
+
id: item.id,
|
|
3566
|
+
serviceId: item.service_id,
|
|
3567
|
+
name: item.name,
|
|
3568
|
+
durationMinutes: item.duration_minutes,
|
|
3569
|
+
price: item.unit_price,
|
|
3570
|
+
assigneeId: item.assignee_id
|
|
3571
|
+
}));
|
|
3572
|
+
const itemDuration = bookingItems.reduce((sum, item) => sum + (item.durationMinutes ?? 0), 0);
|
|
3573
|
+
return {
|
|
3574
|
+
id: booking.id,
|
|
3575
|
+
tenantId: booking.tenant_id,
|
|
3576
|
+
kind: booking.kind,
|
|
3577
|
+
startsAt: booking.starts_at,
|
|
3578
|
+
endsAt: booking.ends_at,
|
|
3579
|
+
status: booking.status,
|
|
3580
|
+
notes: booking.notes,
|
|
3581
|
+
orderId: booking.order_id,
|
|
3582
|
+
locationId: booking.location_id,
|
|
3583
|
+
metadata: booking.metadata ?? {},
|
|
3584
|
+
clientId: booking.party_id,
|
|
3585
|
+
// Simple events (no party) fall back to the title stored in metadata.
|
|
3586
|
+
clientName: client?.name ?? booking.metadata?.title ?? null,
|
|
3587
|
+
clientPhone: client?.phone ?? null,
|
|
3588
|
+
clientEmail: client?.email ?? null,
|
|
3589
|
+
clientAvatarUrl: client?.avatar_url ?? null,
|
|
3590
|
+
professionalId: booking.assignee_id,
|
|
3591
|
+
professionalName: professional?.name ?? null,
|
|
3592
|
+
professionalAvatarUrl: professional?.avatar_url ?? null,
|
|
3593
|
+
locationName: location?.name ?? null,
|
|
3594
|
+
orderTotal: order?.total ?? null,
|
|
3595
|
+
orderStatus: order?.status ?? null,
|
|
3596
|
+
services: bookingItems.length ? bookingItems : null,
|
|
3597
|
+
totalDurationMinutes: itemDuration || getDurationMinutes(booking.starts_at, booking.ends_at),
|
|
3598
|
+
createdAt: booking.created_at,
|
|
3599
|
+
updatedAt: booking.updated_at
|
|
3600
|
+
};
|
|
3601
|
+
});
|
|
3602
|
+
}
|
|
3603
|
+
async function getCanonicalBookings(core, query) {
|
|
3604
|
+
let q = core.from("bookings").select("id, tenant_id, kind, starts_at, ends_at, status, notes, order_id, location_id, metadata, party_id, assignee_id, created_at, updated_at").gte("starts_at", query.dateRange.start).lte("starts_at", query.dateRange.end);
|
|
3605
|
+
if (query.kind) q = q.eq("kind", query.kind);
|
|
3606
|
+
if (query.statuses?.length) q = q.in("status", query.statuses);
|
|
3607
|
+
if (query.professionalIds?.length) q = q.in("assignee_id", query.professionalIds);
|
|
3608
|
+
if (query.locationId) q = q.eq("location_id", query.locationId);
|
|
3609
|
+
const { data, error } = await q.order("starts_at", { ascending: true });
|
|
3610
|
+
if (error) throw error;
|
|
3611
|
+
return hydrateCanonicalBookings(core, data ?? []);
|
|
3612
|
+
}
|
|
3613
|
+
async function getCanonicalBookingById(core, id) {
|
|
3614
|
+
const columns = "id, tenant_id, kind, starts_at, ends_at, status, notes, order_id, location_id, metadata, party_id, assignee_id, created_at, updated_at";
|
|
3615
|
+
let { data, error } = await core.from("bookings").select(columns).eq("id", id).maybeSingle();
|
|
3616
|
+
if (error) throw error;
|
|
3617
|
+
if (!data) {
|
|
3618
|
+
const fallback = await core.from("bookings").select(columns).eq("order_id", id).maybeSingle();
|
|
3619
|
+
if (fallback.error) throw fallback.error;
|
|
3620
|
+
data = fallback.data;
|
|
3621
|
+
}
|
|
3622
|
+
if (!data) return null;
|
|
3623
|
+
const [booking] = await hydrateCanonicalBookings(core, [data]);
|
|
3624
|
+
return booking ?? null;
|
|
3625
|
+
}
|
|
3626
|
+
function createSupabaseAgendaProvider() {
|
|
3627
|
+
return {
|
|
3628
|
+
// v_bookings is in public schema (PostgREST default)
|
|
3629
|
+
async getBookings(query) {
|
|
3630
|
+
const { core, pub } = getClients();
|
|
3631
|
+
let q = pub.from("v_bookings").select("*").gte("starts_at", query.dateRange.start).lte("starts_at", query.dateRange.end);
|
|
3632
|
+
if (query.kind) q = q.eq("kind", query.kind);
|
|
3633
|
+
if (query.statuses?.length) q = q.in("status", query.statuses);
|
|
3634
|
+
if (query.professionalIds?.length) q = q.in("professional_id", query.professionalIds);
|
|
3635
|
+
if (query.locationId) q = q.eq("location_id", query.locationId);
|
|
3636
|
+
q = q.order("starts_at", { ascending: true });
|
|
3637
|
+
const { data, error } = await q;
|
|
3638
|
+
if (error) throw error;
|
|
3639
|
+
const viewBookings = (data ?? []).map(mapBooking);
|
|
3640
|
+
const canonicalBookings = await getCanonicalBookings(core, query);
|
|
3641
|
+
return mergeBookings(viewBookings, canonicalBookings);
|
|
3642
|
+
},
|
|
3643
|
+
async getBookingById(id) {
|
|
3644
|
+
const { core, pub } = getClients();
|
|
3645
|
+
const { data, error } = await pub.from("v_bookings").select("*").eq("id", id).maybeSingle();
|
|
3646
|
+
if (error) throw error;
|
|
3647
|
+
if (data) return mapBooking(data);
|
|
3648
|
+
return getCanonicalBookingById(core, id);
|
|
3649
|
+
},
|
|
3650
|
+
async createBooking(input) {
|
|
3651
|
+
const { core } = getClients();
|
|
3652
|
+
const tenantId = getTenantId();
|
|
3653
|
+
if (!tenantId) throw new Error("No active tenant");
|
|
3654
|
+
const hasServices = input.services.length > 0;
|
|
3655
|
+
const totalDuration = input.services.reduce((sum, s) => sum + s.durationMinutes, 0);
|
|
3656
|
+
const totalPrice = input.services.reduce((sum, s) => sum + s.price, 0);
|
|
3657
|
+
const endsAt = addMinutes(input.startsAt, totalDuration || 30);
|
|
3658
|
+
const serviceNames = input.services.map((s) => s.name).join(", ");
|
|
3659
|
+
let contactName;
|
|
3660
|
+
if (input.clientId) {
|
|
3661
|
+
const { data: person } = await core.from("persons").select("name").eq("id", input.clientId).single();
|
|
3662
|
+
contactName = person?.name;
|
|
3663
|
+
}
|
|
3664
|
+
const eventTitle = input.title?.trim() || void 0;
|
|
3665
|
+
const { data: order, error: orderErr } = await core.from("orders").insert({
|
|
3666
|
+
tenant_id: tenantId,
|
|
3667
|
+
kind: input.kind ?? "appointment",
|
|
3668
|
+
status: "scheduled",
|
|
3669
|
+
party_id: input.clientId || null,
|
|
3670
|
+
assignee_id: input.professionalId,
|
|
3671
|
+
location_id: input.locationId ?? null,
|
|
3672
|
+
subtotal: totalPrice,
|
|
3673
|
+
total: totalPrice,
|
|
3674
|
+
notes: input.notes ?? null,
|
|
3675
|
+
metadata: {
|
|
3676
|
+
source: "agenda",
|
|
3677
|
+
...serviceNames ? { serviceNames, itemsSummary: serviceNames } : {},
|
|
3678
|
+
...contactName ? { contactName } : {},
|
|
3679
|
+
...eventTitle ? { title: eventTitle } : {}
|
|
3680
|
+
}
|
|
3681
|
+
}).select("id").single();
|
|
3682
|
+
if (orderErr) throw orderErr;
|
|
3683
|
+
const { data: booking, error: bookingErr } = await core.from("bookings").insert({
|
|
3684
|
+
tenant_id: tenantId,
|
|
3685
|
+
kind: input.kind ?? "appointment",
|
|
3686
|
+
party_id: input.clientId || null,
|
|
3687
|
+
assignee_id: input.professionalId,
|
|
3688
|
+
location_id: input.locationId ?? null,
|
|
3689
|
+
order_id: order.id,
|
|
3690
|
+
starts_at: input.startsAt,
|
|
3691
|
+
ends_at: endsAt,
|
|
3692
|
+
status: "scheduled",
|
|
3693
|
+
notes: input.notes ?? null,
|
|
3694
|
+
metadata: {
|
|
3695
|
+
source: "agenda",
|
|
3696
|
+
...serviceNames ? { serviceNames, itemsSummary: serviceNames } : {},
|
|
3697
|
+
...contactName ? { contactName } : {},
|
|
3698
|
+
...eventTitle ? { title: eventTitle } : {}
|
|
3699
|
+
}
|
|
3700
|
+
}).select("id").single();
|
|
3701
|
+
if (bookingErr) throw bookingErr;
|
|
3702
|
+
if (hasServices) {
|
|
3703
|
+
const orderItems = input.services.map((s, i) => ({
|
|
3704
|
+
order_id: order.id,
|
|
3705
|
+
service_id: s.serviceId,
|
|
3706
|
+
name: s.name,
|
|
3707
|
+
quantity: 1,
|
|
3708
|
+
unit_price: s.price,
|
|
3709
|
+
total: s.price,
|
|
3710
|
+
sort_order: i,
|
|
3711
|
+
duration_minutes: s.durationMinutes,
|
|
3712
|
+
assignee_id: s.assigneeId ?? input.professionalId
|
|
3713
|
+
}));
|
|
3714
|
+
const { error: oiErr } = await core.from("order_items").insert(orderItems);
|
|
3715
|
+
if (oiErr) throw oiErr;
|
|
3716
|
+
}
|
|
3717
|
+
const createdBooking = await this.getBookingById(booking.id);
|
|
3718
|
+
if (!createdBooking) throw new Error("Created booking did not resolve from agenda read model");
|
|
3719
|
+
return createdBooking;
|
|
3720
|
+
},
|
|
3721
|
+
async updateBooking(id, data) {
|
|
3722
|
+
const { core } = getClients();
|
|
3723
|
+
const { data: existing, error: existingErr } = await core.from("bookings").select("id, order_id, starts_at, assignee_id, metadata").eq("id", id).single();
|
|
3724
|
+
if (existingErr) throw existingErr;
|
|
3725
|
+
const updates = {};
|
|
3726
|
+
if (data.startsAt) updates.starts_at = data.startsAt;
|
|
3727
|
+
if (data.endsAt) updates.ends_at = data.endsAt;
|
|
3728
|
+
if (data.title !== void 0) {
|
|
3729
|
+
updates.metadata = { ...existing?.metadata ?? {}, title: data.title };
|
|
3730
|
+
}
|
|
3731
|
+
if (data.status) {
|
|
3732
|
+
updates.status = data.status;
|
|
3733
|
+
}
|
|
3734
|
+
if (data.notes !== void 0) updates.notes = data.notes;
|
|
3735
|
+
if (data.professionalId) updates.assignee_id = data.professionalId;
|
|
3736
|
+
if (data.clientId) updates.party_id = data.clientId;
|
|
3737
|
+
if (data.locationId) updates.location_id = data.locationId;
|
|
3738
|
+
if (data.services) {
|
|
3739
|
+
const totalDuration = data.services.reduce((s, svc) => s + svc.durationMinutes, 0);
|
|
3740
|
+
data.services.reduce((s, svc) => s + svc.price, 0);
|
|
3741
|
+
const startsAt = data.startsAt ?? existing?.starts_at;
|
|
3742
|
+
if (startsAt) {
|
|
3743
|
+
const ends = new Date(new Date(startsAt).getTime() + totalDuration * 6e4);
|
|
3744
|
+
updates.ends_at = ends.toISOString();
|
|
3745
|
+
}
|
|
3746
|
+
}
|
|
3747
|
+
const effectiveAssigneeId = data.professionalId ?? existing?.assignee_id ?? null;
|
|
3748
|
+
const { error } = await core.from("bookings").update(updates).eq("id", id);
|
|
3749
|
+
if (error) throw error;
|
|
3750
|
+
const orderId = existing?.order_id;
|
|
3751
|
+
if (orderId) {
|
|
3752
|
+
const orderUpdates = {};
|
|
3753
|
+
if (data.notes !== void 0) orderUpdates.notes = data.notes;
|
|
3754
|
+
if (data.professionalId) orderUpdates.assignee_id = data.professionalId;
|
|
3755
|
+
if (data.clientId) orderUpdates.party_id = data.clientId;
|
|
3756
|
+
if (data.locationId) orderUpdates.location_id = data.locationId;
|
|
3757
|
+
if (data.services) {
|
|
3758
|
+
const totalPrice = data.services.reduce((s, svc) => s + svc.price, 0);
|
|
3759
|
+
orderUpdates.subtotal = totalPrice;
|
|
3760
|
+
orderUpdates.total = totalPrice;
|
|
3761
|
+
}
|
|
3762
|
+
if (Object.keys(orderUpdates).length > 0) {
|
|
3763
|
+
const { error: orderErr } = await core.from("orders").update(orderUpdates).eq("id", orderId);
|
|
3764
|
+
if (orderErr) throw orderErr;
|
|
3765
|
+
}
|
|
3766
|
+
if (data.services) {
|
|
3767
|
+
await core.from("order_items").delete().eq("order_id", orderId);
|
|
3768
|
+
if (data.services.length > 0) {
|
|
3769
|
+
const items = data.services.map((svc, idx) => ({
|
|
3770
|
+
order_id: orderId,
|
|
3771
|
+
service_id: svc.serviceId || null,
|
|
3772
|
+
name: svc.name,
|
|
3773
|
+
quantity: 1,
|
|
3774
|
+
unit_price: svc.price,
|
|
3775
|
+
total: svc.price,
|
|
3776
|
+
sort_order: idx,
|
|
3777
|
+
duration_minutes: svc.durationMinutes,
|
|
3778
|
+
assignee_id: svc.assigneeId ?? effectiveAssigneeId
|
|
3779
|
+
}));
|
|
3780
|
+
const { error: itemErr } = await core.from("order_items").insert(items);
|
|
3781
|
+
if (itemErr) throw itemErr;
|
|
3782
|
+
}
|
|
3783
|
+
}
|
|
3784
|
+
}
|
|
3785
|
+
return await this.getBookingById(id);
|
|
3786
|
+
},
|
|
3787
|
+
async deleteBooking(id) {
|
|
3788
|
+
const { core, pub } = getClients();
|
|
3789
|
+
const { data: booking, error: bookingErr } = await core.from("bookings").select("order_id").eq("id", id).single();
|
|
3790
|
+
if (bookingErr) throw bookingErr;
|
|
3791
|
+
const orderId = booking?.order_id;
|
|
3792
|
+
const { count, error: movementErr } = orderId ? await pub.from("financial_movements").select("id", { count: "exact", head: true }).eq("invoice_id", orderId) : { count: 0, error: null };
|
|
3793
|
+
if (movementErr) throw movementErr;
|
|
3794
|
+
if (orderId && (count ?? 0) > 0) {
|
|
3795
|
+
await pub.from("financial_movements").update({ status: "cancelled" }).eq("invoice_id", orderId);
|
|
3796
|
+
await core.from("orders").update({ status: "cancelled" }).eq("id", orderId);
|
|
3797
|
+
const { error } = await core.from("bookings").update({ status: "cancelled" }).eq("id", id);
|
|
3798
|
+
if (error) throw error;
|
|
3799
|
+
} else {
|
|
3800
|
+
if (orderId) {
|
|
3801
|
+
await pub.from("financial_movements").delete().eq("invoice_id", orderId);
|
|
3802
|
+
}
|
|
3803
|
+
const { error } = await core.from("bookings").delete().eq("id", id);
|
|
3804
|
+
if (error) throw error;
|
|
3805
|
+
if (orderId) {
|
|
3806
|
+
const { error: orderErr } = await core.from("orders").delete().eq("id", orderId);
|
|
3807
|
+
if (orderErr) throw orderErr;
|
|
3808
|
+
}
|
|
3809
|
+
}
|
|
3810
|
+
},
|
|
3811
|
+
async updateBookingStatus(id, status) {
|
|
3812
|
+
return this.updateBooking(id, { status });
|
|
3813
|
+
},
|
|
3814
|
+
async checkConflict(params) {
|
|
3815
|
+
const { core } = getClients();
|
|
3816
|
+
let q = core.from("bookings").select("id", { count: "exact", head: true }).eq("assignee_id", params.assigneeId).eq("kind", "appointment").not("status", "in", '("cancelled","no_show")').not("starts_at", "is", null).lt("starts_at", params.endsAt).gt("ends_at", params.startsAt);
|
|
3817
|
+
if (params.excludeBookingId) {
|
|
3818
|
+
q = q.neq("id", params.excludeBookingId);
|
|
3819
|
+
}
|
|
3820
|
+
const { count, error } = await q;
|
|
3821
|
+
if (error) throw error;
|
|
3822
|
+
return (count ?? 0) > 0;
|
|
3823
|
+
},
|
|
3824
|
+
async getAvailableSlots(params) {
|
|
3825
|
+
const { pub } = getClients();
|
|
3826
|
+
const { data, error } = await pub.rpc("get_available_slots", {
|
|
3827
|
+
p_tenant_id: getTenantId(),
|
|
3828
|
+
p_assignee_id: params.assigneeId,
|
|
3829
|
+
p_date: params.date,
|
|
3830
|
+
p_duration_minutes: params.durationMinutes,
|
|
3831
|
+
p_slot_interval: params.slotInterval ?? 30
|
|
3832
|
+
});
|
|
3833
|
+
if (error) throw error;
|
|
3834
|
+
return (data ?? []).map((row) => ({ start: row.slot_start, end: row.slot_end }));
|
|
3835
|
+
},
|
|
3836
|
+
async getSchedules(professionalId) {
|
|
3837
|
+
const { core } = getClients();
|
|
3838
|
+
let q = core.from("schedules").select("*").eq("kind", "working_hours");
|
|
3839
|
+
if (professionalId) q = q.eq("assignee_id", professionalId);
|
|
3840
|
+
q = q.order("day_of_week").order("starts_at");
|
|
3841
|
+
const { data, error } = await q;
|
|
3842
|
+
if (error) throw error;
|
|
3843
|
+
return (data ?? []).map(mapSchedule);
|
|
3844
|
+
},
|
|
3845
|
+
async saveSchedule(input) {
|
|
3846
|
+
const { core } = getClients();
|
|
3847
|
+
const tenantId = getTenantId();
|
|
3848
|
+
if (!tenantId) throw new Error("No active tenant");
|
|
3849
|
+
const { data, error } = await core.from("schedules").insert({
|
|
3850
|
+
tenant_id: tenantId,
|
|
3851
|
+
kind: "working_hours",
|
|
3852
|
+
assignee_id: input.assigneeId,
|
|
3853
|
+
location_id: input.locationId ?? null,
|
|
3854
|
+
day_of_week: input.dayOfWeek ?? null,
|
|
3855
|
+
specific_date: input.specificDate ?? null,
|
|
3856
|
+
starts_at: input.startsAt,
|
|
3857
|
+
ends_at: input.endsAt,
|
|
3858
|
+
is_active: input.isActive !== false,
|
|
3859
|
+
metadata: input.metadata ?? {}
|
|
3860
|
+
}).select("*").single();
|
|
3861
|
+
if (error) throw error;
|
|
3862
|
+
return mapSchedule(data);
|
|
3863
|
+
},
|
|
3864
|
+
async deleteSchedule(id) {
|
|
3865
|
+
const { core } = getClients();
|
|
3866
|
+
const { error } = await core.from("schedules").delete().eq("id", id);
|
|
3867
|
+
if (error) throw error;
|
|
3868
|
+
},
|
|
3869
|
+
async getProfessionals() {
|
|
3870
|
+
const { core } = getClients();
|
|
3871
|
+
const { data, error } = await core.from("persons").select("id, name, avatar_url, is_active").eq("kind", "staff").eq("is_active", true).order("name");
|
|
3872
|
+
if (error) throw error;
|
|
3873
|
+
return (data ?? []).map((p) => ({
|
|
3874
|
+
id: p.id,
|
|
3875
|
+
name: p.name,
|
|
3876
|
+
avatarUrl: p.avatar_url,
|
|
3877
|
+
locationId: null,
|
|
3878
|
+
locationName: null,
|
|
3879
|
+
isActive: p.is_active
|
|
3880
|
+
}));
|
|
3881
|
+
},
|
|
3882
|
+
async getLocations() {
|
|
3883
|
+
const { core } = getClients();
|
|
3884
|
+
const { data, error } = await core.from("locations").select("id, name").eq("is_active", true).order("name");
|
|
3885
|
+
if (error) throw error;
|
|
3886
|
+
return (data ?? []).map((l) => ({ id: l.id, name: l.name }));
|
|
3887
|
+
},
|
|
3888
|
+
async getConfirmationsPending(daysAhead = 2) {
|
|
3889
|
+
const { pub } = getClients();
|
|
3890
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
3891
|
+
const cutoff = /* @__PURE__ */ new Date();
|
|
3892
|
+
cutoff.setDate(cutoff.getDate() + daysAhead);
|
|
3893
|
+
const { data, error } = await pub.from("v_bookings").select("*").eq("status", "scheduled").gte("starts_at", now).lte("starts_at", cutoff.toISOString()).order("starts_at");
|
|
3894
|
+
if (error) throw error;
|
|
3895
|
+
return (data ?? []).map(mapBooking);
|
|
3896
|
+
},
|
|
3897
|
+
async sendConfirmation(bookingId, channel) {
|
|
3898
|
+
const { core } = getClients();
|
|
3899
|
+
const { data: booking } = await core.from("bookings").select("metadata").eq("id", bookingId).single();
|
|
3900
|
+
const metadata = booking?.metadata ?? {};
|
|
3901
|
+
const confirmations = metadata.confirmations ?? [];
|
|
3902
|
+
confirmations.push({ channel, sentAt: (/* @__PURE__ */ new Date()).toISOString(), status: "sent" });
|
|
3903
|
+
await core.from("bookings").update({ metadata: { ...metadata, confirmations } }).eq("id", bookingId);
|
|
3904
|
+
},
|
|
3905
|
+
async completeBooking(bookingId) {
|
|
3906
|
+
const { core } = getClients();
|
|
3907
|
+
const { data: existing, error: existingErr } = await core.from("bookings").select("order_id").eq("id", bookingId).single();
|
|
3908
|
+
if (existingErr) throw existingErr;
|
|
3909
|
+
const { error } = await core.from("bookings").update({ status: "completed" }).eq("id", bookingId);
|
|
3910
|
+
if (error) throw error;
|
|
3911
|
+
const orderId = existing?.order_id;
|
|
3912
|
+
const booking = await this.getBookingById(bookingId);
|
|
3913
|
+
if (!booking) throw new Error("Booking not found");
|
|
3914
|
+
return { booking, orderId: orderId ?? bookingId };
|
|
3915
|
+
}
|
|
3916
|
+
};
|
|
3917
|
+
}
|
|
3476
3918
|
function getCurrentBookingQuery(state, dateRange = state.dateRange) {
|
|
3477
3919
|
return {
|
|
3478
3920
|
dateRange,
|
|
@@ -4221,6 +4663,287 @@ var agendaLocales = {
|
|
|
4221
4663
|
en,
|
|
4222
4664
|
"pt-BR": ptBR
|
|
4223
4665
|
};
|
|
4666
|
+
|
|
4667
|
+
// src/migrations/index.ts
|
|
4668
|
+
var MIGRATION_001_PUBLIC_BOOKING = `-- ============================================================================
|
|
4669
|
+
-- plugin-agenda 001: booking read model + public (anon) booking surface
|
|
4670
|
+
--
|
|
4671
|
+
-- Ships the objects the agenda providers reference but the spine (packages/db
|
|
4672
|
+
-- migrations 001\u2013008) does not define:
|
|
4673
|
+
-- \xA71 order_items columns the admin provider writes (duration/assignee)
|
|
4674
|
+
-- \xA72 public.v_bookings \u2014 admin read model (authenticated, RLS applies)
|
|
4675
|
+
-- \xA73 public.v_public_services \u2014 anon-readable service catalog
|
|
4676
|
+
-- \xA74 public.get_available_slots \u2014 anon RPC (mirrors the mock algorithm)
|
|
4677
|
+
-- \xA75 public.create_public_booking \u2014 anon RPC (validated, race-safe write)
|
|
4678
|
+
--
|
|
4679
|
+
-- Anon-access design: public booking pages run with the publishable key and no
|
|
4680
|
+
-- session, so the canonical tenant RLS yields nothing. Anon access is granted
|
|
4681
|
+
-- ONLY via the column-whitelisted view (\xA73) and SECURITY DEFINER RPCs (\xA74, \xA75)
|
|
4682
|
+
-- with tight input validation and hardcoded statuses.
|
|
4683
|
+
-- ============================================================================
|
|
4684
|
+
|
|
4685
|
+
-- \xA71 \u2014 columns the agenda provider writes but 004_archetypes lacks
|
|
4686
|
+
ALTER TABLE saas_core.order_items
|
|
4687
|
+
ADD COLUMN IF NOT EXISTS duration_minutes integer,
|
|
4688
|
+
ADD COLUMN IF NOT EXISTS assignee_id uuid REFERENCES saas_core.persons(id) ON DELETE SET NULL;
|
|
4689
|
+
|
|
4690
|
+
-- Upsert key for public customers (per tenant, by normalized phone)
|
|
4691
|
+
CREATE UNIQUE INDEX IF NOT EXISTS persons_tenant_customer_phone
|
|
4692
|
+
ON saas_core.persons(tenant_id, phone)
|
|
4693
|
+
WHERE kind = 'customer' AND phone IS NOT NULL;
|
|
4694
|
+
|
|
4695
|
+
-- Slot-conflict scan
|
|
4696
|
+
CREATE INDEX IF NOT EXISTS bookings_assignee_time
|
|
4697
|
+
ON saas_core.bookings(assignee_id, starts_at, ends_at)
|
|
4698
|
+
WHERE status NOT IN ('cancelled', 'no_show');
|
|
4699
|
+
|
|
4700
|
+
-- \xA72 \u2014 admin read model (security_invoker \u2192 underlying RLS applies; anon sees 0 rows)
|
|
4701
|
+
CREATE OR REPLACE VIEW public.v_bookings
|
|
4702
|
+
WITH (security_invoker = true) AS
|
|
4703
|
+
SELECT
|
|
4704
|
+
b.id, b.tenant_id, b.kind, b.starts_at, b.ends_at, b.status, b.notes,
|
|
4705
|
+
b.order_id, b.location_id, b.metadata, b.created_at, b.updated_at,
|
|
4706
|
+
b.party_id AS client_id,
|
|
4707
|
+
COALESCE(pc.name, b.metadata->>'title') AS client_name,
|
|
4708
|
+
pc.phone AS client_phone,
|
|
4709
|
+
pc.email AS client_email,
|
|
4710
|
+
pc.avatar_url AS client_avatar_url,
|
|
4711
|
+
b.assignee_id AS professional_id,
|
|
4712
|
+
ps.name AS professional_name,
|
|
4713
|
+
ps.avatar_url AS professional_avatar_url,
|
|
4714
|
+
l.name AS location_name,
|
|
4715
|
+
o.total AS order_total,
|
|
4716
|
+
o.status AS order_status,
|
|
4717
|
+
(EXTRACT(EPOCH FROM (COALESCE(b.ends_at, b.starts_at) - b.starts_at))::int / 60)
|
|
4718
|
+
AS total_duration_minutes
|
|
4719
|
+
FROM saas_core.bookings b
|
|
4720
|
+
LEFT JOIN saas_core.persons pc ON pc.id = b.party_id
|
|
4721
|
+
LEFT JOIN saas_core.persons ps ON ps.id = b.assignee_id
|
|
4722
|
+
LEFT JOIN saas_core.locations l ON l.id = b.location_id
|
|
4723
|
+
LEFT JOIN saas_core.orders o ON o.id = b.order_id;
|
|
4724
|
+
|
|
4725
|
+
GRANT SELECT ON public.v_bookings TO authenticated, service_role;
|
|
4726
|
+
REVOKE SELECT ON public.v_bookings FROM anon;
|
|
4727
|
+
|
|
4728
|
+
-- \xA73 \u2014 anon-readable public catalog (owner-rights view = deliberate, whitelisted)
|
|
4729
|
+
CREATE OR REPLACE VIEW public.v_public_services AS
|
|
4730
|
+
SELECT
|
|
4731
|
+
s.id, s.tenant_id, s.name, s.description, s.price, s.currency,
|
|
4732
|
+
COALESCE(s.duration_minutes, 30) AS duration_minutes,
|
|
4733
|
+
s.image_url,
|
|
4734
|
+
COALESCE((s.metadata->>'sort_order')::int, 0) AS sort_order
|
|
4735
|
+
FROM saas_core.services s
|
|
4736
|
+
WHERE s.is_active AND s.status = 'active';
|
|
4737
|
+
|
|
4738
|
+
GRANT SELECT ON public.v_public_services TO anon, authenticated, service_role;
|
|
4739
|
+
|
|
4740
|
+
-- \xA74 \u2014 availability RPC (mirrors plugin-agenda/src/data/mock.ts getAvailableSlots)
|
|
4741
|
+
CREATE OR REPLACE FUNCTION public.get_available_slots(
|
|
4742
|
+
p_tenant_id uuid,
|
|
4743
|
+
p_date date,
|
|
4744
|
+
p_duration_minutes integer,
|
|
4745
|
+
p_assignee_id uuid DEFAULT NULL,
|
|
4746
|
+
p_slot_interval integer DEFAULT 30
|
|
4747
|
+
) RETURNS TABLE (slot_start timestamptz, slot_end timestamptz)
|
|
4748
|
+
LANGUAGE plpgsql STABLE SECURITY DEFINER
|
|
4749
|
+
SET search_path = saas_core, public
|
|
4750
|
+
AS $$
|
|
4751
|
+
DECLARE
|
|
4752
|
+
v_tz text;
|
|
4753
|
+
BEGIN
|
|
4754
|
+
-- tight validation: this function is anon-callable
|
|
4755
|
+
IF p_duration_minutes IS NULL OR p_duration_minutes NOT BETWEEN 5 AND 480 THEN
|
|
4756
|
+
RAISE EXCEPTION 'invalid duration';
|
|
4757
|
+
END IF;
|
|
4758
|
+
IF p_slot_interval IS NULL OR p_slot_interval NOT BETWEEN 5 AND 240 THEN
|
|
4759
|
+
RAISE EXCEPTION 'invalid interval';
|
|
4760
|
+
END IF;
|
|
4761
|
+
IF p_date IS NULL OR p_date < current_date OR p_date > current_date + 90 THEN
|
|
4762
|
+
RETURN;
|
|
4763
|
+
END IF;
|
|
4764
|
+
|
|
4765
|
+
SELECT COALESCE(t.settings->>'timezone', 'America/Sao_Paulo') INTO v_tz
|
|
4766
|
+
FROM tenants t WHERE t.id = p_tenant_id;
|
|
4767
|
+
IF v_tz IS NULL THEN RETURN; END IF; -- unknown tenant \u2192 empty, not an error
|
|
4768
|
+
|
|
4769
|
+
RETURN QUERY
|
|
4770
|
+
WITH day_schedules AS (
|
|
4771
|
+
SELECT sc.assignee_id, sc.starts_at AS win_start, sc.ends_at AS win_end
|
|
4772
|
+
FROM schedules sc
|
|
4773
|
+
WHERE sc.tenant_id = p_tenant_id
|
|
4774
|
+
AND sc.kind = 'working_hours'
|
|
4775
|
+
AND sc.is_active
|
|
4776
|
+
AND (p_assignee_id IS NULL OR sc.assignee_id = p_assignee_id)
|
|
4777
|
+
AND (
|
|
4778
|
+
sc.specific_date = p_date
|
|
4779
|
+
OR (sc.specific_date IS NULL AND sc.day_of_week = EXTRACT(dow FROM p_date)::smallint)
|
|
4780
|
+
)
|
|
4781
|
+
),
|
|
4782
|
+
candidate AS (
|
|
4783
|
+
SELECT
|
|
4784
|
+
ds.assignee_id,
|
|
4785
|
+
gs AS s_start,
|
|
4786
|
+
gs + make_interval(mins => p_duration_minutes) AS s_end
|
|
4787
|
+
FROM day_schedules ds
|
|
4788
|
+
CROSS JOIN LATERAL generate_series(
|
|
4789
|
+
(p_date + ds.win_start) AT TIME ZONE v_tz,
|
|
4790
|
+
((p_date + ds.win_end) AT TIME ZONE v_tz) - make_interval(mins => p_duration_minutes),
|
|
4791
|
+
make_interval(mins => p_slot_interval)
|
|
4792
|
+
) AS gs
|
|
4793
|
+
)
|
|
4794
|
+
SELECT DISTINCT c.s_start, c.s_end
|
|
4795
|
+
FROM candidate c
|
|
4796
|
+
WHERE NOT EXISTS (
|
|
4797
|
+
SELECT 1 FROM bookings b
|
|
4798
|
+
WHERE b.tenant_id = p_tenant_id
|
|
4799
|
+
AND b.assignee_id = c.assignee_id
|
|
4800
|
+
AND b.status NOT IN ('cancelled', 'no_show')
|
|
4801
|
+
AND b.starts_at < c.s_end
|
|
4802
|
+
AND COALESCE(b.ends_at, b.starts_at) > c.s_start
|
|
4803
|
+
)
|
|
4804
|
+
ORDER BY 1;
|
|
4805
|
+
END;
|
|
4806
|
+
$$;
|
|
4807
|
+
|
|
4808
|
+
REVOKE ALL ON FUNCTION public.get_available_slots(uuid, date, integer, uuid, integer) FROM public;
|
|
4809
|
+
GRANT EXECUTE ON FUNCTION public.get_available_slots(uuid, date, integer, uuid, integer)
|
|
4810
|
+
TO anon, authenticated, service_role;
|
|
4811
|
+
|
|
4812
|
+
-- \xA75 \u2014 public booking write RPC (validated, race-safe, hardcoded statuses)
|
|
4813
|
+
CREATE OR REPLACE FUNCTION public.create_public_booking(
|
|
4814
|
+
p_tenant_id uuid,
|
|
4815
|
+
p_service_id uuid,
|
|
4816
|
+
p_starts_at timestamptz,
|
|
4817
|
+
p_name text,
|
|
4818
|
+
p_phone text,
|
|
4819
|
+
p_email text DEFAULT NULL,
|
|
4820
|
+
p_notes text DEFAULT NULL
|
|
4821
|
+
) RETURNS TABLE (booking_id uuid, starts_at timestamptz, ends_at timestamptz)
|
|
4822
|
+
LANGUAGE plpgsql SECURITY DEFINER
|
|
4823
|
+
SET search_path = saas_core, public
|
|
4824
|
+
AS $$
|
|
4825
|
+
DECLARE
|
|
4826
|
+
v_service record;
|
|
4827
|
+
v_person_id uuid;
|
|
4828
|
+
v_assignee uuid;
|
|
4829
|
+
v_ends timestamptz;
|
|
4830
|
+
v_order uuid;
|
|
4831
|
+
v_booking uuid;
|
|
4832
|
+
v_phone text;
|
|
4833
|
+
v_tz text;
|
|
4834
|
+
v_notes text;
|
|
4835
|
+
BEGIN
|
|
4836
|
+
-- validation: anon-callable, everything tight
|
|
4837
|
+
IF p_name IS NULL OR length(trim(p_name)) NOT BETWEEN 2 AND 120 THEN
|
|
4838
|
+
RAISE EXCEPTION 'invalid name';
|
|
4839
|
+
END IF;
|
|
4840
|
+
v_phone := regexp_replace(COALESCE(p_phone, ''), '\\D', '', 'g');
|
|
4841
|
+
IF length(v_phone) NOT BETWEEN 8 AND 15 THEN
|
|
4842
|
+
RAISE EXCEPTION 'invalid phone';
|
|
4843
|
+
END IF;
|
|
4844
|
+
IF p_email IS NOT NULL AND (length(p_email) > 254 OR p_email !~ '^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$') THEN
|
|
4845
|
+
RAISE EXCEPTION 'invalid email';
|
|
4846
|
+
END IF;
|
|
4847
|
+
v_notes := left(p_notes, 500);
|
|
4848
|
+
IF p_starts_at IS NULL OR p_starts_at <= now() OR p_starts_at > now() + interval '90 days' THEN
|
|
4849
|
+
RAISE EXCEPTION 'invalid start';
|
|
4850
|
+
END IF;
|
|
4851
|
+
|
|
4852
|
+
SELECT s.id, s.name, s.price, COALESCE(s.duration_minutes, 30) AS duration_minutes
|
|
4853
|
+
INTO v_service
|
|
4854
|
+
FROM services s
|
|
4855
|
+
WHERE s.id = p_service_id AND s.tenant_id = p_tenant_id
|
|
4856
|
+
AND s.is_active AND s.status = 'active';
|
|
4857
|
+
IF NOT FOUND THEN RAISE EXCEPTION 'unknown service'; END IF;
|
|
4858
|
+
|
|
4859
|
+
v_ends := p_starts_at + make_interval(mins => v_service.duration_minutes);
|
|
4860
|
+
SELECT COALESCE(t.settings->>'timezone', 'America/Sao_Paulo') INTO v_tz
|
|
4861
|
+
FROM tenants t WHERE t.id = p_tenant_id;
|
|
4862
|
+
IF v_tz IS NULL THEN RAISE EXCEPTION 'unknown tenant'; END IF;
|
|
4863
|
+
|
|
4864
|
+
-- pick the assignee whose working hours cover the slot
|
|
4865
|
+
SELECT sc.assignee_id INTO v_assignee
|
|
4866
|
+
FROM schedules sc
|
|
4867
|
+
WHERE sc.tenant_id = p_tenant_id
|
|
4868
|
+
AND sc.kind = 'working_hours'
|
|
4869
|
+
AND sc.is_active
|
|
4870
|
+
AND (
|
|
4871
|
+
sc.specific_date = (p_starts_at AT TIME ZONE v_tz)::date
|
|
4872
|
+
OR (sc.specific_date IS NULL
|
|
4873
|
+
AND sc.day_of_week = EXTRACT(dow FROM p_starts_at AT TIME ZONE v_tz)::smallint)
|
|
4874
|
+
)
|
|
4875
|
+
AND (p_starts_at AT TIME ZONE v_tz)::time >= sc.starts_at
|
|
4876
|
+
AND (v_ends AT TIME ZONE v_tz)::time <= sc.ends_at
|
|
4877
|
+
ORDER BY sc.assignee_id
|
|
4878
|
+
LIMIT 1;
|
|
4879
|
+
IF v_assignee IS NULL THEN RAISE EXCEPTION 'slot outside working hours'; END IF;
|
|
4880
|
+
|
|
4881
|
+
-- serialize per (tenant, assignee) to kill the double-book race
|
|
4882
|
+
PERFORM pg_advisory_xact_lock(hashtextextended(p_tenant_id::text || v_assignee::text, 0));
|
|
4883
|
+
|
|
4884
|
+
IF EXISTS (
|
|
4885
|
+
SELECT 1 FROM bookings b
|
|
4886
|
+
WHERE b.tenant_id = p_tenant_id
|
|
4887
|
+
AND b.assignee_id = v_assignee
|
|
4888
|
+
AND b.status NOT IN ('cancelled', 'no_show')
|
|
4889
|
+
AND b.starts_at < v_ends
|
|
4890
|
+
AND COALESCE(b.ends_at, b.starts_at) > p_starts_at
|
|
4891
|
+
) THEN
|
|
4892
|
+
RAISE EXCEPTION 'slot no longer available';
|
|
4893
|
+
END IF;
|
|
4894
|
+
|
|
4895
|
+
-- anti-spam: cap open future public bookings per phone per tenant
|
|
4896
|
+
IF (
|
|
4897
|
+
SELECT count(*) FROM bookings b
|
|
4898
|
+
JOIN persons p ON p.id = b.party_id
|
|
4899
|
+
WHERE b.tenant_id = p_tenant_id
|
|
4900
|
+
AND p.phone = v_phone
|
|
4901
|
+
AND b.starts_at > now()
|
|
4902
|
+
AND b.status = 'scheduled'
|
|
4903
|
+
) >= 3 THEN
|
|
4904
|
+
RAISE EXCEPTION 'too many open bookings for this phone';
|
|
4905
|
+
END IF;
|
|
4906
|
+
|
|
4907
|
+
-- upsert the customer by (tenant, phone) \u2014 matches persons_tenant_customer_phone
|
|
4908
|
+
INSERT INTO persons (tenant_id, kind, name, phone, email, metadata)
|
|
4909
|
+
VALUES (p_tenant_id, 'customer', trim(p_name), v_phone, p_email,
|
|
4910
|
+
jsonb_build_object('source', 'public_booking'))
|
|
4911
|
+
ON CONFLICT (tenant_id, phone) WHERE kind = 'customer' AND phone IS NOT NULL
|
|
4912
|
+
DO UPDATE SET
|
|
4913
|
+
name = EXCLUDED.name,
|
|
4914
|
+
email = COALESCE(EXCLUDED.email, persons.email),
|
|
4915
|
+
updated_at = now()
|
|
4916
|
+
RETURNING id INTO v_person_id;
|
|
4917
|
+
|
|
4918
|
+
-- order + booking + order_item (statuses hardcoded \u2014 caller cannot set)
|
|
4919
|
+
INSERT INTO orders (tenant_id, kind, status, party_id, assignee_id, subtotal, total, notes, metadata)
|
|
4920
|
+
VALUES (p_tenant_id, 'appointment', 'scheduled', v_person_id, v_assignee,
|
|
4921
|
+
v_service.price, v_service.price, v_notes,
|
|
4922
|
+
jsonb_build_object('source', 'public_booking',
|
|
4923
|
+
'serviceNames', v_service.name,
|
|
4924
|
+
'contactName', trim(p_name)))
|
|
4925
|
+
RETURNING id INTO v_order;
|
|
4926
|
+
|
|
4927
|
+
INSERT INTO bookings (tenant_id, kind, party_id, assignee_id, order_id,
|
|
4928
|
+
starts_at, ends_at, status, notes, metadata)
|
|
4929
|
+
VALUES (p_tenant_id, 'appointment', v_person_id, v_assignee, v_order,
|
|
4930
|
+
p_starts_at, v_ends, 'scheduled', v_notes,
|
|
4931
|
+
jsonb_build_object('source', 'public_booking', 'serviceNames', v_service.name))
|
|
4932
|
+
RETURNING id INTO v_booking;
|
|
4933
|
+
|
|
4934
|
+
INSERT INTO order_items (order_id, service_id, name, quantity, unit_price, total,
|
|
4935
|
+
sort_order, duration_minutes, assignee_id)
|
|
4936
|
+
VALUES (v_order, v_service.id, v_service.name, 1, v_service.price, v_service.price,
|
|
4937
|
+
0, v_service.duration_minutes, v_assignee);
|
|
4938
|
+
|
|
4939
|
+
RETURN QUERY SELECT v_booking, p_starts_at, v_ends;
|
|
4940
|
+
END;
|
|
4941
|
+
$$;
|
|
4942
|
+
|
|
4943
|
+
REVOKE ALL ON FUNCTION public.create_public_booking(uuid, uuid, timestamptz, text, text, text, text) FROM public;
|
|
4944
|
+
GRANT EXECUTE ON FUNCTION public.create_public_booking(uuid, uuid, timestamptz, text, text, text, text)
|
|
4945
|
+
TO anon, authenticated, service_role;
|
|
4946
|
+
`;
|
|
4224
4947
|
function computeCommissionAmount(total, ratePercent) {
|
|
4225
4948
|
const base = Number(total) || 0;
|
|
4226
4949
|
const rate = Number(ratePercent) || 0;
|
|
@@ -4635,8 +5358,8 @@ function createAgendaPlugin(options) {
|
|
|
4635
5358
|
const config = resolveConfig(options);
|
|
4636
5359
|
core.registerTranslations(agendaLocales);
|
|
4637
5360
|
const provider2 = options?.dataProvider ?? core.createSafeDataProvider(
|
|
4638
|
-
() =>
|
|
4639
|
-
() =>
|
|
5361
|
+
() => createSupabaseAgendaProvider(),
|
|
5362
|
+
() => chunkLFUELUIT_cjs.createMockAgendaProvider()
|
|
4640
5363
|
);
|
|
4641
5364
|
const store = createAgendaStore(provider2, options?.financialBridge);
|
|
4642
5365
|
const registries = [...agendaRegistries, ...options?.settingsRegistries ?? []];
|
|
@@ -4780,13 +5503,21 @@ function createAgendaPlugin(options) {
|
|
|
4780
5503
|
permission: { feature: "appointments", action: "read" }
|
|
4781
5504
|
}
|
|
4782
5505
|
],
|
|
5506
|
+
migrations: [
|
|
5507
|
+
{
|
|
5508
|
+
id: "agenda-001-public-booking",
|
|
5509
|
+
version: "0.3.0",
|
|
5510
|
+
sql: MIGRATION_001_PUBLIC_BOOKING,
|
|
5511
|
+
description: "Booking read model (v_bookings) + anon public booking surface (v_public_services, get_available_slots, create_public_booking) + order_items duration/assignee columns"
|
|
5512
|
+
}
|
|
5513
|
+
],
|
|
4783
5514
|
locales: agendaLocales
|
|
4784
5515
|
};
|
|
4785
5516
|
}
|
|
4786
5517
|
|
|
4787
5518
|
Object.defineProperty(exports, "createMockAgendaProvider", {
|
|
4788
5519
|
enumerable: true,
|
|
4789
|
-
get: function () { return
|
|
5520
|
+
get: function () { return chunkLFUELUIT_cjs.createMockAgendaProvider; }
|
|
4790
5521
|
});
|
|
4791
5522
|
exports.bookingToEvent = bookingToEvent2;
|
|
4792
5523
|
exports.computeCommissionAmount = computeCommissionAmount;
|