@crombolo/data 0.18.0 → 0.20.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/data",
3
- "version": "0.18.0",
3
+ "version": "0.20.0",
4
4
  "description": "SQLite and Postgres storage for crombolo: schema, migrations and repositories.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -28,6 +28,6 @@
28
28
  "access": "public"
29
29
  },
30
30
  "dependencies": {
31
- "@crombolo/core": "0.18.0"
31
+ "@crombolo/core": "0.20.0"
32
32
  }
33
33
  }
package/src/index.ts CHANGED
@@ -43,6 +43,7 @@ export * from './repositories/two-factor.ts'
43
43
  export * from './repositories/user-data.ts'
44
44
  export * from './repositories/users.ts'
45
45
  export * from './repositories/webhooks.ts'
46
+ export * from './schema/approval-schedules.ts'
46
47
  export * from './schema/approvals.ts'
47
48
  export * from './schema/assistant.ts'
48
49
  export * from './schema/audit-log.ts'
@@ -9,7 +9,14 @@ import type { Db } from '../database.ts'
9
9
  import { DbDate } from '../dialect.ts'
10
10
 
11
11
  export type ApprovalAction = 'publish' | 'unpublish'
12
- export type ApprovalState = 'waiting' | 'approved' | 'sent-back' | 'withdrawn'
12
+ /** `scheduled`: approved, and open until its time comes (story 029). */
13
+ export type ApprovalState =
14
+ | 'waiting'
15
+ | 'scheduled'
16
+ | 'approved'
17
+ | 'sent-back'
18
+ | 'withdrawn'
19
+ | 'superseded'
13
20
  export type ApprovalStepState = 'waiting' | 'approved' | 'sent-back' | 'discarded'
14
21
  export type ApprovalHistoryKind =
15
22
  | 'sent'
@@ -20,6 +27,15 @@ export type ApprovalHistoryKind =
20
27
  | 'restarted'
21
28
  | 'published'
22
29
  | 'unpublished'
30
+ | 'scheduled'
31
+ | 'superseded'
32
+
33
+ /** When a request asked to publish or take down one culture (null: the invariant variant). */
34
+ export interface ApprovalScheduleEntry {
35
+ culture: string | null
36
+ publishTime: Date | null
37
+ unpublishTime: Date | null
38
+ }
23
39
 
24
40
  export interface ApprovalRequest {
25
41
  id: number
@@ -28,6 +44,8 @@ export interface ApprovalRequest {
28
44
  /** Null for the invariant variant, or the whole page. */
29
45
  cultures: string[] | null
30
46
  action: ApprovalAction
47
+ /** The times it asked for; null to publish on approval. */
48
+ schedule: ApprovalScheduleEntry[] | null
31
49
  /** The page's head event when it was sent, or after the last edit the workflow let through. */
32
50
  eventId: number | null
33
51
  workflowKey: string
@@ -62,12 +80,29 @@ export interface ApprovalHistoryEntry {
62
80
  createDate: Date
63
81
  }
64
82
 
83
+ const scheduleFrom = (json: string): ApprovalScheduleEntry[] =>
84
+ (
85
+ JSON.parse(json) as Array<{
86
+ culture: string | null
87
+ publishTime: string | null
88
+ unpublishTime: string | null
89
+ }>
90
+ ).map((e) => ({
91
+ culture: e.culture,
92
+ publishTime: e.publishTime ? new Date(e.publishTime) : null,
93
+ unpublishTime: e.unpublishTime ? new Date(e.unpublishTime) : null,
94
+ }))
95
+
96
+ /** States a request is still open in: it holds the page until it ends. */
97
+ const OPEN = "state IN ('waiting', 'scheduled')"
98
+
65
99
  const requestFrom = (row: Record<string, unknown>): ApprovalRequest => ({
66
100
  id: Number(row.id),
67
101
  key: normaliseUuid(String(row.key)),
68
102
  nodeKey: normaliseUuid(String(row.node_key)),
69
103
  cultures: row.cultures ? (JSON.parse(String(row.cultures)) as string[]) : null,
70
104
  action: row.action === 'unpublish' ? 'unpublish' : 'publish',
105
+ schedule: row.schedule ? scheduleFrom(String(row.schedule)) : null,
71
106
  eventId: row.event_id === null || row.event_id === undefined ? null : Number(row.event_id),
72
107
  workflowKey: String(row.workflow_key),
73
108
  workflowVersion: String(row.workflow_version),
@@ -119,12 +154,13 @@ export class ApprovalRepository {
119
154
  workflowVersion: string
120
155
  /** The user's key. */
121
156
  requestedBy: string
157
+ schedule?: ApprovalScheduleEntry[] | null
122
158
  }): Promise<ApprovalRequest> {
123
159
  const key = crypto.randomUUID()
124
160
  await this.#db.exec(
125
161
  `INSERT INTO approval_request (key, node_key, cultures, action, event_id, workflow_key,
126
- workflow_version, workflow, state, requested_by, requested_at)
127
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'waiting', ?, ?)`,
162
+ workflow_version, workflow, state, requested_by, requested_at, schedule)
163
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'waiting', ?, ?, ?)`,
128
164
  [
129
165
  key,
130
166
  normaliseUuid(input.nodeKey),
@@ -136,6 +172,7 @@ export class ApprovalRepository {
136
172
  JSON.stringify(input.workflow),
137
173
  normaliseUuid(input.requestedBy),
138
174
  DbDate.toDb(new Date()),
175
+ input.schedule?.length ? JSON.stringify(input.schedule) : null,
139
176
  ],
140
177
  )
141
178
  return (await this.byKey(key)) as ApprovalRequest
@@ -148,10 +185,10 @@ export class ApprovalRepository {
148
185
  return rows[0] ? requestFrom(rows[0]) : undefined
149
186
  }
150
187
 
151
- /** The request a page is waiting on, if any: at most one at a time. */
152
- async waitingFor(nodeKey: string): Promise<ApprovalRequest | undefined> {
188
+ /** The request a page is waiting on or scheduled under, if any: at most one at a time. */
189
+ async openFor(nodeKey: string): Promise<ApprovalRequest | undefined> {
153
190
  const rows = await this.#db.query(
154
- "SELECT * FROM approval_request WHERE node_key = ? AND state = 'waiting' ORDER BY id DESC",
191
+ `SELECT * FROM approval_request WHERE node_key = ? AND ${OPEN} ORDER BY id DESC`,
155
192
  [normaliseUuid(nodeKey)],
156
193
  )
157
194
  return rows[0] ? requestFrom(rows[0]) : undefined
@@ -166,10 +203,16 @@ export class ApprovalRepository {
166
203
  return rows.map(requestFrom)
167
204
  }
168
205
 
169
- /** Every request still waiting, oldest first. */
170
- async waiting(): Promise<ApprovalRequest[]> {
206
+ /** Every request still open (waiting or scheduled), oldest first. */
207
+ async open(): Promise<ApprovalRequest[]> {
208
+ const rows = await this.#db.query(`SELECT * FROM approval_request WHERE ${OPEN} ORDER BY id`)
209
+ return rows.map(requestFrom)
210
+ }
211
+
212
+ /** Every request approved and waiting for its time, oldest first. */
213
+ async scheduled(): Promise<ApprovalRequest[]> {
171
214
  const rows = await this.#db.query(
172
- "SELECT * FROM approval_request WHERE state = 'waiting' ORDER BY id",
215
+ "SELECT * FROM approval_request WHERE state = 'scheduled' ORDER BY id",
173
216
  )
174
217
  return rows.map(requestFrom)
175
218
  }
@@ -177,10 +220,10 @@ export class ApprovalRepository {
177
220
  /** Requests that have finished, newest first. */
178
221
  async finished(skip: number, take: number): Promise<{ total: number; items: ApprovalRequest[] }> {
179
222
  const counted = await this.#db.query<{ n: number }>(
180
- "SELECT COUNT(*) AS n FROM approval_request WHERE state <> 'waiting'",
223
+ `SELECT COUNT(*) AS n FROM approval_request WHERE NOT ${OPEN}`,
181
224
  )
182
225
  const rows = await this.#db.query(
183
- "SELECT * FROM approval_request WHERE state <> 'waiting' ORDER BY id DESC LIMIT ? OFFSET ?",
226
+ `SELECT * FROM approval_request WHERE NOT ${OPEN} ORDER BY id DESC LIMIT ? OFFSET ?`,
184
227
  [take, skip],
185
228
  )
186
229
  return { total: Number(counted[0]?.n ?? 0), items: rows.map(requestFrom) }
@@ -197,7 +240,7 @@ export class ApprovalRepository {
197
240
  async setState(id: number, state: ApprovalState): Promise<void> {
198
241
  await this.#db.exec('UPDATE approval_request SET state = ?, finished_at = ? WHERE id = ?', [
199
242
  state,
200
- state === 'waiting' ? null : DbDate.toDb(new Date()),
243
+ state === 'waiting' || state === 'scheduled' ? null : DbDate.toDb(new Date()),
201
244
  id,
202
245
  ])
203
246
  }
@@ -363,10 +363,16 @@ export class DocumentRepository {
363
363
  return this.#load(node)
364
364
  }
365
365
 
366
- async children(parentKey: string | null, skip: number, take: number): Promise<Page<NodeRow>> {
366
+ /** A page of children, leaving out `excludeKeys` from the page and the total alike. */
367
+ async children(
368
+ parentKey: string | null,
369
+ skip: number,
370
+ take: number,
371
+ excludeKeys: readonly string[] = [],
372
+ ): Promise<Page<NodeRow>> {
367
373
  const parentId = parentKey ? (await this.#nodes.byKey(parentKey))?.id : SystemNodes.Root
368
374
  if (parentId === undefined) return { total: 0, items: [] }
369
- return this.#nodes.children(parentId, this.objectType, skip, take)
375
+ return this.#nodes.children(parentId, this.objectType, skip, take, false, excludeKeys)
370
376
  }
371
377
 
372
378
  /** The recycle bin's tree: trashed documents, top level being what was trashed directly. */
@@ -133,17 +133,29 @@ export class NodeRepository {
133
133
  skip: number,
134
134
  take: number,
135
135
  trashed = false,
136
+ excludeKeys: readonly string[] = [],
136
137
  ): Promise<Page<NodeRow>> {
138
+ const where = `parent_id = ? AND node_object_type = ? AND trashed = ?${
139
+ excludeKeys.length > 0
140
+ ? ` AND unique_id NOT IN (${excludeKeys.map(() => '?').join(', ')})`
141
+ : ''
142
+ }`
143
+ const params = [
144
+ parentId,
145
+ objectType,
146
+ this.#db.dialect.boolValue(trashed),
147
+ ...excludeKeys.map(normaliseUuid),
148
+ ]
137
149
  const totals = await this.#db.query<{ n: number }>(
138
- 'SELECT COUNT(*) AS n FROM node WHERE parent_id = ? AND node_object_type = ? AND trashed = ?',
139
- [parentId, objectType, this.#db.dialect.boolValue(trashed)],
150
+ `SELECT COUNT(*) AS n FROM node WHERE ${where}`,
151
+ params,
140
152
  )
141
153
  const rows = await this.#db.query(
142
154
  `SELECT ${NODE_COLUMNS} FROM node
143
- WHERE parent_id = ? AND node_object_type = ? AND trashed = ?
155
+ WHERE ${where}
144
156
  ORDER BY sort_order, id
145
157
  LIMIT ? OFFSET ?`,
146
- [parentId, objectType, this.#db.dialect.boolValue(trashed), take, skip],
158
+ [...params, take, skip],
147
159
  )
148
160
  return { total: Number(totals[0]?.n ?? 0), items: rows.map(mapNode) }
149
161
  }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Migration 044 — `approval_request.schedule`: when a request asked to publish
3
+ * or take the page down, per culture (story 029). Requests from before it have
4
+ * none and publish on approval, as they always did. docs/workflows.md.
5
+ */
6
+ import type { Db } from '../database.ts'
7
+ import type { Migration } from '../migrations.ts'
8
+ import { schemaOps } from '../schema-ops.ts'
9
+ import { STATE_CONTENT_NOTIFICATIONS } from './content-notifications.ts'
10
+
11
+ export const STATE_APPROVAL_SCHEDULES = '{3c9e5b17-0044-4a2d-8f61-7b0d4e2a9c44}'
12
+
13
+ export const approvalSchedulesMigration: Migration = {
14
+ from: STATE_CONTENT_NOTIFICATIONS,
15
+ to: STATE_APPROVAL_SCHEDULES,
16
+ name: 'AddApprovalSchedules',
17
+ kind: 'expand',
18
+ release: '0.20.0',
19
+ async up(db: Db) {
20
+ await schemaOps(db).addColumn('approval_request', 'schedule', db.dialect.types.text)
21
+ },
22
+ }
@@ -13,6 +13,7 @@
13
13
  */
14
14
 
15
15
  import { MigrationPlan } from '../migrations.ts'
16
+ import { approvalSchedulesMigration } from './approval-schedules.ts'
16
17
  import { approvalsMigration } from './approvals.ts'
17
18
  import { assistantMigration } from './assistant.ts'
18
19
  import { auditLogMigration } from './audit-log.ts'
@@ -101,4 +102,5 @@ export const cromboloPlan = new MigrationPlan([
101
102
  remoteMigration,
102
103
  trashedDateMigration,
103
104
  contentNotificationsMigration,
105
+ approvalSchedulesMigration,
104
106
  ])