@odla-ai/crm 0.10.0 → 0.10.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
@@ -68,6 +68,7 @@ __export(index_exports, {
68
68
  getRecordByOrigin: () => getRecordByOrigin,
69
69
  getRecordOrigin: () => getRecordOrigin,
70
70
  initialStage: () => initialStage,
71
+ isEmailTierName: () => isEmailTierName,
71
72
  linkIdentity: () => linkIdentity,
72
73
  lintTemplate: () => lintTemplate,
73
74
  listActivities: () => listActivities,
@@ -113,7 +114,8 @@ __export(index_exports, {
113
114
  updateRecord: () => updateRecord,
114
115
  updateTask: () => updateTask,
115
116
  updateView: () => updateView,
116
- upsertRecordOrigin: () => upsertRecordOrigin
117
+ upsertRecordOrigin: () => upsertRecordOrigin,
118
+ validateEmailCopy: () => validateEmailCopy
117
119
  });
118
120
  module.exports = __toCommonJS(index_exports);
119
121
 
@@ -1780,6 +1782,43 @@ async function saveConfig(deps, patch, opts = {}) {
1780
1782
  return { config: merged, duplicate };
1781
1783
  }
1782
1784
 
1785
+ // src/email-validation.ts
1786
+ function isEmailTierName(value, id) {
1787
+ return typeof value === "string" && !!value.trim() && value.trim() !== id && !/^(?:unknown|undefined|null|n\/a|(?:sku|price|tier|prod)_[\w-]+|[0-9a-f]{8}-[0-9a-f-]{27,})$/i.test(value.trim()) && !/[{}\r\n]/.test(value);
1788
+ }
1789
+ function validateEmailCopy(copy, options = {}) {
1790
+ const issues = [];
1791
+ const subject = typeof copy.subject === "string" ? copy.subject : "";
1792
+ const text = typeof copy.text === "string" ? copy.text : "";
1793
+ if (!subject.trim()) issues.push({ code: "subject-missing", message: "Add an email subject." });
1794
+ if (!text.trim()) issues.push({ code: "body-missing", message: "Add an email body." });
1795
+ if (/[\r\n]/.test(subject) || subject.length > 200) issues.push({ code: "subject-invalid", message: "Use a single-line subject of at most 200 characters." });
1796
+ if (text.length > 1e4) issues.push({ code: "body-invalid", message: "Keep the email body within 10,000 characters." });
1797
+ const combined = `${subject}
1798
+ ${text}`;
1799
+ 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
+ issues.push({ code: "internal-copy", message: "Replace internal draft or retirement notes with recipient-facing copy." });
1801
+ }
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}}." });
1804
+ for (const variable of tokens) {
1805
+ if (options.allowedVariables && !options.allowedVariables.includes(variable)) issues.push({ code: "variable-unknown", variable, message: `This event does not supply {{${variable}}}.` });
1806
+ }
1807
+ if (options.values) {
1808
+ for (const variable of /* @__PURE__ */ new Set([...tokens, ...options.requiredVariables ?? []])) {
1809
+ const value = options.values[variable];
1810
+ if (typeof value !== "string" || !value.trim() && !options.optionalVariables?.includes(variable)) {
1811
+ issues.push({ code: "value-missing", variable, message: `Missing event value: ${variable}.` });
1812
+ } else if ((variable === "tierName" || variable === "tier") && !isEmailTierName(value, options.tierId)) {
1813
+ issues.push({ code: "tier-name-invalid", variable, message: "Resolve the membership tier's display name before sending." });
1814
+ } else if (/\{\{|\}\}/.test(value)) {
1815
+ issues.push({ code: "value-invalid", variable, message: `Unresolved placeholder in ${variable}.` });
1816
+ }
1817
+ }
1818
+ }
1819
+ return issues;
1820
+ }
1821
+
1783
1822
  // src/email.ts
1784
1823
  async function sendTemplatedEmail(deps, opts) {
1785
1824
  const { crm, db, now, newId } = resolveDeps(deps);
@@ -1788,7 +1827,8 @@ async function sendTemplatedEmail(deps, opts) {
1788
1827
  const config = await getConfig(deps);
1789
1828
  const stored = config?.emailTemplates[opts.templateId] ?? null;
1790
1829
  if (!stored || !stored.subject && !stored.text) return { sent: false, reason: "no_template_copy" };
1791
- if (!stored.enabled && !opts.force) return { sent: false, reason: "disabled" };
1830
+ if (typeof stored.enabled !== "boolean") return { sent: false, reason: "template-invalid" };
1831
+ if (stored.enabled !== true && !opts.force) return { sent: false, reason: "disabled" };
1792
1832
  const record = await mustGetRecord(deps, opts.recordId);
1793
1833
  const channels = await listChannels(deps, record.id);
1794
1834
  const channel = opts.channelId ? channels.find((c) => c.id === opts.channelId) : channels.find((c) => c.kind === "email" && c.isPrimary);
@@ -1807,8 +1847,10 @@ async function sendTemplatedEmail(deps, opts) {
1807
1847
  headers["List-Unsubscribe"] = `<${unsubscribeUrl}>`;
1808
1848
  headers["List-Unsubscribe-Post"] = "List-Unsubscribe=One-Click";
1809
1849
  }
1850
+ if (validateEmailCopy(stored, { allowedVariables: spec.vars, values: vars }).length) return { sent: false, reason: "template-invalid" };
1810
1851
  let subject = renderTemplate(stored.subject, vars);
1811
1852
  const text = renderTemplate(stored.text, vars);
1853
+ if (validateEmailCopy({ subject, text }, { allowedVariables: [] }).length) return { sent: false, reason: "template-invalid" };
1812
1854
  let to = channel.value;
1813
1855
  let redirected = false;
1814
1856
  let logOnly = !deps.sender;