@wirecat/cli-core 0.605.0 → 0.606.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 (83) hide show
  1. package/CHANGELOG.md +98 -0
  2. package/dist/cli/index.d.ts +1 -1
  3. package/dist/cli/index.js +1 -0
  4. package/dist/cli/store-settings.js +38 -4
  5. package/dist/crm/cli/index.js +3 -1
  6. package/dist/crm/index.d.ts +2 -2
  7. package/dist/crm/index.js +1 -1
  8. package/dist/crm/source.d.ts +35 -5
  9. package/dist/crm/sync.d.ts +15 -7
  10. package/dist/crm/sync.js +169 -11
  11. package/dist/domain/crm.d.ts +41 -2
  12. package/dist/domain/deals.d.ts +5 -2
  13. package/dist/domain/deals.js +5 -2
  14. package/dist/domain/relations.d.ts +125 -0
  15. package/dist/domain/relations.js +107 -0
  16. package/dist/entities/deal.d.ts +2 -2
  17. package/dist/entities/deal.js +2 -1
  18. package/dist/entities/index.d.ts +17 -13
  19. package/dist/entities/relationship.d.ts +8 -2
  20. package/dist/entities/relationship.js +8 -2
  21. package/dist/entities/task-results.d.ts +10 -10
  22. package/dist/errors/reasons.js +12 -0
  23. package/dist/index.d.ts +2 -1
  24. package/dist/index.js +1 -0
  25. package/dist/mail/cli/review.js +20 -6
  26. package/dist/mail/index.d.ts +1 -1
  27. package/dist/mail/marks.d.ts +10 -5
  28. package/dist/mail/marks.js +16 -8
  29. package/dist/mail/review.d.ts +5 -0
  30. package/dist/services/link-scorers/scan.js +2 -1
  31. package/dist/services/link-scorers/shared.js +2 -1
  32. package/dist/services/project-status/index.js +2 -1
  33. package/dist/services/stored-project-gathering.js +4 -3
  34. package/dist/store/exports.d.ts +1 -0
  35. package/dist/store/exports.js +1 -0
  36. package/dist/store/index.d.ts +6 -1
  37. package/dist/store/index.js +1 -0
  38. package/dist/store/project-transfer/request.d.ts +4 -1
  39. package/dist/store/project-transfer/schema.d.ts +9 -2
  40. package/dist/store/project-transfer/schema.js +4 -0
  41. package/dist/store/project-transfer/snapshot-model.d.ts +8 -2
  42. package/dist/store/sqlite/crm-settings.d.ts +28 -0
  43. package/dist/store/sqlite/crm-settings.js +80 -0
  44. package/dist/store/sqlite/crm.d.ts +42 -2
  45. package/dist/store/sqlite/crm.js +397 -36
  46. package/dist/store/sqlite/deals.js +3 -2
  47. package/dist/store/sqlite/involvement-queue.js +4 -3
  48. package/dist/store/sqlite/knowledge.d.ts +7 -3
  49. package/dist/store/sqlite/knowledge.js +43 -31
  50. package/dist/store/sqlite/link-kinds.d.ts +2 -3
  51. package/dist/store/sqlite/link-kinds.js +4 -2
  52. package/dist/store/sqlite/manifest.js +5 -0
  53. package/dist/store/sqlite/memories.d.ts +1 -1
  54. package/dist/store/sqlite/memories.js +33 -7
  55. package/dist/store/sqlite/migrations.generated.js +31 -0
  56. package/dist/store/sqlite/notes.d.ts +3 -2
  57. package/dist/store/sqlite/notes.js +23 -6
  58. package/dist/store/sqlite/organizations.d.ts +8 -0
  59. package/dist/store/sqlite/organizations.js +14 -0
  60. package/dist/store/sqlite/person-preparation.d.ts +1 -1
  61. package/dist/store/sqlite/person-preparation.js +3 -2
  62. package/dist/store/sqlite/project-transfer/records.js +14 -3
  63. package/dist/store/sqlite/project-transfer/snapshot.d.ts +4 -1
  64. package/dist/store/sqlite/project-transfer/snapshot.js +6 -0
  65. package/dist/store/sqlite/relation-list.d.ts +43 -0
  66. package/dist/store/sqlite/relation-list.js +133 -0
  67. package/dist/store/sqlite/relation-proposals.d.ts +28 -6
  68. package/dist/store/sqlite/relation-proposals.js +134 -10
  69. package/dist/store/sqlite/relation-reads.d.ts +8 -0
  70. package/dist/store/sqlite/relation-reads.js +7 -0
  71. package/dist/store/sqlite/relation-roles.d.ts +26 -0
  72. package/dist/store/sqlite/relation-roles.js +91 -0
  73. package/dist/store/sqlite/reported-relations.d.ts +63 -0
  74. package/dist/store/sqlite/reported-relations.js +132 -0
  75. package/dist/store/sqlite/schema/link-revisions.d.ts +136 -0
  76. package/dist/store/sqlite/schema/link-revisions.js +11 -0
  77. package/dist/store/sqlite/schema/links.d.ts +360 -0
  78. package/dist/store/sqlite/schema/links.js +32 -0
  79. package/dist/store/sqlite/schema.d.ts +2 -296
  80. package/dist/store/sqlite/schema.js +2 -24
  81. package/dist/store/store.d.ts +3 -0
  82. package/dist/store/store.js +5 -0
  83. package/package.json +3 -3
@@ -0,0 +1,28 @@
1
+ import type { CacheDatabase } from "../driver.js";
2
+ import type { AccountKey } from "../types.js";
3
+ /** `reported`: what a CRM account says is a relationship nobody confirmed; `review`: it waits as a proposal. */
4
+ export declare const CRM_LINK_MODES: readonly ["reported", "review"];
5
+ export type CrmLinkMode = (typeof CRM_LINK_MODES)[number];
6
+ /** What a CRM company type may mean: the company buys from the own company, sells to it, or partners with it. */
7
+ export declare const COMPANY_TYPE_MEANINGS: readonly ["customer-of", "supplier", "partner-of"];
8
+ export type CompanyTypeMeaning = (typeof COMPANY_TYPE_MEANINGS)[number];
9
+ declare const NAMES: readonly ["links", "company-types"];
10
+ type CrmSettingName = (typeof NAMES)[number];
11
+ export declare const crmSettingKey: (key: AccountKey, name: CrmSettingName) => string;
12
+ /** The account and the setting a key names; a portal's account may hold dots, so it is read from both ends. */
13
+ export declare const parseCrmSetting: (setting: string) => {
14
+ key: AccountKey;
15
+ name: CrmSettingName;
16
+ } | undefined;
17
+ /** A value this build cannot read is the default: the CRM's relationships stay reported. */
18
+ export declare const crmLinkMode: (database: CacheDatabase, key: AccountKey) => CrmLinkMode;
19
+ /** Only the owner's mapping gives a company type a meaning; none by default. */
20
+ export declare const crmCompanyTypes: (database: CacheDatabase, key: AccountKey) => Record<string, CompanyTypeMeaning>;
21
+ /** `links`: reported or review; `company-types`: `TYPE=meaning` pairs, comma-separated. */
22
+ export declare const parseCrmSettingValue: (name: CrmSettingName, value: string) => unknown;
23
+ export declare const saveCrmSetting: (database: CacheDatabase, setting: string, value: unknown, now: number) => void;
24
+ export declare const crmSettingsOf: (database: CacheDatabase) => {
25
+ setting: string;
26
+ value: unknown;
27
+ }[];
28
+ export {};
@@ -0,0 +1,80 @@
1
+ import { CliError } from "@wirecat/cli-base";
2
+ /** `reported`: what a CRM account says is a relationship nobody confirmed; `review`: it waits as a proposal. */
3
+ export const CRM_LINK_MODES = ["reported", "review"];
4
+ /** What a CRM company type may mean: the company buys from the own company, sells to it, or partners with it. */
5
+ export const COMPANY_TYPE_MEANINGS = ["customer-of", "supplier", "partner-of"];
6
+ const NAMES = ["links", "company-types"];
7
+ export const crmSettingKey = (key, name) => `crm.${key.provider}:${key.account}.${name}`;
8
+ /** The account and the setting a key names; a portal's account may hold dots, so it is read from both ends. */
9
+ export const parseCrmSetting = (setting) => {
10
+ const match = /^crm\.([a-z0-9-]+):(.+)\.(links|company-types)$/.exec(setting);
11
+ return match
12
+ ? { key: { provider: match[1], account: match[2] }, name: match[3] }
13
+ : undefined;
14
+ };
15
+ const stored = (database, setting) => {
16
+ const row = database.prepare("SELECT value FROM store_settings WHERE key = ?").get(setting);
17
+ if (!row)
18
+ return undefined;
19
+ try {
20
+ return JSON.parse(String(row.value));
21
+ }
22
+ catch {
23
+ return undefined;
24
+ }
25
+ };
26
+ /** A value this build cannot read is the default: the CRM's relationships stay reported. */
27
+ export const crmLinkMode = (database, key) => {
28
+ const value = stored(database, crmSettingKey(key, "links"));
29
+ return value === "review" ? "review" : "reported";
30
+ };
31
+ /** Only the owner's mapping gives a company type a meaning; none by default. */
32
+ export const crmCompanyTypes = (database, key) => {
33
+ const value = stored(database, crmSettingKey(key, "company-types"));
34
+ if (typeof value !== "object" || value === null || Array.isArray(value))
35
+ return {};
36
+ return Object.fromEntries(Object.entries(value).filter((entry) => COMPANY_TYPE_MEANINGS.includes(entry[1])));
37
+ };
38
+ /** `links`: reported or review; `company-types`: `TYPE=meaning` pairs, comma-separated. */
39
+ export const parseCrmSettingValue = (name, value) => {
40
+ const text = value.trim();
41
+ if (name === "links") {
42
+ if (!CRM_LINK_MODES.includes(text))
43
+ throw new CliError("validation_error", `a CRM account's links setting is ${CRM_LINK_MODES.join(" or ")}`);
44
+ return text;
45
+ }
46
+ const pairs = text
47
+ .split(",")
48
+ .map((pair) => pair.trim())
49
+ .filter(Boolean);
50
+ const map = {};
51
+ for (const pair of pairs) {
52
+ const [type, meaning] = pair.split("=").map((part) => part.trim());
53
+ if (!type || !COMPANY_TYPE_MEANINGS.includes(meaning ?? ""))
54
+ throw new CliError("validation_error", `company-types takes TYPE=meaning pairs, the meaning ${COMPANY_TYPE_MEANINGS.join(", ")}: not "${pair}"`);
55
+ map[type] = meaning;
56
+ }
57
+ return map;
58
+ };
59
+ export const saveCrmSetting = (database, setting, value, now) => {
60
+ if (value === undefined)
61
+ database.prepare("DELETE FROM store_settings WHERE key = ?").run(setting);
62
+ else
63
+ database
64
+ .prepare("INSERT OR REPLACE INTO store_settings (key, value, at) VALUES (?, ?, ?)")
65
+ .run(setting, JSON.stringify(value), now);
66
+ };
67
+ export const crmSettingsOf = (database) => database
68
+ .prepare("SELECT key, value FROM store_settings WHERE key LIKE 'crm.%' ORDER BY key")
69
+ .all()
70
+ .flatMap((row) => {
71
+ const parsed = parseCrmSetting(String(row.key));
72
+ if (!parsed)
73
+ return [];
74
+ return [
75
+ {
76
+ setting: String(row.key),
77
+ value: parsed.name === "links" ? crmLinkMode(database, parsed.key) : crmCompanyTypes(database, parsed.key),
78
+ },
79
+ ];
80
+ });
@@ -1,4 +1,4 @@
1
- import type { CrmField, CrmItem, CrmKind, CrmOrganization, CrmPerson, CrmPipeline } from "../../domain/crm.js";
1
+ import { type CrmDepartment, type CrmField, type CrmItem, type CrmKind, type CrmOrganization, type CrmPerson, type CrmPipeline, type CrmUser } from "../../domain/crm.js";
2
2
  import { type DealKind } from "../../domain/deals.js";
3
3
  import type { AccountPeople } from "../people.js";
4
4
  import type { AccountKey } from "../types.js";
@@ -9,9 +9,28 @@ export interface CrmSkipped {
9
9
  reason: "crm.stage-unknown" | "crm.item-invalid";
10
10
  message: string;
11
11
  }
12
+ /**
13
+ * A relationship the CRM reported that the store did not write: a contact, company or user not synced yet (tried
14
+ * again on the next sync), ends the registry refuses, an own company it does not know yet, or an email two people
15
+ * share.
16
+ */
17
+ export interface CrmRelationIssue {
18
+ /** The CRM row it came from; absent for the account as a whole. */
19
+ externalId?: string;
20
+ reason: "crm.reference-unknown" | "crm.own-unknown" | "crm.email-ambiguous" | "relationship.reference-unknown" | "relationship.invalid";
21
+ message: string;
22
+ }
12
23
  export interface CrmSaved {
13
24
  saved: number;
14
25
  skipped: CrmSkipped[];
26
+ /** Relationships left out; the rows themselves were kept. */
27
+ issues?: CrmRelationIssue[];
28
+ /** Relationships left out because the owner removed or rejected that very one. */
29
+ skippedOwnerRejected?: number;
30
+ /** Deal owners the CRM names among users it cannot list: nothing waits for them. */
31
+ unlistedUsers?: number;
32
+ /** Relationships that waited too long, or were too many, for an end that never came. */
33
+ droppedPending?: number;
15
34
  }
16
35
  /**
17
36
  * A CRM account's copies in the store, written as the CRM has them: nothing waits for review (NEED-2271 A), and
@@ -22,11 +41,32 @@ export interface CrmCopies {
22
41
  /** One kind's pipelines and their stages, by the CRM's ids. A stage the CRM dropped stays: deals may stand in it. */
23
42
  savePipelines(key: AccountKey, kind: DealKind, pipelines: CrmPipeline[]): Promise<CrmSaved>;
24
43
  /** Deals or leads; one whose stage is not stored, or whose fields the store refuses, is skipped and named. */
25
- saveItems(key: AccountKey, kind: DealKind, items: CrmItem[]): Promise<CrmSaved>;
44
+ /** `users: false`: the source lists no users, so a deal owner not stored yet is counted, never waited for. */
45
+ saveItems(key: AccountKey, kind: DealKind, items: CrmItem[], options?: {
46
+ users?: boolean;
47
+ }): Promise<CrmSaved>;
26
48
  /** Contacts, as identities of the account's provider; a contact joins a person only by its exact email. */
27
49
  savePeople(key: AccountKey, people: CrmPerson[]): Promise<CrmSaved>;
28
50
  /** Companies, each an organization of its own made on first sight, kept apart from any of the same name. */
29
51
  saveOrganizations(key: AccountKey, organizations: CrmOrganization[]): Promise<CrmSaved>;
52
+ /**
53
+ * The account's whole list of users: identities `<account>/user/<id>`, joined by exact email; each active one
54
+ * `member-of` every own company as an `employee`; with `departments`, `reports-to` the head of its department, a head
55
+ * the head of the parent one (`metadata.derived = "department-head"`). An inactive user's relationships are
56
+ * withdrawn; one missing from the list keeps them, since a sign-in that lost its rights lists no user at all.
57
+ * Without `departments` no reporting line is touched.
58
+ */
59
+ saveUsers(key: AccountKey, users: CrmUser[], departments?: CrmDepartment[]): Promise<CrmSaved>;
60
+ /**
61
+ * Writes the relationships earlier syncs held back for an end not stored then, and drops those that waited past
62
+ * their time; answers how many of each.
63
+ */
64
+ retryPending(key: AccountKey): Promise<{
65
+ written: number;
66
+ dropped: number;
67
+ }>;
68
+ /** Every stored company of the account against its company-types setting; run after a sync read the companies. */
69
+ saveCompanyTypes(key: AccountKey): Promise<CrmSaved>;
30
70
  /** One kind's whole list of the CRM's own fields: one it lacks is marked deleted, one listed again comes back. */
31
71
  saveFields(key: AccountKey, kind: CrmKind, fields: CrmField[]): Promise<CrmSaved>;
32
72
  /** The kind's fields as last synced, in the CRM's order; none before the first sync. */