@otea/cli 0.0.1 → 0.1.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/LICENSE +661 -0
- package/README.es.md +151 -0
- package/README.md +251 -3
- package/THIRD_PARTY_NOTICES.md +114 -0
- package/bin/otea.mjs +28 -0
- package/dist/app/agent-detail.js +26 -0
- package/dist/app/agent-draft.js +429 -0
- package/dist/app/agent-prompts.js +52 -0
- package/dist/app/agents.js +77 -0
- package/dist/app/asking.js +43 -0
- package/dist/app/board-archive.js +243 -0
- package/dist/app/board-migration.js +272 -0
- package/dist/app/boards.js +870 -0
- package/dist/app/branch-owner.js +48 -0
- package/dist/app/brief.js +53 -0
- package/dist/app/choices.js +61 -0
- package/dist/app/collections.js +101 -0
- package/dist/app/context.js +1 -0
- package/dist/app/crop.js +56 -0
- package/dist/app/devices.js +62 -0
- package/dist/app/draft-request.js +82 -0
- package/dist/app/events.js +12 -0
- package/dist/app/evidence.js +287 -0
- package/dist/app/gates.js +610 -0
- package/dist/app/guide.js +37 -0
- package/dist/app/home.js +145 -0
- package/dist/app/init-boards.js +36 -0
- package/dist/app/intake.js +25 -0
- package/dist/app/item-search.js +141 -0
- package/dist/app/items.js +318 -0
- package/dist/app/keep-waiting.js +44 -0
- package/dist/app/land-order.js +107 -0
- package/dist/app/language.js +52 -0
- package/dist/app/launcher.js +56 -0
- package/dist/app/lease-take.js +54 -0
- package/dist/app/leases.js +86 -0
- package/dist/app/machine.js +39 -0
- package/dist/app/merged.js +89 -0
- package/dist/app/migrate.js +171 -0
- package/dist/app/next.js +1637 -0
- package/dist/app/notes.js +319 -0
- package/dist/app/notify-targets.js +113 -0
- package/dist/app/opened.js +25 -0
- package/dist/app/owner-only.js +34 -0
- package/dist/app/performance.js +28 -0
- package/dist/app/phone.js +97 -0
- package/dist/app/presence.js +596 -0
- package/dist/app/preset-views.js +25 -0
- package/dist/app/presets.js +129 -0
- package/dist/app/priority.js +156 -0
- package/dist/app/profile.js +86 -0
- package/dist/app/project.js +247 -0
- package/dist/app/proofs.js +179 -0
- package/dist/app/questions.js +215 -0
- package/dist/app/reader-filter.js +58 -0
- package/dist/app/relations.js +164 -0
- package/dist/app/relay.js +98 -0
- package/dist/app/repos.js +176 -0
- package/dist/app/restart.js +92 -0
- package/dist/app/set-aside.js +64 -0
- package/dist/app/system-locale.js +6 -0
- package/dist/app/tailscale-cli.js +77 -0
- package/dist/app/tailscale-testing.js +41 -0
- package/dist/app/tailscale.js +177 -0
- package/dist/app/tips.js +30 -0
- package/dist/app/topics-everywhere.js +53 -0
- package/dist/app/topics.js +453 -0
- package/dist/app/undo.js +52 -0
- package/dist/app/validate.js +131 -0
- package/dist/app/watch.js +48 -0
- package/dist/cli/command.js +44 -0
- package/dist/cli/commands.js +3303 -0
- package/dist/cli/exit-codes.js +70 -0
- package/dist/cli/forge.js +176 -0
- package/dist/cli/format.js +340 -0
- package/dist/cli/gh.js +17 -0
- package/dist/cli/gitrepo.js +489 -0
- package/dist/cli/help.js +101 -0
- package/dist/cli/hooks.js +232 -0
- package/dist/cli/main.js +37 -0
- package/dist/cli/merged-outside.js +58 -0
- package/dist/cli/names.js +154 -0
- package/dist/cli/opener.js +21 -0
- package/dist/cli/owner.js +154 -0
- package/dist/cli/phone.js +221 -0
- package/dist/cli/ports.js +24 -0
- package/dist/cli/probe.js +18 -0
- package/dist/cli/run.js +246 -0
- package/dist/cli/serve-file.js +21 -0
- package/dist/cli/stats.js +55 -0
- package/dist/cli/version.js +3 -0
- package/dist/core/agent-guide.js +31 -0
- package/dist/core/agent-prompts.js +139 -0
- package/dist/core/authority.js +94 -0
- package/dist/core/board/analysis.js +86 -0
- package/dist/core/board/choices.js +100 -0
- package/dist/core/board/compile.js +362 -0
- package/dist/core/board/engine.js +523 -0
- package/dist/core/board/hints.js +46 -0
- package/dist/core/board/history.js +146 -0
- package/dist/core/board/ids.js +87 -0
- package/dist/core/board/joins.js +30 -0
- package/dist/core/board/mentions.js +68 -0
- package/dist/core/board/merged.js +18 -0
- package/dist/core/board/model.js +135 -0
- package/dist/core/board/next.js +1116 -0
- package/dist/core/board/notes.js +298 -0
- package/dist/core/board/performance.js +218 -0
- package/dist/core/board/plan.js +299 -0
- package/dist/core/board/problems.js +232 -0
- package/dist/core/board/proofs.js +84 -0
- package/dist/core/board/questions.js +114 -0
- package/dist/core/board/reading.js +220 -0
- package/dist/core/board/readings.js +534 -0
- package/dist/core/board/schema.js +335 -0
- package/dist/core/board/shape-words.js +179 -0
- package/dist/core/board/template-words.js +23 -0
- package/dist/core/board/templates-catalogue.js +965 -0
- package/dist/core/board/templates-coverage.js +261 -0
- package/dist/core/board/templates-flows.js +779 -0
- package/dist/core/board/templates.js +586 -0
- package/dist/core/brief.js +93 -0
- package/dist/core/diagram.js +80 -0
- package/dist/core/errors.js +12 -0
- package/dist/core/evidence.js +33 -0
- package/dist/core/fields.js +129 -0
- package/dist/core/hierarchy.js +30 -0
- package/dist/core/icons.js +181 -0
- package/dist/core/image-crop.js +166 -0
- package/dist/core/item.js +49 -0
- package/dist/core/lease.js +31 -0
- package/dist/core/modules.js +318 -0
- package/dist/core/notify/planner.js +117 -0
- package/dist/core/notify/signals.js +66 -0
- package/dist/core/notify/text.js +60 -0
- package/dist/core/path-key.js +22 -0
- package/dist/core/placement.js +161 -0
- package/dist/core/preset-labels.js +66 -0
- package/dist/core/preset-notes.js +109 -0
- package/dist/core/preset.js +541 -0
- package/dist/core/profile.js +55 -0
- package/dist/core/project-id.js +20 -0
- package/dist/core/refusal.js +1 -0
- package/dist/core/relations.js +208 -0
- package/dist/core/repo.js +54 -0
- package/dist/core/samples.js +161 -0
- package/dist/core/search-words.js +68 -0
- package/dist/core/test-report.js +120 -0
- package/dist/core/time.js +8 -0
- package/dist/core/validation.js +60 -0
- package/dist/core/views.js +171 -0
- package/dist/core/workflow.js +51 -0
- package/dist/mcp/commands.js +101 -0
- package/dist/mcp/server.js +239 -0
- package/dist/mcp/words.js +95 -0
- package/dist/server/boards.js +232 -0
- package/dist/server/changes.js +42 -0
- package/dist/server/diff-tip.js +83 -0
- package/dist/server/drift.js +87 -0
- package/dist/server/errors.js +87 -0
- package/dist/server/evidence.js +82 -0
- package/dist/server/hosts.js +6 -0
- package/dist/server/hub.js +98 -0
- package/dist/server/lan-access.js +62 -0
- package/dist/server/lan.js +164 -0
- package/dist/server/listen.js +70 -0
- package/dist/server/notifier.js +156 -0
- package/dist/server/notify-api.js +151 -0
- package/dist/server/pairing.js +124 -0
- package/dist/server/projects.js +191 -0
- package/dist/server/push.js +199 -0
- package/dist/server/server.js +960 -0
- package/dist/server/session.js +43 -0
- package/dist/server/stream.js +36 -0
- package/dist/server/test-push.js +38 -0
- package/dist/server/token.js +37 -0
- package/dist/server/transports.js +54 -0
- package/dist/server/ui.js +44 -0
- package/dist/server/writes.js +361 -0
- package/dist/server/wrong-address.js +36 -0
- package/dist/store/boards.js +111 -0
- package/dist/store/database.js +45 -0
- package/dist/store/machine.js +77 -0
- package/dist/store/migrations.js +493 -0
- package/dist/store/queries.js +585 -0
- package/dist/store/registry.js +72 -0
- package/package.json +72 -4
- package/presets/architecture/playbook.md +69 -0
- package/presets/architecture/preset.yaml +80 -0
- package/presets/architecture/samples.yaml +23 -0
- package/presets/bug-triage/playbook.md +129 -0
- package/presets/bug-triage/preset.yaml +127 -0
- package/presets/bug-triage/samples/wall-slide-after.png +0 -0
- package/presets/bug-triage/samples/wall-slide-before.png +0 -0
- package/presets/bug-triage/samples.yaml +72 -0
- package/presets/change/playbook.md +115 -0
- package/presets/change/preset.yaml +141 -0
- package/presets/change/samples.yaml +62 -0
- package/presets/review/playbook.md +51 -0
- package/presets/review/preset.yaml +62 -0
- package/presets/review/samples.yaml +27 -0
- package/presets/tasks/playbook.md +78 -0
- package/presets/tasks/preset.yaml +61 -0
- package/presets/tasks/samples.yaml +46 -0
- package/schemas/board.schema.json +764 -0
- package/ui/dist/THIRD_PARTY_NOTICES.md +114 -0
- package/ui/dist/assets/dm-mono-latin-400-normal--0xN8mdc.woff +0 -0
- package/ui/dist/assets/dm-mono-latin-400-normal-4GdczIuU.woff2 +0 -0
- package/ui/dist/assets/dm-mono-latin-500-normal-CN8Miw6E.woff +0 -0
- package/ui/dist/assets/dm-mono-latin-500-normal-DRMDZjhP.woff2 +0 -0
- package/ui/dist/assets/dm-mono-latin-ext-400-normal-1aZr6b2b.woff +0 -0
- package/ui/dist/assets/dm-mono-latin-ext-400-normal-C2zvOubV.woff2 +0 -0
- package/ui/dist/assets/dm-mono-latin-ext-500-normal-BtRyHRi6.woff2 +0 -0
- package/ui/dist/assets/dm-mono-latin-ext-500-normal-Dw3M13d8.woff +0 -0
- package/ui/dist/assets/index-C3OzJope.js +103 -0
- package/ui/dist/assets/index-COdEjckQ.css +1 -0
- package/ui/dist/assets/newsreader-latin-ext-opsz-italic-CSPGnFdO.woff2 +0 -0
- package/ui/dist/assets/newsreader-latin-ext-opsz-normal-BQn1nviT.woff2 +0 -0
- package/ui/dist/assets/newsreader-latin-opsz-italic-kJKFiXvB.woff2 +0 -0
- package/ui/dist/assets/newsreader-latin-opsz-normal-s-izfB6B.woff2 +0 -0
- package/ui/dist/assets/newsreader-vietnamese-opsz-italic-DTr-ymY4.woff2 +0 -0
- package/ui/dist/assets/newsreader-vietnamese-opsz-normal-BjhtXyW2.woff2 +0 -0
- package/ui/dist/assets/pdf-YwugTw7Y.js +54 -0
- package/ui/dist/assets/pdf-engine-CkBopwck.js +1 -0
- package/ui/dist/assets/pdf-rELvHkft.js +56 -0
- package/ui/dist/assets/pdf.worker.min-B5Yc7dP4.mjs +27 -0
- package/ui/dist/assets/pdf.worker.min-B7U8ptvh.js +1 -0
- package/ui/dist/assets/pdf.worker.min-CIqJB0yW.mjs +28 -0
- package/ui/dist/assets/pdf.worker.min-UqOn5EDC.js +1 -0
- package/ui/dist/assets/schibsted-grotesk-latin-ext-wght-normal-hsMS0n0O.woff2 +0 -0
- package/ui/dist/assets/schibsted-grotesk-latin-wght-normal-Bb8VGrTG.woff2 +0 -0
- package/ui/dist/icons/apple-touch-icon.png +0 -0
- package/ui/dist/icons/otea-192.png +0 -0
- package/ui/dist/icons/otea-512.png +0 -0
- package/ui/dist/icons/otea-badge.png +0 -0
- package/ui/dist/icons/otea-maskable-512.png +0 -0
- package/ui/dist/index.html +25 -0
- package/ui/dist/manifest.webmanifest +24 -0
- package/ui/dist/otea-idle.svg +5 -0
- package/ui/dist/otea-working.svg +5 -0
- package/ui/dist/otea-yours-working.svg +5 -0
- package/ui/dist/otea-yours.svg +5 -0
- package/ui/dist/sw.js +1 -0
|
@@ -0,0 +1,870 @@
|
|
|
1
|
+
import { compileBoard } from '../core/board/compile.js';
|
|
2
|
+
import { isClosed, positionOf, stateOf } from '../core/board/engine.js';
|
|
3
|
+
import { newId } from '../core/board/ids.js';
|
|
4
|
+
import { DONE, guardedBranch } from '../core/board/model.js';
|
|
5
|
+
import { changePlan, itemEffects, itemMoves, } from '../core/board/plan.js';
|
|
6
|
+
import { blocking, problemsOf } from '../core/board/problems.js';
|
|
7
|
+
import { stateName } from '../core/board/reading.js';
|
|
8
|
+
import { parseDefinition, readDefinition } from '../core/board/schema.js';
|
|
9
|
+
import { blankBoard, boardLang, instantiate, instantiateSet, listedTemplates, requestsBoard, requireTemplate, TEMPLATE_SETS, TEMPLATES, } from '../core/board/templates.js';
|
|
10
|
+
import { OteaError } from '../core/errors.js';
|
|
11
|
+
import { boardsStamp, deleteBoard, deleteDraft, findBoard, findBoardOfCollection, findDraft, findVersion, insertBoard, insertVersion, listBoards, listVersions, maxVersion, renameCollection, retireBoard, setBoardCollection, setLiveVersion, upsertDraft, } from '../store/boards.js';
|
|
12
|
+
import { transaction } from '../store/database.js';
|
|
13
|
+
import { appendEvent, clearLease, collectionTaken, deleteCollection, findItem, insertCollection, listCollections, selectItems, setCollectionPreset, updateItemContent, updateItemState, } from '../store/queries.js';
|
|
14
|
+
import { draftTitle } from './draft-request.js';
|
|
15
|
+
import { systemClock } from './items.js';
|
|
16
|
+
import { linksOf, liveBoards } from './next.js';
|
|
17
|
+
function versionInfo(row) {
|
|
18
|
+
return { version: row.version, publishedAt: row.publishedAt, publishedBy: row.publishedBy };
|
|
19
|
+
}
|
|
20
|
+
function recordOf(ctx, row) {
|
|
21
|
+
const live = row.liveVersion === null ? undefined : findVersion(ctx.db, row.id, row.liveVersion);
|
|
22
|
+
const draft = findDraft(ctx.db, row.id);
|
|
23
|
+
return {
|
|
24
|
+
id: row.id,
|
|
25
|
+
collection: row.collection,
|
|
26
|
+
builtin: row.builtin,
|
|
27
|
+
live: live ? { ...versionInfo(live), definition: readDefinition(live.definition) } : null,
|
|
28
|
+
draft: draft
|
|
29
|
+
? {
|
|
30
|
+
definition: readDefinition(draft.definition),
|
|
31
|
+
baseVersion: draft.baseVersion,
|
|
32
|
+
updatedAt: draft.updatedAt,
|
|
33
|
+
requestItem: draft.requestItem,
|
|
34
|
+
agentNote: draft.agentNote === null ? null : JSON.parse(draft.agentNote),
|
|
35
|
+
}
|
|
36
|
+
: null,
|
|
37
|
+
versions: listVersions(ctx.db, row.id).map(versionInfo),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function requireRow(ctx, id) {
|
|
41
|
+
const row = findBoard(ctx.db, id);
|
|
42
|
+
if (!row)
|
|
43
|
+
throw new OteaError('not_found', `There is no board ${id}`);
|
|
44
|
+
return row;
|
|
45
|
+
}
|
|
46
|
+
// A board Studio may change: an archived one is read-only until it is restored (board-archive.ts).
|
|
47
|
+
function requireStanding(ctx, id) {
|
|
48
|
+
const row = requireRow(ctx, id);
|
|
49
|
+
if (row.archivedAt !== null) {
|
|
50
|
+
const board = row.collection ?? id;
|
|
51
|
+
throw new OteaError('board_archived', `${board} is archived: restore it to change it`, {
|
|
52
|
+
kind: 'board_archived',
|
|
53
|
+
board,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return row;
|
|
57
|
+
}
|
|
58
|
+
export function getBoardRecord(ctx, id) {
|
|
59
|
+
return recordOf(ctx, requireRow(ctx, id));
|
|
60
|
+
}
|
|
61
|
+
function openCount(ctx, collection) {
|
|
62
|
+
if (collection === null)
|
|
63
|
+
return 0;
|
|
64
|
+
return selectItems(ctx.db, { collection }).filter(({ state }) => !isClosed(positionOf(state)))
|
|
65
|
+
.length;
|
|
66
|
+
}
|
|
67
|
+
function statusOf(record) {
|
|
68
|
+
if (record.draft?.requestItem && record.draft.agentNote === null)
|
|
69
|
+
return 'waiting';
|
|
70
|
+
if (!record.live)
|
|
71
|
+
return 'draft';
|
|
72
|
+
return record.draft ? 'changed' : 'live';
|
|
73
|
+
}
|
|
74
|
+
export function listBoardSummaries(ctx) {
|
|
75
|
+
return listBoards(ctx.db).map((row) => {
|
|
76
|
+
const record = recordOf(ctx, row);
|
|
77
|
+
const definition = (record.draft ?? record.live)?.definition;
|
|
78
|
+
if (!definition)
|
|
79
|
+
throw new OteaError('not_found', `Board ${row.id} has neither draft nor version`);
|
|
80
|
+
return {
|
|
81
|
+
id: row.id,
|
|
82
|
+
name: definition.name,
|
|
83
|
+
item: definition.item,
|
|
84
|
+
icon: definition.icon,
|
|
85
|
+
builtin: row.builtin,
|
|
86
|
+
status: statusOf(record),
|
|
87
|
+
liveVersion: record.live?.version ?? null,
|
|
88
|
+
collection: row.collection,
|
|
89
|
+
open: openCount(ctx, row.collection),
|
|
90
|
+
requestItem: record.draft?.requestItem ?? null,
|
|
91
|
+
definition,
|
|
92
|
+
};
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
// Every board name in the project but `except`'s, live and draft, plus collections not on a board.
|
|
96
|
+
function namesTaken(ctx, except) {
|
|
97
|
+
const names = listBoards(ctx.db, { archived: true })
|
|
98
|
+
.filter(({ id }) => id !== except)
|
|
99
|
+
.flatMap((row) => {
|
|
100
|
+
const record = recordOf(ctx, row);
|
|
101
|
+
return [record.live?.definition.name, record.draft?.definition.name].filter((name) => name !== undefined);
|
|
102
|
+
});
|
|
103
|
+
const loose = listCollections(ctx.db)
|
|
104
|
+
.filter(({ name }) => !findBoardOfCollection(ctx.db, name))
|
|
105
|
+
.map(({ name }) => name);
|
|
106
|
+
return [...names, ...loose];
|
|
107
|
+
}
|
|
108
|
+
const same = (a, b) => a.trim().toLocaleLowerCase() === b.trim().toLocaleLowerCase();
|
|
109
|
+
// Every board as a split reads it, by its draft (the person's latest words) or its live version;
|
|
110
|
+
// `as` stands in for board `id`'s own definition.
|
|
111
|
+
export function boardRefs(ctx, id, as) {
|
|
112
|
+
return listBoards(ctx.db).flatMap((row) => {
|
|
113
|
+
const record = recordOf(ctx, row);
|
|
114
|
+
const definition = row.id === id && as ? as : (record.draft ?? record.live)?.definition;
|
|
115
|
+
if (!definition)
|
|
116
|
+
return [];
|
|
117
|
+
return [
|
|
118
|
+
{
|
|
119
|
+
id: row.id,
|
|
120
|
+
name: definition.name,
|
|
121
|
+
...(definition.split ? { split: definition.split } : {}),
|
|
122
|
+
},
|
|
123
|
+
];
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
export function boardProblems(ctx, id, definition) {
|
|
127
|
+
return problemsOf(definition, {
|
|
128
|
+
takenNames: namesTaken(ctx, id),
|
|
129
|
+
id,
|
|
130
|
+
boards: boardRefs(ctx, id, definition),
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
export function createBoard(ctx, input, now = systemClock, ids = newId) {
|
|
134
|
+
return transaction(ctx.db, () => insertNewBoard(ctx, input, now, ids));
|
|
135
|
+
}
|
|
136
|
+
// Creates the board inside the caller's transaction, so it can go with what asked for it.
|
|
137
|
+
export function insertNewBoard(ctx, input, now = systemClock, ids = newId) {
|
|
138
|
+
const name = input.name.trim();
|
|
139
|
+
if (name === '')
|
|
140
|
+
throw new OteaError('invalid_name', 'A board needs a name', { kind: 'board_name_empty' });
|
|
141
|
+
{
|
|
142
|
+
if (namesTaken(ctx).some((taken) => same(taken, name)))
|
|
143
|
+
throw new OteaError('already_exists', `There is already a board called ${name}`, {
|
|
144
|
+
kind: 'board_name_taken',
|
|
145
|
+
name,
|
|
146
|
+
});
|
|
147
|
+
const names = { name, ...(input.item?.trim() ? { item: input.item.trim() } : {}) };
|
|
148
|
+
let definition;
|
|
149
|
+
if (input.template !== undefined) {
|
|
150
|
+
// A board that only comes in a set is never offered on its own.
|
|
151
|
+
const template = TEMPLATES.find(({ key, hidden }) => key === input.template && !hidden);
|
|
152
|
+
if (!template)
|
|
153
|
+
throw new OteaError('not_found', `There is no template ${input.template}`);
|
|
154
|
+
definition = instantiate(template, input.lang, names, ids);
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
definition = blankBoard(input.lang, names, ids);
|
|
158
|
+
}
|
|
159
|
+
const id = ids('b');
|
|
160
|
+
const at = now();
|
|
161
|
+
insertBoard(ctx.db, { id, createdAt: at, builtin: false });
|
|
162
|
+
upsertDraft(ctx.db, {
|
|
163
|
+
boardId: id,
|
|
164
|
+
definition: JSON.stringify(definition),
|
|
165
|
+
baseVersion: null,
|
|
166
|
+
updatedAt: at,
|
|
167
|
+
requestItem: input.requestItem ?? null,
|
|
168
|
+
agentNote: null,
|
|
169
|
+
});
|
|
170
|
+
return getBoardRecord(ctx, id);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
function assertDraftUnchanged(ctx, id, stamp) {
|
|
174
|
+
if (stamp === undefined)
|
|
175
|
+
return;
|
|
176
|
+
const current = findDraft(ctx.db, id)?.updatedAt ?? null;
|
|
177
|
+
if (current !== stamp)
|
|
178
|
+
throw new OteaError('board_conflict', 'The draft changed since you opened it (an agent handed in its board, or another window saved it): reload to see it', { kind: 'draft_changed' });
|
|
179
|
+
}
|
|
180
|
+
// Writes the whole draft. `baseVersion` is the live version the editor started from: when another
|
|
181
|
+
// publish happened since, the write is refused, so nobody publishes over changes they never saw.
|
|
182
|
+
export function saveDraft(ctx, id, input, now = systemClock, agentNote) {
|
|
183
|
+
const reading = parseDefinition(input.definition);
|
|
184
|
+
if (!reading.ok)
|
|
185
|
+
throw new OteaError('invalid_board', `The draft is not a board: ${reading.problems.join('; ')}`, { kind: 'draft_invalid' });
|
|
186
|
+
return transaction(ctx.db, () => {
|
|
187
|
+
const row = requireStanding(ctx, id);
|
|
188
|
+
if (row.liveVersion !== input.baseVersion)
|
|
189
|
+
throw new OteaError('board_conflict', `The board changed since this draft started: it is at version ${row.liveVersion ?? 'none'} now`, { kind: 'board_changed', version: row.liveVersion });
|
|
190
|
+
assertDraftUnchanged(ctx, id, input.draftStamp);
|
|
191
|
+
const previous = findDraft(ctx.db, id);
|
|
192
|
+
upsertDraft(ctx.db, {
|
|
193
|
+
boardId: id,
|
|
194
|
+
definition: JSON.stringify(reading.definition),
|
|
195
|
+
baseVersion: input.baseVersion,
|
|
196
|
+
updatedAt: now(),
|
|
197
|
+
requestItem: previous?.requestItem ?? null,
|
|
198
|
+
agentNote: agentNote ? JSON.stringify(agentNote) : (previous?.agentNote ?? null),
|
|
199
|
+
});
|
|
200
|
+
if (previous?.requestItem)
|
|
201
|
+
retitleRequest(ctx, previous.requestItem, readDefinition(previous.definition), reading.definition, now);
|
|
202
|
+
return {
|
|
203
|
+
record: getBoardRecord(ctx, id),
|
|
204
|
+
problems: boardProblems(ctx, id, reading.definition),
|
|
205
|
+
...(reading.notes ? { notes: reading.notes } : {}),
|
|
206
|
+
};
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
// A board renamed while it waits on its Pedido: the Pedido's title names it as it is called now,
|
|
210
|
+
// unless someone gave the Pedido a title of their own (DR-22).
|
|
211
|
+
function retitleRequest(ctx, item, before, after, now) {
|
|
212
|
+
if (before.name.trim() === after.name.trim())
|
|
213
|
+
return;
|
|
214
|
+
const pedido = findItem(ctx.db, item);
|
|
215
|
+
const lang = boardLang(before);
|
|
216
|
+
if (!pedido || pedido.title !== draftTitle(lang, before.name))
|
|
217
|
+
return;
|
|
218
|
+
if (isClosed(positionOf(pedido.state)))
|
|
219
|
+
return;
|
|
220
|
+
const title = draftTitle(lang, after.name);
|
|
221
|
+
const at = now();
|
|
222
|
+
updateItemContent(ctx.db, item, {
|
|
223
|
+
title,
|
|
224
|
+
fields: pedido.fields,
|
|
225
|
+
presetVersion: pedido.presetVersion,
|
|
226
|
+
updatedAt: at,
|
|
227
|
+
});
|
|
228
|
+
appendEvent(ctx.db, {
|
|
229
|
+
itemId: item,
|
|
230
|
+
type: 'updated',
|
|
231
|
+
actor: { kind: 'human', name: 'human' },
|
|
232
|
+
at,
|
|
233
|
+
data: { changes: { title: { from: pedido.title, to: title } } },
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
// Discards the draft; a board that was never published goes with it.
|
|
237
|
+
export function discardDraft(ctx, id, now = systemClock) {
|
|
238
|
+
return transaction(ctx.db, () => {
|
|
239
|
+
const row = requireStanding(ctx, id);
|
|
240
|
+
// A board never published takes with it the drafts its template set made with it that were
|
|
241
|
+
// never published either (Implementación with Ideas): none is left over on its own (P4-16).
|
|
242
|
+
const mates = row.liveVersion === null ? (getBoardRecord(ctx, id).draft?.definition.madeWith ?? []) : [];
|
|
243
|
+
const drop = (board) => {
|
|
244
|
+
deleteDraft(ctx.db, board);
|
|
245
|
+
if (maxVersion(ctx.db, board) === 0)
|
|
246
|
+
deleteBoard(ctx.db, board);
|
|
247
|
+
else
|
|
248
|
+
retireBoard(ctx.db, board, now());
|
|
249
|
+
};
|
|
250
|
+
if (row.liveVersion === null) {
|
|
251
|
+
drop(id);
|
|
252
|
+
for (const mate of mates) {
|
|
253
|
+
const other = findBoard(ctx.db, mate);
|
|
254
|
+
if (other && other.liveVersion === null && other.archivedAt === null)
|
|
255
|
+
drop(mate);
|
|
256
|
+
}
|
|
257
|
+
return { deleted: true };
|
|
258
|
+
}
|
|
259
|
+
deleteDraft(ctx.db, id);
|
|
260
|
+
return { deleted: false };
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
function requireDraft(ctx, id) {
|
|
264
|
+
const draft = getBoardRecord(ctx, id).draft;
|
|
265
|
+
if (!draft)
|
|
266
|
+
throw new OteaError('not_found', 'The board has no draft to publish');
|
|
267
|
+
return draft;
|
|
268
|
+
}
|
|
269
|
+
// Where each item of the board stands, with how many of its parts and its blockers are open (what
|
|
270
|
+
// a step that starts waiting for them would hold).
|
|
271
|
+
function itemStates(ctx, collection) {
|
|
272
|
+
if (collection === null)
|
|
273
|
+
return [];
|
|
274
|
+
return selectItems(ctx.db, { collection }).map(({ id, state }) => {
|
|
275
|
+
const { parts, blockers } = linksOf(ctx, id);
|
|
276
|
+
const open = (linked) => linked.filter(({ closed }) => !closed).length;
|
|
277
|
+
return { id, state, openParts: open(parts), openBlockers: open(blockers) };
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
export function publishPlan(ctx, id) {
|
|
281
|
+
const record = getBoardRecord(ctx, id);
|
|
282
|
+
const { definition: draft, requestItem } = requireDraft(ctx, id);
|
|
283
|
+
const live = record.live?.definition ?? null;
|
|
284
|
+
const items = itemStates(ctx, record.collection);
|
|
285
|
+
return {
|
|
286
|
+
from: record.live?.version ?? null,
|
|
287
|
+
to: maxVersion(ctx.db, id) + 1,
|
|
288
|
+
changes: changePlan(live, draft, {
|
|
289
|
+
boardName: (other) => boardRefs(ctx, id, draft).find((ref) => ref.id === other)?.name,
|
|
290
|
+
}),
|
|
291
|
+
effects: live
|
|
292
|
+
? itemEffects(live, draft, items)
|
|
293
|
+
: { moves: [], needs: [], reopened: [], waiting: [], frozen: [] },
|
|
294
|
+
problems: boardProblems(ctx, id, draft),
|
|
295
|
+
request: openRequest(ctx, requestItem),
|
|
296
|
+
guardingBoards: liveBoards(ctx)
|
|
297
|
+
.filter((board) => board.id !== id && guardedBranch(board.def))
|
|
298
|
+
.map((board) => ({ id: board.id, name: board.def.name })),
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
// The Pedido a draft was asked on, while it is open.
|
|
302
|
+
function openRequest(ctx, id) {
|
|
303
|
+
const item = id === null ? undefined : findItem(ctx.db, id);
|
|
304
|
+
if (!item || isClosed(positionOf(item.state)))
|
|
305
|
+
return null;
|
|
306
|
+
return { id: item.id, title: item.title };
|
|
307
|
+
}
|
|
308
|
+
// What closing the Pedido on publish says, in the board's language.
|
|
309
|
+
const REQUEST_CLOSED = {
|
|
310
|
+
en: (name) => `Published the board ${name}`,
|
|
311
|
+
es: (name) => `Publicado el tablero ${name}`,
|
|
312
|
+
};
|
|
313
|
+
// Prefixes nobody wants on every id, in English or Spanish.
|
|
314
|
+
const AWKWARD = new Set(['ASS', 'CUL', 'FAG', 'FUC', 'FUK', 'KKK', 'NIG', 'PUT', 'SEX', 'TIT']);
|
|
315
|
+
// The first three letters of a word that do not read as an awkward word: the first letter kept,
|
|
316
|
+
// then the next letters in order (ASSET is ASE, PUTTER is PUE); the first three when every pick is.
|
|
317
|
+
function pleasantPrefix(word) {
|
|
318
|
+
const first = word.slice(0, 3);
|
|
319
|
+
if (word.length <= 3 || !AWKWARD.has(first))
|
|
320
|
+
return first;
|
|
321
|
+
for (let i = 1; i < word.length; i++)
|
|
322
|
+
for (let j = i + 1; j < word.length; j++) {
|
|
323
|
+
const pick = `${word[0]}${word[i]}${word[j]}`;
|
|
324
|
+
if (!AWKWARD.has(pick))
|
|
325
|
+
return pick;
|
|
326
|
+
}
|
|
327
|
+
return first;
|
|
328
|
+
}
|
|
329
|
+
// A prefix for a new board's item ids: the first letters of what each item is called.
|
|
330
|
+
export function prefixOf(ctx, item, name) {
|
|
331
|
+
const letters = (text) => text
|
|
332
|
+
.normalize('NFD')
|
|
333
|
+
.replace(/[̀-ͯ]/g, '')
|
|
334
|
+
.toUpperCase()
|
|
335
|
+
.replace(/[^A-Z0-9]/g, '');
|
|
336
|
+
const base = pleasantPrefix(letters(item) || letters(name) || 'ITM');
|
|
337
|
+
const free = (prefix) => !collectionTaken(ctx.db, '\u0000', prefix);
|
|
338
|
+
if (free(base))
|
|
339
|
+
return base;
|
|
340
|
+
// Taken: the initials of the name's later words tell it apart (Cambios web is CAMW), words of
|
|
341
|
+
// one or two letters (de, la) aside; then one letter more of the item (Camb is CAMB); then a
|
|
342
|
+
// number.
|
|
343
|
+
const initials = name
|
|
344
|
+
.split(/\s+/)
|
|
345
|
+
.slice(1)
|
|
346
|
+
.map(letters)
|
|
347
|
+
.filter((word) => word.length > 2)
|
|
348
|
+
.map((word) => word[0])
|
|
349
|
+
.join('');
|
|
350
|
+
if (initials !== '' && free(`${base}${initials}`))
|
|
351
|
+
return `${base}${initials}`;
|
|
352
|
+
const word = letters(item) || letters(name);
|
|
353
|
+
const longer = `${base}${word.slice(base.length, base.length + 1)}`;
|
|
354
|
+
if (longer.length > base.length && free(longer))
|
|
355
|
+
return longer;
|
|
356
|
+
for (let n = 1;; n++) {
|
|
357
|
+
const prefix = n === 1 ? base : `${base}${n}`;
|
|
358
|
+
if (!collectionTaken(ctx.db, '\u0000', prefix))
|
|
359
|
+
return prefix;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
// The prefix the person chose for a new board's ids: two to six letters or digits, starting with a
|
|
363
|
+
// letter, and no other board's.
|
|
364
|
+
function chosenPrefix(ctx, wanted) {
|
|
365
|
+
const prefix = wanted.trim().toUpperCase();
|
|
366
|
+
if (!/^[A-Z][A-Z0-9]{1,5}$/.test(prefix))
|
|
367
|
+
throw new OteaError('invalid_name', `The prefix ${wanted.trim() || '""'} is not 2 to 6 letters or digits starting with a letter`);
|
|
368
|
+
if (collectionTaken(ctx.db, '\u0000', prefix))
|
|
369
|
+
throw new OteaError('already_exists', `Another board's ids already start with ${prefix}`);
|
|
370
|
+
return prefix;
|
|
371
|
+
}
|
|
372
|
+
// `boards`: the definitions the moves leave and land in, whose step names the events keep, so the
|
|
373
|
+
// history still reads them once a step is deleted.
|
|
374
|
+
function moveItems(ctx, moves, by, at, data, boards) {
|
|
375
|
+
const held = new Map(itemsWithLease(ctx, moves.map(({ id }) => id)).map(({ id, holder }) => [id, holder]));
|
|
376
|
+
for (const move of moves) {
|
|
377
|
+
updateItemState(ctx.db, move.id, move.to, at);
|
|
378
|
+
appendEvent(ctx.db, {
|
|
379
|
+
itemId: move.id,
|
|
380
|
+
type: 'transitioned',
|
|
381
|
+
actor: by,
|
|
382
|
+
at,
|
|
383
|
+
data: {
|
|
384
|
+
from: move.from,
|
|
385
|
+
to: move.to,
|
|
386
|
+
...(boards
|
|
387
|
+
? { fromName: stateName(boards.from, move.from), toName: stateName(boards.to, move.to) }
|
|
388
|
+
: {}),
|
|
389
|
+
channel: 'ui',
|
|
390
|
+
...data,
|
|
391
|
+
},
|
|
392
|
+
});
|
|
393
|
+
// The agent holding it was working on the step it left: the lease goes with that work.
|
|
394
|
+
const holder = held.get(move.id);
|
|
395
|
+
if (holder !== undefined) {
|
|
396
|
+
clearLease(ctx.db, move.id);
|
|
397
|
+
appendEvent(ctx.db, {
|
|
398
|
+
itemId: move.id,
|
|
399
|
+
type: 'released',
|
|
400
|
+
actor: by,
|
|
401
|
+
at,
|
|
402
|
+
data: { holder, cause: String(data.reason ?? 'publish') },
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
function itemsWithLease(ctx, ids) {
|
|
408
|
+
if (ids.length === 0)
|
|
409
|
+
return [];
|
|
410
|
+
const rows = ctx.db
|
|
411
|
+
.prepare(`SELECT id, lease_holder AS holder FROM items
|
|
412
|
+
WHERE lease_holder IS NOT NULL AND id IN (${ids.map(() => '?').join(', ')})`)
|
|
413
|
+
.all(...ids);
|
|
414
|
+
return rows.map((row) => ({ id: String(row.id), holder: String(row.holder) }));
|
|
415
|
+
}
|
|
416
|
+
// Publishes the draft as the next version, in one transaction: the board's collection (created on
|
|
417
|
+
// a first publish, renamed with the board), the version, the item moves and their events.
|
|
418
|
+
export function publishBoard(ctx, id, input, now = systemClock) {
|
|
419
|
+
const { alongside = [], ...own } = input;
|
|
420
|
+
const result = transaction(ctx.db, () => {
|
|
421
|
+
assertMadeWith(ctx, id, alongside, 'publish');
|
|
422
|
+
const mates = alongside.map((mate) => publishWithin(ctx, mate, { moves: {}, by: own.by, ...(own.closeRequest ? { closeRequest: true } : {}) }, now));
|
|
423
|
+
const main = publishWithin(ctx, id, own, now);
|
|
424
|
+
return mates.length ? { ...main, alongside: mates } : main;
|
|
425
|
+
});
|
|
426
|
+
syncBoardPresets(ctx);
|
|
427
|
+
// Each record was read before the next publish finished: read them again.
|
|
428
|
+
if (!result.alongside)
|
|
429
|
+
return result;
|
|
430
|
+
return {
|
|
431
|
+
...result,
|
|
432
|
+
record: getBoardRecord(ctx, id),
|
|
433
|
+
alongside: result.alongside.map((mate) => ({
|
|
434
|
+
...mate,
|
|
435
|
+
record: getBoardRecord(ctx, mate.record.id),
|
|
436
|
+
})),
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
// The boards published or undone with `id`: each one its template set made with it, and each one
|
|
440
|
+
// not yet published (publish) or still on the first version it went live with (restore).
|
|
441
|
+
function assertMadeWith(ctx, id, mates, action) {
|
|
442
|
+
if (mates.length === 0)
|
|
443
|
+
return;
|
|
444
|
+
const record = getBoardRecord(ctx, id);
|
|
445
|
+
const made = new Set((record.draft ?? record.live)?.definition.madeWith ?? []);
|
|
446
|
+
for (const mate of mates) {
|
|
447
|
+
const row = requireStanding(ctx, mate);
|
|
448
|
+
if (mate === id || !made.has(mate))
|
|
449
|
+
throw new OteaError('invalid_board', `Board ${mate} was not made with board ${id}`);
|
|
450
|
+
if (action === 'publish' && row.liveVersion !== null)
|
|
451
|
+
throw new OteaError('invalid_board', `Board ${mate} is already published`);
|
|
452
|
+
if (action === 'restore' && row.liveVersion !== 1)
|
|
453
|
+
throw new OteaError('not_undoable', `Board ${mate} is not on its first version`);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
// The body of a publish, inside a transaction the caller holds.
|
|
457
|
+
function publishWithin(ctx, id, input, now) {
|
|
458
|
+
const row = requireStanding(ctx, id);
|
|
459
|
+
const record = recordOf(ctx, row);
|
|
460
|
+
assertDraftUnchanged(ctx, id, input.draftStamp);
|
|
461
|
+
const { definition: draft, requestItem, agentNote } = requireDraft(ctx, id);
|
|
462
|
+
const problems = boardProblems(ctx, id, draft);
|
|
463
|
+
if (blocking(problems)) {
|
|
464
|
+
const codes = problems.filter(({ level }) => level === 'error').map(({ code }) => code);
|
|
465
|
+
throw new OteaError('not_publishable', `The draft has errors: ${codes.join(', ')}`, {
|
|
466
|
+
kind: 'draft_errors',
|
|
467
|
+
codes,
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
const version = maxVersion(ctx.db, id) + 1;
|
|
471
|
+
compileBoard(id, version, draft);
|
|
472
|
+
const at = now();
|
|
473
|
+
const live = record.live?.definition ?? null;
|
|
474
|
+
let moved = [];
|
|
475
|
+
if (live && row.collection !== null) {
|
|
476
|
+
const plan = itemMoves(live, draft, itemStates(ctx, row.collection), input.moves);
|
|
477
|
+
if (!plan.ok)
|
|
478
|
+
throw new OteaError('invalid_board', `Items of ${plan.step} need a step that exists`);
|
|
479
|
+
moved = plan.moves;
|
|
480
|
+
}
|
|
481
|
+
insertVersion(ctx.db, {
|
|
482
|
+
boardId: id,
|
|
483
|
+
version,
|
|
484
|
+
definition: JSON.stringify(draft),
|
|
485
|
+
publishedAt: at,
|
|
486
|
+
publishedBy: input.by.name,
|
|
487
|
+
});
|
|
488
|
+
setLiveVersion(ctx.db, id, version);
|
|
489
|
+
let collection = row.collection;
|
|
490
|
+
if (collection === null) {
|
|
491
|
+
collection = draft.name.trim();
|
|
492
|
+
insertCollection(ctx.db, {
|
|
493
|
+
name: collection,
|
|
494
|
+
prefix: input.prefix === undefined
|
|
495
|
+
? prefixOf(ctx, draft.item, draft.name)
|
|
496
|
+
: chosenPrefix(ctx, input.prefix),
|
|
497
|
+
preset: id,
|
|
498
|
+
presetVersion: version,
|
|
499
|
+
nextSeq: 1,
|
|
500
|
+
icon: null,
|
|
501
|
+
});
|
|
502
|
+
setBoardCollection(ctx.db, id, collection);
|
|
503
|
+
}
|
|
504
|
+
else if (collection !== draft.name.trim()) {
|
|
505
|
+
renameCollection(ctx.db, collection, draft.name.trim());
|
|
506
|
+
collection = draft.name.trim();
|
|
507
|
+
}
|
|
508
|
+
setCollectionPreset(ctx.db, collection, id, version);
|
|
509
|
+
const boards = live ? { from: live, to: draft } : undefined;
|
|
510
|
+
moveItems(ctx, moved, input.by, at, { reason: 'publish', board_version: version }, boards);
|
|
511
|
+
const request = input.closeRequest ? openRequest(ctx, requestItem) : null;
|
|
512
|
+
const from = request ? findItem(ctx.db, request.id)?.state : undefined;
|
|
513
|
+
if (request && from !== undefined) {
|
|
514
|
+
const reason = REQUEST_CLOSED[boardLang(draft)](draft.name.trim());
|
|
515
|
+
// What undoing this publish needs to reopen the Pedido with its draft (closedRequest).
|
|
516
|
+
moveItems(ctx, [{ id: request.id, from, to: DONE }], input.by, at, {
|
|
517
|
+
reason,
|
|
518
|
+
published_board: id,
|
|
519
|
+
board_version: version,
|
|
520
|
+
agent_note: agentNote,
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
deleteDraft(ctx.db, id);
|
|
524
|
+
return { version, moved, record: getBoardRecord(ctx, id) };
|
|
525
|
+
}
|
|
526
|
+
// Undoes a publish: `version` (the one before, or null to undo a first publish) is live again, the
|
|
527
|
+
// items the undone publish moved go back where they were, and the undone version's changes return
|
|
528
|
+
// to the draft.
|
|
529
|
+
export function restoreBoard(ctx, id, input, now = systemClock) {
|
|
530
|
+
const { alongside = [], ...own } = input;
|
|
531
|
+
transaction(ctx.db, () => {
|
|
532
|
+
if (alongside.length && own.version !== null)
|
|
533
|
+
throw new OteaError('not_undoable', 'Only a first publish is undone with other boards');
|
|
534
|
+
assertMadeWith(ctx, id, alongside, 'restore');
|
|
535
|
+
restoreWithin(ctx, id, own, now);
|
|
536
|
+
for (const mate of alongside)
|
|
537
|
+
restoreWithin(ctx, mate, { version: null, by: own.by }, now);
|
|
538
|
+
});
|
|
539
|
+
syncBoardPresets(ctx);
|
|
540
|
+
return getBoardRecord(ctx, id);
|
|
541
|
+
}
|
|
542
|
+
// The body of a restore, inside a transaction the caller holds.
|
|
543
|
+
function restoreWithin(ctx, id, input, now) {
|
|
544
|
+
const row = requireStanding(ctx, id);
|
|
545
|
+
if (row.liveVersion === null)
|
|
546
|
+
throw new OteaError('not_undoable', 'The board is not published');
|
|
547
|
+
const undone = findVersion(ctx.db, id, row.liveVersion);
|
|
548
|
+
if (!undone)
|
|
549
|
+
throw new OteaError('not_found', `Board ${id} lost version ${row.liveVersion}`);
|
|
550
|
+
if (input.version !== null && !findVersion(ctx.db, id, input.version))
|
|
551
|
+
throw new OteaError('not_found', `Board ${id} has no version ${input.version}`);
|
|
552
|
+
const at = now();
|
|
553
|
+
const collection = row.collection;
|
|
554
|
+
const request = closedRequest(ctx, id, row.liveVersion);
|
|
555
|
+
if (input.version === null) {
|
|
556
|
+
if (collection !== null && selectItems(ctx.db, { collection }).length > 0)
|
|
557
|
+
throw new OteaError('not_undoable', 'Items were created on the board since it was published', { kind: 'restore_used' });
|
|
558
|
+
setLiveVersion(ctx.db, id, null);
|
|
559
|
+
if (collection !== null) {
|
|
560
|
+
ctx.db.prepare('UPDATE boards SET collection = NULL WHERE id = ?').run(id);
|
|
561
|
+
deleteCollection(ctx.db, collection);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
else {
|
|
565
|
+
const version = input.version;
|
|
566
|
+
const back = readDefinition(findVersion(ctx.db, id, version)?.definition ?? '{}');
|
|
567
|
+
const name = back.name.trim();
|
|
568
|
+
if (collection !== null && !same(collection, name)) {
|
|
569
|
+
if (namesTaken(ctx, id).some((taken) => same(taken, name)))
|
|
570
|
+
throw new OteaError('already_exists', `Another board is called ${name} now: rename it before you undo this`, { kind: 'restore_name_taken', name });
|
|
571
|
+
}
|
|
572
|
+
if (collection !== null) {
|
|
573
|
+
const states = new Map(itemStates(ctx, collection).map(({ id: item, state }) => [item, state]));
|
|
574
|
+
const moves = publishMoves(ctx, collection, row.liveVersion)
|
|
575
|
+
.filter((move) => states.get(move.id) === move.to)
|
|
576
|
+
.map(({ id: item, from, to }) => ({ id: item, from: to, to: from }));
|
|
577
|
+
for (const move of moves)
|
|
578
|
+
states.set(move.id, move.to);
|
|
579
|
+
// Every open item must stand somewhere the restored version has, or it would be stuck.
|
|
580
|
+
const undoneDef = readDefinition(undone.definition);
|
|
581
|
+
const known = compiledStates(back);
|
|
582
|
+
for (const [item, state] of states) {
|
|
583
|
+
const position = positionOf(state);
|
|
584
|
+
if (isClosed(position) || known.has(state))
|
|
585
|
+
continue;
|
|
586
|
+
const step = undoneDef.steps.find((s) => s.id === position.step);
|
|
587
|
+
throw new OteaError('not_undoable', `${item} stands in ${step?.name ?? position.step}, which version ${version} does not have`, { kind: 'restore_step_lost', item, step: step?.name ?? position.step, version });
|
|
588
|
+
}
|
|
589
|
+
setLiveVersion(ctx.db, id, version);
|
|
590
|
+
if (collection !== name)
|
|
591
|
+
renameCollection(ctx.db, collection, name);
|
|
592
|
+
setCollectionPreset(ctx.db, name, id, version);
|
|
593
|
+
const boards = { from: undoneDef, to: back };
|
|
594
|
+
moveItems(ctx, moves, input.by, at, { reason: 'restore', board_version: version }, boards);
|
|
595
|
+
}
|
|
596
|
+
else {
|
|
597
|
+
setLiveVersion(ctx.db, id, version);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
// The Pedido the undone publish closed opens again where it stood, its draft back on it.
|
|
601
|
+
if (request) {
|
|
602
|
+
const back = { id: request.item, from: DONE, to: request.from };
|
|
603
|
+
moveItems(ctx, [back], input.by, at, { reason: 'restore', unpublished_board: id });
|
|
604
|
+
}
|
|
605
|
+
// A draft written since the publish is the person's newer work: it stays, on the restored base.
|
|
606
|
+
const kept = findDraft(ctx.db, id);
|
|
607
|
+
upsertDraft(ctx.db, {
|
|
608
|
+
boardId: id,
|
|
609
|
+
definition: kept?.definition ?? undone.definition,
|
|
610
|
+
baseVersion: input.version,
|
|
611
|
+
updatedAt: at,
|
|
612
|
+
requestItem: kept?.requestItem ?? request?.item ?? null,
|
|
613
|
+
agentNote: kept ? kept.agentNote : (request?.agentNote ?? null),
|
|
614
|
+
});
|
|
615
|
+
return getBoardRecord(ctx, id);
|
|
616
|
+
}
|
|
617
|
+
// The Pedido that publishing `version` of board `id` closed, while it still stands closed by it:
|
|
618
|
+
// where it stood before and the draft's agent note, to give back on undo.
|
|
619
|
+
function closedRequest(ctx, id, version) {
|
|
620
|
+
const rows = ctx.db
|
|
621
|
+
.prepare(`SELECT id, item_id AS item, data FROM events
|
|
622
|
+
WHERE type = 'transitioned' AND data LIKE ? ORDER BY id DESC`)
|
|
623
|
+
.all(`%"published_board":${JSON.stringify(id)}%`);
|
|
624
|
+
for (const row of rows) {
|
|
625
|
+
const data = JSON.parse(String(row.data));
|
|
626
|
+
if (data.published_board !== id || data.board_version !== version)
|
|
627
|
+
continue;
|
|
628
|
+
const item = String(row.item);
|
|
629
|
+
if (findItem(ctx.db, item)?.state !== DONE)
|
|
630
|
+
return null;
|
|
631
|
+
const last = ctx.db
|
|
632
|
+
.prepare(`SELECT MAX(id) AS id FROM events WHERE item_id = ? AND type = 'transitioned'`)
|
|
633
|
+
.get(item);
|
|
634
|
+
if (last?.id !== row.id)
|
|
635
|
+
return null;
|
|
636
|
+
const note = data.agent_note;
|
|
637
|
+
return {
|
|
638
|
+
item,
|
|
639
|
+
from: String(data.from),
|
|
640
|
+
agentNote: Array.isArray(note) ? JSON.stringify(note) : null,
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
return null;
|
|
644
|
+
}
|
|
645
|
+
function assertPair(ctx, parent, child) {
|
|
646
|
+
const record = getBoardRecord(ctx, parent);
|
|
647
|
+
requireRow(ctx, child);
|
|
648
|
+
const split = (record.draft ?? record.live)?.definition.split;
|
|
649
|
+
if (split?.board !== child)
|
|
650
|
+
throw new OteaError('invalid_board', `Board ${parent} is not split into board ${child}`);
|
|
651
|
+
}
|
|
652
|
+
function stamped(input, role, moves) {
|
|
653
|
+
const stamp = input.draftStamps?.[role];
|
|
654
|
+
const close = input.closeRequests?.[role] === true ? { closeRequest: true } : {};
|
|
655
|
+
return { moves, by: input.by, ...(stamp === undefined ? {} : { draftStamp: stamp }), ...close };
|
|
656
|
+
}
|
|
657
|
+
// Publishes the pair's drafts in one transaction, the child first so the parent's split names a
|
|
658
|
+
// live board: both, or (when only one has changes) the one named, or nothing at all.
|
|
659
|
+
export function publishPair(ctx, input, now = systemClock) {
|
|
660
|
+
const { parent: parentMoves, child: childMoves } = input.moves;
|
|
661
|
+
if (!parentMoves && !childMoves)
|
|
662
|
+
throw new OteaError('invalid_board', 'Name the boards to publish: nothing was asked for');
|
|
663
|
+
const result = transaction(ctx.db, () => {
|
|
664
|
+
assertPair(ctx, input.parent, input.child);
|
|
665
|
+
const child = childMoves
|
|
666
|
+
? publishWithin(ctx, input.child, stamped(input, 'child', childMoves), now)
|
|
667
|
+
: null;
|
|
668
|
+
const parent = parentMoves
|
|
669
|
+
? publishWithin(ctx, input.parent, stamped(input, 'parent', parentMoves), now)
|
|
670
|
+
: null;
|
|
671
|
+
return { parent, child };
|
|
672
|
+
});
|
|
673
|
+
syncBoardPresets(ctx);
|
|
674
|
+
// The parent's record was read before the child's publish finished: read both again.
|
|
675
|
+
return {
|
|
676
|
+
parent: result.parent && { ...result.parent, record: getBoardRecord(ctx, input.parent) },
|
|
677
|
+
child: result.child && { ...result.child, record: getBoardRecord(ctx, input.child) },
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
// Undoes a joint publish in one transaction, the parent first (it names the child): both boards
|
|
681
|
+
// go back, or neither does.
|
|
682
|
+
export function restorePair(ctx, input, now = systemClock) {
|
|
683
|
+
const { parent: parentVersion, child: childVersion } = input.versions;
|
|
684
|
+
if (parentVersion === undefined && childVersion === undefined)
|
|
685
|
+
throw new OteaError('invalid_board', 'Name the boards to restore: nothing was asked for');
|
|
686
|
+
transaction(ctx.db, () => {
|
|
687
|
+
assertPair(ctx, input.parent, input.child);
|
|
688
|
+
if (parentVersion !== undefined)
|
|
689
|
+
restoreWithin(ctx, input.parent, { version: parentVersion, by: input.by }, now);
|
|
690
|
+
if (childVersion !== undefined)
|
|
691
|
+
restoreWithin(ctx, input.child, { version: childVersion, by: input.by }, now);
|
|
692
|
+
});
|
|
693
|
+
syncBoardPresets(ctx);
|
|
694
|
+
return {
|
|
695
|
+
parent: parentVersion === undefined ? null : getBoardRecord(ctx, input.parent),
|
|
696
|
+
child: childVersion === undefined ? null : getBoardRecord(ctx, input.child),
|
|
697
|
+
};
|
|
698
|
+
}
|
|
699
|
+
// The states a version's compiled workflow has for its steps (compile.ts phasesOf).
|
|
700
|
+
function compiledStates(def) {
|
|
701
|
+
return new Set(def.steps.flatMap((step) => {
|
|
702
|
+
const doing = stateOf({ step: step.id, phase: 'doing' });
|
|
703
|
+
if (step.reviewer === 'none')
|
|
704
|
+
return [doing];
|
|
705
|
+
return [doing, stateOf({ step: step.id, phase: 'review' })];
|
|
706
|
+
}));
|
|
707
|
+
}
|
|
708
|
+
// The moves a publish made, read back from its events.
|
|
709
|
+
function publishMoves(ctx, collection, version) {
|
|
710
|
+
const rows = ctx.db
|
|
711
|
+
.prepare(`SELECT e.item_id AS id, e.data AS data FROM events e JOIN items i ON i.id = e.item_id
|
|
712
|
+
WHERE i.collection = ? AND e.type = 'transitioned' ORDER BY e.id`)
|
|
713
|
+
.all(collection);
|
|
714
|
+
return rows.flatMap((row) => {
|
|
715
|
+
const data = JSON.parse(String(row.data));
|
|
716
|
+
if (data.reason !== 'publish' || data.board_version !== version)
|
|
717
|
+
return [];
|
|
718
|
+
return [{ id: String(row.id), from: String(data.from), to: String(data.to) }];
|
|
719
|
+
});
|
|
720
|
+
}
|
|
721
|
+
// The preset catalog with every published board compiled in. A context remembers what it compiled
|
|
722
|
+
// and from which catalog, so a call costs one query when nothing was published.
|
|
723
|
+
const synced = new WeakMap();
|
|
724
|
+
export function syncBoardPresets(ctx) {
|
|
725
|
+
const stamp = boardsStamp(ctx.db);
|
|
726
|
+
const known = synced.get(ctx);
|
|
727
|
+
if (known && known.stamp === stamp && ctx.presets === known.result)
|
|
728
|
+
return;
|
|
729
|
+
const base = known && ctx.presets === known.result ? known.base : ctx.presets;
|
|
730
|
+
const presets = new Map(base.presets);
|
|
731
|
+
// An archived board's items are still read under it.
|
|
732
|
+
for (const row of listBoards(ctx.db, { archived: true })) {
|
|
733
|
+
if (row.liveVersion === null)
|
|
734
|
+
continue;
|
|
735
|
+
const version = findVersion(ctx.db, row.id, row.liveVersion);
|
|
736
|
+
if (!version)
|
|
737
|
+
continue;
|
|
738
|
+
const preset = {
|
|
739
|
+
...compileBoard(row.id, row.liveVersion, readDefinition(version.definition)),
|
|
740
|
+
origin: 'project',
|
|
741
|
+
};
|
|
742
|
+
presets.set(row.id, preset);
|
|
743
|
+
}
|
|
744
|
+
const result = { ...base, presets };
|
|
745
|
+
ctx.presets = result;
|
|
746
|
+
synced.set(ctx, { stamp, base, result });
|
|
747
|
+
}
|
|
748
|
+
// The project's built-in board for loose requests, published once; returns its id.
|
|
749
|
+
export function ensureRequestsBoard(ctx, lang, now = systemClock, ids = newId) {
|
|
750
|
+
const builtin = () => listBoards(ctx.db).find((row) => row.builtin);
|
|
751
|
+
const existing = builtin();
|
|
752
|
+
if (existing)
|
|
753
|
+
return existing.id;
|
|
754
|
+
const id = transaction(ctx.db, () => {
|
|
755
|
+
const other = builtin();
|
|
756
|
+
if (other)
|
|
757
|
+
return other.id;
|
|
758
|
+
const definition = requestsBoard(lang, ids);
|
|
759
|
+
const board = ids('b');
|
|
760
|
+
const at = now();
|
|
761
|
+
insertBoard(ctx.db, { id: board, createdAt: at, builtin: true });
|
|
762
|
+
insertVersion(ctx.db, {
|
|
763
|
+
boardId: board,
|
|
764
|
+
version: 1,
|
|
765
|
+
definition: JSON.stringify(definition),
|
|
766
|
+
publishedAt: at,
|
|
767
|
+
publishedBy: 'otea',
|
|
768
|
+
});
|
|
769
|
+
setLiveVersion(ctx.db, board, 1);
|
|
770
|
+
insertCollection(ctx.db, {
|
|
771
|
+
name: definition.name,
|
|
772
|
+
prefix: prefixOf(ctx, definition.item, definition.name),
|
|
773
|
+
preset: board,
|
|
774
|
+
presetVersion: 1,
|
|
775
|
+
nextSeq: 1,
|
|
776
|
+
icon: null,
|
|
777
|
+
});
|
|
778
|
+
setBoardCollection(ctx.db, board, definition.name);
|
|
779
|
+
return board;
|
|
780
|
+
});
|
|
781
|
+
syncBoardPresets(ctx);
|
|
782
|
+
return id;
|
|
783
|
+
}
|
|
784
|
+
// What Studio's home offers: the listed single-board templates, then the sets.
|
|
785
|
+
export function listTemplates(lang) {
|
|
786
|
+
return listedTemplates(lang);
|
|
787
|
+
}
|
|
788
|
+
// Creates every board of a set as a draft, in one transaction: none, or all of them. A split
|
|
789
|
+
// names its child by the child's new board id.
|
|
790
|
+
export function createBoardSet(ctx, input, now = systemClock, ids = newId) {
|
|
791
|
+
return transaction(ctx.db, () => insertNewBoardSet(ctx, input, now, ids));
|
|
792
|
+
}
|
|
793
|
+
// Creates the set's boards inside the caller's transaction, so they can go with what asked for them.
|
|
794
|
+
export function insertNewBoardSet(ctx, input, now = systemClock, ids = newId) {
|
|
795
|
+
const set = TEMPLATE_SETS.find(({ key }) => key === input.set);
|
|
796
|
+
if (!set)
|
|
797
|
+
throw new OteaError('not_found', `There is no template ${input.set}`);
|
|
798
|
+
{
|
|
799
|
+
const boards = set.boards.map((_, index) => {
|
|
800
|
+
const asked = input.boards[index] ?? {};
|
|
801
|
+
return {
|
|
802
|
+
id: ids('b'),
|
|
803
|
+
...(asked.name?.trim() ? { name: asked.name.trim() } : {}),
|
|
804
|
+
...(asked.item?.trim() ? { item: asked.item.trim() } : {}),
|
|
805
|
+
};
|
|
806
|
+
});
|
|
807
|
+
const taken = namesTaken(ctx);
|
|
808
|
+
const seen = [];
|
|
809
|
+
const isTaken = (wanted) => [...taken, ...seen].some((name) => same(name, wanted));
|
|
810
|
+
// Each board's name is settled before the boards are made, so every text of the set names
|
|
811
|
+
// the boards of this set (a second Ideas set plans its tasks on Implementación 2).
|
|
812
|
+
const named = set.boards.map((key, index) => {
|
|
813
|
+
const given = boards[index]?.name;
|
|
814
|
+
let name = given ?? requireTemplate(key).board[input.lang];
|
|
815
|
+
// A later board nobody named keeps the template's name with the first number free (a second
|
|
816
|
+
// Ideas set makes Implementación 2): only a name someone gave is refused when taken.
|
|
817
|
+
if (index > 0 && given === undefined && isTaken(name)) {
|
|
818
|
+
let n = 2;
|
|
819
|
+
while (isTaken(`${name} ${n}`))
|
|
820
|
+
n++;
|
|
821
|
+
name = `${name} ${n}`;
|
|
822
|
+
}
|
|
823
|
+
if (isTaken(name))
|
|
824
|
+
throw new OteaError('already_exists', `There is already a board called ${name}`, {
|
|
825
|
+
kind: 'board_name_taken',
|
|
826
|
+
name,
|
|
827
|
+
});
|
|
828
|
+
seen.push(name);
|
|
829
|
+
return { ...boards[index], id: boards[index]?.id ?? ids('b'), name };
|
|
830
|
+
});
|
|
831
|
+
const defs = instantiateSet(set, input.lang, named, ids);
|
|
832
|
+
const at = now();
|
|
833
|
+
return defs.map((definition, index) => {
|
|
834
|
+
const id = boards[index]?.id ?? ids('b');
|
|
835
|
+
insertBoard(ctx.db, { id, createdAt: at, builtin: false });
|
|
836
|
+
upsertDraft(ctx.db, {
|
|
837
|
+
boardId: id,
|
|
838
|
+
definition: JSON.stringify(definition),
|
|
839
|
+
baseVersion: null,
|
|
840
|
+
updatedAt: at,
|
|
841
|
+
requestItem: index === 0 ? (input.requestItem ?? null) : null,
|
|
842
|
+
agentNote: null,
|
|
843
|
+
});
|
|
844
|
+
return getBoardRecord(ctx, id);
|
|
845
|
+
});
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
// Creates a board (from a template, or blank) and publishes it at once: what the home does, for the
|
|
849
|
+
// CLI.
|
|
850
|
+
export function addBoard(ctx, input, now = systemClock) {
|
|
851
|
+
const { by, prefix, ...create } = input;
|
|
852
|
+
const record = createBoard(ctx, create, now);
|
|
853
|
+
try {
|
|
854
|
+
return publishBoard(ctx, record.id, { moves: {}, by, ...(prefix === undefined ? {} : { prefix }) }, now);
|
|
855
|
+
}
|
|
856
|
+
catch (error) {
|
|
857
|
+
discardDraft(ctx, record.id, now);
|
|
858
|
+
throw error;
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
// Creates every board of a set and publishes them at once, a part board before the board that
|
|
862
|
+
// divides into it: what the home does, for the CLI and otea init.
|
|
863
|
+
export function addBoardSet(ctx, input, now = systemClock) {
|
|
864
|
+
const { by, ...create } = input;
|
|
865
|
+
const records = createBoardSet(ctx, create, now);
|
|
866
|
+
const published = [...records]
|
|
867
|
+
.reverse()
|
|
868
|
+
.map((record) => publishBoard(ctx, record.id, { moves: {}, by }, now));
|
|
869
|
+
return published.reverse();
|
|
870
|
+
}
|