@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
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
// The Courses pack's headless entry (stage 42, step 7; ADRs 0418 and 0453): functions and hooks
|
|
2
|
+
// over the typed client, no markup. A client app imports it as `@composable-os/pack-courses/client`
|
|
3
|
+
// and hands it its generated client; the courses blocks render what it returns.
|
|
4
|
+
//
|
|
5
|
+
// Nothing here decides who may watch, what is for sale or what a course costs. A lesson's material
|
|
6
|
+
// arrives only for a holder (the host removes it for everyone else), and a video plays only with a
|
|
7
|
+
// token the host signs in `media.play`, which `usePlayback` asks for again before it expires.
|
|
8
|
+
import { packHooks } from "@composable-os/client";
|
|
9
|
+
import { LESSON_KINDS, } from "../outline.js";
|
|
10
|
+
export { completion, findLesson, lessonsOf, nextLesson, watchedToEnd } from "../outline.js";
|
|
11
|
+
export const COURSE = "courses.course";
|
|
12
|
+
export const PLAN = "courses.plan";
|
|
13
|
+
/** What a member is told about a trial the host did not give. Display only. */
|
|
14
|
+
export function noTrialWords(reason) {
|
|
15
|
+
switch (reason) {
|
|
16
|
+
case "sign_in":
|
|
17
|
+
return "The free trial is for signed-in members. Sign in for it, or continue and pay from today.";
|
|
18
|
+
case "used":
|
|
19
|
+
return "You have had your free trial on this plan, so this one is paid from today.";
|
|
20
|
+
case "held":
|
|
21
|
+
return "A checkout with your free trial is already open. Finish that one, or continue and pay from today.";
|
|
22
|
+
case "unconfirmed":
|
|
23
|
+
return "We could not check your earlier checkout with the payment provider just now, so no free trial can be given yet. Try again in a few minutes, or continue and pay from today.";
|
|
24
|
+
case "permission":
|
|
25
|
+
return "A free trial cannot be given from here; this one is paid from today.";
|
|
26
|
+
case "continuation":
|
|
27
|
+
return "You have free time with us on your continuing subscription, and this plan is not the one it is for: this one is paid from today, with no free trial. Go back to continue free until your date.";
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/** The member's offer for `planId`, if they have one. */
|
|
31
|
+
export function offerFor(offers, planId) {
|
|
32
|
+
return offers?.find((o) => o.planObject === PLAN && o.planId === planId);
|
|
33
|
+
}
|
|
34
|
+
/** What a member is told about their offer, in words. Display only. */
|
|
35
|
+
export function offerWords(offer) {
|
|
36
|
+
if (offer.askUs) {
|
|
37
|
+
return `Your subscription with us is paid up to ${day(offer.freeUntil)}. The plan it continues on is not on sale just now: ask us, and nothing is charged before then.`;
|
|
38
|
+
}
|
|
39
|
+
return offer.chargedNow
|
|
40
|
+
? `Your subscription with us was paid up to ${day(offer.freeUntil)}, which is too soon for free time here: continuing is charged from today.`
|
|
41
|
+
: `Your subscription with us is paid up to ${day(offer.freeUntil)}. Continue it here: nothing is charged until then.`;
|
|
42
|
+
}
|
|
43
|
+
// --- Plain functions ------------------------------------------------------------------------------
|
|
44
|
+
/** A token is asked for again this long before it expires. */
|
|
45
|
+
export const PLAYBACK_REFRESH_BEFORE_MS = 5 * 60_000;
|
|
46
|
+
/**
|
|
47
|
+
* How long to wait before asking for a new token: until `PLAYBACK_REFRESH_BEFORE_MS` before it
|
|
48
|
+
* expires, never less than 10 seconds (a token about to expire is asked for again at once after).
|
|
49
|
+
*/
|
|
50
|
+
export function refreshInMs(expiresAt, now = Date.now()) {
|
|
51
|
+
const at = Date.parse(expiresAt);
|
|
52
|
+
if (Number.isNaN(at))
|
|
53
|
+
return 10_000;
|
|
54
|
+
return Math.max(10_000, at - PLAYBACK_REFRESH_BEFORE_MS - now);
|
|
55
|
+
}
|
|
56
|
+
/** A price as the host gave it, in its currency (`99.00 AUD`). Display only: never worked out. */
|
|
57
|
+
export function formatPrice(price, currency) {
|
|
58
|
+
if (price === null || price === undefined)
|
|
59
|
+
return "";
|
|
60
|
+
try {
|
|
61
|
+
return currency
|
|
62
|
+
? new Intl.NumberFormat(undefined, { style: "currency", currency }).format(price)
|
|
63
|
+
: String(price);
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return `${price}${currency ? ` ${currency}` : ""}`;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* An amount the host gave in the currency's minor unit (a subscription's price per period), in
|
|
71
|
+
* that currency: `29.00 AUD`. Display only: the minor unit's digits are the currency's own.
|
|
72
|
+
*/
|
|
73
|
+
export function formatMinor(minor, currency) {
|
|
74
|
+
try {
|
|
75
|
+
const format = new Intl.NumberFormat(undefined, { style: "currency", currency });
|
|
76
|
+
const digits = format.resolvedOptions().maximumFractionDigits ?? 2;
|
|
77
|
+
return format.format(minor / 10 ** digits);
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
return `${minor} ${currency}`;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** A date as a reader sees it (`3 Dec 2026`), from the host's ISO timestamp. Display only. */
|
|
84
|
+
export function day(iso) {
|
|
85
|
+
if (!iso)
|
|
86
|
+
return "";
|
|
87
|
+
const at = new Date(iso);
|
|
88
|
+
return Number.isNaN(at.getTime())
|
|
89
|
+
? ""
|
|
90
|
+
: at.toLocaleDateString(undefined, { day: "numeric", month: "short", year: "numeric" });
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* What a subscription's state means to its member, in words, from what the host reports. Display
|
|
94
|
+
* only: whether it gives access is the host's (the seam), never worked out from this.
|
|
95
|
+
*/
|
|
96
|
+
export function subscriptionStatus(sub) {
|
|
97
|
+
const ends = sub.cancelAtPeriodEnd ? sub.currentPeriodEnd : sub.cancelAt;
|
|
98
|
+
switch (sub.state) {
|
|
99
|
+
case "trialing":
|
|
100
|
+
// A trial, or a continuation's free time until its paid-up date: both are free until then.
|
|
101
|
+
return ends
|
|
102
|
+
? `Free, ending on ${day(ends)}: nothing will be charged`
|
|
103
|
+
: `Free until ${day(sub.trialEnd ?? sub.currentPeriodEnd)}, then charged each ${sub.interval}`;
|
|
104
|
+
case "active":
|
|
105
|
+
return ends
|
|
106
|
+
? `Active until ${day(ends)}, then it ends`
|
|
107
|
+
: `Active: next charge on ${day(sub.currentPeriodEnd)}`;
|
|
108
|
+
case "past_due":
|
|
109
|
+
return sub.actionUrl
|
|
110
|
+
? "A payment needs you to confirm it"
|
|
111
|
+
: "A payment failed: the payment provider will try again";
|
|
112
|
+
case "unpaid":
|
|
113
|
+
return "Unpaid: update your card to keep watching";
|
|
114
|
+
case "paused":
|
|
115
|
+
return "Paused";
|
|
116
|
+
case "incomplete":
|
|
117
|
+
return "Waiting for its first payment";
|
|
118
|
+
case "expired":
|
|
119
|
+
return "Never started: its first payment did not go through";
|
|
120
|
+
case "ended":
|
|
121
|
+
return `Ended${sub.endedAt ? ` on ${day(sub.endedAt)}` : ""}`;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/** Whether a subscription is still the member's to cancel, resume or pay (not ended). */
|
|
125
|
+
export const isLive = (sub) => sub.state !== "ended" && sub.state !== "expired";
|
|
126
|
+
/** A lesson's length as a reader sees it: `12 min`. Display only. */
|
|
127
|
+
export function minutes(duration) {
|
|
128
|
+
return typeof duration === "number" && duration > 0 ? `${duration} min` : "";
|
|
129
|
+
}
|
|
130
|
+
/** Where a phone asks the host for a picture (the course cover): as the Content pack's. */
|
|
131
|
+
export function hostMediaUrl(hostUrl) {
|
|
132
|
+
const host = hostUrl.replace(/\/+$/, "");
|
|
133
|
+
return (id, width) => `${host}/api/v1/media/${encodeURIComponent(id)}${width ? `?w=${Math.round(width)}` : ""}`;
|
|
134
|
+
}
|
|
135
|
+
/** A picture on the site's own media route (`/api/media/<id>?w=`), for the web blocks. */
|
|
136
|
+
export function mediaPath(id, width, base = "") {
|
|
137
|
+
return `${base.replace(/\/+$/, "")}/api/media/${encodeURIComponent(id)}${width ? `?w=${Math.round(width)}` : ""}`;
|
|
138
|
+
}
|
|
139
|
+
// --- The course builder's pure outline edits (as Programs' layoutEdits) -------------------------
|
|
140
|
+
/** A key from a title, unused in `taken`: `wrist-prep`, `wrist-prep-2`. */
|
|
141
|
+
export function newKey(title, taken) {
|
|
142
|
+
const base = title
|
|
143
|
+
.toLowerCase()
|
|
144
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
145
|
+
.replace(/^-+|-+$/g, "")
|
|
146
|
+
.slice(0, 32) || "lesson";
|
|
147
|
+
let key = base;
|
|
148
|
+
for (let n = 2; taken.has(key); n += 1)
|
|
149
|
+
key = `${base}-${n}`;
|
|
150
|
+
return key;
|
|
151
|
+
}
|
|
152
|
+
const keysOf = (outline) => new Set(outline.sections.flatMap((s) => [s.key, ...(s.lessons ?? []).map((l) => l.key)]));
|
|
153
|
+
/**
|
|
154
|
+
* Pure edits of an outline: each returns a new outline and never changes a lesson's key once it
|
|
155
|
+
* has one (progress is kept by it). The builder block saves the result with `record.update`.
|
|
156
|
+
*/
|
|
157
|
+
export const outlineEdits = {
|
|
158
|
+
addSection(outline, title) {
|
|
159
|
+
return {
|
|
160
|
+
sections: [...outline.sections, { key: newKey(title, keysOf(outline)), title, lessons: [] }],
|
|
161
|
+
};
|
|
162
|
+
},
|
|
163
|
+
renameSection(outline, key, title) {
|
|
164
|
+
return {
|
|
165
|
+
sections: outline.sections.map((s) => (s.key === key ? { ...s, title } : s)),
|
|
166
|
+
};
|
|
167
|
+
},
|
|
168
|
+
removeSection(outline, key) {
|
|
169
|
+
return { sections: outline.sections.filter((s) => s.key !== key) };
|
|
170
|
+
},
|
|
171
|
+
addLesson(outline, section, kind, title) {
|
|
172
|
+
if (!LESSON_KINDS.includes(kind))
|
|
173
|
+
return outline;
|
|
174
|
+
const key = newKey(title, keysOf(outline));
|
|
175
|
+
const lesson = { kind, key, title };
|
|
176
|
+
return {
|
|
177
|
+
sections: outline.sections.map((s) => s.key === section ? { ...s, lessons: [...(s.lessons ?? []), lesson] } : s),
|
|
178
|
+
};
|
|
179
|
+
},
|
|
180
|
+
/** Changes a lesson's fields; its key and kind stay. */
|
|
181
|
+
updateLesson(outline, key, patch) {
|
|
182
|
+
const { key: _key, kind: _kind, ...rest } = patch;
|
|
183
|
+
return {
|
|
184
|
+
sections: outline.sections.map((s) => ({
|
|
185
|
+
...s,
|
|
186
|
+
lessons: (s.lessons ?? []).map((l) => (l.key === key ? { ...l, ...rest } : l)),
|
|
187
|
+
})),
|
|
188
|
+
};
|
|
189
|
+
},
|
|
190
|
+
removeLesson(outline, key) {
|
|
191
|
+
return {
|
|
192
|
+
sections: outline.sections.map((s) => ({
|
|
193
|
+
...s,
|
|
194
|
+
lessons: (s.lessons ?? []).filter((l) => l.key !== key),
|
|
195
|
+
})),
|
|
196
|
+
};
|
|
197
|
+
},
|
|
198
|
+
/** Moves a lesson to `index` of `section` (by drag or by keyboard). */
|
|
199
|
+
moveLesson(outline, key, section, index) {
|
|
200
|
+
const moving = outline.sections.flatMap((s) => s.lessons ?? []).find((l) => l.key === key);
|
|
201
|
+
if (!moving || !outline.sections.some((s) => s.key === section))
|
|
202
|
+
return outline;
|
|
203
|
+
const without = outlineEdits.removeLesson(outline, key);
|
|
204
|
+
return {
|
|
205
|
+
sections: without.sections.map((s) => {
|
|
206
|
+
if (s.key !== section)
|
|
207
|
+
return s;
|
|
208
|
+
const lessons = [...(s.lessons ?? [])];
|
|
209
|
+
lessons.splice(Math.max(0, Math.min(index, lessons.length)), 0, moving);
|
|
210
|
+
return { ...s, lessons };
|
|
211
|
+
}),
|
|
212
|
+
};
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
/**
|
|
216
|
+
* The operations each hook calls (ADR 0453). The public's client gets the catalogue, the course
|
|
217
|
+
* page and buying; a customer's client also their lessons, playback, progress and courses; the
|
|
218
|
+
* staff client the course builder's and the enrolment hooks too.
|
|
219
|
+
*/
|
|
220
|
+
export const COURSES_HOOKS_CALL = {
|
|
221
|
+
useCatalogue: ["x.courses.catalogue"],
|
|
222
|
+
useCourse: ["x.courses.course"],
|
|
223
|
+
useBuy: ["x.courses.buy", "order.checkout"],
|
|
224
|
+
useLesson: ["x.courses.lesson"],
|
|
225
|
+
usePlayback: ["media.play"],
|
|
226
|
+
useProgress: ["x.courses.progress"],
|
|
227
|
+
useMyCourses: ["x.courses.myCourses"],
|
|
228
|
+
useStartSession: ["x.courses.startSession"],
|
|
229
|
+
useEnrol: ["x.courses.enrol"],
|
|
230
|
+
useEndEnrolment: ["x.courses.endEnrolment"],
|
|
231
|
+
useStats: ["x.courses.stats"],
|
|
232
|
+
// The course subscription (stage 32a). Every billing action is the web's: a native block may
|
|
233
|
+
// read the subscription only (the block lint's noPurchase).
|
|
234
|
+
usePlans: ["x.courses.plans"],
|
|
235
|
+
useSubscribe: ["subscription.start"],
|
|
236
|
+
useMySubscriptions: ["subscription.list"],
|
|
237
|
+
useMyOffers: ["subscription.offer.mine"],
|
|
238
|
+
useCancelSubscription: ["subscription.cancel"],
|
|
239
|
+
useResumeSubscription: ["subscription.resume"],
|
|
240
|
+
useBillingPortal: ["subscription.portal"],
|
|
241
|
+
useWorkingCopy: ["record.get"],
|
|
242
|
+
useSaveOutline: ["record.update"],
|
|
243
|
+
usePublishCourse: ["record.publish"],
|
|
244
|
+
useLibraryVideos: ["media.list"],
|
|
245
|
+
usePublishedPrograms: ["record.list"],
|
|
246
|
+
};
|
|
247
|
+
/**
|
|
248
|
+
* Hooks for a React or React Native app:
|
|
249
|
+
* `const courses = createCoursesHooks({ useQuery, useMutation }, api)`. The hooks a client cannot
|
|
250
|
+
* back are not there, and calling one does not compile.
|
|
251
|
+
*/
|
|
252
|
+
export function createCoursesHooks(react, client) {
|
|
253
|
+
return packHooks(client, COURSES_HOOKS_CALL, (api) => allCoursesHooks(react, api));
|
|
254
|
+
}
|
|
255
|
+
const none = (name) => ({
|
|
256
|
+
queryKey: [name, "none"],
|
|
257
|
+
queryFn: async () => undefined,
|
|
258
|
+
});
|
|
259
|
+
function allCoursesHooks(react, api) {
|
|
260
|
+
const { useQuery, useMutation } = react;
|
|
261
|
+
return {
|
|
262
|
+
/** The catalogue: published courses, bundles and themes, with the host's prices. */
|
|
263
|
+
useCatalogue(input = {}) {
|
|
264
|
+
return useQuery(api.query("x.courses.catalogue", input));
|
|
265
|
+
},
|
|
266
|
+
/** One published course by its address, with whether the caller holds it. */
|
|
267
|
+
useCourse(slug) {
|
|
268
|
+
const options = slug
|
|
269
|
+
? api.query("x.courses.course", { slug })
|
|
270
|
+
: none("course");
|
|
271
|
+
return useQuery({ ...options, enabled: slug !== null });
|
|
272
|
+
},
|
|
273
|
+
/** Start an order for a course or a bundle; then `checkout` it (web only). */
|
|
274
|
+
useBuy() {
|
|
275
|
+
return {
|
|
276
|
+
start: useMutation(api.mutation("x.courses.buy")),
|
|
277
|
+
checkout: useMutation(api.mutation("order.checkout")),
|
|
278
|
+
};
|
|
279
|
+
},
|
|
280
|
+
/** One lesson for a holder (or a free preview). */
|
|
281
|
+
useLesson(courseId, lessonKey) {
|
|
282
|
+
const options = courseId && lessonKey
|
|
283
|
+
? api.query("x.courses.lesson", { courseId, lessonKey })
|
|
284
|
+
: none("lesson");
|
|
285
|
+
return useQuery({ ...options, enabled: Boolean(courseId && lessonKey) });
|
|
286
|
+
},
|
|
287
|
+
/**
|
|
288
|
+
* A token to play a lesson's video (or a trailer), from the host's `media.play`; asked for
|
|
289
|
+
* again `PLAYBACK_REFRESH_BEFORE_MS` before it expires, so a long video keeps playing.
|
|
290
|
+
*/
|
|
291
|
+
usePlayback(video) {
|
|
292
|
+
const input = video
|
|
293
|
+
? {
|
|
294
|
+
id: video.media,
|
|
295
|
+
...(video.record ? { record: video.record } : {}),
|
|
296
|
+
...(video.field ? { field: video.field } : {}),
|
|
297
|
+
}
|
|
298
|
+
: null;
|
|
299
|
+
const options = input ? api.query("media.play", input) : none("playback");
|
|
300
|
+
return useQuery({
|
|
301
|
+
...options,
|
|
302
|
+
enabled: input !== null,
|
|
303
|
+
refetchInterval: (query) => {
|
|
304
|
+
const data = query.state.data;
|
|
305
|
+
return data ? refreshInMs(data.expiresAt) : false;
|
|
306
|
+
},
|
|
307
|
+
});
|
|
308
|
+
},
|
|
309
|
+
/** Mark a lesson started or done, and keep where its video was left. */
|
|
310
|
+
useProgress() {
|
|
311
|
+
return useMutation(api.mutation("x.courses.progress"));
|
|
312
|
+
},
|
|
313
|
+
/** What the caller holds, with progress. */
|
|
314
|
+
useMyCourses() {
|
|
315
|
+
return useQuery(api.query("x.courses.myCourses", {}));
|
|
316
|
+
},
|
|
317
|
+
/** A session lesson's own Programs session, once it is assigned. */
|
|
318
|
+
useStartSession(courseId, lessonKey) {
|
|
319
|
+
const options = courseId && lessonKey
|
|
320
|
+
? api.query("x.courses.startSession", { courseId, lessonKey })
|
|
321
|
+
: none("session");
|
|
322
|
+
return useQuery({
|
|
323
|
+
...options,
|
|
324
|
+
enabled: Boolean(courseId && lessonKey),
|
|
325
|
+
// "Preparing your sessions": ask again in a few seconds until it is there.
|
|
326
|
+
refetchInterval: (query) => (query.state.data?.preparing ? 3000 : false),
|
|
327
|
+
});
|
|
328
|
+
},
|
|
329
|
+
/** Staff: give a person access, with a reason. */
|
|
330
|
+
useEnrol() {
|
|
331
|
+
return useMutation(api.mutation("x.courses.enrol"));
|
|
332
|
+
},
|
|
333
|
+
/** Staff: end an enrolment staff gave or one imported. */
|
|
334
|
+
useEndEnrolment() {
|
|
335
|
+
return useMutation(api.mutation("x.courses.endEnrolment"));
|
|
336
|
+
},
|
|
337
|
+
/** Staff: enrolments and lessons done. */
|
|
338
|
+
useStats(courseId) {
|
|
339
|
+
return useQuery(api.query("x.courses.stats", courseId ? { courseId } : {}));
|
|
340
|
+
},
|
|
341
|
+
/** The course plans on offer, with the host's prices and trial days. */
|
|
342
|
+
usePlans() {
|
|
343
|
+
return useQuery(api.query("x.courses.plans", {}));
|
|
344
|
+
},
|
|
345
|
+
/**
|
|
346
|
+
* Start a course subscription: the host opens the provider's page (web only). Whether the
|
|
347
|
+
* trial is given is the host's (`noTrial` says why not).
|
|
348
|
+
*/
|
|
349
|
+
useSubscribe() {
|
|
350
|
+
return useMutation(api.mutation("subscription.start"));
|
|
351
|
+
},
|
|
352
|
+
/** The signed-in customer's own course subscriptions, newest first. */
|
|
353
|
+
useMySubscriptions() {
|
|
354
|
+
return useQuery(api.query("subscription.list", {
|
|
355
|
+
filter: { field: "planObject", op: "eq", value: PLAN },
|
|
356
|
+
limit: 20,
|
|
357
|
+
}));
|
|
358
|
+
},
|
|
359
|
+
/**
|
|
360
|
+
* The signed-in customer's own continuation offers (moving from another platform): free until
|
|
361
|
+
* their paid-up date. Read only; taking one is `useSubscribe` with `offerId` (web only).
|
|
362
|
+
*/
|
|
363
|
+
useMyOffers(options = {}) {
|
|
364
|
+
return useQuery({
|
|
365
|
+
...api.query("subscription.offer.mine", {}),
|
|
366
|
+
enabled: options.enabled ?? true,
|
|
367
|
+
});
|
|
368
|
+
},
|
|
369
|
+
/** End their own subscription when its period ends (web only). */
|
|
370
|
+
useCancelSubscription() {
|
|
371
|
+
return useMutation(api.mutation("subscription.cancel"));
|
|
372
|
+
},
|
|
373
|
+
/** Undo that, before the period ends (web only). */
|
|
374
|
+
useResumeSubscription() {
|
|
375
|
+
return useMutation(api.mutation("subscription.resume"));
|
|
376
|
+
},
|
|
377
|
+
/** The provider's portal for their card and past invoices (web only, never in a frame). */
|
|
378
|
+
useBillingPortal() {
|
|
379
|
+
return useMutation(api.mutation("subscription.portal"));
|
|
380
|
+
},
|
|
381
|
+
/** Staff: a course's working copy, as the builder edits it. */
|
|
382
|
+
useWorkingCopy(courseId) {
|
|
383
|
+
return useQuery(api.query("record.get", { object: COURSE, id: courseId }));
|
|
384
|
+
},
|
|
385
|
+
/** Staff: save the course's working copy (its outline). */
|
|
386
|
+
useSaveOutline() {
|
|
387
|
+
return useMutation(api.mutation("record.update"));
|
|
388
|
+
},
|
|
389
|
+
/** Staff: publish the course (a person; an assistant's request waits for one). */
|
|
390
|
+
usePublishCourse() {
|
|
391
|
+
return useMutation(api.mutation("record.publish"));
|
|
392
|
+
},
|
|
393
|
+
/** Staff: the library's ready videos, to pick a lesson's video. */
|
|
394
|
+
useLibraryVideos() {
|
|
395
|
+
return useQuery(api.query("media.list", {
|
|
396
|
+
filter: {
|
|
397
|
+
and: [
|
|
398
|
+
{ field: "kind", op: "eq", value: "video" },
|
|
399
|
+
{ field: "state", op: "eq", value: "ready" },
|
|
400
|
+
],
|
|
401
|
+
},
|
|
402
|
+
limit: 100,
|
|
403
|
+
}));
|
|
404
|
+
},
|
|
405
|
+
/** Staff: the published Programs programs, to pick a session lesson's program. */
|
|
406
|
+
usePublishedPrograms() {
|
|
407
|
+
return useQuery(api.query("record.list", {
|
|
408
|
+
object: "programs.program",
|
|
409
|
+
filter: { field: "publishState", op: "eq", value: "published" },
|
|
410
|
+
limit: 100,
|
|
411
|
+
}));
|
|
412
|
+
},
|
|
413
|
+
};
|
|
414
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
const PAID_IN = "o.paid_at >= :paid_from AND o.paid_at < :paid_to";
|
|
2
|
+
const IN = (column) => `${column} >= :paid_from AND ${column} < :paid_to`;
|
|
3
|
+
const WEEK = (column) => `date(${column}, '-' || ((strftime('%w', ${column}) + 6) % 7) || ' days')`;
|
|
4
|
+
/** A course's or a bundle's paid lines in the period, with what they sold. */
|
|
5
|
+
const LINES = `FROM rec_order_line l JOIN rec_order o ON o.id = l.order_id
|
|
6
|
+
WHERE l.object_type IN ('courses.course', 'courses.bundle') AND o.state IN ('paid', 'part_paid')
|
|
7
|
+
AND ${PAID_IN}`;
|
|
8
|
+
export const courses = {
|
|
9
|
+
title: "Courses",
|
|
10
|
+
description: "Revenue by month by course and bundle, enrolments by course, completions, lessons done by " +
|
|
11
|
+
"week, and students with nothing done in two weeks.",
|
|
12
|
+
filters: [{ type: "dateRange", id: "paid", label: "Dates", default: "this_quarter" }],
|
|
13
|
+
widgets: [
|
|
14
|
+
{
|
|
15
|
+
id: "revenue",
|
|
16
|
+
type: "kpi",
|
|
17
|
+
title: "Revenue",
|
|
18
|
+
description: "Line totals of course and bundle orders paid in the period (not refunded).",
|
|
19
|
+
sql: `SELECT coalesce(sum(l.line_total), 0) / 100.0 AS value ${LINES}`,
|
|
20
|
+
mapping: { value: "value" },
|
|
21
|
+
options: { compare: true, format: "currency" },
|
|
22
|
+
layout: { w: 3, h: 1 },
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
id: "enrolments",
|
|
26
|
+
type: "kpi",
|
|
27
|
+
title: "New enrolments",
|
|
28
|
+
description: "Enrolments made in the period, bought, given or imported.",
|
|
29
|
+
sql: `SELECT count(*) AS value FROM rec_x_courses_enrolment e WHERE ${IN("e.created_at")}`,
|
|
30
|
+
mapping: { value: "value" },
|
|
31
|
+
options: { compare: true },
|
|
32
|
+
clickThrough: {
|
|
33
|
+
object: "courses.enrolment",
|
|
34
|
+
filter: {
|
|
35
|
+
and: [
|
|
36
|
+
{ field: "createdAt", op: "gte", value: { $param: "paid_from" } },
|
|
37
|
+
{ field: "createdAt", op: "lt", value: { $param: "paid_to" } },
|
|
38
|
+
],
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
layout: { w: 3, h: 1 },
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
id: "lessons-done",
|
|
45
|
+
type: "kpi",
|
|
46
|
+
title: "Lessons done",
|
|
47
|
+
description: "Lessons students marked done in the period.",
|
|
48
|
+
sql: `SELECT count(*) AS value FROM rec_x_courses_progress p
|
|
49
|
+
WHERE p.state = 'done' AND ${IN("p.done_at")}`,
|
|
50
|
+
mapping: { value: "value" },
|
|
51
|
+
options: { compare: true },
|
|
52
|
+
layout: { w: 3, h: 1 },
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
id: "completed",
|
|
56
|
+
type: "kpi",
|
|
57
|
+
title: "Completed (imported)",
|
|
58
|
+
description: "Enrolments brought from the old platform marked completed.",
|
|
59
|
+
sql: "SELECT count(*) AS value FROM rec_x_courses_enrolment e WHERE e.completed = 1",
|
|
60
|
+
mapping: { value: "value" },
|
|
61
|
+
options: { compare: false },
|
|
62
|
+
layout: { w: 3, h: 1 },
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
id: "revenue-by-month",
|
|
66
|
+
type: "bar",
|
|
67
|
+
title: "Revenue by month",
|
|
68
|
+
description: "Paid line totals per month, by course or bundle.",
|
|
69
|
+
sql: `SELECT substr(o.paid_at, 1, 7) AS month, l.title AS item,
|
|
70
|
+
sum(l.line_total) / 100.0 AS revenue ${LINES}
|
|
71
|
+
GROUP BY month, l.title ORDER BY month, item`,
|
|
72
|
+
mapping: { x: "month", y: ["revenue"], series: "item" },
|
|
73
|
+
layout: { w: 6, h: 2 },
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
id: "enrolments-by-course",
|
|
77
|
+
type: "leaderboard",
|
|
78
|
+
title: "Enrolments by course",
|
|
79
|
+
description: "Active enrolments per course (a bundle's are counted on the bundle).",
|
|
80
|
+
sql: `SELECT c.id AS course_id, c.title AS label, count(e.id) AS value
|
|
81
|
+
FROM rec_x_courses_course c JOIN rec_x_courses_enrolment e ON e.course = c.id
|
|
82
|
+
WHERE e.status = 'active' GROUP BY c.id, c.title ORDER BY value DESC, label LIMIT 10`,
|
|
83
|
+
mapping: { label: "label", value: "value" },
|
|
84
|
+
clickThrough: {
|
|
85
|
+
object: "courses.enrolment",
|
|
86
|
+
filter: { field: "course", op: "eq", value: { $row: "course_id" } },
|
|
87
|
+
},
|
|
88
|
+
layout: { w: 6, h: 2 },
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
id: "done-by-week",
|
|
92
|
+
type: "line",
|
|
93
|
+
title: "Lessons done by week",
|
|
94
|
+
description: "Lessons marked done per week of the period.",
|
|
95
|
+
sql: `SELECT ${WEEK("p.done_at")} AS week, count(*) AS done
|
|
96
|
+
FROM rec_x_courses_progress p WHERE p.state = 'done' AND ${IN("p.done_at")}
|
|
97
|
+
GROUP BY week ORDER BY week`,
|
|
98
|
+
mapping: { x: "week", y: ["done"] },
|
|
99
|
+
layout: { w: 6, h: 2 },
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
id: "quiet",
|
|
103
|
+
type: "table",
|
|
104
|
+
title: "Nothing done in two weeks",
|
|
105
|
+
description: "Students with an active enrolment and no lesson started or done in the last 14 days.",
|
|
106
|
+
sql: `SELECT p.id AS id, p.display_name AS student,
|
|
107
|
+
(SELECT max(g.updated_at) FROM rec_x_courses_progress g WHERE g.holder = e.holder) AS last_seen
|
|
108
|
+
FROM rec_x_courses_enrolment e JOIN rec_person p ON p.id = e.holder
|
|
109
|
+
WHERE e.status = 'active' AND NOT EXISTS (
|
|
110
|
+
SELECT 1 FROM rec_x_courses_progress g WHERE g.holder = e.holder
|
|
111
|
+
AND g.updated_at >= date(:today, '-14 days'))
|
|
112
|
+
GROUP BY p.id ORDER BY last_seen, student LIMIT 50`,
|
|
113
|
+
mapping: { columns: ["student", "last_seen"] },
|
|
114
|
+
clickThrough: { object: "person", filter: { field: "id", op: "eq", value: { $row: "id" } } },
|
|
115
|
+
layout: { w: 6, h: 2 },
|
|
116
|
+
},
|
|
117
|
+
],
|
|
118
|
+
};
|
|
119
|
+
export const coursesDashboards = { courses };
|