@wirecat/cli-core 0.606.0 → 0.607.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.
Files changed (209) hide show
  1. package/CHANGELOG.md +186 -1
  2. package/dist/calendar/index.d.ts +1 -0
  3. package/dist/calendar/index.js +1 -0
  4. package/dist/calendar/reads.js +5 -0
  5. package/dist/cli/app-mcp-command.d.ts +6 -2
  6. package/dist/cli/app-mcp-command.js +11 -2
  7. package/dist/cli/csv.d.ts +10 -0
  8. package/dist/cli/csv.js +22 -0
  9. package/dist/cli/early-refusal.d.ts +10 -0
  10. package/dist/cli/early-refusal.js +19 -0
  11. package/dist/cli/index.d.ts +3 -0
  12. package/dist/cli/index.js +3 -0
  13. package/dist/cli/profile-token.d.ts +29 -0
  14. package/dist/cli/profile-token.js +65 -0
  15. package/dist/cli/program.js +4 -2
  16. package/dist/crm/cli/csv.d.ts +23 -0
  17. package/dist/crm/cli/csv.js +135 -0
  18. package/dist/crm/cli/index.d.ts +1 -1
  19. package/dist/crm/cli/index.js +33 -21
  20. package/dist/crm/cli/pipeline-view.d.ts +3 -0
  21. package/dist/crm/cli/pipeline-view.js +36 -0
  22. package/dist/crm/writes.d.ts +6 -4
  23. package/dist/crm/writes.js +17 -13
  24. package/dist/documents.d.ts +2 -0
  25. package/dist/documents.js +2 -0
  26. package/dist/domain/business.d.ts +79 -0
  27. package/dist/domain/business.js +42 -0
  28. package/dist/domain/finance.d.ts +2 -0
  29. package/dist/domain/finance.js +2 -2
  30. package/dist/domain/google-documents.d.ts +2 -0
  31. package/dist/domain/google-documents.js +3 -1
  32. package/dist/domain/money.d.ts +2 -0
  33. package/dist/domain/money.js +11 -3
  34. package/dist/domain/providers.d.ts +36 -1
  35. package/dist/domain/providers.js +69 -7
  36. package/dist/domain/record-fields.d.ts +42 -0
  37. package/dist/domain/record-fields.js +122 -0
  38. package/dist/domain/record-types.d.ts +489 -16
  39. package/dist/domain/record-types.js +374 -7
  40. package/dist/domain/records.d.ts +96 -5
  41. package/dist/domain/records.js +178 -32
  42. package/dist/domain/ref-summaries.js +5 -0
  43. package/dist/domain/references.d.ts +1 -1
  44. package/dist/domain/references.js +5 -0
  45. package/dist/domain/vcard.d.ts +25 -0
  46. package/dist/domain/vcard.js +267 -0
  47. package/dist/entities/deal.d.ts +14 -0
  48. package/dist/entities/deal.js +3 -0
  49. package/dist/entities/index.d.ts +12 -0
  50. package/dist/errors/reasons.js +61 -2
  51. package/dist/fetch/addresses.d.ts +5 -0
  52. package/dist/fetch/addresses.js +76 -0
  53. package/dist/fetch/page.d.ts +64 -0
  54. package/dist/fetch/page.js +256 -0
  55. package/dist/index.d.ts +5 -3
  56. package/dist/index.js +5 -3
  57. package/dist/meetings/calendar-text.d.ts +1 -1
  58. package/dist/meetings/calendar-text.js +4 -0
  59. package/dist/meetings/event-store.d.ts +2 -2
  60. package/dist/search/account-list.d.ts +4 -0
  61. package/dist/search/account-list.js +19 -8
  62. package/dist/search/query.js +2 -2
  63. package/dist/services/events-search.js +2 -2
  64. package/dist/services/index.d.ts +1 -1
  65. package/dist/services/index.js +1 -1
  66. package/dist/services/ingest.d.ts +56 -8
  67. package/dist/services/ingest.js +95 -61
  68. package/dist/services/messages-search.js +3 -2
  69. package/dist/services/metadata.d.ts +1 -1
  70. package/dist/services/notes-search.js +1 -0
  71. package/dist/services/proposals/kinds/contracts.d.ts +4 -0
  72. package/dist/services/proposals/kinds/contracts.js +9 -0
  73. package/dist/services/proposals/kinds/finance.d.ts +4 -0
  74. package/dist/services/proposals/kinds/finance.js +14 -35
  75. package/dist/services/proposals/kinds/orders.js +7 -30
  76. package/dist/services/proposals/kinds/shared.d.ts +27 -1
  77. package/dist/services/proposals/kinds/shared.js +34 -0
  78. package/dist/services/proposals/kinds/source-review.d.ts +4 -0
  79. package/dist/services/proposals/kinds/source-review.js +45 -0
  80. package/dist/services/proposals/kinds/subscriptions.d.ts +9 -0
  81. package/dist/services/proposals/kinds/subscriptions.js +24 -0
  82. package/dist/services/proposals/kinds/transactions.d.ts +4 -0
  83. package/dist/services/proposals/kinds/transactions.js +10 -0
  84. package/dist/services/proposals/registry.js +9 -0
  85. package/dist/services/search-all-meetings.js +2 -2
  86. package/dist/services/search-all.js +4 -3
  87. package/dist/services/search-kind.js +3 -3
  88. package/dist/store/decisions.js +5 -1
  89. package/dist/store/index.d.ts +14 -3
  90. package/dist/store/index.js +3 -0
  91. package/dist/store/people.js +1 -1
  92. package/dist/store/source-review.d.ts +64 -0
  93. package/dist/store/source-review.js +7 -0
  94. package/dist/store/sqlite/accounts.d.ts +1 -1
  95. package/dist/store/sqlite/accounts.js +24 -1
  96. package/dist/store/sqlite/added-sources.d.ts +82 -0
  97. package/dist/store/sqlite/added-sources.js +237 -0
  98. package/dist/store/sqlite/business-contracts.d.ts +90 -0
  99. package/dist/store/sqlite/business-contracts.js +182 -0
  100. package/dist/store/sqlite/calendar-events.js +7 -3
  101. package/dist/store/sqlite/calendar-forget.js +3 -0
  102. package/dist/store/sqlite/calendar.d.ts +21 -1
  103. package/dist/store/sqlite/calendar.js +27 -7
  104. package/dist/store/sqlite/chat-rekey.js +7 -0
  105. package/dist/store/sqlite/chats.d.ts +3 -1
  106. package/dist/store/sqlite/chats.js +15 -1
  107. package/dist/store/sqlite/crm-rows.d.ts +44 -0
  108. package/dist/store/sqlite/crm-rows.js +195 -0
  109. package/dist/store/sqlite/crm.d.ts +2 -11
  110. package/dist/store/sqlite/crm.js +11 -166
  111. package/dist/store/sqlite/deals.d.ts +4 -8
  112. package/dist/store/sqlite/deals.js +5 -3
  113. package/dist/store/sqlite/document-transfer/snapshot.js +2 -2
  114. package/dist/store/sqlite/emails.js +2 -0
  115. package/dist/store/sqlite/entity-types.d.ts +14 -9
  116. package/dist/store/sqlite/entity-types.js +4 -10
  117. package/dist/store/sqlite/evidence-signature.d.ts +114 -0
  118. package/dist/store/sqlite/evidence-signature.js +349 -0
  119. package/dist/store/sqlite/evidence-sources.d.ts +2 -1
  120. package/dist/store/sqlite/evidence-sources.js +7 -3
  121. package/dist/store/sqlite/facts.d.ts +7 -2
  122. package/dist/store/sqlite/facts.js +21 -5
  123. package/dist/store/sqlite/finance.d.ts +18 -105
  124. package/dist/store/sqlite/finance.js +100 -219
  125. package/dist/store/sqlite/knowledge.d.ts +8 -2
  126. package/dist/store/sqlite/knowledge.js +65 -71
  127. package/dist/store/sqlite/labelled.d.ts +3 -0
  128. package/dist/store/sqlite/labelled.js +11 -0
  129. package/dist/store/sqlite/link-kinds.d.ts +2 -0
  130. package/dist/store/sqlite/link-kinds.js +2 -0
  131. package/dist/store/sqlite/manifest.js +2 -0
  132. package/dist/store/sqlite/memories.d.ts +23 -9
  133. package/dist/store/sqlite/memories.js +41 -13
  134. package/dist/store/sqlite/messages.js +2 -0
  135. package/dist/store/sqlite/migrations.generated.js +103 -0
  136. package/dist/store/sqlite/money-lines.d.ts +48 -0
  137. package/dist/store/sqlite/money-lines.js +76 -0
  138. package/dist/store/sqlite/money-transactions.d.ts +132 -0
  139. package/dist/store/sqlite/money-transactions.js +295 -0
  140. package/dist/store/sqlite/notes.d.ts +11 -2
  141. package/dist/store/sqlite/notes.js +20 -25
  142. package/dist/store/sqlite/orders.d.ts +36 -6
  143. package/dist/store/sqlite/orders.js +126 -46
  144. package/dist/store/sqlite/payment-facts.js +1 -1
  145. package/dist/store/sqlite/private-people.js +2 -0
  146. package/dist/store/sqlite/products.d.ts +78 -0
  147. package/dist/store/sqlite/products.js +131 -0
  148. package/dist/store/sqlite/project-transfer/records.js +26 -1
  149. package/dist/store/sqlite/project-transfer/snapshot.js +5 -3
  150. package/dist/store/sqlite/proposed-actions.d.ts +1 -1
  151. package/dist/store/sqlite/proposed-actions.js +24 -7
  152. package/dist/store/sqlite/providers.d.ts +0 -4
  153. package/dist/store/sqlite/providers.js +20 -2
  154. package/dist/store/sqlite/record-matching.d.ts +80 -0
  155. package/dist/store/sqlite/record-matching.js +264 -0
  156. package/dist/store/sqlite/record-projection.d.ts +89 -0
  157. package/dist/store/sqlite/record-projection.js +50 -0
  158. package/dist/store/sqlite/record-projectors-direct.d.ts +17 -0
  159. package/dist/store/sqlite/record-projectors-direct.js +125 -0
  160. package/dist/store/sqlite/record-projectors-money.d.ts +26 -0
  161. package/dist/store/sqlite/record-projectors-money.js +214 -0
  162. package/dist/store/sqlite/record-projectors-phone.d.ts +28 -0
  163. package/dist/store/sqlite/record-projectors-phone.js +212 -0
  164. package/dist/store/sqlite/record-projectors.d.ts +3 -44
  165. package/dist/store/sqlite/record-projectors.js +7 -67
  166. package/dist/store/sqlite/record-relations.d.ts +24 -0
  167. package/dist/store/sqlite/record-relations.js +92 -0
  168. package/dist/store/sqlite/record-remap.d.ts +12 -0
  169. package/dist/store/sqlite/record-remap.js +92 -0
  170. package/dist/store/sqlite/record-run.d.ts +71 -0
  171. package/dist/store/sqlite/record-run.js +179 -0
  172. package/dist/store/sqlite/record-settling.d.ts +9 -0
  173. package/dist/store/sqlite/record-settling.js +74 -0
  174. package/dist/store/sqlite/record-stamps.d.ts +13 -0
  175. package/dist/store/sqlite/record-stamps.js +30 -0
  176. package/dist/store/sqlite/record-writes.d.ts +43 -0
  177. package/dist/store/sqlite/record-writes.js +307 -0
  178. package/dist/store/sqlite/records.d.ts +52 -42
  179. package/dist/store/sqlite/records.js +123 -353
  180. package/dist/store/sqlite/ref-summaries.d.ts +6 -5
  181. package/dist/store/sqlite/ref-summaries.js +60 -8
  182. package/dist/store/sqlite/refunds.d.ts +31 -0
  183. package/dist/store/sqlite/refunds.js +112 -0
  184. package/dist/store/sqlite/reviewed.d.ts +109 -0
  185. package/dist/store/sqlite/reviewed.js +173 -0
  186. package/dist/store/sqlite/schema.d.ts +2840 -83
  187. package/dist/store/sqlite/schema.js +256 -1
  188. package/dist/store/sqlite/source-review-marks.d.ts +54 -0
  189. package/dist/store/sqlite/source-review-marks.js +143 -0
  190. package/dist/store/sqlite/source-review.d.ts +54 -0
  191. package/dist/store/sqlite/source-review.js +221 -0
  192. package/dist/store/sqlite/stage-amounts.d.ts +14 -0
  193. package/dist/store/sqlite/stage-amounts.js +23 -0
  194. package/dist/store/sqlite/subscriptions.d.ts +135 -0
  195. package/dist/store/sqlite/subscriptions.js +294 -0
  196. package/dist/store/sqlite/text-input.d.ts +3 -0
  197. package/dist/store/sqlite/text-input.js +13 -0
  198. package/dist/store/sqlite/things.js +2 -9
  199. package/dist/store/sqlite/transaction.d.ts +15 -0
  200. package/dist/store/sqlite/transaction.js +57 -0
  201. package/dist/store/sqlite/wallets.d.ts +88 -0
  202. package/dist/store/sqlite/wallets.js +143 -0
  203. package/dist/store/store-errors.d.ts +1 -0
  204. package/dist/store/store-errors.js +45 -0
  205. package/dist/store/store.d.ts +16 -1
  206. package/dist/store/store.js +30 -6
  207. package/package.json +5 -2
  208. package/schemas/examples/records-business.jsonl +11 -0
  209. package/schemas/records-envelope.v1.schema.json +1160 -5
@@ -1,26 +1,393 @@
1
- /** Types with a table of their own; anything else a source sends is `custom:<name>` and stays a record only. */
2
- export const RECORD_ENTITY_TYPES = ["invoice", "payment", "person", "organization", "project"];
1
+ import { CATALOG_KINDS, CONTRACT_KINDS, CONTRACT_STATUSES, ORDER_DIRECTIONS, SUBSCRIPTION_STATUSES, TRANSACTION_STATUSES, WALLET_KINDS, } from "./business.js";
2
+ import { DEAL_KINDS, DEAL_STATUSES } from "./deals.js";
3
+ import { INVOICE_DIRECTIONS, MONEY_DIRECTIONS } from "./finance.js";
4
+ import { ORDER_PURPOSES, ORDER_STATUSES } from "./orders.js";
5
+ import { arrayOf, currency, day, decimal, enumOf, instant, objectOf, signedDecimal, text, } from "./record-fields.js";
6
+ /**
7
+ * A key v1 already took as any JSON: typing it would refuse sheets v1 kept, so a value that does not read is simply
8
+ * no identifier or no name.
9
+ */
10
+ const kept = (description) => ({ description });
11
+ const PARTY = ["person", "organization"];
12
+ const ORGANIZATION = ["organization"];
13
+ const lineItem = objectOf({
14
+ description: text("What the line is for; the product's name when left out", 2000),
15
+ quantity: decimal("A decimal, 1 when left out"),
16
+ unit_price: decimal("A decimal in the parent's currency"),
17
+ amount: decimal("A decimal in the parent's currency: quantity × unit price"),
18
+ // Untyped: with options.lines text an invoice line's product is data, whatever v1 sent in it.
19
+ product: { description: "The product record's external id in this account, read with options.lines products" },
20
+ product_ref: { type: "string", pattern: "^product:[1-9][0-9]*$", description: "A product: ref of the store" },
21
+ });
22
+ export const SMS_DIRECTIONS = ["in", "out"];
23
+ export const CALL_DIRECTIONS = ["in", "out", "missed"];
24
+ const phoneFields = {
25
+ phone: text("The other side's number as the phone shows it: read to E.164 with the store's phone_countries", 64),
26
+ name: text("The other side's name as the phone shows it", 200),
27
+ };
28
+ /** The person record of the number: matched by phone or made, as a person record is. */
29
+ const COUNTERPART = { counterpart: { targets: ["person"], linked: true } };
30
+ const linesIn = (table, parent) => ({ table, parent, references: { product: "product" }, refs: { product_ref: "product" } });
31
+ const dealFields = (kind) => objectOf({
32
+ title: text(`The ${kind}'s title`, 200),
33
+ pipeline: text("The pipeline record's external id in this account", 512),
34
+ stage: text("The stage's external id in that pipeline", 512),
35
+ status: enumOf(DEAL_STATUSES, "Left out, the stage's outcome says it"),
36
+ amount: decimal("A decimal in currency"),
37
+ currency,
38
+ close_date: day("When it is expected to close"),
39
+ closed_date: day("When it was won or lost"),
40
+ closed_reason: text("Why it was won or lost", 2000),
41
+ url: text("The source's page for it", 2000),
42
+ }, { required: ["title", "pipeline", "stage"] });
43
+ /** The one registry of typed entities: refs, tables, the owner's decisions and the envelope read it. */
3
44
  export const RECORD_TYPES = {
4
45
  invoice: {
5
46
  table: "invoices",
6
47
  review: "proposal",
7
- roles: { issuer: {}, counterparty: {}, project: {} },
48
+ update: "flag",
49
+ tombstone: true,
50
+ fields: objectOf({
51
+ direction: enumOf(INVOICE_DIRECTIONS),
52
+ number: text("The invoice's number", 80),
53
+ currency,
54
+ issue_date: day("The day it was issued"),
55
+ due_date: day("The day it is due"),
56
+ total: decimal("The printed total: refused when it is not the sum of the lines"),
57
+ lines: arrayOf(lineItem, 500, "The invoice's lines"),
58
+ }),
59
+ roles: {
60
+ issuer: { targets: PARTY, v1: true, column: "issuer", pair: true },
61
+ counterparty: { targets: PARTY, v1: true, column: "counterparty", pair: true },
62
+ project: { targets: ["project"], v1: true, column: "project_id" },
63
+ contract: { targets: ["contract"], column: "contract_id" },
64
+ subscription: { targets: ["subscription"], column: "subscription_id" },
65
+ },
66
+ nested: { lines: linesIn("invoice_lines", "invoice_id") },
8
67
  },
9
68
  payment: {
10
69
  table: "payments",
11
70
  review: "proposal",
12
- roles: { payer: {}, payee: {}, project: {}, settles: { many: true, amount: true } },
71
+ update: "re-settle",
72
+ tombstone: true,
73
+ fields: objectOf({
74
+ direction: enumOf(MONEY_DIRECTIONS),
75
+ amount: decimal("A decimal in currency"),
76
+ currency,
77
+ date: day("The day it was paid"),
78
+ reference: text("The payment's reference", 2000),
79
+ }),
80
+ roles: {
81
+ payer: { targets: PARTY, v1: true, column: "payer", pair: true },
82
+ payee: { targets: PARTY, v1: true, column: "payee", pair: true },
83
+ project: { targets: ["project"], v1: true, column: "project_id" },
84
+ settles: { targets: ["invoice"], many: true, amount: true, v1: true },
85
+ refund_of: { targets: ["payment"], column: "refund_of" },
86
+ subscription: { targets: ["subscription"], column: "subscription_id" },
87
+ },
88
+ },
89
+ person: {
90
+ table: "persons",
91
+ review: "identity",
92
+ update: "in-place",
93
+ tombstone: false,
94
+ fields: objectOf({
95
+ name: kept("The person's name"),
96
+ email: kept("E-mail addresses, one or a list: an exact one links the record to the one person who has it"),
97
+ phone: kept("Phone numbers, one or a list: an exact one, read to E.164, links like an e-mail"),
98
+ }),
99
+ roles: { "member-of": { targets: ORGANIZATION, relationship: "member-of", kept: true } },
100
+ },
101
+ organization: {
102
+ table: "organizations",
103
+ review: "identity",
104
+ update: "in-place",
105
+ tombstone: true,
106
+ fields: objectOf({
107
+ name: kept("The organization's name"),
108
+ identifiers: kept("Strong identifiers, a list of {scheme, value}: scheme email, domain, tax-id:<cc> or registration:<cc>. One that " +
109
+ "another organization has links the record to it; any other item is kept as data"),
110
+ }),
111
+ roles: {
112
+ "customer-of": { targets: ORGANIZATION, relationship: "customer-of", kept: true },
113
+ "partner-of": { targets: ORGANIZATION, relationship: "partner-of", kept: true },
114
+ },
115
+ },
116
+ project: {
117
+ table: "projects",
118
+ review: "identity",
119
+ update: "in-place",
120
+ tombstone: true,
121
+ fields: objectOf({
122
+ name: kept("The project's name"),
123
+ key: kept("The project's key: an existing project with it is this one"),
124
+ description: kept("What the project is"),
125
+ }),
126
+ roles: { organization: { targets: ORGANIZATION, column: "organization_id" } },
127
+ },
128
+ order: {
129
+ table: "orders",
130
+ review: "proposal",
131
+ update: "re-propose",
132
+ tombstone: true,
133
+ fields: objectOf({
134
+ number: text("The order's number at the supplier", 80),
135
+ direction: enumOf(ORDER_DIRECTIONS, "purchase when left out; in a sale the owner is the supplier"),
136
+ purpose: enumOf(ORDER_PURPOSES, "business when left out"),
137
+ status: enumOf(ORDER_STATUSES, "placed when left out"),
138
+ supplier_name: text("The shop or supplier as the source names it", 200),
139
+ order_date: day("The day it was ordered"),
140
+ fulfilled_date: day("The day it was delivered"),
141
+ currency,
142
+ total: decimal("With lines, their sum"),
143
+ source_url: text("The order's page at the shop", 2000),
144
+ lines: arrayOf(lineItem, 500, "The order's lines"),
145
+ }),
146
+ roles: {
147
+ customer: { targets: PARTY, column: "customer", pair: true },
148
+ supplier: { targets: PARTY, column: "supplier", pair: true },
149
+ project: { targets: ["project"], column: "project_id" },
150
+ contract: { targets: ["contract"], column: "contract_id" },
151
+ deal: { targets: ["deal"], column: "deal_id" },
152
+ invoice: { targets: ["invoice"], column: "invoice_id" },
153
+ payment: { targets: ["payment"], column: "payment_id" },
154
+ },
155
+ nested: { lines: linesIn("order_lines", "order_id") },
156
+ },
157
+ product: {
158
+ table: "catalog_items",
159
+ review: "direct",
160
+ update: "in-place",
161
+ tombstone: true,
162
+ fields: objectOf({
163
+ kind: enumOf(CATALOG_KINDS, "product when left out"),
164
+ name: text("The product's name", 200),
165
+ sku: text("The supplier's code for it", 80),
166
+ unit: text("hour, pcs, month: free text", 40),
167
+ unit_price: decimal("A decimal in currency"),
168
+ currency,
169
+ description: text("What it is", 2000),
170
+ archived_date: day("No longer sold from this day"),
171
+ }, { required: ["name"] }),
172
+ roles: { supplier: { targets: PARTY, column: "supplier", pair: true } },
173
+ },
174
+ contract: {
175
+ table: "contracts",
176
+ review: "valued-proposal",
177
+ update: "re-propose",
178
+ tombstone: true,
179
+ fields: objectOf({
180
+ name: text("The contract's name", 200),
181
+ number: text("The contract's number", 80),
182
+ kind: enumOf(CONTRACT_KINDS),
183
+ status: enumOf(CONTRACT_STATUSES, "active when left out"),
184
+ signed_date: day("The day it was signed"),
185
+ effective_date: day("The day it starts"),
186
+ expiration_date: day("The day it ends; open-ended when left out"),
187
+ auto_renew: { type: "boolean", description: "Whether it renews itself" },
188
+ renewal: { type: "string", pattern: "^[1-9][0-9]{0,2}-(day|week|month|year)$", description: "1-year" },
189
+ notice_days: { type: "integer", minimum: 0, maximum: 3650, description: "Days of notice before it ends" },
190
+ terms: text("Renewal, cancellation and other terms, as text", 20_000),
191
+ value: decimal("A decimal in currency: the contract is then a proposal"),
192
+ currency,
193
+ }, { required: ["name", "kind"] }),
194
+ roles: {
195
+ project: { targets: ["project"], column: "project_id" },
196
+ deal: { targets: ["deal"], column: "deal_id" },
197
+ party: { targets: PARTY, many: true, relationship: "related-to", kept: true },
198
+ document: { targets: ["document"], many: true, kept: true },
199
+ },
200
+ },
201
+ subscription: {
202
+ table: "subscriptions",
203
+ review: "proposal",
204
+ update: "re-propose",
205
+ tombstone: true,
206
+ fields: objectOf({
207
+ name: text("Its name; the product's when left out", 200),
208
+ direction: enumOf(INVOICE_DIRECTIONS, "receivable: the owner bills it; payable: the owner pays it"),
209
+ status: enumOf(SUBSCRIPTION_STATUSES),
210
+ interval: { type: "string", pattern: "^[1-9][0-9]{0,2}-(day|week|month|year)$", description: "1-month" },
211
+ start_date: day("The first day"),
212
+ trial_end_date: day("The trial's last day"),
213
+ next_renewal_date: day("The next renewal"),
214
+ cancelled_date: day("The day it was cancelled"),
215
+ end_date: day("The last day"),
216
+ prices: arrayOf(objectOf({
217
+ amount: decimal("A decimal in currency"),
218
+ currency,
219
+ effective_from: day("The day the price starts; the subscription's start when left out"),
220
+ }, { required: ["amount", "currency"] }), 100, "Its prices, each from a day; a price is never overwritten"),
221
+ }, { required: ["direction", "interval", "start_date"] }),
222
+ roles: {
223
+ supplier: { targets: PARTY, column: "supplier", pair: true },
224
+ customer: { targets: PARTY, column: "customer", pair: true },
225
+ product: { targets: ["product"], column: "catalog_item_id" },
226
+ contract: { targets: ["contract"], column: "contract_id" },
227
+ project: { targets: ["project"], column: "project_id" },
228
+ },
229
+ nested: { prices: { table: "subscription_prices", parent: "subscription_id" } },
230
+ },
231
+ wallet: {
232
+ table: "financial_accounts",
233
+ review: "direct",
234
+ update: "in-place",
235
+ tombstone: true,
236
+ fields: objectOf({
237
+ name: text("The account's name", 200),
238
+ kind: enumOf(WALLET_KINDS, "bank when left out"),
239
+ currency,
240
+ institution: text("The bank, as text", 200),
241
+ number_hint: text("The last 4 characters of its number; a whole IBAN or card number is refused", 4),
242
+ opening_balance: signedDecimal("Kept in the record only: the owner sets the wallet's opening balance"),
243
+ opening_date: day("The day of that balance"),
244
+ }, { required: ["name", "currency"] }),
245
+ roles: { holder: { targets: PARTY, column: "holder", pair: true } },
246
+ },
247
+ transaction: {
248
+ table: "financial_transactions",
249
+ review: "proposal",
250
+ update: "re-propose",
251
+ tombstone: true,
252
+ fields: objectOf({
253
+ direction: enumOf(MONEY_DIRECTIONS),
254
+ amount: decimal("A decimal above 0 in the wallet's currency"),
255
+ currency,
256
+ status: enumOf(TRANSACTION_STATUSES, "booked when left out"),
257
+ date: day("The day the statement shows"),
258
+ booked_date: day("The day it was booked; the statement day when left out"),
259
+ description: text("As the statement says it", 2000),
260
+ counterparty_name: text("As the statement names the other side", 200),
261
+ reference: text("The statement's reference", 200),
262
+ splits: arrayOf(objectOf({
263
+ amount: decimal("A decimal above 0"),
264
+ category: text("Free text, such as rent", 200),
265
+ note: text("A note", 2000),
266
+ }, { required: ["amount", "category"] }), 100, "Categories of its amount; what they leave is uncategorized"),
267
+ }, { required: ["direction", "amount", "date"] }),
268
+ roles: {
269
+ wallet: { targets: ["wallet"], required: true, column: "financial_account_id" },
270
+ counterparty: { targets: PARTY, column: "counterparty", pair: true },
271
+ payment: { targets: ["payment"], column: "payment_id" },
272
+ project: { targets: ["project"], column: "project_id" },
273
+ },
274
+ nested: { splits: { table: "transaction_splits", parent: "transaction_id" } },
275
+ },
276
+ deal: {
277
+ table: "deals",
278
+ review: "direct",
279
+ update: "in-place",
280
+ tombstone: true,
281
+ fields: dealFields("deal"),
282
+ references: { pipeline: "pipeline" },
283
+ roles: {
284
+ organization: { targets: ORGANIZATION, column: "organization_id" },
285
+ person: { targets: ["person"], many: true, relationship: "related-to", kept: true },
286
+ project: { targets: ["project"], column: "project_id" },
287
+ },
288
+ },
289
+ lead: {
290
+ table: "leads",
291
+ review: "direct",
292
+ update: "in-place",
293
+ tombstone: true,
294
+ fields: dealFields("lead"),
295
+ references: { pipeline: "pipeline" },
296
+ roles: {
297
+ organization: { targets: ORGANIZATION, column: "organization_id" },
298
+ person: { targets: ["person"], many: true, relationship: "related-to", kept: true },
299
+ project: { targets: ["project"], column: "project_id" },
300
+ deal: { targets: ["deal"], column: "deal_id" },
301
+ },
302
+ },
303
+ message: {
304
+ table: "messages",
305
+ review: "direct",
306
+ update: "in-place",
307
+ tombstone: true,
308
+ importOnly: true,
309
+ fields: objectOf({
310
+ kind: enumOf(["sms"], "sms: a text message of the phone"),
311
+ direction: enumOf(SMS_DIRECTIONS, "in: the other side wrote it; out: the owner did"),
312
+ ...phoneFields,
313
+ thread: text("The phone's thread id; the number when left out", 512),
314
+ body: { type: "string", maxLength: 20_000, description: "The text" },
315
+ sent_at: instant("When it was sent"),
316
+ read: { type: "boolean", description: "An incoming message the owner opened" },
317
+ }, { required: ["kind", "direction", "phone", "body", "sent_at"] }),
318
+ roles: COUNTERPART,
319
+ },
320
+ event: {
321
+ table: "events",
322
+ review: "direct",
323
+ update: "in-place",
324
+ tombstone: true,
325
+ importOnly: true,
326
+ fields: objectOf({
327
+ kind: enumOf(["call"], "call: a phone call"),
328
+ direction: enumOf(CALL_DIRECTIONS, "in, out, or missed: a call the owner did not take"),
329
+ ...phoneFields,
330
+ started_at: instant("When it rang"),
331
+ duration: { type: "integer", minimum: 0, maximum: 604_800, description: "Seconds talked; 0 when missed" },
332
+ }, { required: ["kind", "direction", "phone", "started_at"] }),
333
+ roles: COUNTERPART,
334
+ },
335
+ pipeline: {
336
+ table: "pipelines",
337
+ review: "direct",
338
+ update: "in-place",
339
+ tombstone: false,
340
+ fields: objectOf({
341
+ kind: enumOf(DEAL_KINDS, "deal when left out"),
342
+ name: text("The pipeline's name", 80),
343
+ stages: arrayOf(objectOf({
344
+ external_id: text("The stage's id at the source", 512),
345
+ name: text("The stage's name", 80),
346
+ position: { type: "integer", minimum: 0, maximum: 1000 },
347
+ outcome: enumOf(DEAL_STATUSES, "open when left out"),
348
+ }, { required: ["external_id", "name"] }), 30, "The stages in order"),
349
+ }, { required: ["name", "stages"] }),
350
+ roles: {},
351
+ nested: { stages: { table: "pipeline_stages", parent: "pipeline_id" } },
13
352
  },
14
- person: { table: "persons", review: "identity", roles: {} },
15
- organization: { table: "organizations", review: "identity", roles: {} },
16
- project: { table: "projects", review: "identity", roles: {} },
353
+ };
354
+ export const isRecordType = (type) => Object.hasOwn(RECORD_TYPES, type);
355
+ /** A business record (an invoice, a deal, a wallet): not a person, organization or project. */
356
+ export const isBusinessType = (type) => isRecordType(type) && RECORD_TYPES[type].review !== "identity";
357
+ export const isIdentityType = (type) => typeof type === "string" && isRecordType(type) && RECORD_TYPES[type].review === "identity";
358
+ export const RECORD_ENTITY_TYPES = Object.keys(RECORD_TYPES);
359
+ /** Each registered type's table, for the store's polymorphic pointers. */
360
+ export const RECORD_TABLES = Object.fromEntries(Object.entries(RECORD_TYPES).map(([type, spec]) => [type, spec.table]));
361
+ const decidedOf = (types) => Object.fromEntries(Object.entries(types).flatMap(([type, spec]) => spec.review === "proposal" || spec.review === "valued-proposal" ? [[type, spec.table]] : []));
362
+ /**
363
+ * Where the owner's decision on each reviewed type is kept. A subscription's price is decided row by row, so it is
364
+ * one of them though it has no ref of its own.
365
+ */
366
+ export const DECIDED_TABLES = {
367
+ ...decidedOf(RECORD_TYPES),
368
+ subscription_price: "subscription_prices",
17
369
  };
18
370
  /** A role word, a source provider, and the name after `custom:`. */
19
371
  export const WORD_BODY = "[a-z0-9][a-z0-9_-]{0,62}";
20
372
  export const TYPE_PATTERN = `^(${RECORD_ENTITY_TYPES.join("|")}|custom:${WORD_BODY})$`;
21
373
  export const TYPE_RULE = `a type is one of ${RECORD_ENTITY_TYPES.join(", ")} or custom:<name>`;
22
374
  export const rolesOf = (type) => Object.entries(RECORD_TYPES[type].roles);
375
+ export const roleOf = (type, role) => {
376
+ if (!isRecordType(type))
377
+ return undefined;
378
+ const roles = RECORD_TYPES[type].roles;
379
+ return Object.hasOwn(roles, role) ? roles[role] : undefined;
380
+ };
381
+ export const nestedOf = (type) => Object.entries(RECORD_TYPES[type].nested ?? {});
382
+ export const referencesOf = (type) => Object.entries(RECORD_TYPES[type].references ?? {});
23
383
  export const roleNames = (types, keep) => [
24
384
  ...new Set(types.flatMap((type) => rolesOf(type).flatMap(([name, role]) => (keep(role) ? [name] : [])))),
25
385
  ];
26
386
  export const AMOUNT_ROLES = roleNames(RECORD_ENTITY_TYPES, (role) => role.amount);
387
+ /** v1's roles of a type, which `fields.<role>` may also name: v1 refused such a field when it was not text. */
388
+ export const FIELD_REFERENCE_ROLES = roleNames(RECORD_ENTITY_TYPES, (role) => role.v1 && !role.many);
389
+ /** The types whose records v1 projected: their v1 roles keep v1's reading. */
390
+ export const hasV1Roles = (type) => rolesOf(type).some(([, role]) => role.v1);
391
+ export const IDENTITY_TYPES = RECORD_ENTITY_TYPES.filter((type) => RECORD_TYPES[type].review === "identity");
392
+ /** `a` or `an` before a type word: `an event`, `an invoice`, `a message`. */
393
+ export const article = (word) => (/^[aeiou]/.test(word) ? "an" : "a");
@@ -5,11 +5,26 @@ export type RecordOperation = (typeof RECORD_OPERATIONS)[number];
5
5
  export interface RecordRelation {
6
6
  /** What the target is to this record: `counterparty`, `payer`, `settles`, or a source's own word. */
7
7
  role: string;
8
- type: string;
9
- external_id: string;
8
+ /** With `external_id`: another record of this account. Left out when `ref` names a store entity instead. */
9
+ type?: string;
10
+ external_id?: string;
11
+ /** A store entity, such as `organization:12`, in place of `type` and `external_id`. */
12
+ ref?: string;
13
+ /** On a relationship role: the part the target plays, such as `decision-maker` or `customer`. */
14
+ as?: string;
10
15
  /** A `settles` relation's share of the payment, as a decimal in its currency; without one, as far as both allow. */
11
16
  amount?: string;
12
17
  }
18
+ /** How an envelope's lines read `product`: `text` keeps an invoice line's product as data, as v1 did. */
19
+ export declare const LINE_OPTIONS: readonly ["text", "products"];
20
+ export interface EnvelopeOptions {
21
+ lines?: (typeof LINE_OPTIONS)[number];
22
+ /**
23
+ * The country a phone number without `+` is read in, such as `es`. Without it such a number is an identifier only
24
+ * when exactly one country of the store's `phone_countries` setting reads it as valid.
25
+ */
26
+ phone_region?: string;
27
+ }
13
28
  export interface EnvelopeRecord {
14
29
  /** The caller's name for this line, unique in the envelope; the receipt cites it. */
15
30
  key: string;
@@ -29,6 +44,7 @@ export interface EnvelopeSource {
29
44
  export interface RecordsEnvelope {
30
45
  schema_version: 1;
31
46
  source: EnvelopeSource;
47
+ options?: EnvelopeOptions;
32
48
  records: EnvelopeRecord[];
33
49
  }
34
50
  /** A refusal: `pointer` is an RFC 6901 JSON pointer into the envelope, `code` a stable word callers may branch on. */
@@ -53,6 +69,9 @@ export declare const RECORDS_ENVELOPE_SCHEMA: {
53
69
  readonly source: {
54
70
  readonly $ref: "#/$defs/source";
55
71
  };
72
+ readonly options: {
73
+ readonly $ref: "#/$defs/options";
74
+ };
56
75
  readonly records: {
57
76
  readonly type: "array";
58
77
  readonly items: {
@@ -85,6 +104,21 @@ export declare const RECORDS_ENVELOPE_SCHEMA: {
85
104
  };
86
105
  };
87
106
  };
107
+ readonly options: {
108
+ readonly type: "object";
109
+ readonly additionalProperties: false;
110
+ readonly properties: {
111
+ readonly lines: {
112
+ readonly enum: readonly ["text", "products"];
113
+ readonly description: string;
114
+ };
115
+ readonly phone_region: {
116
+ readonly type: "string";
117
+ readonly pattern: "^[A-Za-z]{2}$";
118
+ readonly description: string;
119
+ };
120
+ };
121
+ };
88
122
  readonly record: {
89
123
  readonly type: "object";
90
124
  readonly required: readonly ["key", "type", "external_id", "operation"];
@@ -124,11 +158,33 @@ export declare const RECORDS_ENVELOPE_SCHEMA: {
124
158
  };
125
159
  };
126
160
  };
161
+ readonly allOf: {
162
+ if: {
163
+ properties: {
164
+ type: {
165
+ const: "contract" | "deal" | "event" | "invoice" | "lead" | "message" | "order" | "organization" | "payment" | "person" | "pipeline" | "product" | "project" | "subscription" | "transaction" | "wallet";
166
+ };
167
+ };
168
+ required: string[];
169
+ };
170
+ then: {
171
+ properties: {
172
+ fields: {
173
+ $ref: string;
174
+ };
175
+ };
176
+ };
177
+ }[];
127
178
  };
128
179
  readonly relation: {
129
180
  readonly type: "object";
130
- readonly required: readonly ["role", "type", "external_id"];
181
+ readonly required: readonly ["role"];
131
182
  readonly additionalProperties: false;
183
+ readonly oneOf: readonly [{
184
+ readonly required: readonly ["type", "external_id"];
185
+ }, {
186
+ readonly required: readonly ["ref"];
187
+ }];
132
188
  readonly properties: {
133
189
  readonly role: {
134
190
  readonly type: "string";
@@ -143,6 +199,16 @@ export declare const RECORDS_ENVELOPE_SCHEMA: {
143
199
  readonly minLength: 1;
144
200
  readonly maxLength: 512;
145
201
  };
202
+ readonly ref: {
203
+ readonly type: "string";
204
+ readonly pattern: "^[a-z]+:[^\\s]{1,500}$";
205
+ readonly description: "A store entity, such as organization:12";
206
+ };
207
+ readonly as: {
208
+ readonly type: "string";
209
+ readonly pattern: "^[a-z0-9][a-z0-9_-]{0,62}$";
210
+ readonly description: "On a relationship role: the part the target plays, such as decision-maker";
211
+ };
146
212
  readonly amount: {
147
213
  readonly type: "string";
148
214
  readonly pattern: "^[0-9]{1,16}(\\.[0-9]{1,6})?$";
@@ -153,14 +219,39 @@ export declare const RECORDS_ENVELOPE_SCHEMA: {
153
219
  };
154
220
  };
155
221
  export declare const pointerOf: (...segments: (string | number)[]) => string;
222
+ /** The type a relation points at: its own `type`, or the prefix of its `ref`. */
223
+ export declare const targetTypeOf: (relation: RecordRelation) => string;
156
224
  /** Why one record cannot be taken, or nothing; `index` is its place in `records`. */
157
225
  export declare const recordErrors: (record: unknown, index: number) => IngestError[];
158
226
  /** The envelope's own errors (not its records'): when there is one, no record is taken. */
159
227
  export declare const envelopeErrors: (envelope: unknown, account: string) => IngestError[];
228
+ /** A JSONL stream's first line, or an envelope without its records: `schema_version`, `source` and `options`. */
229
+ export declare const headerErrors: (envelope: unknown, account: string, more?: string[]) => IngestError[];
160
230
  /** The same fields and relations in any key or relation order give the same fingerprint. */
161
231
  export declare const recordFingerprint: (fields: Record<string, unknown>, relations: RecordRelation[]) => string;
232
+ /** A file's text without the byte-order mark a spreadsheet export puts before it. */
233
+ export declare const withoutBom: (text: string) => string;
234
+ /** One JSONL line as JSON; a line that is not JSON comes back as the text, for the record check to refuse at its index. */
235
+ export declare const jsonLine: (line: string) => unknown;
162
236
  /**
163
- * A JSONL import as the envelope it stands for: the first line holds `schema_version` and `source`, each further
164
- * non-empty line one record. A line that is not JSON comes back as a record the envelope check refuses, at its index.
237
+ * A JSONL import as the envelope it stands for: the first line holds `schema_version`, `source` and `options`, each
238
+ * further non-empty line one record.
165
239
  */
166
240
  export declare const envelopeOfJsonl: (text: string) => unknown;
241
+ /**
242
+ * A file's text as the envelope it stands for: one JSON envelope, or JSONL, told apart by the first non-empty line;
243
+ * a JSON object holding `records` is a whole envelope.
244
+ */
245
+ export declare const envelopeOfText: (text: string) => unknown;
246
+ /** A reference a record's fields make to another record of the account, kept so the record waits for it. */
247
+ export interface FieldReference {
248
+ /** `fields.<key>` or `fields.<list>.<key>`: a dot keeps it apart from any role a source can send. */
249
+ role: string;
250
+ type: string;
251
+ external_id: string;
252
+ }
253
+ /**
254
+ * What a record's fields name by external id: a deal's pipeline, a line's product. An invoice's line products only
255
+ * with `options.lines: products`, since v1 kept them as data.
256
+ */
257
+ export declare const fieldReferences: (record: EnvelopeRecord, options?: EnvelopeOptions) => FieldReference[];