@fayz-ai/plugin-crm 0.8.0 → 0.8.1

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.
Files changed (57) hide show
  1. package/dist/components/CrmGeneralSettings.d.ts.map +1 -1
  2. package/dist/data/supabase.d.ts.map +1 -1
  3. package/dist/index.cjs +168 -33
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +169 -34
  7. package/dist/index.js.map +1 -1
  8. package/dist/lib/form-answers.d.ts +10 -0
  9. package/dist/lib/form-answers.d.ts.map +1 -0
  10. package/dist/locales/en.d.ts.map +1 -1
  11. package/dist/locales/pt-BR.d.ts.map +1 -1
  12. package/dist/migrations/index.d.ts +2 -0
  13. package/dist/migrations/index.d.ts.map +1 -1
  14. package/dist/public/context.d.ts +23 -0
  15. package/dist/public/context.d.ts.map +1 -0
  16. package/dist/public/createPublicFormsPlugin.d.ts +38 -0
  17. package/dist/public/createPublicFormsPlugin.d.ts.map +1 -0
  18. package/dist/public/data.supabase.d.ts +7 -0
  19. package/dist/public/data.supabase.d.ts.map +1 -0
  20. package/dist/public/inbox.d.ts +22 -0
  21. package/dist/public/inbox.d.ts.map +1 -0
  22. package/dist/public/index.cjs +306 -0
  23. package/dist/public/index.cjs.map +1 -0
  24. package/dist/public/index.d.ts +10 -0
  25. package/dist/public/index.d.ts.map +1 -0
  26. package/dist/public/index.js +298 -0
  27. package/dist/public/index.js.map +1 -0
  28. package/dist/public/types.d.ts +92 -0
  29. package/dist/public/types.d.ts.map +1 -0
  30. package/dist/views/DealSidebar.d.ts.map +1 -1
  31. package/dist/views/LeadDetailView.d.ts.map +1 -1
  32. package/dist/views/LeadFormView.d.ts.map +1 -1
  33. package/dist/views/LeadListView.d.ts.map +1 -1
  34. package/dist/views/PipelineView.d.ts.map +1 -1
  35. package/dist/views/QuoteFormView.d.ts.map +1 -1
  36. package/package.json +11 -5
  37. package/src/components/CrmGeneralSettings.tsx +35 -15
  38. package/src/data/supabase.ts +47 -4
  39. package/src/index.ts +9 -0
  40. package/src/lib/form-answers.tsx +39 -0
  41. package/src/locales/en.ts +1 -0
  42. package/src/locales/pt-BR.ts +1 -0
  43. package/src/migrations/005_public_lead.sql +189 -0
  44. package/src/migrations/006_lead_enters_pipeline.sql +265 -0
  45. package/src/migrations/index.ts +461 -1
  46. package/src/public/context.tsx +69 -0
  47. package/src/public/createPublicFormsPlugin.ts +188 -0
  48. package/src/public/data.supabase.ts +86 -0
  49. package/src/public/inbox.ts +136 -0
  50. package/src/public/index.ts +30 -0
  51. package/src/public/types.ts +113 -0
  52. package/src/views/DealSidebar.tsx +31 -0
  53. package/src/views/LeadDetailView.tsx +53 -0
  54. package/src/views/LeadFormView.tsx +10 -0
  55. package/src/views/LeadListView.tsx +19 -0
  56. package/src/views/PipelineView.tsx +34 -1
  57. package/src/views/QuoteFormView.tsx +5 -0
@@ -1 +1 @@
1
- {"version":3,"file":"CrmGeneralSettings.d.ts","sourceRoot":"","sources":["../../src/components/CrmGeneralSettings.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAIzB,wBAAgB,kBAAkB,sBA+BjC"}
1
+ {"version":3,"file":"CrmGeneralSettings.d.ts","sourceRoot":"","sources":["../../src/components/CrmGeneralSettings.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAuBzB,wBAAgB,kBAAkB,sBAgCjC"}
@@ -1 +1 @@
1
- {"version":3,"file":"supabase.d.ts","sourceRoot":"","sources":["../../src/data/supabase.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAmC9C,wBAAgB,yBAAyB,CAAC,OAAO,CAAC,EAAE;IAClD,gBAAgB,CAAC,EAAE;QAAE,aAAa,EAAE,MAAM,CAAC;QAAC,cAAc,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAA;CACvF,GAAG,eAAe,CAulBlB"}
1
+ {"version":3,"file":"supabase.d.ts","sourceRoot":"","sources":["../../src/data/supabase.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAwD9C,wBAAgB,yBAAyB,CAAC,OAAO,CAAC,EAAE;IAClD,gBAAgB,CAAC,EAAE;QAAE,aAAa,EAAE,MAAM,CAAC;QAAC,cAAc,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAA;CACvF,GAAG,eAAe,CA6mBlB"}
package/dist/index.cjs CHANGED
@@ -19,44 +19,47 @@ var CrmContextProvider = ctx.ContextProvider;
19
19
  var useCrmConfig = ctx.useConfig;
20
20
  var useCrmProvider = ctx.useProvider;
21
21
  var useCrmStore = ctx.useStore;
22
+ var CRM_DEFAULTS = {
23
+ autoAssign: false,
24
+ requireSource: true,
25
+ leadScoring: false,
26
+ duplicateDetection: true,
27
+ autoCreateDeal: true,
28
+ requireCloseDate: false,
29
+ rottingDeals: true,
30
+ weightedPipeline: true,
31
+ autoNumberQuotes: true,
32
+ defaultValidity: true,
33
+ requireApproval: false,
34
+ overdueAlerts: true,
35
+ stageChangeAlerts: false,
36
+ newLeadNotif: true
37
+ };
22
38
  function CrmGeneralSettings() {
23
39
  const t2 = core.useTranslation();
40
+ const s = saas.useTenantPluginSettings("crm", CRM_DEFAULTS);
24
41
  return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-4", children: [
25
42
  /* @__PURE__ */ jsxRuntime.jsxs(saas.SettingsGroup, { title: t2("crm.settings.leadManagement"), description: t2("crm.settings.leadManagementDesc"), children: [
26
- /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.autoAssign"), description: t2("crm.settings.autoAssignDesc"), checked: false, onChange: () => {
27
- } }),
28
- /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.requireSource"), description: t2("crm.settings.requireSourceDesc"), checked: true, onChange: () => {
29
- } }),
30
- /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.leadScoring"), description: t2("crm.settings.leadScoringDesc"), checked: false, onChange: () => {
31
- } }),
32
- /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.duplicateDetection"), description: t2("crm.settings.duplicateDetectionDesc"), checked: true, onChange: () => {
33
- } })
43
+ /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.autoAssign"), description: t2("crm.settings.autoAssignDesc"), checked: s.get("autoAssign"), onChange: (v) => s.set("autoAssign", v) }),
44
+ /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.requireSource"), description: t2("crm.settings.requireSourceDesc"), checked: s.get("requireSource"), onChange: (v) => s.set("requireSource", v) }),
45
+ /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.leadScoring"), description: t2("crm.settings.leadScoringDesc"), checked: s.get("leadScoring"), onChange: (v) => s.set("leadScoring", v) }),
46
+ /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.duplicateDetection"), description: t2("crm.settings.duplicateDetectionDesc"), checked: s.get("duplicateDetection"), onChange: (v) => s.set("duplicateDetection", v) })
34
47
  ] }),
35
48
  /* @__PURE__ */ jsxRuntime.jsxs(saas.SettingsGroup, { title: t2("crm.settings.deals"), description: t2("crm.settings.dealsDesc"), children: [
36
- /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.autoCreateDeal"), description: t2("crm.settings.autoCreateDealDesc"), checked: true, onChange: () => {
37
- } }),
38
- /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.requireCloseDate"), description: t2("crm.settings.requireCloseDateDesc"), checked: false, onChange: () => {
39
- } }),
40
- /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.rottingDeals"), description: t2("crm.settings.rottingDealsDesc"), checked: true, onChange: () => {
41
- } }),
42
- /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.weightedPipeline"), description: t2("crm.settings.weightedPipelineDesc"), checked: true, onChange: () => {
43
- } })
49
+ /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.autoCreateDeal"), description: t2("crm.settings.autoCreateDealDesc"), checked: s.get("autoCreateDeal"), onChange: (v) => s.set("autoCreateDeal", v) }),
50
+ /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.requireCloseDate"), description: t2("crm.settings.requireCloseDateDesc"), checked: s.get("requireCloseDate"), onChange: (v) => s.set("requireCloseDate", v) }),
51
+ /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.rottingDeals"), description: t2("crm.settings.rottingDealsDesc"), checked: s.get("rottingDeals"), onChange: (v) => s.set("rottingDeals", v) }),
52
+ /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.weightedPipeline"), description: t2("crm.settings.weightedPipelineDesc"), checked: s.get("weightedPipeline"), onChange: (v) => s.set("weightedPipeline", v) })
44
53
  ] }),
45
54
  /* @__PURE__ */ jsxRuntime.jsxs(saas.SettingsGroup, { title: t2("crm.settings.quotes"), description: t2("crm.settings.quotesDesc"), children: [
46
- /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.autoNumberQuotes"), description: t2("crm.settings.autoNumberQuotesDesc"), checked: true, onChange: () => {
47
- } }),
48
- /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.defaultValidity"), description: t2("crm.settings.defaultValidityDesc"), checked: true, onChange: () => {
49
- } }),
50
- /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.requireApproval"), description: t2("crm.settings.requireApprovalDesc"), checked: false, onChange: () => {
51
- } })
55
+ /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.autoNumberQuotes"), description: t2("crm.settings.autoNumberQuotesDesc"), checked: s.get("autoNumberQuotes"), onChange: (v) => s.set("autoNumberQuotes", v) }),
56
+ /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.defaultValidity"), description: t2("crm.settings.defaultValidityDesc"), checked: s.get("defaultValidity"), onChange: (v) => s.set("defaultValidity", v) }),
57
+ /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.requireApproval"), description: t2("crm.settings.requireApprovalDesc"), checked: s.get("requireApproval"), onChange: (v) => s.set("requireApproval", v) })
52
58
  ] }),
53
59
  /* @__PURE__ */ jsxRuntime.jsxs(saas.SettingsGroup, { title: t2("crm.settings.notifications"), description: t2("crm.settings.notificationsDesc"), children: [
54
- /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.overdueAlerts"), description: t2("crm.settings.overdueAlertsDesc"), checked: true, onChange: () => {
55
- } }),
56
- /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.stageChangeAlerts"), description: t2("crm.settings.stageChangeAlertsDesc"), checked: false, onChange: () => {
57
- } }),
58
- /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.newLeadNotif"), description: t2("crm.settings.newLeadNotifDesc"), checked: true, onChange: () => {
59
- } })
60
+ /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.overdueAlerts"), description: t2("crm.settings.overdueAlertsDesc"), checked: s.get("overdueAlerts"), onChange: (v) => s.set("overdueAlerts", v) }),
61
+ /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.stageChangeAlerts"), description: t2("crm.settings.stageChangeAlertsDesc"), checked: s.get("stageChangeAlerts"), onChange: (v) => s.set("stageChangeAlerts", v) }),
62
+ /* @__PURE__ */ jsxRuntime.jsx(saas.ToggleRow, { label: t2("crm.settings.newLeadNotif"), description: t2("crm.settings.newLeadNotifDesc"), checked: s.get("newLeadNotif"), onChange: (v) => s.set("newLeadNotif", v) })
60
63
  ] })
61
64
  ] });
62
65
  }
@@ -266,6 +269,23 @@ function QuoteStatusDropdown({ quote, onStatusChange, size = "default" }) {
266
269
  showReject && /* @__PURE__ */ jsxRuntime.jsx(RejectModal, { onConfirm: handleReject, onCancel: () => setShowReject(false) })
267
270
  ] });
268
271
  }
272
+
273
+ // src/lib/form-answers.tsx
274
+ function humanizeKey(key) {
275
+ const spaced = key.replace(/[_-]+/g, " ").replace(/([a-z\d])([A-Z])/g, "$1 $2");
276
+ return spaced.charAt(0).toUpperCase() + spaced.slice(1);
277
+ }
278
+ function renderFieldValue(value) {
279
+ if (value === null || value === void 0 || value === "") return null;
280
+ if (Array.isArray(value)) return value.length ? value.join(", ") : null;
281
+ if (typeof value === "boolean") return value ? "Sim" : "N\xE3o";
282
+ if (typeof value === "object") return JSON.stringify(value);
283
+ return String(value);
284
+ }
285
+ function answerEntries(fields) {
286
+ if (!fields || typeof fields !== "object" || Array.isArray(fields)) return [];
287
+ return Object.entries(fields).filter(([, v]) => v !== null && v !== void 0 && v !== "");
288
+ }
269
289
  var TYPE_ICONS = {
270
290
  call: lucideReact.Phone,
271
291
  email: lucideReact.Mail,
@@ -294,6 +314,19 @@ function TabBar({ active, counts, onChange }) {
294
314
  tab.id
295
315
  )) });
296
316
  }
317
+ function FormAnswers({ lead }) {
318
+ const t2 = core.useTranslation();
319
+ const entries = answerEntries(lead?.metadata?.fields);
320
+ if (entries.length === 0) return null;
321
+ const formId = lead?.metadata?.formId;
322
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
323
+ /* @__PURE__ */ jsxRuntime.jsx("h4", { className: "text-[11px] font-semibold text-muted-foreground uppercase tracking-wider", children: formId ? `${t2("crm.leadDetail.formAnswers")} \xB7 ${formId}` : t2("crm.leadDetail.formAnswers") }),
324
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "rounded-xl border divide-y", children: entries.map(([key, value]) => /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-start gap-3 px-4 py-2.5", children: [
325
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-[11px] text-muted-foreground shrink-0 min-w-24", children: humanizeKey(key) }),
326
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-xs text-right flex-1 break-words", children: renderFieldValue(value) ?? /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-muted-foreground/50", children: "\u2014" }) })
327
+ ] }, key)) })
328
+ ] });
329
+ }
297
330
  function OverviewTab({ deal, lead, currency }) {
298
331
  const t2 = core.useTranslation();
299
332
  const initial = (lead?.name ?? deal.contactName ?? deal.title).charAt(0).toUpperCase();
@@ -359,6 +392,7 @@ function OverviewTab({ deal, lead, currency }) {
359
392
  ] })
360
393
  ] })
361
394
  ] }),
395
+ /* @__PURE__ */ jsxRuntime.jsx(FormAnswers, { lead }),
362
396
  deal.tags && deal.tags.length > 0 && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
363
397
  /* @__PURE__ */ jsxRuntime.jsx("h4", { className: "text-[11px] font-semibold text-muted-foreground uppercase tracking-wider", children: t2("crm.dealSidebar.tags") }),
364
398
  /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex gap-1.5 flex-wrap", children: deal.tags.map((tag) => /* @__PURE__ */ jsxRuntime.jsx("span", { className: "inline-flex items-center rounded-full bg-muted px-2.5 py-0.5 text-[10px] font-medium text-muted-foreground", children: tag }, tag)) })
@@ -668,6 +702,22 @@ function PipelineView({ onDealClick, onViewLead, onViewQuote, onAddLead }) {
668
702
  React14.useEffect(() => {
669
703
  if (pipelines.length > 0) fetchDealsByStage(pipelines[0].id);
670
704
  }, [pipelines]);
705
+ const pipelineId = pipelines[0]?.id;
706
+ React14.useEffect(() => {
707
+ if (!pipelineId) return;
708
+ const supabase = core.getSupabaseClientOptional();
709
+ if (!supabase) return;
710
+ const channel = supabase.channel(`crm:board:${pipelineId}`).on(
711
+ "postgres_changes",
712
+ { event: "*", schema: "public", table: "plg_crm_deal_extensions" },
713
+ () => {
714
+ void fetchDealsByStage(pipelineId);
715
+ }
716
+ ).subscribe();
717
+ return () => {
718
+ supabase.removeChannel(channel);
719
+ };
720
+ }, [pipelineId]);
671
721
  React14.useEffect(() => {
672
722
  if (savingDealIds.size === 0) setOptimisticBoard(null);
673
723
  }, [dealsByStage, savingDealIds]);
@@ -792,6 +842,24 @@ var columns = [
792
842
  const opt = STATUS_OPTIONS.find((o) => o.value === status);
793
843
  return /* @__PURE__ */ jsxRuntime.jsx("span", { className: `inline-flex items-center rounded-full px-2 py-0.5 text-[10px] font-medium ${opt?.color ?? "bg-muted text-muted-foreground"}`, children: opt?.label ?? status });
794
844
  }
845
+ },
846
+ {
847
+ // When the lead came in. The list is already sorted by it (provider orders
848
+ // created_at desc) but never showed it, so "is this enquiry from today or
849
+ // from March?" was unanswerable without opening the record.
850
+ accessorKey: "createdAt",
851
+ header: "Entrada",
852
+ cell: ({ getValue }) => {
853
+ const raw = getValue();
854
+ if (!raw) return /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-muted-foreground text-xs", children: "\u2014" });
855
+ const at = new Date(raw);
856
+ if (Number.isNaN(at.getTime())) return /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-muted-foreground text-xs", children: "\u2014" });
857
+ return /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "text-muted-foreground text-xs whitespace-nowrap", title: at.toLocaleString(), children: [
858
+ at.toLocaleDateString(void 0, { day: "2-digit", month: "2-digit", year: "2-digit" }),
859
+ " \xB7 ",
860
+ at.toLocaleTimeString(void 0, { hour: "2-digit", minute: "2-digit" })
861
+ ] });
862
+ }
795
863
  }
796
864
  ];
797
865
  function LeadListView({ onNew, onEdit }) {
@@ -834,6 +902,7 @@ function LeadListView({ onNew, onEdit }) {
834
902
  function LeadFormView({ onSaved }) {
835
903
  const t2 = core.useTranslation();
836
904
  const createLead = useCrmStore((s) => s.createLead);
905
+ const guardLeads = saas.useLimitGuard("leads");
837
906
  const fetchPipelines = useCrmStore((s) => s.fetchPipelines);
838
907
  const pipelines = useCrmStore((s) => s.pipelines);
839
908
  React14.useEffect(() => {
@@ -850,9 +919,12 @@ function LeadFormView({ onSaved }) {
850
919
  ui.toast.error(t2("common.formIncomplete"));
851
920
  return;
852
921
  }
922
+ if (await guardLeads() === "blocked") return;
853
923
  setSaving(true);
854
924
  try {
855
925
  const lead = await createLead({ name, email: email || void 0, phone: phone || void 0, company: company || void 0, notes: notes || void 0 });
926
+ saas.invalidateLimit("leads");
927
+ saas.invalidateLimit("deals");
856
928
  onSaved?.(lead.id);
857
929
  } finally {
858
930
  setSaving(false);
@@ -1102,6 +1174,7 @@ function QuoteFormView({ quoteId, leadId, onSaved }) {
1102
1174
  const { currency, itemTypes } = config;
1103
1175
  const createQuote = useCrmStore((s) => s.createQuote);
1104
1176
  const updateQuote = useCrmStore((s) => s.updateQuote);
1177
+ const guardQuotes = saas.useLimitGuard("quotes");
1105
1178
  const isEdit = !!quoteId;
1106
1179
  const [loadingExisting, setLoadingExisting] = React14.useState(isEdit || !!leadId);
1107
1180
  const [linkedLeadId, setLinkedLeadId] = React14.useState(leadId ?? "");
@@ -1180,6 +1253,7 @@ function QuoteFormView({ quoteId, leadId, onSaved }) {
1180
1253
  ui.toast.error(t2("crm.quoteForm.errNoItems"));
1181
1254
  return;
1182
1255
  }
1256
+ if (!isEdit && await guardQuotes() === "blocked") return;
1183
1257
  setSaving(true);
1184
1258
  try {
1185
1259
  const input = {
@@ -1205,6 +1279,7 @@ function QuoteFormView({ quoteId, leadId, onSaved }) {
1205
1279
  onSaved?.(quoteId);
1206
1280
  } else {
1207
1281
  const created = await createQuote(input);
1282
+ saas.invalidateLimit("quotes");
1208
1283
  onSaved?.(created?.id);
1209
1284
  }
1210
1285
  } finally {
@@ -1723,6 +1798,17 @@ var STATUS_COLORS2 = {
1723
1798
  converted: "bg-warning-soft text-warning-soft-foreground",
1724
1799
  lost: "bg-destructive-soft text-destructive-soft-foreground"
1725
1800
  };
1801
+ function humanizeKey2(key) {
1802
+ const spaced = key.replace(/[_-]+/g, " ").replace(/([a-z\d])([A-Z])/g, "$1 $2");
1803
+ return spaced.charAt(0).toUpperCase() + spaced.slice(1);
1804
+ }
1805
+ function renderFieldValue2(value) {
1806
+ if (value === null || value === void 0 || value === "") return null;
1807
+ if (Array.isArray(value)) return value.length ? value.join(", ") : null;
1808
+ if (typeof value === "boolean") return value ? "Sim" : "N\xE3o";
1809
+ if (typeof value === "object") return JSON.stringify(value);
1810
+ return String(value);
1811
+ }
1726
1812
  function FieldRow({ label, value }) {
1727
1813
  return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-3 gap-4 py-3", children: [
1728
1814
  /* @__PURE__ */ jsxRuntime.jsx("dt", { className: "text-sm font-medium text-muted-foreground", children: label }),
@@ -1819,6 +1905,24 @@ function LeadDetailView({ leadId, onBack, onCreateQuote, onViewQuote }) {
1819
1905
  /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-5 md:border-b md:last:border-b-0", children: /* @__PURE__ */ jsxRuntime.jsx(FieldRow, { label: t2("crm.leadDetail.source"), value: lead.sourceName }) })
1820
1906
  ] }) })
1821
1907
  ] }),
1908
+ (() => {
1909
+ const meta = lead.metadata ?? {};
1910
+ const fields = Object.entries(meta.fields ?? {}).filter(([, v]) => renderFieldValue2(v) !== null);
1911
+ const utm = Object.entries(meta.utm ?? {}).filter(([, v]) => renderFieldValue2(v) !== null);
1912
+ if (!fields.length && !utm.length) return null;
1913
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
1914
+ /* @__PURE__ */ jsxRuntime.jsx("h3", { className: "text-sm font-semibold text-foreground mb-1", children: t2("crm.leadDetail.formAnswers") }),
1915
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "rounded-xl border bg-card shadow-sm", children: [
1916
+ /* @__PURE__ */ jsxRuntime.jsx("dl", { className: "grid divide-y md:grid-cols-2 md:divide-y-0", children: fields.map(([key, value]) => /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-5 md:border-b md:last:border-b-0", children: /* @__PURE__ */ jsxRuntime.jsx(FieldRow, { label: humanizeKey2(key), value: renderFieldValue2(value) }) }, key)) }),
1917
+ utm.length > 0 && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "border-t px-5 py-3 flex flex-wrap gap-x-4 gap-y-1", children: utm.map(([key, value]) => /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "text-xs text-muted-foreground", children: [
1918
+ humanizeKey2(key.replace(/^utm_/, "")),
1919
+ ":",
1920
+ " ",
1921
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-foreground", children: renderFieldValue2(value) })
1922
+ ] }, key)) })
1923
+ ] })
1924
+ ] });
1925
+ })(),
1822
1926
  deal && /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
1823
1927
  /* @__PURE__ */ jsxRuntime.jsx("h3", { className: "text-sm font-semibold text-foreground mb-1", children: t2("crm.leadDetail.pipeline") }),
1824
1928
  /* @__PURE__ */ jsxRuntime.jsx("div", { className: "rounded-xl border bg-card shadow-sm", children: /* @__PURE__ */ jsxRuntime.jsxs("dl", { className: "grid divide-y md:grid-cols-2 md:divide-y-0", children: [
@@ -2808,6 +2912,12 @@ function getClients() {
2808
2912
  if (!supabase) throw new Error("Supabase not initialized");
2809
2913
  return { core: supabase, pub: supabase };
2810
2914
  }
2915
+ async function mergePersonMetadata(personId, patch) {
2916
+ const { core } = getClients();
2917
+ const { data: row } = await core.from("people").select("metadata").eq("id", personId).maybeSingle();
2918
+ const current = row?.metadata ?? {};
2919
+ await core.from("people").update({ metadata: { ...current, ...patch } }).eq("id", personId);
2920
+ }
2811
2921
  function createSupabaseCrmProvider(options) {
2812
2922
  const provider = {
2813
2923
  // --- Pipelines (public.plg_crm_pipelines + plg_crm_pipeline_stages) ---
@@ -2866,8 +2976,16 @@ function createSupabaseCrmProvider(options) {
2866
2976
  phone: r.phone,
2867
2977
  company: r.company,
2868
2978
  status: r.lead_status ?? "new",
2979
+ sourceName: r.source_name,
2869
2980
  tags: r.tags ?? [],
2870
2981
  notes: r.notes,
2982
+ // Answers from a public form. Kept as an opaque bag so a new landing
2983
+ // page needs no schema change; the detail view renders whatever is here.
2984
+ metadata: {
2985
+ formId: r.form_id ?? void 0,
2986
+ fields: r.custom_fields ?? {},
2987
+ utm: r.utm ?? {}
2988
+ },
2871
2989
  tenantId: r.tenant_id,
2872
2990
  createdAt: r.created_at,
2873
2991
  updatedAt: r.updated_at
@@ -2898,14 +3016,14 @@ function createSupabaseCrmProvider(options) {
2898
3016
  if (partial.email !== void 0) row.email = partial.email;
2899
3017
  if (partial.phone !== void 0) row.phone = partial.phone;
2900
3018
  if (partial.notes !== void 0) row.notes = partial.notes;
2901
- if (partial.status) row.metadata = { status: partial.status };
2902
- const { data, error } = await core.from("people").update(row).eq("id", id).select().single();
3019
+ const { error } = await core.from("people").update(row).eq("id", id).select().single();
2903
3020
  if (error) throw new Error(error.message);
3021
+ if (partial.status) await mergePersonMetadata(id, { status: partial.status });
2904
3022
  return await provider.getLeadById(id);
2905
3023
  },
2906
3024
  async convertLeadToDeal(leadId, dealInput) {
2907
- const { core } = getClients();
2908
- await core.from("people").update({ metadata: { status: "converted" } }).eq("id", leadId);
3025
+ getClients();
3026
+ await mergePersonMetadata(leadId, { status: "converted" });
2909
3027
  return provider.createDeal({ ...dealInput, leadId });
2910
3028
  },
2911
3029
  // --- Deals (via view — single query with all joins) ---
@@ -2963,6 +3081,11 @@ function createSupabaseCrmProvider(options) {
2963
3081
  status: r.status ?? "open",
2964
3082
  contactName: r.contact_name,
2965
3083
  tags: r.tags ?? [],
3084
+ // v_deals exposes de.lead_id and Deal declares leadId, but the mapping
3085
+ // dropped it — so DealSidebar's `if (d?.leadId)` never fired and the
3086
+ // card opened blank: no contact, no form answers. For a card that came
3087
+ // from a form, the lead IS the content.
3088
+ leadId: r.lead_id ?? void 0,
2966
3089
  tenantId: r.tenant_id,
2967
3090
  createdAt: r.created_at,
2968
3091
  updatedAt: r.updated_at
@@ -2984,6 +3107,7 @@ function createSupabaseCrmProvider(options) {
2984
3107
  status: r.status ?? "open",
2985
3108
  contactName: r.contact_name,
2986
3109
  tags: r.tags ?? [],
3110
+ leadId: r.lead_id ?? void 0,
2987
3111
  tenantId: r.tenant_id,
2988
3112
  createdAt: r.created_at,
2989
3113
  updatedAt: r.updated_at
@@ -3079,7 +3203,7 @@ function createSupabaseCrmProvider(options) {
3079
3203
  const { data: ext } = await pub.from(T.dealExtensions).select("lead_id").eq("order_id", dealId).single();
3080
3204
  if (ext?.lead_id) {
3081
3205
  const newLeadStatus = deriveLeadStatus(typedStage);
3082
- await core.from("people").update({ metadata: { status: newLeadStatus } }).eq("id", ext.lead_id);
3206
+ await mergePersonMetadata(ext.lead_id, { status: newLeadStatus });
3083
3207
  }
3084
3208
  const { data: quotes } = await core.from("orders").select("id, status").eq("kind", "quote");
3085
3209
  for (const q of quotes ?? []) {
@@ -3930,6 +4054,7 @@ var en = {
3930
4054
  "crm.dealSidebar.viewLeadProfile": "View Full Lead Profile",
3931
4055
  "crm.leadDetail.assignedTo": "Assigned to",
3932
4056
  "crm.leadDetail.backToList": "Back to list",
4057
+ "crm.leadDetail.formAnswers": "Form answers",
3933
4058
  "crm.leadDetail.company": "Company",
3934
4059
  "crm.leadDetail.contactInfo": "Contact Information",
3935
4060
  "crm.leadDetail.createQuote": "Create Quote",
@@ -4223,6 +4348,7 @@ var ptBR = {
4223
4348
  "crm.dealSidebar.viewLeadProfile": "Ver Perfil Completo do Lead",
4224
4349
  "crm.leadDetail.assignedTo": "Atribu\xEDdo a",
4225
4350
  "crm.leadDetail.backToList": "Voltar \xE0 lista",
4351
+ "crm.leadDetail.formAnswers": "Dados do formul\xE1rio",
4226
4352
  "crm.leadDetail.company": "Empresa",
4227
4353
  "crm.leadDetail.contactInfo": "Informa\xE7\xF5es de Contato",
4228
4354
  "crm.leadDetail.createQuote": "Criar Or\xE7amento",
@@ -4526,6 +4652,15 @@ function createCrmPlugin(options) {
4526
4652
  { id: "crm.quotes", label: config.labels.quotes, group: config.labels.pageTitle },
4527
4653
  { id: "crm.pipeline", label: config.labels.pipeline, group: config.labels.pageTitle }
4528
4654
  ],
4655
+ // Stock quantity caps. leads/deals/quotes share core tables partitioned by
4656
+ // `kind` (people/orders), so each declaration carries a kindFilter. A lead
4657
+ // create auto-spawns a deal, so the lead guard also gates deal creation;
4658
+ // `deals` has no standalone create UI (declared for usage banners/counts).
4659
+ declaredLimits: [
4660
+ { key: "leads", label: "Leads", table: "people", kindFilter: "lead" },
4661
+ { key: "deals", label: "Deals", table: "orders", kindFilter: "deal" },
4662
+ { key: "quotes", label: "Quotes", table: "orders", kindFilter: "quote" }
4663
+ ],
4529
4664
  navigation: [
4530
4665
  {
4531
4666
  section: options?.navSection ?? "main",