@wirecat/cli-core 0.605.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.
- package/CHANGELOG.md +284 -1
- package/dist/calendar/index.d.ts +1 -0
- package/dist/calendar/index.js +1 -0
- package/dist/calendar/reads.js +5 -0
- package/dist/cli/app-mcp-command.d.ts +6 -2
- package/dist/cli/app-mcp-command.js +11 -2
- package/dist/cli/csv.d.ts +10 -0
- package/dist/cli/csv.js +22 -0
- package/dist/cli/early-refusal.d.ts +10 -0
- package/dist/cli/early-refusal.js +19 -0
- package/dist/cli/index.d.ts +4 -1
- package/dist/cli/index.js +4 -0
- package/dist/cli/profile-token.d.ts +29 -0
- package/dist/cli/profile-token.js +65 -0
- package/dist/cli/program.js +4 -2
- package/dist/cli/store-settings.js +38 -4
- package/dist/crm/cli/csv.d.ts +23 -0
- package/dist/crm/cli/csv.js +135 -0
- package/dist/crm/cli/index.d.ts +1 -1
- package/dist/crm/cli/index.js +36 -22
- package/dist/crm/cli/pipeline-view.d.ts +3 -0
- package/dist/crm/cli/pipeline-view.js +36 -0
- package/dist/crm/index.d.ts +2 -2
- package/dist/crm/index.js +1 -1
- package/dist/crm/source.d.ts +35 -5
- package/dist/crm/sync.d.ts +15 -7
- package/dist/crm/sync.js +169 -11
- package/dist/crm/writes.d.ts +6 -4
- package/dist/crm/writes.js +17 -13
- package/dist/documents.d.ts +2 -0
- package/dist/documents.js +2 -0
- package/dist/domain/business.d.ts +79 -0
- package/dist/domain/business.js +42 -0
- package/dist/domain/crm.d.ts +41 -2
- package/dist/domain/deals.d.ts +5 -2
- package/dist/domain/deals.js +5 -2
- package/dist/domain/finance.d.ts +2 -0
- package/dist/domain/finance.js +2 -2
- package/dist/domain/google-documents.d.ts +2 -0
- package/dist/domain/google-documents.js +3 -1
- package/dist/domain/money.d.ts +2 -0
- package/dist/domain/money.js +11 -3
- package/dist/domain/providers.d.ts +36 -1
- package/dist/domain/providers.js +69 -7
- package/dist/domain/record-fields.d.ts +42 -0
- package/dist/domain/record-fields.js +122 -0
- package/dist/domain/record-types.d.ts +489 -16
- package/dist/domain/record-types.js +374 -7
- package/dist/domain/records.d.ts +96 -5
- package/dist/domain/records.js +178 -32
- package/dist/domain/ref-summaries.js +5 -0
- package/dist/domain/references.d.ts +1 -1
- package/dist/domain/references.js +5 -0
- package/dist/domain/relations.d.ts +125 -0
- package/dist/domain/relations.js +107 -0
- package/dist/domain/vcard.d.ts +25 -0
- package/dist/domain/vcard.js +267 -0
- package/dist/entities/deal.d.ts +16 -2
- package/dist/entities/deal.js +5 -1
- package/dist/entities/index.d.ts +29 -13
- package/dist/entities/relationship.d.ts +8 -2
- package/dist/entities/relationship.js +8 -2
- package/dist/entities/task-results.d.ts +10 -10
- package/dist/errors/reasons.js +73 -2
- package/dist/fetch/addresses.d.ts +5 -0
- package/dist/fetch/addresses.js +76 -0
- package/dist/fetch/page.d.ts +64 -0
- package/dist/fetch/page.js +256 -0
- package/dist/index.d.ts +7 -4
- package/dist/index.js +6 -3
- package/dist/mail/cli/review.js +20 -6
- package/dist/mail/index.d.ts +1 -1
- package/dist/mail/marks.d.ts +10 -5
- package/dist/mail/marks.js +16 -8
- package/dist/mail/review.d.ts +5 -0
- package/dist/meetings/calendar-text.d.ts +1 -1
- package/dist/meetings/calendar-text.js +4 -0
- package/dist/meetings/event-store.d.ts +2 -2
- package/dist/search/account-list.d.ts +4 -0
- package/dist/search/account-list.js +19 -8
- package/dist/search/query.js +2 -2
- package/dist/services/events-search.js +2 -2
- package/dist/services/index.d.ts +1 -1
- package/dist/services/index.js +1 -1
- package/dist/services/ingest.d.ts +56 -8
- package/dist/services/ingest.js +95 -61
- package/dist/services/link-scorers/scan.js +2 -1
- package/dist/services/link-scorers/shared.js +2 -1
- package/dist/services/messages-search.js +3 -2
- package/dist/services/metadata.d.ts +1 -1
- package/dist/services/notes-search.js +1 -0
- package/dist/services/project-status/index.js +2 -1
- package/dist/services/proposals/kinds/contracts.d.ts +4 -0
- package/dist/services/proposals/kinds/contracts.js +9 -0
- package/dist/services/proposals/kinds/finance.d.ts +4 -0
- package/dist/services/proposals/kinds/finance.js +14 -35
- package/dist/services/proposals/kinds/orders.js +7 -30
- package/dist/services/proposals/kinds/shared.d.ts +27 -1
- package/dist/services/proposals/kinds/shared.js +34 -0
- package/dist/services/proposals/kinds/source-review.d.ts +4 -0
- package/dist/services/proposals/kinds/source-review.js +45 -0
- package/dist/services/proposals/kinds/subscriptions.d.ts +9 -0
- package/dist/services/proposals/kinds/subscriptions.js +24 -0
- package/dist/services/proposals/kinds/transactions.d.ts +4 -0
- package/dist/services/proposals/kinds/transactions.js +10 -0
- package/dist/services/proposals/registry.js +9 -0
- package/dist/services/search-all-meetings.js +2 -2
- package/dist/services/search-all.js +4 -3
- package/dist/services/search-kind.js +3 -3
- package/dist/services/stored-project-gathering.js +4 -3
- package/dist/store/decisions.js +5 -1
- package/dist/store/exports.d.ts +1 -0
- package/dist/store/exports.js +1 -0
- package/dist/store/index.d.ts +20 -4
- package/dist/store/index.js +4 -0
- package/dist/store/people.js +1 -1
- package/dist/store/project-transfer/request.d.ts +4 -1
- package/dist/store/project-transfer/schema.d.ts +9 -2
- package/dist/store/project-transfer/schema.js +4 -0
- package/dist/store/project-transfer/snapshot-model.d.ts +8 -2
- package/dist/store/source-review.d.ts +64 -0
- package/dist/store/source-review.js +7 -0
- package/dist/store/sqlite/accounts.d.ts +1 -1
- package/dist/store/sqlite/accounts.js +24 -1
- package/dist/store/sqlite/added-sources.d.ts +82 -0
- package/dist/store/sqlite/added-sources.js +237 -0
- package/dist/store/sqlite/business-contracts.d.ts +90 -0
- package/dist/store/sqlite/business-contracts.js +182 -0
- package/dist/store/sqlite/calendar-events.js +7 -3
- package/dist/store/sqlite/calendar-forget.js +3 -0
- package/dist/store/sqlite/calendar.d.ts +21 -1
- package/dist/store/sqlite/calendar.js +27 -7
- package/dist/store/sqlite/chat-rekey.js +7 -0
- package/dist/store/sqlite/chats.d.ts +3 -1
- package/dist/store/sqlite/chats.js +15 -1
- package/dist/store/sqlite/crm-rows.d.ts +44 -0
- package/dist/store/sqlite/crm-rows.js +195 -0
- package/dist/store/sqlite/crm-settings.d.ts +28 -0
- package/dist/store/sqlite/crm-settings.js +80 -0
- package/dist/store/sqlite/crm.d.ts +42 -11
- package/dist/store/sqlite/crm.js +402 -196
- package/dist/store/sqlite/deals.d.ts +4 -8
- package/dist/store/sqlite/deals.js +8 -5
- package/dist/store/sqlite/document-transfer/snapshot.js +2 -2
- package/dist/store/sqlite/emails.js +2 -0
- package/dist/store/sqlite/entity-types.d.ts +14 -9
- package/dist/store/sqlite/entity-types.js +4 -10
- package/dist/store/sqlite/evidence-signature.d.ts +114 -0
- package/dist/store/sqlite/evidence-signature.js +349 -0
- package/dist/store/sqlite/evidence-sources.d.ts +2 -1
- package/dist/store/sqlite/evidence-sources.js +7 -3
- package/dist/store/sqlite/facts.d.ts +7 -2
- package/dist/store/sqlite/facts.js +21 -5
- package/dist/store/sqlite/finance.d.ts +18 -105
- package/dist/store/sqlite/finance.js +100 -219
- package/dist/store/sqlite/involvement-queue.js +4 -3
- package/dist/store/sqlite/knowledge.d.ts +14 -4
- package/dist/store/sqlite/knowledge.js +108 -102
- package/dist/store/sqlite/labelled.d.ts +3 -0
- package/dist/store/sqlite/labelled.js +11 -0
- package/dist/store/sqlite/link-kinds.d.ts +4 -3
- package/dist/store/sqlite/link-kinds.js +6 -2
- package/dist/store/sqlite/manifest.js +7 -0
- package/dist/store/sqlite/memories.d.ts +24 -10
- package/dist/store/sqlite/memories.js +73 -19
- package/dist/store/sqlite/messages.js +2 -0
- package/dist/store/sqlite/migrations.generated.js +134 -0
- package/dist/store/sqlite/money-lines.d.ts +48 -0
- package/dist/store/sqlite/money-lines.js +76 -0
- package/dist/store/sqlite/money-transactions.d.ts +132 -0
- package/dist/store/sqlite/money-transactions.js +295 -0
- package/dist/store/sqlite/notes.d.ts +14 -4
- package/dist/store/sqlite/notes.js +43 -31
- package/dist/store/sqlite/orders.d.ts +36 -6
- package/dist/store/sqlite/orders.js +126 -46
- package/dist/store/sqlite/organizations.d.ts +8 -0
- package/dist/store/sqlite/organizations.js +14 -0
- package/dist/store/sqlite/payment-facts.js +1 -1
- package/dist/store/sqlite/person-preparation.d.ts +1 -1
- package/dist/store/sqlite/person-preparation.js +3 -2
- package/dist/store/sqlite/private-people.js +2 -0
- package/dist/store/sqlite/products.d.ts +78 -0
- package/dist/store/sqlite/products.js +131 -0
- package/dist/store/sqlite/project-transfer/records.js +39 -3
- package/dist/store/sqlite/project-transfer/snapshot.d.ts +4 -1
- package/dist/store/sqlite/project-transfer/snapshot.js +11 -3
- package/dist/store/sqlite/proposed-actions.d.ts +1 -1
- package/dist/store/sqlite/proposed-actions.js +24 -7
- package/dist/store/sqlite/providers.d.ts +0 -4
- package/dist/store/sqlite/providers.js +20 -2
- package/dist/store/sqlite/record-matching.d.ts +80 -0
- package/dist/store/sqlite/record-matching.js +264 -0
- package/dist/store/sqlite/record-projection.d.ts +89 -0
- package/dist/store/sqlite/record-projection.js +50 -0
- package/dist/store/sqlite/record-projectors-direct.d.ts +17 -0
- package/dist/store/sqlite/record-projectors-direct.js +125 -0
- package/dist/store/sqlite/record-projectors-money.d.ts +26 -0
- package/dist/store/sqlite/record-projectors-money.js +214 -0
- package/dist/store/sqlite/record-projectors-phone.d.ts +28 -0
- package/dist/store/sqlite/record-projectors-phone.js +212 -0
- package/dist/store/sqlite/record-projectors.d.ts +3 -44
- package/dist/store/sqlite/record-projectors.js +7 -67
- package/dist/store/sqlite/record-relations.d.ts +24 -0
- package/dist/store/sqlite/record-relations.js +92 -0
- package/dist/store/sqlite/record-remap.d.ts +12 -0
- package/dist/store/sqlite/record-remap.js +92 -0
- package/dist/store/sqlite/record-run.d.ts +71 -0
- package/dist/store/sqlite/record-run.js +179 -0
- package/dist/store/sqlite/record-settling.d.ts +9 -0
- package/dist/store/sqlite/record-settling.js +74 -0
- package/dist/store/sqlite/record-stamps.d.ts +13 -0
- package/dist/store/sqlite/record-stamps.js +30 -0
- package/dist/store/sqlite/record-writes.d.ts +43 -0
- package/dist/store/sqlite/record-writes.js +307 -0
- package/dist/store/sqlite/records.d.ts +52 -42
- package/dist/store/sqlite/records.js +123 -353
- package/dist/store/sqlite/ref-summaries.d.ts +6 -5
- package/dist/store/sqlite/ref-summaries.js +60 -8
- package/dist/store/sqlite/refunds.d.ts +31 -0
- package/dist/store/sqlite/refunds.js +112 -0
- package/dist/store/sqlite/relation-list.d.ts +43 -0
- package/dist/store/sqlite/relation-list.js +133 -0
- package/dist/store/sqlite/relation-proposals.d.ts +28 -6
- package/dist/store/sqlite/relation-proposals.js +134 -10
- package/dist/store/sqlite/relation-reads.d.ts +8 -0
- package/dist/store/sqlite/relation-reads.js +7 -0
- package/dist/store/sqlite/relation-roles.d.ts +26 -0
- package/dist/store/sqlite/relation-roles.js +91 -0
- package/dist/store/sqlite/reported-relations.d.ts +63 -0
- package/dist/store/sqlite/reported-relations.js +132 -0
- package/dist/store/sqlite/reviewed.d.ts +109 -0
- package/dist/store/sqlite/reviewed.js +173 -0
- package/dist/store/sqlite/schema/link-revisions.d.ts +136 -0
- package/dist/store/sqlite/schema/link-revisions.js +11 -0
- package/dist/store/sqlite/schema/links.d.ts +360 -0
- package/dist/store/sqlite/schema/links.js +32 -0
- package/dist/store/sqlite/schema.d.ts +2769 -306
- package/dist/store/sqlite/schema.js +258 -25
- package/dist/store/sqlite/source-review-marks.d.ts +54 -0
- package/dist/store/sqlite/source-review-marks.js +143 -0
- package/dist/store/sqlite/source-review.d.ts +54 -0
- package/dist/store/sqlite/source-review.js +221 -0
- package/dist/store/sqlite/stage-amounts.d.ts +14 -0
- package/dist/store/sqlite/stage-amounts.js +23 -0
- package/dist/store/sqlite/subscriptions.d.ts +135 -0
- package/dist/store/sqlite/subscriptions.js +294 -0
- package/dist/store/sqlite/text-input.d.ts +3 -0
- package/dist/store/sqlite/text-input.js +13 -0
- package/dist/store/sqlite/things.js +2 -9
- package/dist/store/sqlite/transaction.d.ts +15 -0
- package/dist/store/sqlite/transaction.js +57 -0
- package/dist/store/sqlite/wallets.d.ts +88 -0
- package/dist/store/sqlite/wallets.js +143 -0
- package/dist/store/store-errors.d.ts +1 -0
- package/dist/store/store-errors.js +45 -0
- package/dist/store/store.d.ts +19 -1
- package/dist/store/store.js +35 -6
- package/package.json +6 -3
- package/schemas/examples/records-business.jsonl +11 -0
- package/schemas/records-envelope.v1.schema.json +1160 -5
package/dist/crm/sync.js
CHANGED
|
@@ -46,22 +46,128 @@ export const parseCrmCursor = (value) => {
|
|
|
46
46
|
export const cursorName = (kind) => `crm:${kind}`;
|
|
47
47
|
const CONTINUE = "run sync again to continue";
|
|
48
48
|
const fresh = () => ({ v: 1, token: null, round: null, done: 0, updatedAt: null, pass: null, passAt: 0 });
|
|
49
|
+
const GAPS = "crm:gaps";
|
|
50
|
+
/** The gaps the account's last sync recorded; none before its first. */
|
|
51
|
+
export const crmGapsOf = async (store, account) => {
|
|
52
|
+
try {
|
|
53
|
+
const value = JSON.parse((await store.sync.syncState(account, GAPS))?.value ?? "[]");
|
|
54
|
+
return Array.isArray(value) ? value : [];
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
/** Thrown by `CrmCallContext.spend` when the budget is spent; the engine turns it into a `partial` stop. */
|
|
61
|
+
class BudgetSpent extends Error {
|
|
62
|
+
why;
|
|
63
|
+
constructor(why) {
|
|
64
|
+
super(`the run's budget is spent: ${why}`);
|
|
65
|
+
this.why = why;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** The scope a refusal names, when the adapter put one in its details. */
|
|
69
|
+
const scopeOf = (error) => {
|
|
70
|
+
const scope = error instanceof CliError ? error.details?.scope : undefined;
|
|
71
|
+
return typeof scope === "string" && scope ? scope : undefined;
|
|
72
|
+
};
|
|
73
|
+
const refusal = (kind, consequence, error, provider) => {
|
|
74
|
+
// The adapter's own text may carry a request; only its failure code and scope are copied.
|
|
75
|
+
const code = error instanceof CliError ? error.code : "error";
|
|
76
|
+
const scope = scopeOf(error);
|
|
77
|
+
const message = scope
|
|
78
|
+
? `${scope} scope missing: ${consequence}`
|
|
79
|
+
: `${provider} did not list ${kind} (${code}): ${consequence}`;
|
|
80
|
+
return { kind, reason: "refused", code, ...(scope ? { scope } : {}), message };
|
|
81
|
+
};
|
|
49
82
|
const readPipelines = async (run, kind) => {
|
|
50
83
|
const why = run.budget.take();
|
|
51
84
|
if (why)
|
|
52
85
|
return why;
|
|
53
|
-
const pipelines = await run.source.pipelines(kind);
|
|
86
|
+
const pipelines = await run.source.pipelines(kind, run.context);
|
|
54
87
|
const { saved, skipped } = await run.store.crm.savePipelines(run.account, kind, pipelines);
|
|
55
88
|
run.report.pipelines += saved;
|
|
56
89
|
for (const one of skipped)
|
|
57
90
|
run.report.issues.push({ kind, ...one });
|
|
58
91
|
return null;
|
|
59
92
|
};
|
|
93
|
+
const noted = (run, kind, saved) => {
|
|
94
|
+
for (const { externalId, ...one } of saved.issues ?? [])
|
|
95
|
+
run.report.issues.push({ kind, ...one, ...(externalId ? { externalId } : {}) });
|
|
96
|
+
run.report.skippedOwnerRejected += saved.skippedOwnerRejected ?? 0;
|
|
97
|
+
run.unlistedUsers += saved.unlistedUsers ?? 0;
|
|
98
|
+
run.droppedPending += saved.droppedPending ?? 0;
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* The account's users and departments, listed whole each run: before deals, so a deal's owner is a person. A CRM that
|
|
102
|
+
* cannot list departments says so, and no reporting line is touched.
|
|
103
|
+
*/
|
|
104
|
+
const readUsers = async (run) => {
|
|
105
|
+
const { source } = run;
|
|
106
|
+
const gap = (one) => {
|
|
107
|
+
run.gaps.push(one);
|
|
108
|
+
run.report.issues.push({ kind: "user", reason: "crm.not-reported", message: one.message });
|
|
109
|
+
};
|
|
110
|
+
if (!source.users) {
|
|
111
|
+
run.gaps.push({
|
|
112
|
+
kind: "users",
|
|
113
|
+
reason: "not-listed",
|
|
114
|
+
message: `${source.provider} lists no users: deal owners, employees and reporting lines are unknown`,
|
|
115
|
+
});
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
const why = run.budget.take();
|
|
119
|
+
if (why)
|
|
120
|
+
return why;
|
|
121
|
+
let users;
|
|
122
|
+
try {
|
|
123
|
+
users = await source.users(run.context);
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
if (run.signal?.aborted || error instanceof BudgetSpent)
|
|
127
|
+
throw error;
|
|
128
|
+
gap(refusal("users", "nothing stored about them changed", error, source.provider));
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
let departments;
|
|
132
|
+
if (!source.departments)
|
|
133
|
+
gap({
|
|
134
|
+
kind: "departments",
|
|
135
|
+
reason: "not-listed",
|
|
136
|
+
message: `${source.provider} lists no departments: who reports to whom is unknown`,
|
|
137
|
+
});
|
|
138
|
+
else {
|
|
139
|
+
const stop = run.budget.take();
|
|
140
|
+
if (stop)
|
|
141
|
+
return stop;
|
|
142
|
+
try {
|
|
143
|
+
departments = await source.departments(run.context);
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
if (run.signal?.aborted || error instanceof BudgetSpent)
|
|
147
|
+
throw error;
|
|
148
|
+
gap(refusal("departments", "reporting lines unknown", error, source.provider));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
noted(run, "user", await run.store.crm.saveUsers(run.account, users, departments));
|
|
152
|
+
run.report.users = users.length;
|
|
153
|
+
return null;
|
|
154
|
+
};
|
|
155
|
+
/** Runs a step whose source call may spend the budget mid-way: a spent budget is a stop, not a failure. */
|
|
156
|
+
const stopping = async (step) => {
|
|
157
|
+
try {
|
|
158
|
+
return await step();
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
if (error instanceof BudgetSpent)
|
|
162
|
+
return error.why;
|
|
163
|
+
throw error;
|
|
164
|
+
}
|
|
165
|
+
};
|
|
60
166
|
const readFields = async (run, kind) => {
|
|
61
167
|
const why = run.budget.take();
|
|
62
168
|
if (why)
|
|
63
169
|
return why;
|
|
64
|
-
const fields = await run.source.fields.call(run.source, kind);
|
|
170
|
+
const fields = await run.source.fields.call(run.source, kind, run.context);
|
|
65
171
|
const { skipped } = await run.store.crm.saveFields(run.account, kind, fields);
|
|
66
172
|
for (const one of skipped)
|
|
67
173
|
run.report.issues.push({ kind, ...one });
|
|
@@ -74,17 +180,22 @@ const savePage = async (run, kind, page) => {
|
|
|
74
180
|
const saved = kind === "person"
|
|
75
181
|
? await store.crm.savePeople(account, page.items)
|
|
76
182
|
: await store.crm.saveOrganizations(account, page.items);
|
|
183
|
+
noted(run, kind, saved);
|
|
77
184
|
return { saved: saved.saved, stop: null };
|
|
78
185
|
}
|
|
79
186
|
const items = page.items;
|
|
80
|
-
|
|
187
|
+
const users = { users: run.source.users !== undefined };
|
|
188
|
+
const first = await store.crm.saveItems(account, kind, items, users);
|
|
189
|
+
noted(run, kind, first);
|
|
190
|
+
let { saved, skipped } = first;
|
|
81
191
|
let stop = null;
|
|
82
192
|
if (skipped.some(({ reason }) => reason === "crm.stage-unknown") && !run.pipelinesReread.has(kind)) {
|
|
83
193
|
run.pipelinesReread.add(kind);
|
|
84
194
|
stop = await readPipelines(run, kind);
|
|
85
195
|
if (!stop) {
|
|
86
196
|
const again = new Set(skipped.filter(({ reason }) => reason === "crm.stage-unknown").map(({ externalId }) => externalId));
|
|
87
|
-
const retried = await store.crm.saveItems(account, kind, items.filter(({ externalId }) => again.has(externalId)));
|
|
197
|
+
const retried = await store.crm.saveItems(account, kind, items.filter(({ externalId }) => again.has(externalId)), users);
|
|
198
|
+
noted(run, kind, retried);
|
|
88
199
|
saved += retried.saved;
|
|
89
200
|
skipped = [...skipped.filter(({ reason }) => reason !== "crm.stage-unknown"), ...retried.skipped];
|
|
90
201
|
}
|
|
@@ -116,7 +227,7 @@ const syncKind = async (run, kind, options, result) => {
|
|
|
116
227
|
const why = budget.take();
|
|
117
228
|
if (why)
|
|
118
229
|
return why;
|
|
119
|
-
const page = await source.changes(kind, round.from, round.page ?? undefined);
|
|
230
|
+
const page = await source.changes(kind, round.from, round.page ?? undefined, run.context);
|
|
120
231
|
if (signal?.aborted)
|
|
121
232
|
return "cancelled";
|
|
122
233
|
if ("reset" in page) {
|
|
@@ -164,7 +275,7 @@ const syncKind = async (run, kind, options, result) => {
|
|
|
164
275
|
await save({ pass });
|
|
165
276
|
return why;
|
|
166
277
|
}
|
|
167
|
-
const page = await ids.call(source, kind, pass.page ?? undefined);
|
|
278
|
+
const page = await ids.call(source, kind, pass.page ?? undefined, run.context);
|
|
168
279
|
if (signal?.aborted)
|
|
169
280
|
return "cancelled";
|
|
170
281
|
pass = { ...pass, seen: [...pass.seen, ...page.ids], page: page.nextPage ?? null };
|
|
@@ -195,9 +306,10 @@ const syncKind = async (run, kind, options, result) => {
|
|
|
195
306
|
return null;
|
|
196
307
|
};
|
|
197
308
|
/**
|
|
198
|
-
* Brings one CRM account's copy up to date: both kinds' pipelines and each kind's fields, then
|
|
199
|
-
* deals and leads, each from its cursor or listed in full, a page at a time,
|
|
200
|
-
* the cursor, so a run the budget stops, or one that fails, goes on from
|
|
309
|
+
* Brings one CRM account's copy up to date: both kinds' pipelines and each kind's fields, then companies, contacts,
|
|
310
|
+
* the account's users and departments, deals and leads, each from its cursor or listed in full, a page at a time,
|
|
311
|
+
* each page saved with the round's place in the cursor, so a run the budget stops, or one that fails, goes on from
|
|
312
|
+
* there. Relationships held back for an end not stored yet are written at the end of the run. Deletions come from `changes()`, or
|
|
201
313
|
* from a list of every id that marks only after its last page arrived, and never more than half of a kind without
|
|
202
314
|
* `--rescan`.
|
|
203
315
|
*/
|
|
@@ -208,6 +320,7 @@ export const syncCrm = async (source, store, account, options = {}) => {
|
|
|
208
320
|
provider: source.provider,
|
|
209
321
|
account: account.account,
|
|
210
322
|
pipelines: 0,
|
|
323
|
+
skippedOwnerRejected: 0,
|
|
211
324
|
synced: [],
|
|
212
325
|
issues: [],
|
|
213
326
|
complete: true,
|
|
@@ -227,23 +340,44 @@ export const syncCrm = async (source, store, account, options = {}) => {
|
|
|
227
340
|
moved: () => {
|
|
228
341
|
steps += 1;
|
|
229
342
|
},
|
|
343
|
+
gaps: CRM_KINDS.filter((kind) => !source.traits.kinds.includes(kind)).map((kind) => ({
|
|
344
|
+
kind,
|
|
345
|
+
reason: "not-listed",
|
|
346
|
+
message: `${source.provider} has no ${kind}s`,
|
|
347
|
+
})),
|
|
348
|
+
context: {
|
|
349
|
+
spend: () => {
|
|
350
|
+
const why = budget.take();
|
|
351
|
+
if (why)
|
|
352
|
+
throw new BudgetSpent(why);
|
|
353
|
+
},
|
|
354
|
+
gap: ({ kind, message, scope }) => run.gaps.push({ kind, reason: "partial", message, ...(scope ? { scope } : {}) }),
|
|
355
|
+
},
|
|
356
|
+
unlistedUsers: 0,
|
|
357
|
+
droppedPending: 0,
|
|
230
358
|
};
|
|
231
359
|
const kinds = CRM_KINDS.filter((kind) => source.traits.kinds.includes(kind));
|
|
232
360
|
let stopped = null;
|
|
233
361
|
for (const kind of DEAL_KINDS)
|
|
234
362
|
if (!stopped && kinds.includes(kind)) {
|
|
235
|
-
stopped = await readPipelines(run, kind);
|
|
363
|
+
stopped = await stopping(() => readPipelines(run, kind));
|
|
236
364
|
if (options.signal?.aborted)
|
|
237
365
|
stopped = "cancelled";
|
|
238
366
|
}
|
|
239
367
|
if (run.source.fields)
|
|
240
368
|
for (const kind of kinds)
|
|
241
369
|
if (!stopped) {
|
|
242
|
-
stopped = await readFields(run, kind);
|
|
370
|
+
stopped = await stopping(() => readFields(run, kind));
|
|
243
371
|
if (options.signal?.aborted)
|
|
244
372
|
stopped = "cancelled";
|
|
245
373
|
}
|
|
374
|
+
let usersRead = false;
|
|
246
375
|
for (const kind of kinds) {
|
|
376
|
+
if (!usersRead && isItemKind(kind)) {
|
|
377
|
+
usersRead = true;
|
|
378
|
+
if (!stopped)
|
|
379
|
+
stopped = await stopping(() => readUsers(run));
|
|
380
|
+
}
|
|
247
381
|
const result = {
|
|
248
382
|
kind,
|
|
249
383
|
round: "changes",
|
|
@@ -258,16 +392,40 @@ export const syncCrm = async (source, store, account, options = {}) => {
|
|
|
258
392
|
continue;
|
|
259
393
|
try {
|
|
260
394
|
stopped = await syncKind(run, kind, options, result);
|
|
395
|
+
// After every company of the run is stored, so one listed before the own company still gets its meaning.
|
|
396
|
+
if (kind === "organization")
|
|
397
|
+
noted(run, kind, await store.crm.saveCompanyTypes(account));
|
|
261
398
|
}
|
|
262
399
|
catch (error) {
|
|
263
400
|
if (options.signal?.aborted) {
|
|
264
401
|
stopped = "cancelled";
|
|
265
402
|
continue;
|
|
266
403
|
}
|
|
404
|
+
if (error instanceof BudgetSpent) {
|
|
405
|
+
stopped = error.why;
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
if (scopeOf(error))
|
|
409
|
+
run.gaps.push(refusal(kind, `no ${kind} was read`, error, source.provider));
|
|
267
410
|
// An adapter's own failure says what went wrong; anything else may carry a request URL with a secret in it.
|
|
268
411
|
result.error = isCliFailure(error) ? error.message : `the ${kind} copy could not be synced`;
|
|
269
412
|
}
|
|
270
413
|
}
|
|
414
|
+
// A relationship held back for an end a later page or kind brought is written now; it costs no request.
|
|
415
|
+
run.droppedPending += (await store.crm.retryPending(account)).dropped;
|
|
416
|
+
if (run.unlistedUsers)
|
|
417
|
+
report.issues.push({
|
|
418
|
+
kind: "deal",
|
|
419
|
+
reason: "crm.not-reported",
|
|
420
|
+
message: `${run.unlistedUsers} deal owner(s) are ${source.provider} users this CRM does not list; no owner was stored for them`,
|
|
421
|
+
});
|
|
422
|
+
if (run.droppedPending)
|
|
423
|
+
report.issues.push({
|
|
424
|
+
kind: "deal",
|
|
425
|
+
reason: "crm.pending-dropped",
|
|
426
|
+
message: `${run.droppedPending} relationship(s) waited too long for a contact, user or company that never came and were dropped`,
|
|
427
|
+
});
|
|
428
|
+
await store.sync.setSyncState(account, GAPS, JSON.stringify(run.gaps));
|
|
271
429
|
report.progress = {
|
|
272
430
|
requests: budget.used,
|
|
273
431
|
saved: report.synced.reduce((sum, one) => sum + one.saved, 0),
|
package/dist/crm/writes.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { AppIdentity } from "../ports/app.js";
|
|
|
5
5
|
import type { Warn } from "../runs/events.js";
|
|
6
6
|
import { type Asker, type GuardSettings } from "../sends/guard.js";
|
|
7
7
|
import { type PermissionKey } from "../sends/permissions.js";
|
|
8
|
-
import type { Deal } from "../store/sqlite/deals.js";
|
|
8
|
+
import type { Deal, PipelineReport } from "../store/sqlite/deals.js";
|
|
9
9
|
import type { MessageStore } from "../store/store.js";
|
|
10
10
|
import type { AccountKey } from "../store/types.js";
|
|
11
11
|
import type { CrmSource } from "./source.js";
|
|
@@ -54,6 +54,8 @@ export interface CrmFieldsInput {
|
|
|
54
54
|
*/
|
|
55
55
|
custom?: readonly (readonly [name: string, value: string])[];
|
|
56
56
|
}
|
|
57
|
+
/** The named pipeline of this profile's CRM account, or its only one; several and none named is refused. */
|
|
58
|
+
export declare const pipelineOf: (context: Pick<CrmWriteContext, "store" | "account">, kind: DealKind | undefined, reference: string | undefined, param?: string) => Promise<PipelineReport>;
|
|
57
59
|
/** What the field lookup reads of a field definition: a CRM CLI's own type fits. */
|
|
58
60
|
export type NamedField = Pick<CrmField, "code" | "label" | "options">;
|
|
59
61
|
/**
|
|
@@ -62,11 +64,11 @@ export type NamedField = Pick<CrmField, "code" | "label" | "options">;
|
|
|
62
64
|
*/
|
|
63
65
|
export declare const foldFieldName: (name: string) => string;
|
|
64
66
|
/** No field by that name: the fields whose code or label holds it, or is held in it, are its candidates. */
|
|
65
|
-
export declare const unknownField: (fields: readonly NamedField[], name: string) => CliError;
|
|
67
|
+
export declare const unknownField: (fields: readonly NamedField[], name: string, param?: string) => CliError;
|
|
66
68
|
/** A field by its code or its whole label; two with that label are refused with both, never guessed. */
|
|
67
|
-
export declare const fieldNamed: <F extends NamedField>(fields: readonly F[], name: string) => F;
|
|
69
|
+
export declare const fieldNamed: <F extends NamedField>(fields: readonly F[], name: string, param?: string) => F;
|
|
68
70
|
/** A list field's option id, by its id or its whole label; a field with no options takes the value as typed. */
|
|
69
|
-
export declare const optionOf: (field: NamedField, value: string) => string;
|
|
71
|
+
export declare const optionOf: (field: NamedField, value: string, param?: string) => string;
|
|
70
72
|
/** `--field` pairs as typed, split at the first `=`; a pair with no key before it is refused. */
|
|
71
73
|
export declare const fieldPairsOf: (typed: readonly string[]) => [string, string][];
|
|
72
74
|
export interface CrmWritten {
|
package/dist/crm/writes.js
CHANGED
|
@@ -55,19 +55,23 @@ const copyOf = async (context, kind, reference) => {
|
|
|
55
55
|
throw new CliError("not_found", `${reference} is not a copy from this profile's CRM account`);
|
|
56
56
|
return found;
|
|
57
57
|
};
|
|
58
|
-
|
|
58
|
+
/** The named pipeline of this profile's CRM account, or its only one; several and none named is refused. */
|
|
59
|
+
export const pipelineOf = async (context, kind, reference, param = "--pipeline") => {
|
|
60
|
+
const scope = { ...(kind ? { kind } : {}), account: context.account };
|
|
59
61
|
if (reference !== undefined) {
|
|
60
|
-
const found = await context.store.deals.pipeline(reference,
|
|
62
|
+
const found = await context.store.deals.pipeline(reference, scope);
|
|
61
63
|
if (found.externalId === null)
|
|
62
64
|
throw new CliError("not_found", `${reference} is not a pipeline of this CRM account`);
|
|
63
65
|
return found;
|
|
64
66
|
}
|
|
65
|
-
const all = await context.store.deals.pipelines(
|
|
67
|
+
const all = await context.store.deals.pipelines(scope);
|
|
66
68
|
if (all.length === 1)
|
|
67
|
-
return all[0];
|
|
69
|
+
return context.store.deals.pipeline(all[0].ref, scope);
|
|
70
|
+
const what = kind ? `${kind} pipeline` : "pipeline";
|
|
68
71
|
throw new CliError("validation_error", all.length === 0
|
|
69
|
-
? `no ${
|
|
70
|
-
: `this CRM has ${all.length} ${
|
|
72
|
+
? `no ${what} of this CRM is stored; run sync first`
|
|
73
|
+
: `this CRM has ${all.length} ${what}s; name one${param.startsWith("--") ? ` with ${param}` : ""}: ` +
|
|
74
|
+
all.map(({ name, ref }) => `${name} (${ref})`).join(", "), { reason: "crm.pipeline-required", param, allowed: all.map(({ ref }) => ref) });
|
|
71
75
|
};
|
|
72
76
|
/** A stage of `pipeline`, by the CRM's id or by a name only one of its stages has, in any letter case. */
|
|
73
77
|
const stageOf = (pipeline, given) => {
|
|
@@ -128,16 +132,16 @@ const fieldsOf = (input, pipeline, currency) => {
|
|
|
128
132
|
export const foldFieldName = (name) => foldName(name.trim()).replace(/[\s_]+/gu, " ");
|
|
129
133
|
const named = ({ code, label }) => ({ code, label });
|
|
130
134
|
/** No field by that name: the fields whose code or label holds it, or is held in it, are its candidates. */
|
|
131
|
-
export const unknownField = (fields, name) => {
|
|
135
|
+
export const unknownField = (fields, name, param = "--field") => {
|
|
132
136
|
const want = foldFieldName(name);
|
|
133
137
|
const close = fields.filter((field) => [field.code, field.label ?? ""].some((one) => {
|
|
134
138
|
const other = foldFieldName(one);
|
|
135
139
|
return other !== "" && (other.includes(want) || want.includes(other));
|
|
136
140
|
}));
|
|
137
|
-
return new CliError("validation_error", `no custom field is named ${name}${close.length ? "; close ones are in candidates" : ""} — \`fields list\` lists them`, { reason: "field.unknown", param
|
|
141
|
+
return new CliError("validation_error", `no custom field is named ${name}${close.length ? "; close ones are in candidates" : ""} — \`fields list\` lists them`, { reason: "field.unknown", param, candidates: close.map(named) });
|
|
138
142
|
};
|
|
139
143
|
/** A field by its code or its whole label; two with that label are refused with both, never guessed. */
|
|
140
|
-
export const fieldNamed = (fields, name) => {
|
|
144
|
+
export const fieldNamed = (fields, name, param = "--field") => {
|
|
141
145
|
const want = foldFieldName(name);
|
|
142
146
|
const byCode = fields.find((field) => foldFieldName(field.code) === want);
|
|
143
147
|
if (byCode)
|
|
@@ -148,13 +152,13 @@ export const fieldNamed = (fields, name) => {
|
|
|
148
152
|
if (labelled.length > 1)
|
|
149
153
|
throw new CliError("validation_error", `${labelled.length} custom fields are labelled ${name}; name one by its code`, {
|
|
150
154
|
reason: "field.ambiguous",
|
|
151
|
-
param
|
|
155
|
+
param,
|
|
152
156
|
candidates: labelled.map(named),
|
|
153
157
|
});
|
|
154
|
-
throw unknownField(fields, name);
|
|
158
|
+
throw unknownField(fields, name, param);
|
|
155
159
|
};
|
|
156
160
|
/** A list field's option id, by its id or its whole label; a field with no options takes the value as typed. */
|
|
157
|
-
export const optionOf = (field, value) => {
|
|
161
|
+
export const optionOf = (field, value, param = "--field") => {
|
|
158
162
|
if (!field.options)
|
|
159
163
|
return value;
|
|
160
164
|
const want = foldFieldName(value);
|
|
@@ -163,7 +167,7 @@ export const optionOf = (field, value) => {
|
|
|
163
167
|
if (!found)
|
|
164
168
|
throw new CliError("validation_error", `${field.label ?? field.code} has no option ${value}`, {
|
|
165
169
|
reason: "field.unknown-option",
|
|
166
|
-
param
|
|
170
|
+
param,
|
|
167
171
|
field: field.code,
|
|
168
172
|
allowed: field.options,
|
|
169
173
|
});
|
package/dist/documents.d.ts
CHANGED
|
@@ -5,5 +5,7 @@ export { attachmentMaxBytes } from "./attachments/limits.js";
|
|
|
5
5
|
export type { Sheet, SheetCell } from "./attachments/sheets.js";
|
|
6
6
|
export { columnName, sheetText } from "./attachments/sheets.js";
|
|
7
7
|
export * from "./documents/index.js";
|
|
8
|
+
export { isPublicAddress } from "./fetch/addresses.js";
|
|
9
|
+
export { type FetchedPage, type FetchPageOptions, fetchPage, nodeTransport, PAGE_LIMITS, PAGE_USER_AGENT, type PageLimits, type PageLookup, type PageRequest, type PageResponse, type PageTransport, type ResolvedAddress, } from "./fetch/page.js";
|
|
8
10
|
export { registerReader } from "./readers/registry.js";
|
|
9
11
|
export type { ReadContext, Reader, Section } from "./readers/types.js";
|
package/dist/documents.js
CHANGED
|
@@ -3,4 +3,6 @@ export { extractText, importEngine, MAX_FILE_BYTES, MAX_TEXT_CHARS } from "./att
|
|
|
3
3
|
export { attachmentMaxBytes } from "./attachments/limits.js";
|
|
4
4
|
export { columnName, sheetText } from "./attachments/sheets.js";
|
|
5
5
|
export * from "./documents/index.js";
|
|
6
|
+
export { isPublicAddress } from "./fetch/addresses.js";
|
|
7
|
+
export { fetchPage, nodeTransport, PAGE_LIMITS, PAGE_USER_AGENT, } from "./fetch/page.js";
|
|
6
8
|
export { registerReader } from "./readers/registry.js";
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { type FinanceReview, type MoneyDirection } from "./finance.js";
|
|
2
|
+
export declare const CATALOG_KINDS: readonly ["product", "service"];
|
|
3
|
+
export declare const CONTRACT_KINDS: readonly ["sale", "purchase", "service", "lease", "employment", "nda", "license", "other"];
|
|
4
|
+
export declare const CONTRACT_STATUSES: readonly ["draft", "active", "ended", "terminated"];
|
|
5
|
+
export declare const PERIOD_UNITS: readonly ["day", "week", "month", "year"];
|
|
6
|
+
export declare const SUBSCRIPTION_STATUSES: readonly ["trial", "active", "paused", "cancelled", "ended"];
|
|
7
|
+
export declare const WALLET_KINDS: readonly ["bank", "card", "cash", "wallet", "other"];
|
|
8
|
+
/** Only a booked transaction counts in a balance; a pending one is shown beside it. */
|
|
9
|
+
export declare const TRANSACTION_STATUSES: readonly ["pending", "booked"];
|
|
10
|
+
/** `purchase`: the owner buys, as every order before store version 57; `sale`: the owner sells. */
|
|
11
|
+
export declare const ORDER_DIRECTIONS: readonly ["purchase", "sale"];
|
|
12
|
+
export type CatalogKind = (typeof CATALOG_KINDS)[number];
|
|
13
|
+
export type ContractKind = (typeof CONTRACT_KINDS)[number];
|
|
14
|
+
export type ContractStatus = (typeof CONTRACT_STATUSES)[number];
|
|
15
|
+
export type PeriodUnit = (typeof PERIOD_UNITS)[number];
|
|
16
|
+
export type SubscriptionStatus = (typeof SUBSCRIPTION_STATUSES)[number];
|
|
17
|
+
export type WalletKind = (typeof WALLET_KINDS)[number];
|
|
18
|
+
export type TransactionStatus = (typeof TRANSACTION_STATUSES)[number];
|
|
19
|
+
export type OrderDirection = (typeof ORDER_DIRECTIONS)[number];
|
|
20
|
+
export interface Interval {
|
|
21
|
+
count: number;
|
|
22
|
+
unit: PeriodUnit;
|
|
23
|
+
}
|
|
24
|
+
/** `1-month`, `2-week` or a bare `year`, as `--every` takes it. */
|
|
25
|
+
export declare const intervalOf: (text: unknown, reason?: string) => Interval;
|
|
26
|
+
export declare const intervalText: ({ count, unit }: Interval) => string;
|
|
27
|
+
export interface ProductTitleFields {
|
|
28
|
+
name: string;
|
|
29
|
+
kind: CatalogKind;
|
|
30
|
+
unitPrice: number | null;
|
|
31
|
+
currency: string | null;
|
|
32
|
+
unit: string | null;
|
|
33
|
+
archived: boolean;
|
|
34
|
+
}
|
|
35
|
+
/** "Bike rental, month · 10 000.00 RUB per month (service)". */
|
|
36
|
+
export declare const productLine: (product: ProductTitleFields) => string;
|
|
37
|
+
export interface ContractTitleFields {
|
|
38
|
+
id: number;
|
|
39
|
+
name: string;
|
|
40
|
+
number: string | null;
|
|
41
|
+
value: number | null;
|
|
42
|
+
currency: string | null;
|
|
43
|
+
status: ContractStatus;
|
|
44
|
+
review: FinanceReview;
|
|
45
|
+
}
|
|
46
|
+
/** "K-2026-14 · Rental agreement, 120 000.00 RUB (active)". */
|
|
47
|
+
export declare const contractLine: (contract: ContractTitleFields) => string;
|
|
48
|
+
export interface SubscriptionTitleFields {
|
|
49
|
+
name: string;
|
|
50
|
+
interval: Interval;
|
|
51
|
+
/** The current price, when one is confirmed. */
|
|
52
|
+
price: number | null;
|
|
53
|
+
currency: string | null;
|
|
54
|
+
status: SubscriptionStatus;
|
|
55
|
+
review: FinanceReview;
|
|
56
|
+
}
|
|
57
|
+
/** "Hosting, 990.00 RUB every 1-month (active, proposed)". */
|
|
58
|
+
export declare const subscriptionLine: (subscription: SubscriptionTitleFields) => string;
|
|
59
|
+
export interface WalletTitleFields {
|
|
60
|
+
name: string;
|
|
61
|
+
kind: WalletKind;
|
|
62
|
+
currency: string;
|
|
63
|
+
numberHint: string | null;
|
|
64
|
+
closed: boolean;
|
|
65
|
+
}
|
|
66
|
+
/** "Tinkoff · bank, RUB, …1234 (closed)". */
|
|
67
|
+
export declare const walletLine: (wallet: WalletTitleFields) => string;
|
|
68
|
+
export interface TransactionTitleFields {
|
|
69
|
+
direction: MoneyDirection;
|
|
70
|
+
amount: number;
|
|
71
|
+
currency: string;
|
|
72
|
+
/** As the statement names the other side, or else its description. */
|
|
73
|
+
label: string | null;
|
|
74
|
+
date: string;
|
|
75
|
+
status: TransactionStatus;
|
|
76
|
+
review: FinanceReview;
|
|
77
|
+
}
|
|
78
|
+
/** "500.00 RUB out · Кофейня, 14.11 (pending, proposed)". */
|
|
79
|
+
export declare const transactionLine: (transaction: TransactionTitleFields) => string;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { dayMonth, notes } from "./finance.js";
|
|
3
|
+
import { moneyLabel } from "./money.js";
|
|
4
|
+
export const CATALOG_KINDS = ["product", "service"];
|
|
5
|
+
export const CONTRACT_KINDS = ["sale", "purchase", "service", "lease", "employment", "nda", "license", "other"];
|
|
6
|
+
export const CONTRACT_STATUSES = ["draft", "active", "ended", "terminated"];
|
|
7
|
+
export const PERIOD_UNITS = ["day", "week", "month", "year"];
|
|
8
|
+
export const SUBSCRIPTION_STATUSES = ["trial", "active", "paused", "cancelled", "ended"];
|
|
9
|
+
export const WALLET_KINDS = ["bank", "card", "cash", "wallet", "other"];
|
|
10
|
+
/** Only a booked transaction counts in a balance; a pending one is shown beside it. */
|
|
11
|
+
export const TRANSACTION_STATUSES = ["pending", "booked"];
|
|
12
|
+
/** `purchase`: the owner buys, as every order before store version 57; `sale`: the owner sells. */
|
|
13
|
+
export const ORDER_DIRECTIONS = ["purchase", "sale"];
|
|
14
|
+
/** `1-month`, `2-week` or a bare `year`, as `--every` takes it. */
|
|
15
|
+
export const intervalOf = (text, reason = "subscription.interval") => {
|
|
16
|
+
const match = typeof text === "string" ? /^(?:([1-9]\d{0,2})-)?(day|week|month|year)$/.exec(text.trim()) : null;
|
|
17
|
+
if (!match)
|
|
18
|
+
throw new CliError("validation_error", "an interval is a count and a unit, such as 1-month or 2-week", { reason });
|
|
19
|
+
return { count: match[1] === undefined ? 1 : Number(match[1]), unit: match[2] };
|
|
20
|
+
};
|
|
21
|
+
export const intervalText = ({ count, unit }) => `${count}-${unit}`;
|
|
22
|
+
const priced = (units, currency) => units === null || currency === null ? "" : `, ${moneyLabel(units, currency)}`;
|
|
23
|
+
const pending = (review) => (review === "confirmed" ? null : review);
|
|
24
|
+
/** "Bike rental, month · 10 000.00 RUB per month (service)". */
|
|
25
|
+
export const productLine = (product) => {
|
|
26
|
+
const price = product.unitPrice === null || product.currency === null ? "" : moneyLabel(product.unitPrice, product.currency);
|
|
27
|
+
const per = price && product.unit !== null ? ` per ${product.unit}` : "";
|
|
28
|
+
return `${product.name}${price ? ` · ${price}${per}` : ""}${notes(product.kind, product.archived ? "archived" : null)}`;
|
|
29
|
+
};
|
|
30
|
+
/** "K-2026-14 · Rental agreement, 120 000.00 RUB (active)". */
|
|
31
|
+
export const contractLine = (contract) => `${contract.number ?? `#${contract.id}`} · ${contract.name}${priced(contract.value, contract.currency)}` +
|
|
32
|
+
notes(contract.status, pending(contract.review));
|
|
33
|
+
/** "Hosting, 990.00 RUB every 1-month (active, proposed)". */
|
|
34
|
+
export const subscriptionLine = (subscription) => `${subscription.name}${priced(subscription.price, subscription.currency)} every ${intervalText(subscription.interval)}` +
|
|
35
|
+
notes(subscription.status, pending(subscription.review));
|
|
36
|
+
/** "Tinkoff · bank, RUB, …1234 (closed)". */
|
|
37
|
+
export const walletLine = (wallet) => `${wallet.name} · ${wallet.kind}, ${wallet.currency}${wallet.numberHint === null ? "" : `, …${wallet.numberHint}`}` +
|
|
38
|
+
notes(wallet.closed ? "closed" : null);
|
|
39
|
+
/** "500.00 RUB out · Кофейня, 14.11 (pending, proposed)". */
|
|
40
|
+
export const transactionLine = (transaction) => `${moneyLabel(transaction.amount, transaction.currency)} ${transaction.direction}` +
|
|
41
|
+
`${transaction.label === null ? "" : ` · ${transaction.label}`}, ${dayMonth(transaction.date)}` +
|
|
42
|
+
notes(transaction.status === "pending" ? "pending" : null, pending(transaction.review));
|
package/dist/domain/crm.d.ts
CHANGED
|
@@ -40,10 +40,14 @@ export interface CrmItem {
|
|
|
40
40
|
closedReason?: string | null;
|
|
41
41
|
/** A company's id. */
|
|
42
42
|
organization?: string | null;
|
|
43
|
-
/**
|
|
43
|
+
/**
|
|
44
|
+
* Contacts on it, by their id, the part each plays and whether the CRM marks one its main contact. Left out, the CRM
|
|
45
|
+
* did not say who is on it, and the people stored stay.
|
|
46
|
+
*/
|
|
44
47
|
people?: {
|
|
45
48
|
externalId: string;
|
|
46
49
|
role: DealRole;
|
|
50
|
+
primary?: boolean;
|
|
47
51
|
}[];
|
|
48
52
|
/** A lead's: the deal it became. */
|
|
49
53
|
deal?: string | null;
|
|
@@ -52,7 +56,7 @@ export interface CrmItem {
|
|
|
52
56
|
updatedAt?: number | null;
|
|
53
57
|
/** The CRM's own page for it, http or https. */
|
|
54
58
|
url?: string | null;
|
|
55
|
-
/** The CRM user it is assigned to, by the user's id: never a contact's. */
|
|
59
|
+
/** The CRM user it is assigned to, by the user's id: never a contact's. It makes that user's person its `owner`. */
|
|
56
60
|
assignee?: string | null;
|
|
57
61
|
/**
|
|
58
62
|
* What the CRM sends that has no column; never a secret. Its own fields' values are here by code as `CrmValue`,
|
|
@@ -75,14 +79,49 @@ export interface CrmPerson {
|
|
|
75
79
|
name: string | null;
|
|
76
80
|
email?: string | null;
|
|
77
81
|
phone?: string | null;
|
|
82
|
+
/** The job title as the CRM writes it: kept beside the relationship, never a role. */
|
|
83
|
+
position?: string | null;
|
|
84
|
+
/**
|
|
85
|
+
* The companies the contact represents, by their id; each becomes `member-of` with role `contact`. Left out, the CRM
|
|
86
|
+
* did not say, and the relationships stored stay.
|
|
87
|
+
*/
|
|
88
|
+
organizations?: {
|
|
89
|
+
externalId: string;
|
|
90
|
+
primary?: boolean;
|
|
91
|
+
sourceRole?: string | null;
|
|
92
|
+
}[];
|
|
78
93
|
}
|
|
79
94
|
/** A CRM company: it becomes an organization of its own, never one matched by name. */
|
|
80
95
|
export interface CrmOrganization {
|
|
81
96
|
externalId: string;
|
|
82
97
|
name: string;
|
|
83
98
|
url?: string | null;
|
|
99
|
+
/** One of the account's own companies, the owner's side of every deal, not a client. */
|
|
100
|
+
own?: boolean;
|
|
101
|
+
/** The CRM's company type code; `crm.<provider>:<account>.company-types` says what one means, nothing by default. */
|
|
102
|
+
type?: string | null;
|
|
84
103
|
metadata?: Record<string, unknown>;
|
|
85
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* A CRM user: someone of the owner's own company, such as a deal's owner. It becomes an identity of the CRM's provider,
|
|
107
|
+
* `<account>/user/<id>`, joined to a person by exact email only, and `member-of` the own company as an `employee`
|
|
108
|
+
* while it is active.
|
|
109
|
+
*/
|
|
110
|
+
export interface CrmUser {
|
|
111
|
+
externalId: string;
|
|
112
|
+
name: string | null;
|
|
113
|
+
email?: string | null;
|
|
114
|
+
position?: string | null;
|
|
115
|
+
departments?: string[];
|
|
116
|
+
active?: boolean;
|
|
117
|
+
}
|
|
118
|
+
/** A department of the own company: a user reports to its head, a head to the head of the parent. */
|
|
119
|
+
export interface CrmDepartment {
|
|
120
|
+
externalId: string;
|
|
121
|
+
name: string;
|
|
122
|
+
parent?: string | null;
|
|
123
|
+
head?: string | null;
|
|
124
|
+
}
|
|
86
125
|
/** One of a CRM account's own fields on a kind: what the code a row's `metadata` is keyed by means. */
|
|
87
126
|
export interface CrmField {
|
|
88
127
|
/** The CRM's key for it, unique in its kind. */
|
package/dist/domain/deals.d.ts
CHANGED
|
@@ -4,8 +4,11 @@ export declare const DEAL_KINDS: readonly ["deal", "lead"];
|
|
|
4
4
|
export declare const DEAL_STATUSES: readonly ["open", "won", "lost"];
|
|
5
5
|
/** What reaching a stage means, as a CRM marks it: still open, or won or lost. */
|
|
6
6
|
export declare const STAGE_OUTCOMES: readonly ["open", "won", "lost"];
|
|
7
|
-
/**
|
|
8
|
-
|
|
7
|
+
/**
|
|
8
|
+
* The part a person plays on a deal: the owner's side runs it (`owner`, the CRM's "responsible"), the client's side
|
|
9
|
+
* talks, decides or sways the decision.
|
|
10
|
+
*/
|
|
11
|
+
export declare const DEAL_ROLES: readonly ["owner", "contact", "decision-maker", "influencer"];
|
|
9
12
|
export type DealKind = (typeof DEAL_KINDS)[number];
|
|
10
13
|
export type DealStatus = (typeof DEAL_STATUSES)[number];
|
|
11
14
|
export type StageOutcome = DealStatus;
|
package/dist/domain/deals.js
CHANGED
|
@@ -5,8 +5,11 @@ export const DEAL_KINDS = ["deal", "lead"];
|
|
|
5
5
|
export const DEAL_STATUSES = ["open", "won", "lost"];
|
|
6
6
|
/** What reaching a stage means, as a CRM marks it: still open, or won or lost. */
|
|
7
7
|
export const STAGE_OUTCOMES = DEAL_STATUSES;
|
|
8
|
-
/**
|
|
9
|
-
|
|
8
|
+
/**
|
|
9
|
+
* The part a person plays on a deal: the owner's side runs it (`owner`, the CRM's "responsible"), the client's side
|
|
10
|
+
* talks, decides or sways the decision.
|
|
11
|
+
*/
|
|
12
|
+
export const DEAL_ROLES = ["owner", "contact", "decision-maker", "influencer"];
|
|
10
13
|
/** "Site redesign · Proposal, 500 000.00 RUB (won)": a ref summary's title, which `refLabel` prefixes. */
|
|
11
14
|
export const dealLine = (deal) => {
|
|
12
15
|
const money = deal.amount === null || deal.currency === null ? "" : `, ${moneyLabel(deal.amount, deal.currency)}`;
|
package/dist/domain/finance.d.ts
CHANGED
|
@@ -10,6 +10,8 @@ export type MoneyDirection = (typeof MONEY_DIRECTIONS)[number];
|
|
|
10
10
|
export type FinanceReview = (typeof FINANCE_REVIEWS)[number];
|
|
11
11
|
/** An invoice's status from what confirmed payments settle; `draft` and `void` are the owner's and stay. */
|
|
12
12
|
export declare const settledStatus: (current: InvoiceStatus, total: number, paid: number) => InvoiceStatus;
|
|
13
|
+
export declare const dayMonth: (date: string | null) => string | null;
|
|
14
|
+
export declare const notes: (...words: (string | null)[]) => string;
|
|
13
15
|
export interface InvoiceTitleFields {
|
|
14
16
|
id: number;
|
|
15
17
|
number: string | null;
|