@odla-ai/crm 0.10.1 → 0.11.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.
package/dist/index.cjs CHANGED
@@ -109,6 +109,11 @@ __export(index_exports, {
109
109
  setRank: () => setRank,
110
110
  setStage: () => setStage,
111
111
  stageIndexOf: () => stageIndexOf,
112
+ stripTemplateTokens: () => stripTemplateTokens,
113
+ templateLiveVars: () => templateLiveVars,
114
+ templateSectionIssues: () => templateSectionIssues,
115
+ templateSectionNames: () => templateSectionNames,
116
+ templateTokens: () => templateTokens,
112
117
  templateVars: () => templateVars,
113
118
  typeSummary: () => typeSummary,
114
119
  updateRecord: () => updateRecord,
@@ -423,15 +428,74 @@ function canTransition(def, record, to) {
423
428
  }
424
429
 
425
430
  // src/template.ts
426
- var TOKEN_RE = /\{\{\s*([a-zA-Z][a-zA-Z0-9_]*)\s*\}\}/g;
431
+ var NAME = "[a-zA-Z][a-zA-Z0-9_]*";
432
+ var TOKEN_RE = new RegExp(`\\{\\{\\s*([#/]?)\\s*(${NAME})\\s*\\}\\}`, "g");
433
+ var SECTION_RE = new RegExp(`\\{\\{\\s*#\\s*(${NAME})\\s*\\}\\}([\\s\\S]*?)\\{\\{\\s*\\/\\s*\\1\\s*\\}\\}(\\n{0,2})`, "g");
434
+ var MAX_SECTION_PASSES = 10;
435
+ function templateTokens(template) {
436
+ const out = [];
437
+ for (const match of template.matchAll(TOKEN_RE)) {
438
+ out.push({ name: match[2], kind: match[1] === "#" ? "open" : match[1] === "/" ? "close" : "var" });
439
+ }
440
+ return out;
441
+ }
442
+ function stripTemplateTokens(template) {
443
+ return template.replace(TOKEN_RE, "");
444
+ }
445
+ function templateSectionIssues(template) {
446
+ const open = [], issues = [];
447
+ for (const token of templateTokens(template)) {
448
+ if (token.kind === "open") open.push(token.name);
449
+ else if (token.kind === "close") {
450
+ const at = open.lastIndexOf(token.name);
451
+ if (at < 0) issues.push(`{{/${token.name}}} closes a section that was never opened.`);
452
+ else open.splice(at, 1);
453
+ }
454
+ }
455
+ for (const name of open) issues.push(`Close the {{#${name}}} section with {{/${name}}}.`);
456
+ return issues;
457
+ }
458
+ function resolveSections(template, vars) {
459
+ let out = template;
460
+ for (let pass = 0; pass < MAX_SECTION_PASSES; pass++) {
461
+ let changed = false;
462
+ out = out.replace(SECTION_RE, (match, name, inner, trailing, offset, whole) => {
463
+ changed = true;
464
+ if ((vars[name] ?? "").trim()) return inner + trailing;
465
+ const before = whole.slice(0, offset);
466
+ if (!(offset === 0 || before.endsWith("\n"))) return trailing;
467
+ let keep = trailing.slice(1);
468
+ if (before.endsWith("\n\n") && keep.startsWith("\n")) keep = keep.slice(1);
469
+ return keep;
470
+ });
471
+ if (!changed) break;
472
+ }
473
+ return out;
474
+ }
427
475
  function renderTemplate(template, vars) {
428
- return template.replace(TOKEN_RE, (_, name) => vars[name] ?? "");
476
+ return resolveSections(template, vars).replace(TOKEN_RE, (_, kind, name) => kind ? "" : vars[name] ?? "");
429
477
  }
430
478
  function templateVars(template) {
431
479
  const seen = /* @__PURE__ */ new Set();
432
- for (const match of template.matchAll(TOKEN_RE)) seen.add(match[1]);
480
+ for (const token of templateTokens(template)) seen.add(token.name);
433
481
  return [...seen];
434
482
  }
483
+ function templateLiveVars(template, vars) {
484
+ const live = /* @__PURE__ */ new Set(), open = [];
485
+ for (const token of templateTokens(template)) {
486
+ if (token.kind === "open") open.push(token.name);
487
+ else if (token.kind === "close") {
488
+ const at = open.lastIndexOf(token.name);
489
+ if (at >= 0) open.splice(at, 1);
490
+ } else if (open.every((name) => (vars[name] ?? "").trim())) live.add(token.name);
491
+ }
492
+ return [...live];
493
+ }
494
+ function templateSectionNames(template) {
495
+ const tokens = templateTokens(template);
496
+ const plain = new Set(tokens.filter((token) => token.kind === "var").map((token) => token.name));
497
+ return [...new Set(tokens.filter((token) => token.kind !== "var" && !plain.has(token.name)).map((token) => token.name))];
498
+ }
435
499
  function lintTemplate(template, knownVars) {
436
500
  const used = templateVars(template);
437
501
  const known = new Set(knownVars);
@@ -1799,13 +1863,16 @@ ${text}`;
1799
1863
  if (/^\s*(?:\(\s*unused\s*:|Unused\.\s*Switched off\b|(?:TODO|TBD|PLACEHOLDER)\s*:|DRAFT\s*[-:]|\[\s*(?:draft|placeholder)\s*\]|do not (?:send|use) this (?:email|template))/im.test(combined)) {
1800
1864
  issues.push({ code: "internal-copy", message: "Replace internal draft or retirement notes with recipient-facing copy." });
1801
1865
  }
1802
- const tokens = [...new Set([...combined.matchAll(/\{\{(\w+)\}\}/g)].map((match) => match[1]))];
1803
- if (/[{}]/.test(combined.replace(/\{\{\w+\}\}/g, ""))) issues.push({ code: "placeholder-invalid", message: "Fix malformed placeholders; use {{variableName}}." });
1866
+ const tokens = templateVars(combined);
1867
+ const sections = new Set(templateSectionNames(combined));
1868
+ if (/[{}]/.test(stripTemplateTokens(combined))) issues.push({ code: "placeholder-invalid", message: "Fix malformed placeholders; use {{variableName}}, or {{#name}} \u2026 {{/name}} for a section." });
1869
+ for (const message of templateSectionIssues(combined)) issues.push({ code: "section-invalid", message });
1804
1870
  for (const variable of tokens) {
1805
1871
  if (options.allowedVariables && !options.allowedVariables.includes(variable)) issues.push({ code: "variable-unknown", variable, message: `This event does not supply {{${variable}}}.` });
1806
1872
  }
1807
1873
  if (options.values) {
1808
- for (const variable of /* @__PURE__ */ new Set([...tokens, ...options.requiredVariables ?? []])) {
1874
+ for (const name of sections) if (typeof options.values[name] !== "string") issues.push({ code: "value-missing", variable: name, message: `Missing event value: ${name}.` });
1875
+ for (const variable of /* @__PURE__ */ new Set([...templateLiveVars(combined, options.values), ...options.requiredVariables ?? []])) {
1809
1876
  const value = options.values[variable];
1810
1877
  if (typeof value !== "string" || !value.trim() && !options.optionalVariables?.includes(variable)) {
1811
1878
  issues.push({ code: "value-missing", variable, message: `Missing event value: ${variable}.` });
@@ -2078,6 +2145,13 @@ function unsubscribePage(token, done) {
2078
2145
  }
2079
2146
 
2080
2147
  // src/routes/params.ts
2148
+ function pathId(segment) {
2149
+ try {
2150
+ return decodeURIComponent(segment);
2151
+ } catch {
2152
+ throw new CrmInputError("malformed percent-encoding in the path");
2153
+ }
2154
+ }
2081
2155
  function str(body, key) {
2082
2156
  const value = body[key];
2083
2157
  if (typeof value !== "string" || value === "")
@@ -2239,7 +2313,7 @@ async function handleTags(ctx, req, id, rest) {
2239
2313
  return json(result, 201);
2240
2314
  }
2241
2315
  if (req.method === "DELETE" && rest.length === 1) {
2242
- const result = await removeTag(ctx.deps, { recordId: id, tag: decodeURIComponent(rest[0]) });
2316
+ const result = await removeTag(ctx.deps, { recordId: id, tag: rest[0] });
2243
2317
  return json(result);
2244
2318
  }
2245
2319
  return json({ error: "method not allowed" }, 405);
@@ -2587,30 +2661,31 @@ async function handleCrmDiscussionReferences(ctx, req, url) {
2587
2661
 
2588
2662
  // src/routes/index.ts
2589
2663
  async function route(ctx, req, url, seg) {
2590
- const [head, id, action, ...rest] = seg;
2591
- if (head === "discussion-references" && id === void 0)
2664
+ const [head, second, action, ...rest] = seg;
2665
+ if (head === "discussion-references" && second === void 0)
2592
2666
  return handleCrmDiscussionReferences(ctx, req, url);
2593
2667
  if (head === "records") {
2594
- if (id === void 0) return handleRecordsRoot(ctx, req, url);
2668
+ if (second === void 0) return handleRecordsRoot(ctx, req, url);
2669
+ const id = pathId(second);
2595
2670
  if (action === void 0) return handleRecordItem(ctx, req, id);
2596
2671
  if (action === "archive") return handleArchive(ctx, req, id);
2597
2672
  if (action === "stage") return handleStage(ctx, req, id);
2598
2673
  if (action === "rank") return handleRank(ctx, req, id);
2599
- if (action === "tags") return handleTags(ctx, req, id, rest);
2674
+ if (action === "tags") return handleTags(ctx, req, id, rest.map(pathId));
2600
2675
  if (action === "links") return handleLinks(ctx, req, id);
2601
2676
  if (action === "link-identity") return handleLinkIdentity(ctx, req, id);
2602
2677
  if (action === "email") return handleSendEmail(ctx, req, id);
2603
2678
  if (action === "activities") return handleRecordActivities(ctx, req, url, id);
2604
2679
  return null;
2605
2680
  }
2606
- if (head === "activities" && id !== void 0 && action === void 0)
2607
- return handleActivityItem(ctx, req, id);
2608
- if (head === "tasks" && id === void 0) return handleTasks(ctx, req, url);
2609
- if (head === "views") return handleViews(ctx, req, url, seg.slice(1));
2610
- if (head === "summary" && id === void 0) return handleSummary(ctx, req, url);
2611
- if (head === "config" && id === void 0) return handleConfig(ctx, req);
2612
- if (head === "email-log" && id === void 0) return handleEmailLog(ctx, req, url);
2613
- if (head === "rank" && id === "rebalance" && action === void 0)
2681
+ if (head === "activities" && second !== void 0 && action === void 0)
2682
+ return handleActivityItem(ctx, req, pathId(second));
2683
+ if (head === "tasks" && second === void 0) return handleTasks(ctx, req, url);
2684
+ if (head === "views") return handleViews(ctx, req, url, seg.slice(1).map(pathId));
2685
+ if (head === "summary" && second === void 0) return handleSummary(ctx, req, url);
2686
+ if (head === "config" && second === void 0) return handleConfig(ctx, req);
2687
+ if (head === "email-log" && second === void 0) return handleEmailLog(ctx, req, url);
2688
+ if (head === "rank" && second === "rebalance" && action === void 0)
2614
2689
  return handleRebalance(ctx, req);
2615
2690
  return null;
2616
2691
  }