@byline/host-tanstack-start 4.14.0 → 4.15.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/dist/admin-shell/chrome/admin-layout_module.css +1 -1
- package/dist/admin-shell/chrome/admin-menu-drawer.d.ts +3 -1
- package/dist/admin-shell/chrome/admin-menu-drawer.js +14 -2
- package/dist/admin-shell/chrome/admin-menu-drawer_module.css +7 -6
- package/dist/admin-shell/chrome/dashboard.js +2 -2
- package/dist/admin-shell/collections/edit.js +142 -2
- package/dist/admin-shell/scheduled-publications/list.d.ts +4 -0
- package/dist/admin-shell/scheduled-publications/list.js +295 -0
- package/dist/admin-shell/scheduled-publications/list.module.js +25 -0
- package/dist/admin-shell/scheduled-publications/list_module.css +97 -0
- package/dist/routes/create-admin-layout-route.js +8 -3
- package/dist/routes/create-admin-scheduled-publications-route.d.ts +8 -0
- package/dist/routes/create-admin-scheduled-publications-route.js +66 -0
- package/dist/routes/index.d.ts +1 -0
- package/dist/routes/index.js +1 -0
- package/dist/server-fns/collections/get.d.ts +5 -0
- package/dist/server-fns/collections/get.js +19 -3
- package/dist/server-fns/collections/index.d.ts +1 -0
- package/dist/server-fns/collections/index.js +1 -0
- package/dist/server-fns/collections/scheduled-publication-response.d.ts +11 -0
- package/dist/server-fns/collections/scheduled-publication-response.js +5 -0
- package/dist/server-fns/collections/scheduled-publication.d.ts +111 -0
- package/dist/server-fns/collections/scheduled-publication.js +157 -0
- package/package.json +8 -8
- package/src/admin-shell/chrome/admin-layout.module.css +6 -1
- package/src/admin-shell/chrome/admin-menu-drawer.module.css +19 -6
- package/src/admin-shell/chrome/admin-menu-drawer.tsx +23 -1
- package/src/admin-shell/chrome/dashboard.tsx +11 -4
- package/src/admin-shell/collections/edit.tsx +140 -1
- package/src/admin-shell/scheduled-publications/list.module.css +105 -0
- package/src/admin-shell/scheduled-publications/list.tsx +292 -0
- package/src/routes/create-admin-layout-route.tsx +8 -3
- package/src/routes/create-admin-scheduled-publications-route.tsx +83 -0
- package/src/routes/index.ts +1 -0
- package/src/server-fns/collections/get.ts +30 -1
- package/src/server-fns/collections/index.ts +1 -0
- package/src/server-fns/collections/scheduled-publication-response.test.node.ts +45 -0
- package/src/server-fns/collections/scheduled-publication-response.ts +22 -0
- package/src/server-fns/collections/scheduled-publication.ts +270 -0
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This Source Code is subject to the terms of the Mozilla Public
|
|
3
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) Infonomic Company Limited
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { createServerFn } from '@tanstack/react-start'
|
|
10
|
+
|
|
11
|
+
import type { DocumentPublishScheduleInfo } from '@byline/client'
|
|
12
|
+
import { getAdminBylineClient, getAdminRequestContext } from '@byline/client/server'
|
|
13
|
+
import {
|
|
14
|
+
type DocumentPublishSchedule,
|
|
15
|
+
type DocumentPublishScheduleState,
|
|
16
|
+
ERR_NOT_FOUND,
|
|
17
|
+
ERR_VALIDATION,
|
|
18
|
+
type RecurringTaskHealth,
|
|
19
|
+
} from '@byline/core'
|
|
20
|
+
import { listDocumentPublishSchedules } from '@byline/core/services'
|
|
21
|
+
|
|
22
|
+
import { bylineCore } from '../../integrations/byline-core.js'
|
|
23
|
+
import { omitScheduleExecutionState } from './scheduled-publication-response.js'
|
|
24
|
+
import { serialise } from './utils.js'
|
|
25
|
+
|
|
26
|
+
const TASK_NAME = 'documents.publish-scheduled'
|
|
27
|
+
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
|
28
|
+
|
|
29
|
+
export interface ScheduledPublicationRuntime {
|
|
30
|
+
enabled: boolean
|
|
31
|
+
health: {
|
|
32
|
+
status: 'healthy' | 'failed' | 'stale' | 'not_started'
|
|
33
|
+
task: SerializedRecurringTaskHealth | null
|
|
34
|
+
} | null
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
type SerializedRecurringTaskHealth = Omit<
|
|
38
|
+
RecurringTaskHealth,
|
|
39
|
+
'nextRunAt' | 'lastStartedAt' | 'lastSucceededAt' | 'lastFailedAt' | 'databaseNow'
|
|
40
|
+
> & {
|
|
41
|
+
nextRunAt: string
|
|
42
|
+
lastStartedAt: string | null
|
|
43
|
+
lastSucceededAt: string | null
|
|
44
|
+
lastFailedAt: string | null
|
|
45
|
+
databaseNow: string
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type SerializedDocumentPublishSchedule = Omit<
|
|
49
|
+
DocumentPublishScheduleInfo,
|
|
50
|
+
| 'publishAt'
|
|
51
|
+
| 'suspendedAt'
|
|
52
|
+
| 'lastAuthorizedAt'
|
|
53
|
+
| 'scheduledAt'
|
|
54
|
+
| 'updatedAt'
|
|
55
|
+
| 'lastAttemptAt'
|
|
56
|
+
| 'nextAttemptAt'
|
|
57
|
+
> & {
|
|
58
|
+
publishAt: string
|
|
59
|
+
suspendedAt: string | null
|
|
60
|
+
lastAuthorizedAt: string
|
|
61
|
+
scheduledAt: string
|
|
62
|
+
updatedAt: string
|
|
63
|
+
lastAttemptAt: string | null
|
|
64
|
+
nextAttemptAt: string
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface ScheduledPublicationListItem extends SerializedDocumentPublishSchedule {
|
|
68
|
+
collectionPath: string
|
|
69
|
+
collectionLabel: string
|
|
70
|
+
documentPath: string | null
|
|
71
|
+
/**
|
|
72
|
+
* Display name for `lastAuthorizedBy` — full name, or email when the account
|
|
73
|
+
* has no name set. `null` when the account no longer exists, which is a real
|
|
74
|
+
* state rather than an error: the authorizer column is not a foreign key, so
|
|
75
|
+
* a schedule outlives the account that authorized it. Callers fall back to
|
|
76
|
+
* showing the id.
|
|
77
|
+
*/
|
|
78
|
+
lastAuthorizedByName: string | null
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface ScheduledPublicationListResponse {
|
|
82
|
+
schedules: ScheduledPublicationListItem[]
|
|
83
|
+
meta: {
|
|
84
|
+
page: number
|
|
85
|
+
pageSize: number
|
|
86
|
+
total: number
|
|
87
|
+
totalPages: number
|
|
88
|
+
}
|
|
89
|
+
runtime: ScheduledPublicationRuntime
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function assertFeatureEnabled(): void {
|
|
93
|
+
if (bylineCore().config.scheduledPublication?.enabled === true) return
|
|
94
|
+
throw ERR_NOT_FOUND({ message: 'Scheduled publication is not enabled for this installation' })
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function serializeSchedule(
|
|
98
|
+
schedule: DocumentPublishScheduleInfo | DocumentPublishSchedule
|
|
99
|
+
): SerializedDocumentPublishSchedule {
|
|
100
|
+
return serialise(
|
|
101
|
+
omitScheduleExecutionState(schedule)
|
|
102
|
+
) as unknown as SerializedDocumentPublishSchedule
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function scheduledPublicationRuntime(): Promise<ScheduledPublicationRuntime> {
|
|
106
|
+
const core = bylineCore()
|
|
107
|
+
if (core.config.scheduledPublication?.enabled !== true) {
|
|
108
|
+
return { enabled: false, health: null }
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const scheduler = core.db.scheduler
|
|
112
|
+
if (scheduler == null) {
|
|
113
|
+
return { enabled: true, health: { status: 'not_started', task: null } }
|
|
114
|
+
}
|
|
115
|
+
const [task] = await scheduler.health([TASK_NAME])
|
|
116
|
+
if (task == null) {
|
|
117
|
+
return { enabled: true, health: { status: 'not_started', task: null } }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const staleAfter = task.nextRunAt.getTime() + task.intervalMs * 2
|
|
121
|
+
const status = task.leaseExpired
|
|
122
|
+
? 'stale'
|
|
123
|
+
: task.lastStatus === 'failed'
|
|
124
|
+
? 'failed'
|
|
125
|
+
: staleAfter < task.databaseNow.getTime()
|
|
126
|
+
? 'stale'
|
|
127
|
+
: 'healthy'
|
|
128
|
+
return {
|
|
129
|
+
enabled: true,
|
|
130
|
+
health: {
|
|
131
|
+
status,
|
|
132
|
+
task: serialise(task) as unknown as SerializedRecurringTaskHealth,
|
|
133
|
+
},
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Authenticated feature discovery for the admin shell and health warning. */
|
|
138
|
+
export const getScheduledPublicationRuntime = createServerFn({ method: 'GET' }).handler(
|
|
139
|
+
async () => {
|
|
140
|
+
await getAdminRequestContext()
|
|
141
|
+
return scheduledPublicationRuntime()
|
|
142
|
+
}
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
export const scheduleCollectionDocumentPublish = createServerFn({ method: 'POST' })
|
|
146
|
+
.validator(
|
|
147
|
+
(input: { collection: string; id: string; publishAt: string; expectedVersionId: string }) =>
|
|
148
|
+
input
|
|
149
|
+
)
|
|
150
|
+
.handler(async ({ data }) => {
|
|
151
|
+
assertFeatureEnabled()
|
|
152
|
+
const schedule = await getAdminBylineClient()
|
|
153
|
+
.collection(data.collection)
|
|
154
|
+
.schedulePublish(data.id, {
|
|
155
|
+
publishAt: data.publishAt,
|
|
156
|
+
expectedVersionId: data.expectedVersionId,
|
|
157
|
+
})
|
|
158
|
+
return serializeSchedule(schedule)
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
export const confirmCollectionDocumentScheduledPublish = createServerFn({ method: 'POST' })
|
|
162
|
+
.validator((input: { collection: string; id: string; expectedVersionId: string }) => input)
|
|
163
|
+
.handler(async ({ data }) => {
|
|
164
|
+
assertFeatureEnabled()
|
|
165
|
+
const schedule = await getAdminBylineClient()
|
|
166
|
+
.collection(data.collection)
|
|
167
|
+
.confirmScheduledPublish(data.id, { expectedVersionId: data.expectedVersionId })
|
|
168
|
+
return serializeSchedule(schedule)
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
export const cancelCollectionDocumentScheduledPublish = createServerFn({ method: 'POST' })
|
|
172
|
+
.validator((input: { collection: string; id: string }) => input)
|
|
173
|
+
.handler(async ({ data }) => {
|
|
174
|
+
assertFeatureEnabled()
|
|
175
|
+
const schedule = await getAdminBylineClient()
|
|
176
|
+
.collection(data.collection)
|
|
177
|
+
.cancelScheduledPublish(data.id)
|
|
178
|
+
return schedule == null
|
|
179
|
+
? { status: 'not_found' as const }
|
|
180
|
+
: { status: 'cancelled' as const, schedule: serializeSchedule(schedule) }
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
export const listScheduledPublications = createServerFn({ method: 'GET' })
|
|
184
|
+
.validator(
|
|
185
|
+
(input: {
|
|
186
|
+
states?: readonly DocumentPublishScheduleState[]
|
|
187
|
+
lastAuthorizedBy?: string
|
|
188
|
+
page?: number
|
|
189
|
+
pageSize?: number
|
|
190
|
+
}) => input
|
|
191
|
+
)
|
|
192
|
+
.handler(async ({ data }) => {
|
|
193
|
+
assertFeatureEnabled()
|
|
194
|
+
if (data.states?.some((state) => state !== 'armed' && state !== 'needs_reconfirm') === true) {
|
|
195
|
+
throw ERR_VALIDATION({ message: 'state must be armed or needs_reconfirm' })
|
|
196
|
+
}
|
|
197
|
+
if (data.lastAuthorizedBy != null && !UUID_RE.test(data.lastAuthorizedBy)) {
|
|
198
|
+
throw ERR_VALIDATION({ message: 'lastAuthorizedBy must be a UUID' })
|
|
199
|
+
}
|
|
200
|
+
const core = bylineCore()
|
|
201
|
+
const page = data.page ?? 1
|
|
202
|
+
const pageSize = data.pageSize ?? 25
|
|
203
|
+
const requestContext = await getAdminRequestContext()
|
|
204
|
+
const result = await listDocumentPublishSchedules(core, requestContext, {
|
|
205
|
+
states: data.states,
|
|
206
|
+
lastAuthorizedBy: data.lastAuthorizedBy,
|
|
207
|
+
page,
|
|
208
|
+
pageSize,
|
|
209
|
+
})
|
|
210
|
+
const collections = new Map(
|
|
211
|
+
core.collections.map((definition) => {
|
|
212
|
+
const record = core.getCollectionRecord(definition.path)
|
|
213
|
+
return [record.collectionId, definition] as const
|
|
214
|
+
})
|
|
215
|
+
)
|
|
216
|
+
// Resolve authorizer names in one query for the whole page rather than one
|
|
217
|
+
// per row. `last_authorized_by` is deliberately not a foreign key — deleting
|
|
218
|
+
// an admin account must not revoke a schedule it authorized — so an id here
|
|
219
|
+
// may simply have no user row left, and the caller falls back to the id.
|
|
220
|
+
const authorizerIds = [
|
|
221
|
+
...new Set(
|
|
222
|
+
result.schedules
|
|
223
|
+
.map((schedule) => schedule.lastAuthorizedBy)
|
|
224
|
+
.filter((id): id is string => id != null)
|
|
225
|
+
),
|
|
226
|
+
]
|
|
227
|
+
const authorizerNames = new Map<string, string>()
|
|
228
|
+
// `adminStore` is optional on core — an installation can run without the
|
|
229
|
+
// admin subsystem — so a missing store simply means no names to resolve and
|
|
230
|
+
// the column falls back to ids.
|
|
231
|
+
const adminUsers = core.adminStore?.adminUsers
|
|
232
|
+
if (authorizerIds.length > 0 && adminUsers != null) {
|
|
233
|
+
const users = await adminUsers.getByIds(authorizerIds)
|
|
234
|
+
for (const user of users) {
|
|
235
|
+
const fullName = [user.given_name, user.family_name].filter(Boolean).join(' ').trim()
|
|
236
|
+
authorizerNames.set(user.id, fullName.length > 0 ? fullName : user.email)
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const schedules = await Promise.all(
|
|
241
|
+
result.schedules.map(async (schedule): Promise<ScheduledPublicationListItem> => {
|
|
242
|
+
const definition = collections.get(schedule.collectionId)
|
|
243
|
+
const documentPath = await core.db.queries.documents.getCurrentPath({
|
|
244
|
+
collection_id: schedule.collectionId,
|
|
245
|
+
document_id: schedule.documentId,
|
|
246
|
+
})
|
|
247
|
+
return {
|
|
248
|
+
...serializeSchedule(schedule),
|
|
249
|
+
collectionPath: definition?.path ?? schedule.collectionId,
|
|
250
|
+
collectionLabel: definition?.labels.plural ?? definition?.path ?? schedule.collectionId,
|
|
251
|
+
documentPath,
|
|
252
|
+
lastAuthorizedByName:
|
|
253
|
+
schedule.lastAuthorizedBy == null
|
|
254
|
+
? null
|
|
255
|
+
: (authorizerNames.get(schedule.lastAuthorizedBy) ?? null),
|
|
256
|
+
}
|
|
257
|
+
})
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
return {
|
|
261
|
+
schedules,
|
|
262
|
+
meta: {
|
|
263
|
+
page,
|
|
264
|
+
pageSize,
|
|
265
|
+
total: result.total,
|
|
266
|
+
totalPages: Math.max(1, Math.ceil(result.total / pageSize)),
|
|
267
|
+
},
|
|
268
|
+
runtime: await scheduledPublicationRuntime(),
|
|
269
|
+
} satisfies ScheduledPublicationListResponse
|
|
270
|
+
})
|