@crombolo/data 0.19.0 → 0.21.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.19.0",
3
+ "version": "0.21.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.19.0"
31
+ "@crombolo/core": "0.21.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'
@@ -64,6 +65,7 @@ export * from './schema/form-permission-prefix.ts'
64
65
  export * from './schema/group-permissions.ts'
65
66
  export * from './schema/identity.ts'
66
67
  export * from './schema/log-viewer.ts'
68
+ export * from './schema/media-permissions.ts'
67
69
  export * from './schema/member-property-type.ts'
68
70
  export * from './schema/members.ts'
69
71
  export * from './schema/plan.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
  }
@@ -1767,6 +1767,31 @@ export class DocumentRepository {
1767
1767
  return (await this.#propertyTypes(contentTypeNodeId)).map((p) => p.alias)
1768
1768
  }
1769
1769
 
1770
+ /** A document's property types — alias, key and name — compositions included; undefined when there is no document. */
1771
+ async propertyTypeKeys(
1772
+ key: string,
1773
+ ): Promise<Array<{ alias: string; key: string; name: string }> | undefined> {
1774
+ const node = await this.#nodes.byKey(key)
1775
+ if (!node) return undefined
1776
+ const contentTypeNodeId = await this.#contentTypeNodeId(node.id)
1777
+ if (!contentTypeNodeId) return undefined
1778
+ const rows = await this.#db.query<{ alias: string; unique_id: string; name: string }>(
1779
+ `WITH RECURSIVE ancestry(id) AS (
1780
+ SELECT ?
1781
+ UNION
1782
+ SELECT c.parent_content_type_id FROM content_type_composition c JOIN ancestry a ON a.id = c.child_content_type_id
1783
+ )
1784
+ SELECT p.alias, p.unique_id, p.name FROM property_type p
1785
+ WHERE p.content_type_id IN (SELECT id FROM ancestry) AND p.retired_at IS NULL`,
1786
+ [contentTypeNodeId],
1787
+ )
1788
+ return rows.map((row) => ({
1789
+ alias: String(row.alias),
1790
+ key: normaliseUuid(String(row.unique_id)),
1791
+ name: String(row.name),
1792
+ }))
1793
+ }
1794
+
1770
1795
  /** Property types of a content type, including everything it composes; retired ones excluded. */
1771
1796
  async #propertyTypes(contentTypeNodeId: number): Promise<PropertyTypeRow[]> {
1772
1797
  const rows = await this.#db.query(
@@ -596,13 +596,28 @@ export class UserRepository {
596
596
  return {
597
597
  groups: groups.map((group) => {
598
598
  const granular = new Map<string, string[]>()
599
+ const properties = new Map<string, string[]>()
599
600
  for (const row of group.granular)
600
601
  if (row.context === 'Document' && row.key) {
601
602
  const verbs = granular.get(row.key) ?? []
602
603
  if (row.permission) verbs.push(row.permission)
603
604
  granular.set(row.key, verbs)
605
+ } else if (row.context === 'DocumentTypeProperty' && row.key) {
606
+ // `<property type key>|<verb>`; an empty verb is an explicit "nothing"
607
+ const [propertyType, verb = ''] = row.permission.split('|')
608
+ if (!propertyType) continue
609
+ const key = propertyType.toLowerCase()
610
+ const verbs = properties.get(key) ?? []
611
+ if (verb) verbs.push(verb)
612
+ properties.set(key, verbs)
604
613
  }
605
- return { key: group.key, alias: group.alias, defaults: group.permissions, granular }
614
+ return {
615
+ key: group.key,
616
+ alias: group.alias,
617
+ defaults: group.permissions,
618
+ granular,
619
+ properties,
620
+ }
606
621
  }),
607
622
  languages: languageRows.map((l) => String(l.iso_code)),
608
623
  hasAccessToAllLanguages: groups.some((g) => g.hasAccessToAllLanguages),
@@ -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
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Migration 045 — the media verbs, `Crombolo.Media.Read` and
3
+ * `Crombolo.Media.Update` (story 088). docs/backoffice-hosting.md, "Authorization".
4
+ *
5
+ * Before them the Media section alone let a group change every item in it, so
6
+ * every group that has the section — built-in or made on the site — is given
7
+ * both, and keeps exactly the access it had. The built-in groups are also given
8
+ * whatever else the seed grants that they lack, element verbs included.
9
+ * Idempotent, and it never takes a verb away.
10
+ */
11
+ import { MediaPermissions } from '@crombolo/core'
12
+ import type { Db } from '../database.ts'
13
+ import type { Migration } from '../migrations.ts'
14
+ import { STATE_APPROVAL_SCHEDULES } from './approval-schedules.ts'
15
+ import { grantBuiltInPermissions } from './seed.ts'
16
+
17
+ export const STATE_MEDIA_PERMISSIONS = '{8d4f2b61-0045-4c3e-9a17-5e2b7c9d1f45}'
18
+
19
+ export const mediaPermissionsMigration: Migration = {
20
+ from: STATE_APPROVAL_SCHEDULES,
21
+ to: STATE_MEDIA_PERMISSIONS,
22
+ name: 'GrantMediaPermissions',
23
+ kind: 'expand',
24
+ release: '0.21.0',
25
+ async up(db: Db) {
26
+ await grantBuiltInPermissions(db)
27
+ for (const permission of Object.values(MediaPermissions))
28
+ await db.exec(
29
+ `INSERT INTO user_group_permission (user_group_key, permission)
30
+ SELECT g.key, ? FROM user_group g
31
+ WHERE EXISTS (
32
+ SELECT 1 FROM user_group_section s
33
+ WHERE s.user_group_id = g.id AND LOWER(s.app_alias) IN ('media', 'umb.section.media')
34
+ ) AND NOT EXISTS (
35
+ SELECT 1 FROM user_group_permission p WHERE p.user_group_key = g.key AND p.permission = ?
36
+ )`,
37
+ [permission, permission],
38
+ )
39
+ },
40
+ }
@@ -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'
@@ -35,6 +36,7 @@ import { formsMigration } from './forms.ts'
35
36
  import { groupPermissionsMigration } from './group-permissions.ts'
36
37
  import { identityMigration } from './identity.ts'
37
38
  import { logViewerMigration } from './log-viewer.ts'
39
+ import { mediaPermissionsMigration } from './media-permissions.ts'
38
40
  import { memberPropertyTypeMigration } from './member-property-type.ts'
39
41
  import { membersMigration } from './members.ts'
40
42
  import { propertySearchableMigration } from './property-searchable.ts'
@@ -101,4 +103,6 @@ export const cromboloPlan = new MigrationPlan([
101
103
  remoteMigration,
102
104
  trashedDateMigration,
103
105
  contentNotificationsMigration,
106
+ approvalSchedulesMigration,
107
+ mediaPermissionsMigration,
104
108
  ])
@@ -4,7 +4,7 @@
4
4
  * Password hashing is injected rather than imported so this package stays free of
5
5
  * a dependency on @crombolo/auth.
6
6
  */
7
- import { RemotePermissions } from '@crombolo/core'
7
+ import { MediaPermissions, RemotePermissions } from '@crombolo/core'
8
8
  import type { Db } from '../database.ts'
9
9
  import { DbDate } from '../dialect.ts'
10
10
 
@@ -41,6 +41,9 @@ const FORM_FULL = [
41
41
  'Crombolo.FormEntry.Sensitive',
42
42
  ] as const
43
43
 
44
+ /** Browsing and changing media (story 088): every built-in group with the Media section. */
45
+ const MEDIA_FULL = Object.values(MediaPermissions)
46
+
44
47
  export const BUILT_IN_GROUP_PERMISSIONS: Record<string, readonly string[]> = {
45
48
  admin: [
46
49
  'Umb.Document.Create',
@@ -67,6 +70,7 @@ export const BUILT_IN_GROUP_PERMISSIONS: Record<string, readonly string[]> = {
67
70
  ...FORM_FULL,
68
71
  ...ELEMENT_EDIT,
69
72
  ...Object.values(RemotePermissions),
73
+ ...MEDIA_FULL,
70
74
  ],
71
75
  editor: [
72
76
  'Umb.Document.Create',
@@ -93,6 +97,7 @@ export const BUILT_IN_GROUP_PERMISSIONS: Record<string, readonly string[]> = {
93
97
  'Crombolo.FormEntry.Read',
94
98
  'Crombolo.FormEntry.Manage',
95
99
  ...ELEMENT_EDIT,
100
+ ...MEDIA_FULL,
96
101
  ],
97
102
  writer: [
98
103
  'Umb.Document.Create',