@composable-os/pack-courses 0.0.0-stage → 0.1.0-beta.10
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 +109 -0
- package/dist/automations.d.ts +12 -0
- package/dist/automations.js +84 -0
- package/dist/blocks/courses-builder/course-builder.d.ts +5 -0
- package/dist/blocks/courses-builder/course-builder.js +69 -0
- package/dist/blocks/courses-catalogue/catalogue.d.ts +6 -0
- package/dist/blocks/courses-catalogue/catalogue.js +21 -0
- package/dist/blocks/courses-course/course-page.d.ts +10 -0
- package/dist/blocks/courses-course/course-page.js +51 -0
- package/dist/blocks/courses-my-courses/my-courses.d.ts +5 -0
- package/dist/blocks/courses-my-courses/my-courses.js +20 -0
- package/dist/blocks/courses-native-catalogue/catalogue-screen.d.ts +6 -0
- package/dist/blocks/courses-native-catalogue/catalogue-screen.js +15 -0
- package/dist/blocks/courses-native-course/course-screen.d.ts +7 -0
- package/dist/blocks/courses-native-course/course-screen.js +18 -0
- package/dist/blocks/courses-native-my-courses/my-courses-screen.d.ts +5 -0
- package/dist/blocks/courses-native-my-courses/my-courses-screen.js +9 -0
- package/dist/blocks/courses-native-player/lesson-screen.d.ts +19 -0
- package/dist/blocks/courses-native-player/lesson-screen.js +11 -0
- package/dist/blocks/courses-native-subscription/subscription-screen.d.ts +4 -0
- package/dist/blocks/courses-native-subscription/subscription-screen.js +17 -0
- package/dist/blocks/courses-player/player.d.ts +12 -0
- package/dist/blocks/courses-player/player.js +51 -0
- package/dist/blocks/courses-subscribe/subscribe.d.ts +28 -0
- package/dist/blocks/courses-subscribe/subscribe.js +146 -0
- package/dist/blocks/courses-subscription/my-subscription.d.ts +6 -0
- package/dist/blocks/courses-subscription/my-subscription.js +50 -0
- package/dist/client/index.d.ts +1319 -0
- package/dist/client/index.js +414 -0
- package/dist/dashboards.d.ts +5 -0
- package/dist/dashboards.js +119 -0
- package/dist/index.d.ts +809 -0
- package/dist/index.js +427 -0
- package/dist/outline.d.ts +85 -0
- package/dist/outline.js +188 -0
- package/dist/tools.d.ts +255 -0
- package/dist/tools.js +826 -0
- package/package.json +36 -4
- package/README.md +0 -3
package/dist/tools.js
ADDED
|
@@ -0,0 +1,826 @@
|
|
|
1
|
+
// The Courses tools. They become operations x.courses.<tool>, run in the sandbox, and reach the
|
|
2
|
+
// host only through ctx.ops. None holds a rule: a course's price, its sale window and what a paid
|
|
3
|
+
// line grants are `sellable`'s; who holds access to a course, and so which lesson parts a caller
|
|
4
|
+
// reads and which videos they may play, is the host's (`access`, the seam's one question, ADR
|
|
5
|
+
// 0477); progress is written only while the student holds access (`requireAccess`, ADR 0478). A
|
|
6
|
+
// tool asks, and reports what the host answered.
|
|
7
|
+
//
|
|
8
|
+
// A lesson's video plays with a token only `media.play` makes, in the host: `lesson` names the
|
|
9
|
+
// video and where it is shown, and the app asks `media.play` for it (ADR 0478: the signing key
|
|
10
|
+
// stays in cos-app).
|
|
11
|
+
import { defineTool, OpsCallError, z } from "@composable-os/sdk";
|
|
12
|
+
import { asOutline, completion, findLesson, LESSON_KINDS, lessonVideo, nextLesson, watchedToEnd, } from "./outline.js";
|
|
13
|
+
export const THEME = "courses.theme";
|
|
14
|
+
export const COURSE = "courses.course";
|
|
15
|
+
export const BUNDLE = "courses.bundle";
|
|
16
|
+
export const ENROLMENT = "courses.enrolment";
|
|
17
|
+
export const PROGRESS = "courses.progress";
|
|
18
|
+
const PROGRAM = "programs.program";
|
|
19
|
+
const SESSION = "programs.session";
|
|
20
|
+
const id = z.string().min(1).max(64);
|
|
21
|
+
const text = (value) => value === null || value === undefined || value === "" ? null : String(value);
|
|
22
|
+
const num = (value) => typeof value === "number" ? value : value == null || value === "" ? null : Number(value);
|
|
23
|
+
/** What a tool says for a course the caller may not see: the host's one not-found. */
|
|
24
|
+
const notFound = (what, which) => new OpsCallError("not_found", `No ${what} with id ${which}`, { object: what, id: which });
|
|
25
|
+
const isCustomer = (ctx) => ctx.caller?.kind === "customer";
|
|
26
|
+
const isStaff = (ctx) => ctx.caller?.kind === "staff";
|
|
27
|
+
/** Of these courses, the ones the caller holds access to: the host's answer (ADR 0477). */
|
|
28
|
+
async function held(ctx, ids) {
|
|
29
|
+
if (!ids.length || !isCustomer(ctx))
|
|
30
|
+
return new Set();
|
|
31
|
+
const answer = await ctx.ops.call("access.check", {
|
|
32
|
+
object: COURSE,
|
|
33
|
+
ids: ids.slice(0, 100),
|
|
34
|
+
});
|
|
35
|
+
return new Set(answer.held);
|
|
36
|
+
}
|
|
37
|
+
/** The bundle's courses, as its published version lists them. */
|
|
38
|
+
const bundleCourses = (bundle) => {
|
|
39
|
+
const items = bundle.values.courses?.items ?? [];
|
|
40
|
+
return items.filter((x) => typeof x === "string");
|
|
41
|
+
};
|
|
42
|
+
const picture = z
|
|
43
|
+
.object({ media: z.string(), alt: z.string().optional() })
|
|
44
|
+
.passthrough()
|
|
45
|
+
.nullable();
|
|
46
|
+
const video = z.object({ media: z.string(), poster: z.string().optional() }).nullable();
|
|
47
|
+
const lessonSummary = z.object({
|
|
48
|
+
key: z.string(),
|
|
49
|
+
title: z.string(),
|
|
50
|
+
kind: z.enum(LESSON_KINDS),
|
|
51
|
+
duration: z.number().nullable(),
|
|
52
|
+
preview: z.boolean(),
|
|
53
|
+
});
|
|
54
|
+
const courseCard = z.object({
|
|
55
|
+
id: z.string(),
|
|
56
|
+
title: z.string().nullable(),
|
|
57
|
+
slug: z.string().nullable(),
|
|
58
|
+
theme: z.string().nullable(),
|
|
59
|
+
level: z.number().nullable(),
|
|
60
|
+
summary: z.string().nullable(),
|
|
61
|
+
cover: picture,
|
|
62
|
+
trailer: video,
|
|
63
|
+
price: z.number().nullable().describe("In the major unit of `currency`"),
|
|
64
|
+
lessons: z.number(),
|
|
65
|
+
});
|
|
66
|
+
function card(record) {
|
|
67
|
+
const v = record.values;
|
|
68
|
+
const outline = asOutline(v.outline);
|
|
69
|
+
return {
|
|
70
|
+
id: record.id,
|
|
71
|
+
title: text(v.title),
|
|
72
|
+
slug: text(v.slug),
|
|
73
|
+
theme: text(v.theme),
|
|
74
|
+
level: num(v.level),
|
|
75
|
+
summary: text(v.summary),
|
|
76
|
+
cover: v.cover ?? null,
|
|
77
|
+
trailer: v.trailer ?? null,
|
|
78
|
+
price: num(v.price),
|
|
79
|
+
lessons: outline.sections.reduce((n, s) => n + (s.lessons?.length ?? 0), 0),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function summaryOf(outline) {
|
|
83
|
+
return outline.sections.map((section) => ({
|
|
84
|
+
key: section.key,
|
|
85
|
+
title: section.title,
|
|
86
|
+
lessons: (section.lessons ?? []).map((l) => ({
|
|
87
|
+
key: l.key,
|
|
88
|
+
title: l.title,
|
|
89
|
+
kind: l.kind,
|
|
90
|
+
duration: l.duration ?? null,
|
|
91
|
+
preview: l.preview === true,
|
|
92
|
+
})),
|
|
93
|
+
}));
|
|
94
|
+
}
|
|
95
|
+
/** The caller's own progress rows on a course, by lesson key. */
|
|
96
|
+
async function progressRows(ctx, courseId) {
|
|
97
|
+
const page = await ctx.ops.call("my.list", {
|
|
98
|
+
object: PROGRESS,
|
|
99
|
+
filter: { field: "course", op: "eq", value: courseId },
|
|
100
|
+
limit: 200,
|
|
101
|
+
});
|
|
102
|
+
return new Map(page.items.map((row) => [String(row.values.lessonKey), row]));
|
|
103
|
+
}
|
|
104
|
+
const currency = z
|
|
105
|
+
.string()
|
|
106
|
+
.nullable()
|
|
107
|
+
.describe("The currency prices are in (ISO 4217), the deployment's one; null until it is set");
|
|
108
|
+
// --- The catalogue -------------------------------------------------------------------------------
|
|
109
|
+
export const catalogue = defineTool({
|
|
110
|
+
description: "The published courses, bundles and themes, for a catalogue page: each course's title, " +
|
|
111
|
+
"theme, level, summary, cover, trailer, price and number of lessons; each bundle's courses " +
|
|
112
|
+
"and price; the themes in their order. Prices are in the major unit of `currency`. Filter " +
|
|
113
|
+
"by theme or level. Only what the public may read: a draft is not here. Read-only.",
|
|
114
|
+
input: z.object({
|
|
115
|
+
theme: id.optional(),
|
|
116
|
+
level: z.number().int().min(1).max(3).optional(),
|
|
117
|
+
}),
|
|
118
|
+
output: z.object({
|
|
119
|
+
themes: z.array(z.object({
|
|
120
|
+
id: z.string(),
|
|
121
|
+
name: z.string().nullable(),
|
|
122
|
+
slug: z.string().nullable(),
|
|
123
|
+
summary: z.string().nullable(),
|
|
124
|
+
picture,
|
|
125
|
+
})),
|
|
126
|
+
courses: z.array(courseCard),
|
|
127
|
+
bundles: z.array(z.object({
|
|
128
|
+
id: z.string(),
|
|
129
|
+
title: z.string().nullable(),
|
|
130
|
+
slug: z.string().nullable(),
|
|
131
|
+
summary: z.string().nullable(),
|
|
132
|
+
cover: picture,
|
|
133
|
+
courses: z.array(z.string()),
|
|
134
|
+
price: z.number().nullable(),
|
|
135
|
+
})),
|
|
136
|
+
currency,
|
|
137
|
+
}),
|
|
138
|
+
risk: "read",
|
|
139
|
+
audiences: ["customer", "public"],
|
|
140
|
+
async handler(input, ctx) {
|
|
141
|
+
const where = [
|
|
142
|
+
...(input.theme ? [{ field: "theme", op: "eq", value: input.theme }] : []),
|
|
143
|
+
...(input.level ? [{ field: "level", op: "eq", value: input.level }] : []),
|
|
144
|
+
];
|
|
145
|
+
const [themes, courses, bundles] = await Promise.all([
|
|
146
|
+
ctx.ops.call("public.list", {
|
|
147
|
+
object: THEME,
|
|
148
|
+
sort: { field: "order", direction: "asc" },
|
|
149
|
+
limit: 100,
|
|
150
|
+
}),
|
|
151
|
+
ctx.ops.call("public.list", {
|
|
152
|
+
object: COURSE,
|
|
153
|
+
...(where.length ? { filter: where.length === 1 ? where[0] : { and: where } } : {}),
|
|
154
|
+
sort: { field: "title", direction: "asc" },
|
|
155
|
+
limit: 100,
|
|
156
|
+
}),
|
|
157
|
+
ctx.ops.call("public.list", { object: BUNDLE, limit: 50 }),
|
|
158
|
+
]);
|
|
159
|
+
return {
|
|
160
|
+
themes: themes.items.map((t) => ({
|
|
161
|
+
id: t.id,
|
|
162
|
+
name: text(t.values.name),
|
|
163
|
+
slug: text(t.values.slug),
|
|
164
|
+
summary: text(t.values.summary),
|
|
165
|
+
picture: t.values.picture ?? null,
|
|
166
|
+
})),
|
|
167
|
+
courses: courses.items.map(card),
|
|
168
|
+
bundles: bundles.items.map((b) => ({
|
|
169
|
+
id: b.id,
|
|
170
|
+
title: text(b.values.title),
|
|
171
|
+
slug: text(b.values.slug),
|
|
172
|
+
summary: text(b.values.summary),
|
|
173
|
+
cover: b.values.cover ?? null,
|
|
174
|
+
courses: bundleCourses(b),
|
|
175
|
+
price: num(b.values.price),
|
|
176
|
+
})),
|
|
177
|
+
currency: courses.currency ?? bundles.currency ?? null,
|
|
178
|
+
};
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
// --- Course plans --------------------------------------------------------------------------------
|
|
182
|
+
export const PLAN = "courses.plan";
|
|
183
|
+
export const plans = defineTool({
|
|
184
|
+
description: "The course plans on offer: a subscription to every published course, paid every month. Each " +
|
|
185
|
+
"plan's title, description, price per month (in the major unit of `currency`) and the days of " +
|
|
186
|
+
"its free trial. Whether a person gets the trial is the host's, when they subscribe " +
|
|
187
|
+
"(subscription.start): one per person, signed in. Read-only.",
|
|
188
|
+
input: z.object({}),
|
|
189
|
+
output: z.object({
|
|
190
|
+
plans: z.array(z.object({
|
|
191
|
+
id: z.string(),
|
|
192
|
+
title: z.string().nullable(),
|
|
193
|
+
description: z.string().nullable(),
|
|
194
|
+
price: z.number().nullable().describe("Per month, in the major unit of `currency`"),
|
|
195
|
+
trialDays: z.number().nullable(),
|
|
196
|
+
})),
|
|
197
|
+
currency,
|
|
198
|
+
}),
|
|
199
|
+
risk: "read",
|
|
200
|
+
audiences: ["customer", "public"],
|
|
201
|
+
async handler(_input, ctx) {
|
|
202
|
+
const page = await ctx.ops.call("public.list", {
|
|
203
|
+
object: PLAN,
|
|
204
|
+
sort: { field: "price", direction: "asc" },
|
|
205
|
+
limit: 20,
|
|
206
|
+
});
|
|
207
|
+
return {
|
|
208
|
+
plans: page.items.map((p) => ({
|
|
209
|
+
id: p.id,
|
|
210
|
+
title: text(p.values.title),
|
|
211
|
+
description: text(p.values.description),
|
|
212
|
+
price: num(p.values.price),
|
|
213
|
+
trialDays: num(p.values.trialDays),
|
|
214
|
+
})),
|
|
215
|
+
currency: page.currency ?? null,
|
|
216
|
+
};
|
|
217
|
+
},
|
|
218
|
+
});
|
|
219
|
+
// --- One course ----------------------------------------------------------------------------------
|
|
220
|
+
export const course = defineTool({
|
|
221
|
+
description: "One published course by its address (slug): its fields, its description, its outline's " +
|
|
222
|
+
"sections and lessons (title, kind, minutes, whether a free preview) and, for a signed-in " +
|
|
223
|
+
"customer, whether they hold access and which lessons they have done. Not found for a " +
|
|
224
|
+
"draft or an unknown address. Read-only.",
|
|
225
|
+
input: z.object({ slug: z.string().trim().min(1).max(200) }),
|
|
226
|
+
output: courseCard.extend({
|
|
227
|
+
description: z.unknown().describe("Rich text"),
|
|
228
|
+
sections: z.array(z.object({ key: z.string(), title: z.string(), lessons: z.array(lessonSummary) })),
|
|
229
|
+
currency,
|
|
230
|
+
holds: z.boolean().describe("The caller holds access: show open, not buy"),
|
|
231
|
+
done: z.array(z.string()).describe("The lesson keys the caller has done"),
|
|
232
|
+
complete: z.boolean(),
|
|
233
|
+
}),
|
|
234
|
+
risk: "read",
|
|
235
|
+
audiences: ["customer", "public"],
|
|
236
|
+
async handler(input, ctx) {
|
|
237
|
+
const page = await ctx.ops.call("public.list", {
|
|
238
|
+
object: COURSE,
|
|
239
|
+
filter: { field: "slug", op: "eq", value: input.slug },
|
|
240
|
+
limit: 1,
|
|
241
|
+
});
|
|
242
|
+
const record = page.items[0];
|
|
243
|
+
if (!record)
|
|
244
|
+
throw notFound(COURSE, input.slug);
|
|
245
|
+
const outline = asOutline(record.values.outline);
|
|
246
|
+
const holds = (await held(ctx, [record.id])).has(record.id);
|
|
247
|
+
const rows = holds ? await progressRows(ctx, record.id) : new Map();
|
|
248
|
+
const done = [...rows.values()]
|
|
249
|
+
.filter((r) => r.values.state === "done")
|
|
250
|
+
.map((r) => String(r.values.lessonKey));
|
|
251
|
+
return {
|
|
252
|
+
...card(record),
|
|
253
|
+
description: record.values.description ?? null,
|
|
254
|
+
sections: summaryOf(outline),
|
|
255
|
+
currency: page.currency ?? null,
|
|
256
|
+
holds,
|
|
257
|
+
done,
|
|
258
|
+
complete: completion(outline, new Set(done)).complete,
|
|
259
|
+
};
|
|
260
|
+
},
|
|
261
|
+
});
|
|
262
|
+
// --- A lesson ------------------------------------------------------------------------------------
|
|
263
|
+
export const lesson = defineTool({
|
|
264
|
+
description: "One lesson of a published course for someone who holds access to it (or any lesson marked " +
|
|
265
|
+
"a free preview): its kind and its material (a video and notes, a page of text, a training " +
|
|
266
|
+
"session, a download), where the student left its video, and the next lesson. A video is " +
|
|
267
|
+
"named with where it is shown: the app asks media.play for a token to play it. Not found " +
|
|
268
|
+
"without access, as a lesson that does not exist. Read-only.",
|
|
269
|
+
input: z.object({ courseId: id, lessonKey: z.string().min(1).max(40) }),
|
|
270
|
+
output: z.object({
|
|
271
|
+
courseId: z.string(),
|
|
272
|
+
lesson: z.record(z.string(), z.unknown()).describe("The lesson as the outline has it"),
|
|
273
|
+
video: z
|
|
274
|
+
.object({
|
|
275
|
+
media: z.string(),
|
|
276
|
+
poster: z.string().nullable(),
|
|
277
|
+
record: z.object({ object: z.string(), id: z.string() }),
|
|
278
|
+
field: z.string(),
|
|
279
|
+
})
|
|
280
|
+
.nullable()
|
|
281
|
+
.describe("Pass to media.play to get a token and the address it plays at"),
|
|
282
|
+
position: z.number().nullable().describe("Seconds: where the student left the video"),
|
|
283
|
+
state: z.enum(["started", "done"]).nullable(),
|
|
284
|
+
next: lessonSummary.nullable(),
|
|
285
|
+
}),
|
|
286
|
+
risk: "read",
|
|
287
|
+
audiences: ["staff", "customer"],
|
|
288
|
+
async handler(input, ctx) {
|
|
289
|
+
// Staff read the published version; a customer the course as the seam shows them, which
|
|
290
|
+
// has a lesson's material only when they hold access (or the lesson is a free preview).
|
|
291
|
+
let outline;
|
|
292
|
+
if (isStaff(ctx)) {
|
|
293
|
+
const version = await ctx.ops.call("record.version", {
|
|
294
|
+
object: COURSE,
|
|
295
|
+
id: input.courseId,
|
|
296
|
+
});
|
|
297
|
+
outline = asOutline(version.values.outline);
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
const record = await ctx.ops.call("my.get", {
|
|
301
|
+
object: COURSE,
|
|
302
|
+
id: input.courseId,
|
|
303
|
+
});
|
|
304
|
+
outline = asOutline(record.values.outline);
|
|
305
|
+
}
|
|
306
|
+
const found = findLesson(outline, input.lessonKey);
|
|
307
|
+
if (!found)
|
|
308
|
+
throw notFound("lesson", input.lessonKey);
|
|
309
|
+
const holds = isStaff(ctx) || (await held(ctx, [input.courseId])).has(input.courseId);
|
|
310
|
+
if (!holds && found.preview !== true)
|
|
311
|
+
throw notFound("lesson", input.lessonKey);
|
|
312
|
+
const row = isCustomer(ctx) ? (await progressRows(ctx, input.courseId)).get(found.key) : null;
|
|
313
|
+
const v = lessonVideo(found);
|
|
314
|
+
const after = nextLesson(outline, found.key);
|
|
315
|
+
const { section: _section, ...plain } = found;
|
|
316
|
+
return {
|
|
317
|
+
courseId: input.courseId,
|
|
318
|
+
lesson: plain,
|
|
319
|
+
video: v
|
|
320
|
+
? {
|
|
321
|
+
media: v.media,
|
|
322
|
+
poster: v.poster ?? null,
|
|
323
|
+
record: { object: COURSE, id: input.courseId },
|
|
324
|
+
field: "outline",
|
|
325
|
+
}
|
|
326
|
+
: null,
|
|
327
|
+
position: row ? num(row.values.position) : null,
|
|
328
|
+
state: row ? (text(row.values.state) ?? null) : null,
|
|
329
|
+
next: after
|
|
330
|
+
? {
|
|
331
|
+
key: after.key,
|
|
332
|
+
title: after.title,
|
|
333
|
+
kind: after.kind,
|
|
334
|
+
duration: after.duration ?? null,
|
|
335
|
+
preview: after.preview === true,
|
|
336
|
+
}
|
|
337
|
+
: null,
|
|
338
|
+
};
|
|
339
|
+
},
|
|
340
|
+
});
|
|
341
|
+
// --- Progress ------------------------------------------------------------------------------------
|
|
342
|
+
export const progress = defineTool({
|
|
343
|
+
description: "Mark a lesson started or done, and keep where its video was left (seconds), for the " +
|
|
344
|
+
"signed-in student. A video watched to within 10 seconds of its end counts as done (give " +
|
|
345
|
+
"its `duration`). The host refuses it, as a course that does not exist, when the student " +
|
|
346
|
+
"does not hold access to the course. Returns the lesson's progress and the course's.",
|
|
347
|
+
input: z.object({
|
|
348
|
+
courseId: id,
|
|
349
|
+
lessonKey: z.string().min(1).max(40),
|
|
350
|
+
state: z.enum(["started", "done"]).optional(),
|
|
351
|
+
position: z.number().min(0).max(86_400).optional(),
|
|
352
|
+
duration: z.number().min(0).max(86_400).optional().describe("The video's length, seconds"),
|
|
353
|
+
}),
|
|
354
|
+
output: z.object({
|
|
355
|
+
lessonKey: z.string(),
|
|
356
|
+
state: z.enum(["started", "done"]),
|
|
357
|
+
position: z.number().nullable(),
|
|
358
|
+
done: z.number(),
|
|
359
|
+
counted: z.number(),
|
|
360
|
+
complete: z.boolean(),
|
|
361
|
+
}),
|
|
362
|
+
risk: "write",
|
|
363
|
+
audiences: ["customer"],
|
|
364
|
+
async handler(input, ctx) {
|
|
365
|
+
const record = await ctx.ops.call("my.get", {
|
|
366
|
+
object: COURSE,
|
|
367
|
+
id: input.courseId,
|
|
368
|
+
});
|
|
369
|
+
const outline = asOutline(record.values.outline);
|
|
370
|
+
if (!findLesson(outline, input.lessonKey))
|
|
371
|
+
throw notFound("lesson", input.lessonKey);
|
|
372
|
+
const rows = await progressRows(ctx, input.courseId);
|
|
373
|
+
const existing = rows.get(input.lessonKey);
|
|
374
|
+
const now = new Date().toISOString();
|
|
375
|
+
const ended = input.position !== undefined && watchedToEnd(input.position, input.duration);
|
|
376
|
+
const before = text(existing?.values.state);
|
|
377
|
+
const state = input.state === "done" || ended || before === "done" ? "done" : "started";
|
|
378
|
+
const values = {
|
|
379
|
+
state,
|
|
380
|
+
...(input.position !== undefined ? { position: input.position } : {}),
|
|
381
|
+
...(state === "done" && before !== "done" ? { doneAt: now } : {}),
|
|
382
|
+
};
|
|
383
|
+
// The host checks the student holds access to the course on create and on every update.
|
|
384
|
+
if (existing) {
|
|
385
|
+
await ctx.ops.call("my.update", { object: PROGRESS, id: existing.id, values });
|
|
386
|
+
}
|
|
387
|
+
else {
|
|
388
|
+
await ctx.ops.call("my.create", {
|
|
389
|
+
object: PROGRESS,
|
|
390
|
+
values: { course: input.courseId, lessonKey: input.lessonKey, startedAt: now, ...values },
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
const done = new Set([...rows.values()]
|
|
394
|
+
.filter((r) => r.values.state === "done")
|
|
395
|
+
.map((r) => String(r.values.lessonKey)));
|
|
396
|
+
if (state === "done")
|
|
397
|
+
done.add(input.lessonKey);
|
|
398
|
+
const sum = completion(outline, done);
|
|
399
|
+
return {
|
|
400
|
+
lessonKey: input.lessonKey,
|
|
401
|
+
state,
|
|
402
|
+
position: input.position ?? num(existing?.values.position),
|
|
403
|
+
done: sum.done,
|
|
404
|
+
counted: sum.counted,
|
|
405
|
+
complete: sum.complete,
|
|
406
|
+
};
|
|
407
|
+
},
|
|
408
|
+
});
|
|
409
|
+
// --- My courses ----------------------------------------------------------------------------------
|
|
410
|
+
export const myCourses = defineTool({
|
|
411
|
+
description: "Every course the signed-in student holds access to and why (bought, in a bundle, given by " +
|
|
412
|
+
"staff, imported, or through their course subscription), with how far they are (lessons " +
|
|
413
|
+
"done of those that count, complete) and the lesson to continue with. The host says which " +
|
|
414
|
+
"they hold; nothing here decides it. Read-only.",
|
|
415
|
+
input: z.object({}),
|
|
416
|
+
output: z.object({
|
|
417
|
+
courses: z.array(courseCard.extend({
|
|
418
|
+
why: z.enum(["bought", "bundle", "given", "imported", "subscription"]),
|
|
419
|
+
done: z.number(),
|
|
420
|
+
counted: z.number(),
|
|
421
|
+
complete: z.boolean(),
|
|
422
|
+
continueWith: z.string().nullable().describe("The first lesson not done yet"),
|
|
423
|
+
})),
|
|
424
|
+
}),
|
|
425
|
+
risk: "read",
|
|
426
|
+
audiences: ["customer"],
|
|
427
|
+
async handler(_input, ctx) {
|
|
428
|
+
const enrolments = await ctx.ops.call("my.list", { object: ENROLMENT, limit: 200 });
|
|
429
|
+
const why = new Map();
|
|
430
|
+
const completed = new Set();
|
|
431
|
+
const bundles = new Map();
|
|
432
|
+
for (const e of enrolments.items) {
|
|
433
|
+
const reason = e.values.source === "staff"
|
|
434
|
+
? "given"
|
|
435
|
+
: e.values.source === "import"
|
|
436
|
+
? "imported"
|
|
437
|
+
: "bought";
|
|
438
|
+
const courseId = text(e.values.course);
|
|
439
|
+
const bundleId = text(e.values.bundle);
|
|
440
|
+
if (courseId) {
|
|
441
|
+
if (!why.has(courseId))
|
|
442
|
+
why.set(courseId, reason);
|
|
443
|
+
if (e.values.completed === true)
|
|
444
|
+
completed.add(courseId);
|
|
445
|
+
}
|
|
446
|
+
if (bundleId && !bundles.has(bundleId)) {
|
|
447
|
+
try {
|
|
448
|
+
const bundle = await ctx.ops.call("public.get", {
|
|
449
|
+
object: BUNDLE,
|
|
450
|
+
id: bundleId,
|
|
451
|
+
});
|
|
452
|
+
bundles.set(bundleId, bundleCourses(bundle));
|
|
453
|
+
for (const c of bundleCourses(bundle))
|
|
454
|
+
if (!why.has(c))
|
|
455
|
+
why.set(c, "bundle");
|
|
456
|
+
}
|
|
457
|
+
catch {
|
|
458
|
+
// A bundle no longer published gives nothing to list.
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
// A course subscription covers every published course (ADR 0488): the published ones the host
|
|
463
|
+
// says they hold that no enrolment of theirs explains are through it.
|
|
464
|
+
const published = await ctx.ops.call("public.list", {
|
|
465
|
+
object: COURSE,
|
|
466
|
+
sort: { field: "title", direction: "asc" },
|
|
467
|
+
limit: 100,
|
|
468
|
+
});
|
|
469
|
+
const others = published.items.map((c) => c.id).filter((c) => !why.has(c));
|
|
470
|
+
for (const c of await held(ctx, others))
|
|
471
|
+
why.set(c, "subscription");
|
|
472
|
+
// Which of these the host says they hold now (live, in its state, inside its until).
|
|
473
|
+
const holding = await held(ctx, [...why.keys()]);
|
|
474
|
+
const out = [];
|
|
475
|
+
for (const courseId of [...why.keys()].filter((c) => holding.has(c))) {
|
|
476
|
+
const record = await ctx.ops.call("my.get", { object: COURSE, id: courseId });
|
|
477
|
+
const outline = asOutline(record.values.outline);
|
|
478
|
+
const rows = await progressRows(ctx, courseId);
|
|
479
|
+
const done = new Set([...rows.values()]
|
|
480
|
+
.filter((r) => r.values.state === "done")
|
|
481
|
+
.map((r) => String(r.values.lessonKey)));
|
|
482
|
+
const sum = completion(outline, done, completed.has(courseId));
|
|
483
|
+
const first = outline.sections
|
|
484
|
+
.flatMap((s) => s.lessons ?? [])
|
|
485
|
+
.find((l) => l.kind !== "download" && !done.has(l.key));
|
|
486
|
+
out.push({
|
|
487
|
+
...card(record),
|
|
488
|
+
why: why.get(courseId) ?? "bought",
|
|
489
|
+
done: sum.done,
|
|
490
|
+
counted: sum.counted,
|
|
491
|
+
complete: sum.complete,
|
|
492
|
+
continueWith: first?.key ?? null,
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
return { courses: out };
|
|
496
|
+
},
|
|
497
|
+
});
|
|
498
|
+
// --- Buying --------------------------------------------------------------------------------------
|
|
499
|
+
export const buy = defineTool({
|
|
500
|
+
description: "Start an order for one course or one bundle. A signed-in customer buys as themselves; a " +
|
|
501
|
+
"guest gives their email address. The host prices it from the record and refuses one " +
|
|
502
|
+
"outside its sale window. A course the caller already holds access to is refused as one " +
|
|
503
|
+
"that does not exist. Nothing is paid here: the buyer then calls order.checkout with the " +
|
|
504
|
+
"order's id (and a guest's token) to pay on the provider's page. Amounts in the minor unit.",
|
|
505
|
+
input: z
|
|
506
|
+
.object({
|
|
507
|
+
courseId: id.optional(),
|
|
508
|
+
bundleId: id.optional(),
|
|
509
|
+
email: z.string().trim().max(320).optional().describe("A guest's email address"),
|
|
510
|
+
})
|
|
511
|
+
.refine((v) => Boolean(v.courseId) !== Boolean(v.bundleId), {
|
|
512
|
+
message: "Name a course or a bundle, not both",
|
|
513
|
+
}),
|
|
514
|
+
output: z.object({
|
|
515
|
+
id: z.string(),
|
|
516
|
+
state: z.string(),
|
|
517
|
+
currency: z.string(),
|
|
518
|
+
total: z.number(),
|
|
519
|
+
token: z.string().nullable().describe("A guest's key to this order, shown once"),
|
|
520
|
+
}),
|
|
521
|
+
risk: "write",
|
|
522
|
+
audiences: ["customer", "public"],
|
|
523
|
+
async handler(input, ctx) {
|
|
524
|
+
if (input.courseId && (await held(ctx, [input.courseId])).has(input.courseId)) {
|
|
525
|
+
throw notFound(COURSE, input.courseId);
|
|
526
|
+
}
|
|
527
|
+
if (input.bundleId && isCustomer(ctx)) {
|
|
528
|
+
const mine = await ctx.ops.call("my.list", {
|
|
529
|
+
object: ENROLMENT,
|
|
530
|
+
filter: { field: "bundle", op: "eq", value: input.bundleId },
|
|
531
|
+
limit: 5,
|
|
532
|
+
});
|
|
533
|
+
if (mine.items.some((e) => e.values.status === "active"))
|
|
534
|
+
throw notFound(BUNDLE, input.bundleId);
|
|
535
|
+
}
|
|
536
|
+
const order = await ctx.ops.call("order.start", {
|
|
537
|
+
lines: [
|
|
538
|
+
{
|
|
539
|
+
object: input.courseId ? COURSE : BUNDLE,
|
|
540
|
+
id: input.courseId ?? input.bundleId,
|
|
541
|
+
quantity: 1,
|
|
542
|
+
},
|
|
543
|
+
],
|
|
544
|
+
pay: "full",
|
|
545
|
+
...(input.email === undefined ? {} : { email: input.email }),
|
|
546
|
+
});
|
|
547
|
+
return {
|
|
548
|
+
id: order.id,
|
|
549
|
+
state: order.state,
|
|
550
|
+
currency: order.currency,
|
|
551
|
+
total: order.total,
|
|
552
|
+
token: order.token ?? null,
|
|
553
|
+
};
|
|
554
|
+
},
|
|
555
|
+
});
|
|
556
|
+
// --- Staff ---------------------------------------------------------------------------------------
|
|
557
|
+
const enrolmentOut = z.object({
|
|
558
|
+
enrolmentId: z.string(),
|
|
559
|
+
status: z.string(),
|
|
560
|
+
source: z.string(),
|
|
561
|
+
});
|
|
562
|
+
export const enrol = defineTool({
|
|
563
|
+
description: "Give a person access to a course or a bundle, recorded as given by staff with the reason, " +
|
|
564
|
+
"until a date or for as long as it is offered. It is an enrolment like a bought one: the " +
|
|
565
|
+
"person signs in with their email and finds the course. Returns the enrolment.",
|
|
566
|
+
input: z
|
|
567
|
+
.object({
|
|
568
|
+
personId: id,
|
|
569
|
+
courseId: id.optional(),
|
|
570
|
+
bundleId: id.optional(),
|
|
571
|
+
until: z.string().datetime({ offset: true }).optional(),
|
|
572
|
+
reason: z.string().trim().min(3).max(500),
|
|
573
|
+
})
|
|
574
|
+
.refine((v) => Boolean(v.courseId) !== Boolean(v.bundleId), {
|
|
575
|
+
message: "Name a course or a bundle, not both",
|
|
576
|
+
}),
|
|
577
|
+
output: enrolmentOut,
|
|
578
|
+
risk: "write",
|
|
579
|
+
permission: "courses:enrol",
|
|
580
|
+
async handler(input, ctx) {
|
|
581
|
+
const created = await ctx.ops.call("record.create", {
|
|
582
|
+
object: ENROLMENT,
|
|
583
|
+
values: {
|
|
584
|
+
code: `staff-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`,
|
|
585
|
+
holder: input.personId,
|
|
586
|
+
...(input.courseId ? { course: input.courseId } : { bundle: input.bundleId }),
|
|
587
|
+
source: "staff",
|
|
588
|
+
reason: `${input.reason}${ctx.caller?.kind === "staff" ? ` (${ctx.caller.name})` : ""}`.slice(0, 1000),
|
|
589
|
+
from: new Date().toISOString(),
|
|
590
|
+
...(input.until ? { until: input.until } : {}),
|
|
591
|
+
},
|
|
592
|
+
});
|
|
593
|
+
return { enrolmentId: created.id, status: "active", source: "staff" };
|
|
594
|
+
},
|
|
595
|
+
});
|
|
596
|
+
export const endEnrolment = defineTool({
|
|
597
|
+
description: "End an enrolment staff gave or one that was imported, with the reason: the person no " +
|
|
598
|
+
"longer holds access through it. A bought enrolment ends by a refund of its order, as " +
|
|
599
|
+
"every grant does, and is refused here.",
|
|
600
|
+
input: z.object({ enrolmentId: id, reason: z.string().trim().min(3).max(500) }),
|
|
601
|
+
output: enrolmentOut,
|
|
602
|
+
risk: "write",
|
|
603
|
+
permission: "courses:enrol",
|
|
604
|
+
async handler(input, ctx) {
|
|
605
|
+
const row = await ctx.ops.call("record.get", {
|
|
606
|
+
object: ENROLMENT,
|
|
607
|
+
id: input.enrolmentId,
|
|
608
|
+
});
|
|
609
|
+
if (row.values.source === "purchase" || !row.values.source) {
|
|
610
|
+
throw new OpsCallError("invalid_input", "A bought enrolment ends when its order is refunded in full, not here", { reason: "bought" });
|
|
611
|
+
}
|
|
612
|
+
await ctx.ops.call("record.update", {
|
|
613
|
+
object: ENROLMENT,
|
|
614
|
+
id: input.enrolmentId,
|
|
615
|
+
values: { status: "ended", reason: input.reason },
|
|
616
|
+
from: "active",
|
|
617
|
+
});
|
|
618
|
+
return { enrolmentId: input.enrolmentId, status: "ended", source: String(row.values.source) };
|
|
619
|
+
},
|
|
620
|
+
});
|
|
621
|
+
export const stats = defineTool({
|
|
622
|
+
description: "For staff: enrolments by course (active, ended, void), how many students completed it " +
|
|
623
|
+
"(every lesson that counts done), and lessons marked done by week. All courses, or one. " +
|
|
624
|
+
"Read-only.",
|
|
625
|
+
input: z.object({ courseId: id.optional() }),
|
|
626
|
+
output: z.object({
|
|
627
|
+
courses: z.array(z.object({
|
|
628
|
+
courseId: z.string(),
|
|
629
|
+
title: z.string().nullable(),
|
|
630
|
+
active: z.number(),
|
|
631
|
+
ended: z.number(),
|
|
632
|
+
void: z.number(),
|
|
633
|
+
lessonsDone: z.number(),
|
|
634
|
+
})),
|
|
635
|
+
byWeek: z.array(z.object({ week: z.string(), done: z.number() })),
|
|
636
|
+
}),
|
|
637
|
+
risk: "read",
|
|
638
|
+
async handler(input, ctx) {
|
|
639
|
+
const only = input.courseId ? "WHERE c.id = :course" : "";
|
|
640
|
+
const params = input.courseId ? { course: input.courseId } : {};
|
|
641
|
+
const rows = await ctx.ops.call("query.sql", {
|
|
642
|
+
sql: `SELECT c.id AS courseId, c.title AS title,
|
|
643
|
+
(SELECT count(*) FROM rec_x_courses_enrolment e WHERE e.course = c.id AND e.status = 'active') AS active,
|
|
644
|
+
(SELECT count(*) FROM rec_x_courses_enrolment e WHERE e.course = c.id AND e.status = 'ended') AS ended,
|
|
645
|
+
(SELECT count(*) FROM rec_x_courses_enrolment e WHERE e.course = c.id AND e.status = 'void') AS void,
|
|
646
|
+
(SELECT count(*) FROM rec_x_courses_progress p WHERE p.course = c.id AND p.state = 'done') AS lessonsDone
|
|
647
|
+
FROM rec_x_courses_course c ${only} ORDER BY c.title`,
|
|
648
|
+
params,
|
|
649
|
+
});
|
|
650
|
+
const weeks = await ctx.ops.call("query.sql", {
|
|
651
|
+
sql: `SELECT date(p.done_at, '-' || ((strftime('%w', p.done_at) + 6) % 7) || ' days') AS week,
|
|
652
|
+
count(*) AS done
|
|
653
|
+
FROM rec_x_courses_progress p WHERE p.state = 'done' AND p.done_at IS NOT NULL
|
|
654
|
+
${input.courseId ? "AND p.course = :course" : ""}
|
|
655
|
+
GROUP BY week ORDER BY week`,
|
|
656
|
+
params,
|
|
657
|
+
});
|
|
658
|
+
return {
|
|
659
|
+
courses: rows.rows.map((r) => ({
|
|
660
|
+
courseId: String(r.courseId),
|
|
661
|
+
title: text(r.title),
|
|
662
|
+
active: Number(r.active ?? 0),
|
|
663
|
+
ended: Number(r.ended ?? 0),
|
|
664
|
+
void: Number(r.void ?? 0),
|
|
665
|
+
lessonsDone: Number(r.lessonsDone ?? 0),
|
|
666
|
+
})),
|
|
667
|
+
byWeek: weeks.rows.map((r) => ({ week: String(r.week), done: Number(r.done ?? 0) })),
|
|
668
|
+
};
|
|
669
|
+
},
|
|
670
|
+
});
|
|
671
|
+
// --- The assistant's draft -----------------------------------------------------------------------
|
|
672
|
+
/** Words of a brief, for matching it to video names. */
|
|
673
|
+
const wordsOf = (s) => new Set(s
|
|
674
|
+
.toLowerCase()
|
|
675
|
+
.split(/[^a-z0-9]+/)
|
|
676
|
+
.filter((w) => w.length > 2));
|
|
677
|
+
export const draft = defineTool({
|
|
678
|
+
description: 'Draft a course from a brief ("a beginner handstand course in three parts") as a working ' +
|
|
679
|
+
"copy for staff to edit: sections of lessons built only from videos already in the media " +
|
|
680
|
+
"library (ready ones) and programs already published, with each lesson's title. It reads the " +
|
|
681
|
+
"brief with the extract model role when the deployment has AI. It never publishes: a person " +
|
|
682
|
+
"publishes it, and an assistant's request to publish waits for one (ADR 0480). Returns the " +
|
|
683
|
+
"new course.",
|
|
684
|
+
input: z.object({
|
|
685
|
+
brief: z.string().trim().min(3).max(2000),
|
|
686
|
+
title: z.string().trim().min(1).max(200).optional(),
|
|
687
|
+
}),
|
|
688
|
+
output: z.object({
|
|
689
|
+
courseId: z.string(),
|
|
690
|
+
title: z.string(),
|
|
691
|
+
sections: z.number(),
|
|
692
|
+
lessons: z.number(),
|
|
693
|
+
videos: z.array(z.string()),
|
|
694
|
+
programs: z.array(z.string()),
|
|
695
|
+
published: z.literal(false),
|
|
696
|
+
}),
|
|
697
|
+
risk: "write",
|
|
698
|
+
async handler(input, ctx) {
|
|
699
|
+
const library = await ctx.ops.call("media.list", {
|
|
700
|
+
filter: {
|
|
701
|
+
and: [
|
|
702
|
+
{ field: "kind", op: "eq", value: "video" },
|
|
703
|
+
{ field: "state", op: "eq", value: "ready" },
|
|
704
|
+
],
|
|
705
|
+
},
|
|
706
|
+
limit: 200,
|
|
707
|
+
});
|
|
708
|
+
const programs = await ctx.ops.call("record.list", {
|
|
709
|
+
object: PROGRAM,
|
|
710
|
+
filter: { field: "publishState", op: "eq", value: "published" },
|
|
711
|
+
limit: 50,
|
|
712
|
+
});
|
|
713
|
+
let sectionsWanted = 3;
|
|
714
|
+
try {
|
|
715
|
+
const read = await ctx.ai.extract(input.brief, {
|
|
716
|
+
sections: "How many parts or sections the course has, a whole number from 1 to 12",
|
|
717
|
+
});
|
|
718
|
+
const n = Number(read.sections);
|
|
719
|
+
if (Number.isInteger(n) && n >= 1 && n <= 12)
|
|
720
|
+
sectionsWanted = n;
|
|
721
|
+
}
|
|
722
|
+
catch {
|
|
723
|
+
const m = /\b(\d{1,2})\s+(?:parts|sections|modules)\b/i.exec(input.brief);
|
|
724
|
+
if (m)
|
|
725
|
+
sectionsWanted = Math.min(12, Math.max(1, Number(m[1])));
|
|
726
|
+
}
|
|
727
|
+
const brief = wordsOf(input.brief);
|
|
728
|
+
const score = (r) => [...wordsOf(String(r.values.name ?? ""))].filter((w) => brief.has(w)).length;
|
|
729
|
+
const videos = [...library.items]
|
|
730
|
+
.sort((a, b) => score(b) - score(a))
|
|
731
|
+
.slice(0, sectionsWanted * 3);
|
|
732
|
+
const lessons = videos.map((v, i) => ({
|
|
733
|
+
kind: "video",
|
|
734
|
+
key: `lesson-${i + 1}`,
|
|
735
|
+
title: String(v.values.name ?? `Lesson ${i + 1}`)
|
|
736
|
+
.replace(/\.[a-z0-9]{2,4}$/i, "")
|
|
737
|
+
.slice(0, 200),
|
|
738
|
+
video: { media: v.id },
|
|
739
|
+
...(num(v.values.duration) ? { duration: Math.ceil(Number(v.values.duration) / 60) } : {}),
|
|
740
|
+
}));
|
|
741
|
+
const program = programs.items[0];
|
|
742
|
+
if (program) {
|
|
743
|
+
lessons.push({
|
|
744
|
+
kind: "session",
|
|
745
|
+
key: "session-1",
|
|
746
|
+
title: `Train: ${String(program.values.title ?? "session")}`.slice(0, 200),
|
|
747
|
+
program: program.id,
|
|
748
|
+
week: 1,
|
|
749
|
+
session: 0,
|
|
750
|
+
});
|
|
751
|
+
}
|
|
752
|
+
const per = Math.max(1, Math.ceil(lessons.length / sectionsWanted));
|
|
753
|
+
const sections = [];
|
|
754
|
+
for (let s = 0; s * per < lessons.length || (s === 0 && !lessons.length); s += 1) {
|
|
755
|
+
sections.push({
|
|
756
|
+
key: `part-${s + 1}`,
|
|
757
|
+
title: `Part ${s + 1}`,
|
|
758
|
+
lessons: lessons.slice(s * per, (s + 1) * per),
|
|
759
|
+
});
|
|
760
|
+
if (!lessons.length)
|
|
761
|
+
break;
|
|
762
|
+
}
|
|
763
|
+
const title = input.title ?? `Draft: ${input.brief.slice(0, 60)}`;
|
|
764
|
+
const created = await ctx.ops.call("record.create", {
|
|
765
|
+
object: COURSE,
|
|
766
|
+
values: { title, price: 0, summary: input.brief.slice(0, 1000), outline: { sections } },
|
|
767
|
+
});
|
|
768
|
+
return {
|
|
769
|
+
courseId: created.id,
|
|
770
|
+
title,
|
|
771
|
+
sections: sections.length,
|
|
772
|
+
lessons: lessons.length,
|
|
773
|
+
videos: videos.map((v) => v.id),
|
|
774
|
+
programs: program ? [program.id] : [],
|
|
775
|
+
published: false,
|
|
776
|
+
};
|
|
777
|
+
},
|
|
778
|
+
});
|
|
779
|
+
// --- Session lessons -----------------------------------------------------------------------------
|
|
780
|
+
export const startSession = defineTool({
|
|
781
|
+
description: "For a session lesson: the signed-in student's own Programs session for it, once the " +
|
|
782
|
+
"program is assigned to them (it is, from the day they first open the course: the dates " +
|
|
783
|
+
"are a suggested pace). They then log sets with Programs' own tools. `preparing` while the " +
|
|
784
|
+
"assignment is being made. Not found without access to the course.",
|
|
785
|
+
input: z.object({ courseId: id, lessonKey: z.string().min(1).max(40) }),
|
|
786
|
+
output: z.object({
|
|
787
|
+
preparing: z.boolean(),
|
|
788
|
+
sessionId: z.string().nullable(),
|
|
789
|
+
date: z.string().nullable(),
|
|
790
|
+
programId: z.string().nullable(),
|
|
791
|
+
}),
|
|
792
|
+
risk: "read",
|
|
793
|
+
audiences: ["customer"],
|
|
794
|
+
async handler(input, ctx) {
|
|
795
|
+
const record = await ctx.ops.call("my.get", {
|
|
796
|
+
object: COURSE,
|
|
797
|
+
id: input.courseId,
|
|
798
|
+
});
|
|
799
|
+
const found = findLesson(asOutline(record.values.outline), input.lessonKey);
|
|
800
|
+
if (!found || found.kind !== "session")
|
|
801
|
+
throw notFound("lesson", input.lessonKey);
|
|
802
|
+
// Without access the seam gave no program (a holders' part), so there is no session.
|
|
803
|
+
if (!found.program || found.week === undefined || found.session === undefined) {
|
|
804
|
+
throw notFound("lesson", input.lessonKey);
|
|
805
|
+
}
|
|
806
|
+
const sessions = await ctx.ops.call("my.list", {
|
|
807
|
+
object: SESSION,
|
|
808
|
+
filter: {
|
|
809
|
+
and: [
|
|
810
|
+
{ field: "program", op: "eq", value: found.program },
|
|
811
|
+
{ field: "week", op: "eq", value: found.week },
|
|
812
|
+
{ field: "position", op: "eq", value: found.session },
|
|
813
|
+
],
|
|
814
|
+
},
|
|
815
|
+
sort: { field: "date", direction: "desc" },
|
|
816
|
+
limit: 1,
|
|
817
|
+
});
|
|
818
|
+
const session = sessions.items[0];
|
|
819
|
+
return {
|
|
820
|
+
preparing: !session,
|
|
821
|
+
sessionId: session?.id ?? null,
|
|
822
|
+
date: session ? text(session.values.date) : null,
|
|
823
|
+
programId: found.program,
|
|
824
|
+
};
|
|
825
|
+
},
|
|
826
|
+
});
|