@odla-ai/crm 0.1.0 → 0.1.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/README.md CHANGED
@@ -24,7 +24,7 @@ The CRM is a **schema + deny-all rules + a route factory + an admin UI kit** —
24
24
  import { defineCrm, createCrmRoutes } from "@odla-ai/crm";
25
25
  import { init } from "@odla-ai/db";
26
26
 
27
- const crm = defineCrm({
27
+ export const crm = defineCrm({
28
28
  types: {
29
29
  person: {
30
30
  label: "Person",
@@ -53,10 +53,6 @@ const crm = defineCrm({
53
53
  },
54
54
  });
55
55
 
56
- // Provision once (the odla CLI automates this):
57
- // POST crm.schema → /app/:id/schema
58
- // crm.rules → /app/:id/admin/rules
59
-
60
56
  const db = init({ appId, adminToken, endpoint }); // @odla-ai/db admin client
61
57
  const crmRoutes = createCrmRoutes({
62
58
  crm,
@@ -75,6 +71,31 @@ export default {
75
71
  };
76
72
  ```
77
73
 
74
+ Put the resolved CRM in an app module both the Worker and checked-in odla
75
+ config can import, then declare the project-specific integration:
76
+
77
+ ```ts
78
+ // odla.config.mjs
79
+ import { createCrmIntegration } from "@odla-ai/crm";
80
+ import { crm } from "./src/crm.js";
81
+
82
+ export default {
83
+ app: { id: "my-app", name: "My App" },
84
+ services: ["db"],
85
+ integrations: [createCrmIntegration(crm, {
86
+ basePath: "/api/crm",
87
+ notificationEmail: "owner@example.com",
88
+ })],
89
+ links: { dev: "https://dev.example.com" },
90
+ };
91
+ ```
92
+
93
+ `odla-ai provision` collision-checks and merges the CRM schema/rules, then
94
+ creates `crm_config` only when absent. `odla-ai doctor` checks the composed
95
+ namespaces/rules/seed contract offline; `odla-ai smoke` uses `links.<env>` to
96
+ verify the mounted records route returns `401` without credentials. Mounting
97
+ `createCrmRoutes` and supplying `authorize` remain app-owned source work.
98
+
78
99
  And the admin surface, from `@odla-ai/crm/ui` (React or Preact via compat, `--ui-*` app-tier styling):
79
100
 
80
101
  ```tsx
@@ -100,7 +121,7 @@ function People() {
100
121
  | Types, pipelines, gates, scorers, template contracts | Code — `defineCrm` (validated at boot) |
101
122
  | Template copy, enable flags, destination addresses | Data — the `crm_config` row (owner-editable) |
102
123
  | Records, tags, links, activities, channels, send audit, views | odla-db (`CRM_SCHEMA` namespaces) |
103
- | Money | Nowhere here — `BillingProvider` is a declared port; the `@odla-ai/stripe` adapter is the next milestone. The record carries cache columns only. |
124
+ | Money | Nowhere here — `BillingProvider` is an optional port an app-owned client or reusable provider package may implement. The record carries cache columns only. |
104
125
  | Meetings/scheduling | `@odla-ai/calendar` |
105
126
  | Clerk role changes | Your `onTransition` hooks (via `@odla-ai/auth-clerk`) |
106
127
 
package/dist/index.cjs CHANGED
@@ -46,6 +46,7 @@ __export(index_exports, {
46
46
  canTransition: () => canTransition,
47
47
  channelKey: () => channelKey,
48
48
  compileList: () => compileList,
49
+ createCrmIntegration: () => createCrmIntegration,
49
50
  createCrmRoutes: () => createCrmRoutes,
50
51
  createRecord: () => createRecord,
51
52
  crmIntegration: () => crmIntegration,
@@ -360,6 +361,25 @@ function canTransition(def, record, to) {
360
361
  return null;
361
362
  }
362
363
 
364
+ // src/template.ts
365
+ var TOKEN_RE = /\{\{\s*([a-zA-Z][a-zA-Z0-9_]*)\s*\}\}/g;
366
+ function renderTemplate(template, vars) {
367
+ return template.replace(TOKEN_RE, (_, name) => vars[name] ?? "");
368
+ }
369
+ function templateVars(template) {
370
+ const seen = /* @__PURE__ */ new Set();
371
+ for (const match of template.matchAll(TOKEN_RE)) seen.add(match[1]);
372
+ return [...seen];
373
+ }
374
+ function lintTemplate(template, knownVars) {
375
+ const used = templateVars(template);
376
+ const known = new Set(knownVars);
377
+ return {
378
+ unknown: used.filter((v) => !known.has(v)),
379
+ unused: knownVars.filter((v) => !used.includes(v))
380
+ };
381
+ }
382
+
363
383
  // src/validate.ts
364
384
  var EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
365
385
  var STRINGISH = /* @__PURE__ */ new Set(["string", "email", "url", "enum"]);
@@ -534,6 +554,11 @@ function defineCrm(config) {
534
554
  if (tpl.class !== "transactional" && tpl.class !== "marketing")
535
555
  fail(at, `class must be transactional|marketing`);
536
556
  for (const v of tpl.vars) if (!/^[a-zA-Z][a-zA-Z0-9_]*$/.test(v)) fail(at, `bad var "${v}"`);
557
+ if (tpl.defaults) {
558
+ const lint = lintTemplate(`${tpl.defaults.subject}
559
+ ${tpl.defaults.text}`, tpl.vars);
560
+ if (lint.unknown.length) fail(at, `defaults reference unknown vars: ${lint.unknown.join(", ")}`);
561
+ }
537
562
  }
538
563
  const type = (name) => {
539
564
  const def = config.types[name];
@@ -681,25 +706,6 @@ function canEmail(consent, cls) {
681
706
  return true;
682
707
  }
683
708
 
684
- // src/template.ts
685
- var TOKEN_RE = /\{\{\s*([a-zA-Z][a-zA-Z0-9_]*)\s*\}\}/g;
686
- function renderTemplate(template, vars) {
687
- return template.replace(TOKEN_RE, (_, name) => vars[name] ?? "");
688
- }
689
- function templateVars(template) {
690
- const seen = /* @__PURE__ */ new Set();
691
- for (const match of template.matchAll(TOKEN_RE)) seen.add(match[1]);
692
- return [...seen];
693
- }
694
- function lintTemplate(template, knownVars) {
695
- const used = templateVars(template);
696
- const known = new Set(knownVars);
697
- return {
698
- unknown: used.filter((v) => !known.has(v)),
699
- unused: knownVars.filter((v) => !used.includes(v))
700
- };
701
- }
702
-
703
709
  // src/channels.ts
704
710
  var channelKey = (recordId, kind, value) => `${recordId}:${kind}:${value}`;
705
711
  function foldEmailStatus(consent) {
@@ -1300,6 +1306,9 @@ async function saveConfig(deps, patch, opts = {}) {
1300
1306
  throw new CrmInputError(`template "${id}": text must be a string`);
1301
1307
  if (tpl.enabled !== void 0 && typeof tpl.enabled !== "boolean")
1302
1308
  throw new CrmInputError(`template "${id}": enabled must be a boolean`);
1309
+ const copy = [tpl.subject, tpl.text].filter((value) => typeof value === "string").join("\n");
1310
+ const unknown = lintTemplate(copy, declared[id].vars).unknown;
1311
+ if (unknown.length) throw new CrmInputError(`template "${id}": unknown vars ${unknown.join(", ")}`);
1303
1312
  }
1304
1313
  const existing = await getConfig(deps);
1305
1314
  const base = existing ?? {
@@ -2166,7 +2175,7 @@ var crmIntegration = {
2166
2175
  provision: {
2167
2176
  human: [
2168
2177
  "None for the core install \u2014 records/pipelines/email-consent need no external accounts.",
2169
- "When billing lands (@odla-ai/stripe): paste the printed webhook URL into the Stripe dashboard and vault the sk_/whsec_ secrets (Studio, write-only)."
2178
+ "If the app injects billing, configure that provider's webhook and vault secrets according to the app-owned adapter."
2170
2179
  ],
2171
2180
  cli: [
2172
2181
  "POST the merged CRM_SCHEMA to /app/:id/schema.",
@@ -2175,10 +2184,36 @@ var crmIntegration = {
2175
2184
  ],
2176
2185
  doctor: [
2177
2186
  "Every crm_* namespace has rules installed (deny-all: no rule = exposed to nothing, but assert explicitly).",
2178
- "The crm_config row exists and its emailTemplates parse against the code-declared registry.",
2179
- "lintTemplate reports no unknown {{vars}} in stored template copy.",
2187
+ "The guarded crm_config seed targets the declared-unique crm_config.key attribute.",
2188
+ "Code-declared template defaults have no unknown {{vars}}; owner-edited copies are checked when saved."
2189
+ ],
2190
+ smoke: [
2180
2191
  "A GET probe of <basePath>/records without credentials answers 401 (routes mounted, authorize enforced)."
2181
2192
  ]
2182
2193
  }
2183
2194
  };
2195
+ function createCrmIntegration(crm, options = {}) {
2196
+ const basePath = options.basePath ?? "/api/crm";
2197
+ if (!/^\/[A-Za-z0-9._~!$&'()*+,;=:@%/-]*$/.test(basePath) || basePath.endsWith("/")) {
2198
+ throw new Error("createCrmIntegration: basePath must be an absolute path without a trailing slash");
2199
+ }
2200
+ const attrs = {
2201
+ emailTemplates: defaultTemplates(crm),
2202
+ settings: options.settings ?? {},
2203
+ updatedAt: options.now ?? Date.now()
2204
+ };
2205
+ if (options.notificationEmail !== void 0) attrs.notificationEmail = options.notificationEmail;
2206
+ if (options.replyTo !== void 0) attrs.replyTo = options.replyTo;
2207
+ if (options.debugEmail !== void 0) attrs.debugEmail = options.debugEmail;
2208
+ return {
2209
+ ...crmIntegration,
2210
+ seeds: [{
2211
+ id: "config",
2212
+ ns: CRM_NS.config,
2213
+ key: { attr: "key", value: CONFIG_KEY },
2214
+ attrs
2215
+ }],
2216
+ probes: [{ path: `${basePath}/records`, expectedStatus: 401 }]
2217
+ };
2218
+ }
2184
2219
  //# sourceMappingURL=index.cjs.map