@wirecat/cli-core 0.721.0 → 0.723.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 +90 -0
- package/dist/background/daily-archive-timer.d.ts +1 -1
- package/dist/cli/messenger/lacks.d.ts +2 -1
- package/dist/cli/messenger/lacks.js +14 -1
- package/dist/cli/service-api-call.d.ts +6 -0
- package/dist/cli/service-api-call.js +21 -9
- package/dist/cli/service-api.d.ts +5 -0
- package/dist/cli/service-api.js +11 -0
- package/dist/cli/token-session.d.ts +2 -1
- package/dist/cli/token-session.js +2 -2
- package/dist/crm/cli/account.d.ts +60 -0
- package/dist/crm/cli/account.js +147 -0
- package/dist/crm/cli/files.js +2 -1
- package/dist/crm/cli/index.d.ts +3 -0
- package/dist/crm/cli/index.js +4 -0
- package/dist/crm/foreign-contacts.js +2 -1
- package/dist/crm/index.d.ts +3 -2
- package/dist/crm/index.js +1 -0
- package/dist/crm/manifest/manifest.d.ts +60 -1
- package/dist/crm/manifest/manifest.js +88 -0
- package/dist/crm/manifest/source.d.ts +22 -3
- package/dist/crm/manifest/source.js +10 -3
- package/dist/crm/manifest/writer.d.ts +39 -0
- package/dist/crm/manifest/writer.js +298 -0
- package/dist/crm/state-folders.d.ts +5 -0
- package/dist/crm/state-folders.js +2 -0
- package/dist/crm/testing/index.d.ts +1 -1
- package/dist/crm/testing/index.js +1 -1
- package/dist/crm/testing/manifest-fake.d.ts +14 -0
- package/dist/crm/testing/manifest-fake.js +252 -27
- package/dist/errors/reasons.d.ts +1 -1
- package/dist/errors/reasons.js +11 -0
- package/dist/ports/wirecat-tools.js +7 -0
- package/dist/services/files/mirror.d.ts +5 -2
- package/dist/services/files/mirror.js +1 -1
- package/dist/services/metadata.d.ts +1 -1
- package/dist/store/index.d.ts +1 -0
- package/dist/store/sqlite/account-purge.js +3 -0
- package/dist/store/sqlite/crm-stage-entries.js +4 -1
- package/dist/store/sqlite/erase-plan.d.ts +19 -0
- package/dist/store/sqlite/erase-plan.js +74 -0
- package/dist/store/sqlite/messages.js +2 -1
- package/dist/store/sqlite/organization-erase.d.ts +29 -0
- package/dist/store/sqlite/organization-erase.js +83 -0
- package/dist/store/sqlite/organizations.d.ts +10 -1
- package/dist/store/sqlite/organizations.js +6 -1
- package/dist/store/sqlite/person-erase.js +4 -45
- package/package.json +2 -2
|
@@ -171,7 +171,8 @@ export const upsertMessage = (context, key, accountKey, chatKey, received, via,
|
|
|
171
171
|
for (const field of ["views", "comments"])
|
|
172
172
|
if (!validCounter(incoming[field]) && validCounter(previous[field]))
|
|
173
173
|
incoming[field] = previous[field];
|
|
174
|
-
|
|
174
|
+
// A server search hit is a partial copy: what it leaves out (a thread's reply count) is not gone.
|
|
175
|
+
fields.metadata = JSON.stringify(edit || via === "search" ? { ...previous, ...incoming } : incoming);
|
|
175
176
|
}
|
|
176
177
|
}
|
|
177
178
|
catch {
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Decider } from "../decisions.js";
|
|
2
|
+
import type { CacheDatabase } from "../driver.js";
|
|
3
|
+
import type { StoreContext } from "./open.js";
|
|
4
|
+
export interface OrganizationErasure {
|
|
5
|
+
organization: string;
|
|
6
|
+
name: string;
|
|
7
|
+
/** `false`: what `erase` would do, nothing changed. */
|
|
8
|
+
erased: boolean;
|
|
9
|
+
/** Identities the organization held, such as its domain; the identities stay, the holding goes. */
|
|
10
|
+
identities: string[];
|
|
11
|
+
/** Rows that only tied something to the organization, such as a tag, a link or a mention, by `table.column`. */
|
|
12
|
+
deleted: Record<string, number>;
|
|
13
|
+
/** Rows that stay and lose the organization, such as a deal, a lead, a project or an account. */
|
|
14
|
+
detached: Record<string, number>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Deletes an organization and what memo knows of it alone: the tags, links, aliases and mentions that name it, and
|
|
18
|
+
* which identities it held. A deal, lead, project, account, fact or note that names it stays and loses the name.
|
|
19
|
+
* Refused for one of the owner's own companies, and while an invoice, order, subscription or a row it wrote cannot
|
|
20
|
+
* be without it. `apply: false` answers the same without changing anything.
|
|
21
|
+
*/
|
|
22
|
+
export declare const eraseOrganization: (context: StoreContext, ref: string, by: Decider, apply: boolean) => OrganizationErasure;
|
|
23
|
+
/** The organizations an account's records made: read before a purge deletes the records. */
|
|
24
|
+
export declare const importedOrganizations: (database: CacheDatabase, accountKey: number) => number[];
|
|
25
|
+
/**
|
|
26
|
+
* After a purge, the organizations only its records made go too: no other row names them. One the owner tagged,
|
|
27
|
+
* linked, gave a deal to or calls his own stays.
|
|
28
|
+
*/
|
|
29
|
+
export declare const eraseImportedOrganizations: (context: StoreContext, organizations: readonly number[]) => void;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { ErasePlan } from "./erase-plan.js";
|
|
3
|
+
import { forgetDanglingEvidence } from "./evidence-signature.js";
|
|
4
|
+
import { ownerOnly } from "./person-merges.js";
|
|
5
|
+
const OWN_ROWS = new Set(["organization_identities"]);
|
|
6
|
+
const organizationOf = (database, ref) => {
|
|
7
|
+
const asked = /^(?:organization:)?([1-9]\d*)$/u.exec(ref.trim())?.[1];
|
|
8
|
+
const row = asked === undefined
|
|
9
|
+
? undefined
|
|
10
|
+
: database.prepare("SELECT id, name, metadata FROM organizations WHERE id = ?").get(Number(asked));
|
|
11
|
+
if (row === undefined)
|
|
12
|
+
throw new CliError("not_found", `the store has no organization for "${ref}"`, { reason: "organization.not-found" });
|
|
13
|
+
return {
|
|
14
|
+
id: Number(row.id),
|
|
15
|
+
name: String(row.name),
|
|
16
|
+
own: [true, 1].includes(JSON.parse(String(row.metadata ?? "{}"))?.own),
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Deletes an organization and what memo knows of it alone: the tags, links, aliases and mentions that name it, and
|
|
21
|
+
* which identities it held. A deal, lead, project, account, fact or note that names it stays and loses the name.
|
|
22
|
+
* Refused for one of the owner's own companies, and while an invoice, order, subscription or a row it wrote cannot
|
|
23
|
+
* be without it. `apply: false` answers the same without changing anything.
|
|
24
|
+
*/
|
|
25
|
+
export const eraseOrganization = (context, ref, by, apply) => {
|
|
26
|
+
ownerOnly(by, "deletes an organization");
|
|
27
|
+
const { database } = context;
|
|
28
|
+
const organization = organizationOf(database, ref);
|
|
29
|
+
if (organization.own)
|
|
30
|
+
throw new CliError("validation_error", `organization:${organization.id} is one of your own companies`, {
|
|
31
|
+
reason: "organization.own",
|
|
32
|
+
});
|
|
33
|
+
const identities = database
|
|
34
|
+
.prepare(`SELECT i.provider || ':' || i.external_id AS ref FROM organization_identities o
|
|
35
|
+
JOIN identities i ON i.id = o.identity_id WHERE o.organization_id = ? ORDER BY i.id`)
|
|
36
|
+
.all(organization.id)
|
|
37
|
+
.map((one) => String(one.ref));
|
|
38
|
+
const plan = new ErasePlan(database)
|
|
39
|
+
.pointers("organization", "?", [organization.id])
|
|
40
|
+
.foreignKeys("organizations", organization.id, OWN_ROWS);
|
|
41
|
+
if (plan.blocked.length > 0)
|
|
42
|
+
throw new CliError("validation_error", `organization:${organization.id} is still named by ${plan.blocked.join(", ")}; change those first — nothing ` +
|
|
43
|
+
"was deleted", { reason: "organization.erase-blocked", blocked: plan.blocked });
|
|
44
|
+
const answer = (erased) => ({
|
|
45
|
+
organization: `organization:${organization.id}`,
|
|
46
|
+
name: organization.name,
|
|
47
|
+
erased,
|
|
48
|
+
identities,
|
|
49
|
+
deleted: plan.deleted,
|
|
50
|
+
detached: plan.detached,
|
|
51
|
+
});
|
|
52
|
+
if (!apply)
|
|
53
|
+
return answer(false);
|
|
54
|
+
plan.run();
|
|
55
|
+
database.prepare("DELETE FROM organization_identities WHERE organization_id = ?").run(organization.id);
|
|
56
|
+
database.prepare("DELETE FROM organizations WHERE id = ?").run(organization.id);
|
|
57
|
+
forgetDanglingEvidence(database, context.now());
|
|
58
|
+
return answer(true);
|
|
59
|
+
};
|
|
60
|
+
/** The organizations an account's records made: read before a purge deletes the records. */
|
|
61
|
+
export const importedOrganizations = (database, accountKey) => database
|
|
62
|
+
.prepare("SELECT DISTINCT entity_id AS id FROM records WHERE account_id = ? AND entity_type = 'organization'")
|
|
63
|
+
.all(accountKey)
|
|
64
|
+
.map(({ id }) => Number(id));
|
|
65
|
+
/**
|
|
66
|
+
* After a purge, the organizations only its records made go too: no other row names them. One the owner tagged,
|
|
67
|
+
* linked, gave a deal to or calls his own stays.
|
|
68
|
+
*/
|
|
69
|
+
export const eraseImportedOrganizations = (context, organizations) => {
|
|
70
|
+
for (const id of organizations) {
|
|
71
|
+
if (context.database.prepare("SELECT 1 FROM organizations WHERE id = ?").get(id) === undefined)
|
|
72
|
+
continue;
|
|
73
|
+
try {
|
|
74
|
+
const plan = eraseOrganization(context, String(id), "owner", false);
|
|
75
|
+
if (Object.keys(plan.deleted).length === 0 && Object.keys(plan.detached).length === 0)
|
|
76
|
+
eraseOrganization(context, String(id), "owner", true);
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
if (!(error instanceof CliError))
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { type QueryAst } from "@wirecat/cli-base/query";
|
|
2
|
+
import type { Decider } from "../decisions.js";
|
|
2
3
|
import type { Organization } from "./knowledge.js";
|
|
3
4
|
import type { StoreContext } from "./open.js";
|
|
4
5
|
import { type OrganizationFilter } from "./organization-categories.js";
|
|
6
|
+
import { type OrganizationErasure } from "./organization-erase.js";
|
|
5
7
|
type Row = Record<string, unknown>;
|
|
6
8
|
export declare const organizationOf: (row: Row) => Organization;
|
|
7
9
|
export interface OrganizationsStore {
|
|
@@ -21,6 +23,13 @@ export interface OrganizationsStore {
|
|
|
21
23
|
}>;
|
|
22
24
|
/** The owner's own companies: the owner's mark, else a CRM's own-company flag (`metadata.own`). */
|
|
23
25
|
ownOrganizations(): Promise<Organization[]>;
|
|
26
|
+
/**
|
|
27
|
+
* Deletes an organization and what memo knows of it alone; see `eraseOrganization`. `apply: false` answers what
|
|
28
|
+
* would go and changes nothing. The owner's alone; refused for the owner's own companies.
|
|
29
|
+
*/
|
|
30
|
+
eraseOrganization(organization: string, by: Decider, options: {
|
|
31
|
+
apply: boolean;
|
|
32
|
+
}): Promise<OrganizationErasure>;
|
|
24
33
|
}
|
|
25
|
-
export declare const organizationsStoreOver: (
|
|
34
|
+
export declare const organizationsStoreOver: (context: StoreContext) => OrganizationsStore;
|
|
26
35
|
export {};
|
|
@@ -2,6 +2,7 @@ import { CliError } from "@wirecat/cli-base";
|
|
|
2
2
|
import { evaluate, timezoneOf, walkQuery } from "@wirecat/cli-base/query";
|
|
3
3
|
import { ORGANIZATIONS_QUERY } from "../knowledge-query.js";
|
|
4
4
|
import { categorySql } from "./organization-categories.js";
|
|
5
|
+
import { eraseOrganization } from "./organization-erase.js";
|
|
5
6
|
import { queryFilter } from "./query-filter.js";
|
|
6
7
|
import { thingOf } from "./things.js";
|
|
7
8
|
import { write } from "./transaction.js";
|
|
@@ -13,7 +14,11 @@ export const organizationOf = (row) => ({
|
|
|
13
14
|
scope: row.scope,
|
|
14
15
|
createdAt: new Date(Number(row.created_at)).toISOString(),
|
|
15
16
|
});
|
|
16
|
-
export const organizationsStoreOver = (
|
|
17
|
+
export const organizationsStoreOver = (context) => ({
|
|
18
|
+
eraseOrganization: async (organization, by, { apply }) => write(context.database, () => eraseOrganization(context, organization, by, apply)),
|
|
19
|
+
...organizationsReadsOver(context),
|
|
20
|
+
});
|
|
21
|
+
const organizationsReadsOver = ({ database, now }) => ({
|
|
17
22
|
organizations: async (query, filter = {}) => {
|
|
18
23
|
const category = categorySql(filter, now());
|
|
19
24
|
const inCategory = category ? ` AND ${category.sql}` : "";
|
|
@@ -1,24 +1,12 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
2
|
import { OWN, referencesTo } from "./accounts.js";
|
|
3
|
-
import {
|
|
3
|
+
import { count, ErasePlan } from "./erase-plan.js";
|
|
4
4
|
import { forgetDanglingEvidence } from "./evidence-signature.js";
|
|
5
5
|
import { forgetErasedLinks } from "./identity-link-rows.js";
|
|
6
6
|
import { forgetErasedSplits } from "./identity-split-proposals.js";
|
|
7
7
|
import { currentPerson } from "./merged-persons.js";
|
|
8
8
|
import { ownerOnly } from "./person-merges.js";
|
|
9
9
|
import { forgetErasedTestData } from "./test-people.js";
|
|
10
|
-
/** Who wrote or approved a row: the row is the writer's history, never silently rewritten. */
|
|
11
|
-
const AUTHORS = new Set(["author", "confirmed_by", "decided_by", "proposed_by", "actor", "closed_by"]);
|
|
12
|
-
/** Business rows that cannot be without the person: the owner moves them first. */
|
|
13
|
-
const blocking = (table, pointer, database) => AUTHORS.has(pointer) || hasChildren(database, table);
|
|
14
|
-
const hasChildren = (database, table) => database
|
|
15
|
-
.prepare(`SELECT 1 FROM sqlite_master m JOIN pragma_foreign_key_list(m.name) f WHERE m.type = 'table' AND f."table" = ?
|
|
16
|
-
LIMIT 1`)
|
|
17
|
-
.get(table) !== undefined;
|
|
18
|
-
const nullable = (database, table, column) => Number(database.prepare(`SELECT "notnull" AS n FROM pragma_table_info('${table}') WHERE name = ?`).get(column)?.n) ===
|
|
19
|
-
0;
|
|
20
|
-
/** A link is about both ends: one that loses an end means nothing, so it goes. */
|
|
21
|
-
const LINK_ROWS = new Set(["links", "mentions", "task_assignments"]);
|
|
22
10
|
const personOf = (context, uid) => {
|
|
23
11
|
const asked = /^(?:person:)?([1-9]\d*)$/u.exec(uid.trim())?.[1];
|
|
24
12
|
const known = asked === undefined ? undefined : context.database.prepare("SELECT id FROM persons WHERE id = ?").get(Number(asked));
|
|
@@ -26,7 +14,6 @@ const personOf = (context, uid) => {
|
|
|
26
14
|
throw new CliError("not_found", `the store has no person for "${uid}"`, { reason: "person.unknown" });
|
|
27
15
|
return currentPerson(context.database, Number(known.id));
|
|
28
16
|
};
|
|
29
|
-
const count = (database, sql, ...params) => Number(database.prepare(`SELECT count(*) AS n FROM ${sql}`).get(...params)?.n);
|
|
30
17
|
/**
|
|
31
18
|
* Deletes a person and what memo knows of them alone: their identities, the tags, links, aliases and mentions that
|
|
32
19
|
* name them, their involvements. A fact, a note, a record or a task that names them stays and loses the name. A
|
|
@@ -57,35 +44,8 @@ export const erasePerson = (context, uid, by, apply) => {
|
|
|
57
44
|
const going = linked.filter(({ id }) => !named.has(id));
|
|
58
45
|
const kept = linked.filter(({ id }) => named.has(id));
|
|
59
46
|
const goingIds = JSON.stringify(going.map(({ id }) => id));
|
|
60
|
-
const
|
|
61
|
-
const detached =
|
|
62
|
-
const blocked = [];
|
|
63
|
-
const plan = [];
|
|
64
|
-
const pointing = (type, among, params) => {
|
|
65
|
-
for (const { table, pointer } of pointerPairs(database)) {
|
|
66
|
-
const where = `${pointer}_type = '${type}' AND ${pointer}_id IN (${among})`;
|
|
67
|
-
const n = count(database, `"${table}" WHERE ${where}`, ...params);
|
|
68
|
-
if (n === 0)
|
|
69
|
-
continue;
|
|
70
|
-
const key = `${table}.${pointer}`;
|
|
71
|
-
if (LINK_ROWS.has(table) || !nullable(database, table, `${pointer}_id`)) {
|
|
72
|
-
if (!LINK_ROWS.has(table) && blocking(table, pointer, database)) {
|
|
73
|
-
blocked.push(`${n} ${table} (${pointer})`);
|
|
74
|
-
continue;
|
|
75
|
-
}
|
|
76
|
-
deleted[key] = (deleted[key] ?? 0) + n;
|
|
77
|
-
plan.push(() => database.prepare(`DELETE FROM "${table}" WHERE ${where}`).run(...params));
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
detached[key] = (detached[key] ?? 0) + n;
|
|
81
|
-
plan.push(() => database
|
|
82
|
-
.prepare(`UPDATE "${table}" SET ${pointer}_type = NULL, ${pointer}_id = NULL WHERE ${where}`)
|
|
83
|
-
.run(...params));
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
pointing("person", "?", [person]);
|
|
88
|
-
pointing("identity", theirs, [goingIds]);
|
|
47
|
+
const plan = new ErasePlan(database).pointers("person", "?", [person]).pointers("identity", theirs, [goingIds]);
|
|
48
|
+
const { deleted, detached, blocked } = plan;
|
|
89
49
|
const involvements = count(database, `involvements WHERE person_id = ? OR identity_id IN (${theirs})`, person, goingIds);
|
|
90
50
|
if (involvements > 0)
|
|
91
51
|
deleted["involvements.person"] = (deleted["involvements.person"] ?? 0) + involvements;
|
|
@@ -105,8 +65,7 @@ export const erasePerson = (context, uid, by, apply) => {
|
|
|
105
65
|
});
|
|
106
66
|
if (!apply)
|
|
107
67
|
return answer(false);
|
|
108
|
-
|
|
109
|
-
step();
|
|
68
|
+
plan.run();
|
|
110
69
|
database.prepare(`DELETE FROM involvements WHERE person_id = ? OR identity_id IN (${theirs})`).run(person, goingIds);
|
|
111
70
|
database.prepare("UPDATE bots SET owner_person_id = NULL WHERE owner_person_id = ?").run(person);
|
|
112
71
|
for (const table of ["account_identities", "organization_identities", "identity_revisions"])
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wirecat/cli-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.723.0",
|
|
4
4
|
"description": "The messenger-neutral half of a messaging CLI: domain model, message locators, rendering, name resolution, the SQLite seam, the command skeleton and shared read commands, the send guard, run records and the message store",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Viacheslav Ptsarev",
|
|
@@ -334,7 +334,7 @@
|
|
|
334
334
|
"@types/pngjs": "6.0.5",
|
|
335
335
|
"@types/ssh2": "1.15.5",
|
|
336
336
|
"@vitest/coverage-v8": "^5.0.5",
|
|
337
|
-
"@wirecat/cli-base": "0.
|
|
337
|
+
"@wirecat/cli-base": "0.127.0",
|
|
338
338
|
"@wirecat/cli-core-legacy-tags": "npm:@wirecat/cli-core@0.414.0",
|
|
339
339
|
"ajv": "8.17.1",
|
|
340
340
|
"commander": "15.0.0",
|