@fayz-ai/courses 0.9.0 → 0.11.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/README.md +1 -1
- package/dist/{mock-provider.d.ts → data/mock-provider.d.ts} +1 -1
- package/dist/data/mock-provider.d.ts.map +1 -0
- package/dist/{provider.d.ts → data/provider.d.ts} +1 -1
- package/dist/data/provider.d.ts.map +1 -0
- package/dist/data/safe-provider.d.ts.map +1 -0
- package/dist/{seed.d.ts → data/seed.d.ts} +1 -1
- package/dist/data/seed.d.ts.map +1 -0
- package/dist/data/supabase-provider.d.ts.map +1 -0
- package/dist/data/tables.d.ts.map +1 -0
- package/dist/data/tenant.d.ts.map +1 -0
- package/dist/{hooks.d.ts → hooks/index.d.ts} +2 -2
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/index.d.ts +11 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -8
- package/dist/index.js.map +1 -1
- package/package.json +3 -5
- package/dist/hooks.d.ts.map +0 -1
- package/dist/mock-provider.d.ts.map +0 -1
- package/dist/provider.d.ts.map +0 -1
- package/dist/safe-provider.d.ts.map +0 -1
- package/dist/seed.d.ts.map +0 -1
- package/dist/supabase-provider.d.ts.map +0 -1
- package/dist/tables.d.ts.map +0 -1
- package/dist/tenant.d.ts.map +0 -1
- package/src/hooks.ts +0 -125
- package/src/index.ts +0 -33
- package/src/mock-provider.ts +0 -369
- package/src/provider.ts +0 -85
- package/src/safe-provider.ts +0 -17
- package/src/seed.ts +0 -297
- package/src/supabase-provider.ts +0 -425
- package/src/tables.ts +0 -18
- package/src/tenant.ts +0 -21
- package/src/types.ts +0 -268
- /package/dist/{safe-provider.d.ts → data/safe-provider.d.ts} +0 -0
- /package/dist/{supabase-provider.d.ts → data/supabase-provider.d.ts} +0 -0
- /package/dist/{tables.d.ts → data/tables.d.ts} +0 -0
- /package/dist/{tenant.d.ts → data/tenant.d.ts} +0 -0
package/src/hooks.ts
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import { useEffect, useState, useCallback } from 'react'
|
|
2
|
-
import { getCoursesProvider } from './provider'
|
|
3
|
-
import type { Course, Module, Lesson, Enrollment, Progress, EnrolledCourse } from './types'
|
|
4
|
-
|
|
5
|
-
/** Courses a customer owns, with progress % — the "my courses" data. */
|
|
6
|
-
export function useMyCourses(customerId: string | null): { courses: EnrolledCourse[]; loading: boolean; refresh: () => void } {
|
|
7
|
-
const [courses, setCourses] = useState<EnrolledCourse[]>([])
|
|
8
|
-
const [loading, setLoading] = useState(false)
|
|
9
|
-
const [tick, setTick] = useState(0)
|
|
10
|
-
|
|
11
|
-
useEffect(() => {
|
|
12
|
-
if (!customerId) {
|
|
13
|
-
setCourses([])
|
|
14
|
-
return
|
|
15
|
-
}
|
|
16
|
-
let active = true
|
|
17
|
-
setLoading(true)
|
|
18
|
-
;(async () => {
|
|
19
|
-
const provider = getCoursesProvider()
|
|
20
|
-
const enrollments = await provider.listEnrollments(customerId)
|
|
21
|
-
const result: EnrolledCourse[] = []
|
|
22
|
-
for (const enrollment of enrollments) {
|
|
23
|
-
const course = await provider.getCourse(enrollment.courseId)
|
|
24
|
-
if (!course) continue
|
|
25
|
-
const lessons = await provider.listLessons(course.id)
|
|
26
|
-
const progress = await provider.listProgress(enrollment.id)
|
|
27
|
-
const completedLessons = progress.filter((p) => p.completed).length
|
|
28
|
-
result.push({
|
|
29
|
-
course,
|
|
30
|
-
enrollment,
|
|
31
|
-
totalLessons: lessons.length,
|
|
32
|
-
completedLessons,
|
|
33
|
-
progressPercent: lessons.length ? Math.round((completedLessons / lessons.length) * 100) : 0,
|
|
34
|
-
})
|
|
35
|
-
}
|
|
36
|
-
if (active) {
|
|
37
|
-
setCourses(result.sort((a, b) => a.course.sortOrder - b.course.sortOrder))
|
|
38
|
-
setLoading(false)
|
|
39
|
-
}
|
|
40
|
-
})()
|
|
41
|
-
return () => {
|
|
42
|
-
active = false
|
|
43
|
-
}
|
|
44
|
-
}, [customerId, tick])
|
|
45
|
-
|
|
46
|
-
return { courses, loading, refresh: () => setTick((t) => t + 1) }
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** A course's full content tree (modules + lessons) for the player. */
|
|
50
|
-
export function useCourse(idOrSlug: string | null): {
|
|
51
|
-
course: Course | null
|
|
52
|
-
modules: Module[]
|
|
53
|
-
lessons: Lesson[]
|
|
54
|
-
loading: boolean
|
|
55
|
-
} {
|
|
56
|
-
const [course, setCourse] = useState<Course | null>(null)
|
|
57
|
-
const [modules, setModules] = useState<Module[]>([])
|
|
58
|
-
const [lessons, setLessons] = useState<Lesson[]>([])
|
|
59
|
-
const [loading, setLoading] = useState(false)
|
|
60
|
-
|
|
61
|
-
useEffect(() => {
|
|
62
|
-
if (!idOrSlug) return
|
|
63
|
-
let active = true
|
|
64
|
-
setLoading(true)
|
|
65
|
-
;(async () => {
|
|
66
|
-
const provider = getCoursesProvider()
|
|
67
|
-
const c = await provider.getCourse(idOrSlug)
|
|
68
|
-
if (!c) {
|
|
69
|
-
if (active) { setCourse(null); setLoading(false) }
|
|
70
|
-
return
|
|
71
|
-
}
|
|
72
|
-
const [m, l] = await Promise.all([provider.listModules(c.id), provider.listLessons(c.id)])
|
|
73
|
-
if (active) {
|
|
74
|
-
setCourse(c)
|
|
75
|
-
setModules(m)
|
|
76
|
-
setLessons(l)
|
|
77
|
-
setLoading(false)
|
|
78
|
-
}
|
|
79
|
-
})()
|
|
80
|
-
return () => {
|
|
81
|
-
active = false
|
|
82
|
-
}
|
|
83
|
-
}, [idOrSlug])
|
|
84
|
-
|
|
85
|
-
return { course, modules, lessons, loading }
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/** Per-enrollment progress + a completion toggle. */
|
|
89
|
-
export function useCourseProgress(enrollmentId: string | null): {
|
|
90
|
-
progressByLesson: Record<string, Progress>
|
|
91
|
-
isCompleted: (lessonId: string) => boolean
|
|
92
|
-
markLessonComplete: (lessonId: string, completed?: boolean) => Promise<void>
|
|
93
|
-
refresh: () => void
|
|
94
|
-
} {
|
|
95
|
-
const [map, setMap] = useState<Record<string, Progress>>({})
|
|
96
|
-
const [tick, setTick] = useState(0)
|
|
97
|
-
|
|
98
|
-
useEffect(() => {
|
|
99
|
-
if (!enrollmentId) return
|
|
100
|
-
let active = true
|
|
101
|
-
;(async () => {
|
|
102
|
-
const list = await getCoursesProvider().listProgress(enrollmentId)
|
|
103
|
-
if (active) setMap(Object.fromEntries(list.map((p) => [p.lessonId, p])))
|
|
104
|
-
})()
|
|
105
|
-
return () => {
|
|
106
|
-
active = false
|
|
107
|
-
}
|
|
108
|
-
}, [enrollmentId, tick])
|
|
109
|
-
|
|
110
|
-
const markLessonComplete = useCallback(
|
|
111
|
-
async (lessonId: string, completed = true) => {
|
|
112
|
-
if (!enrollmentId) return
|
|
113
|
-
const updated = await getCoursesProvider().setLessonProgress(enrollmentId, lessonId, { completed })
|
|
114
|
-
setMap((m) => ({ ...m, [lessonId]: updated }))
|
|
115
|
-
},
|
|
116
|
-
[enrollmentId],
|
|
117
|
-
)
|
|
118
|
-
|
|
119
|
-
return {
|
|
120
|
-
progressByLesson: map,
|
|
121
|
-
isCompleted: (lessonId) => !!map[lessonId]?.completed,
|
|
122
|
-
markLessonComplete,
|
|
123
|
-
refresh: () => setTick((t) => t + 1),
|
|
124
|
-
}
|
|
125
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
// @fayz-ai/courses — the education domain (the counterpart of @fayz-ai/shop).
|
|
2
|
-
export type {
|
|
3
|
-
Course, CourseStatus, Module, Lesson, Enrollment, Progress, EnrolledCourse,
|
|
4
|
-
CreateCourseInput, UpdateCourseInput,
|
|
5
|
-
CreateModuleInput, UpdateModuleInput,
|
|
6
|
-
CreateLessonInput, UpdateLessonInput,
|
|
7
|
-
ListCoursesOptions,
|
|
8
|
-
// Commerce layer
|
|
9
|
-
Offer, OfferKind, RecurringInterval, CreateOfferInput, UpdateOfferInput,
|
|
10
|
-
Order, OrderDetail, OrderInstallment, PaymentMethod, FinancialStatus, ListOrdersOptions,
|
|
11
|
-
Subscription, SubscriptionStatus, ListSubscriptionsOptions,
|
|
12
|
-
Payout, PayoutStatus, CreatorAccount, FinancialSummary,
|
|
13
|
-
} from './types'
|
|
14
|
-
|
|
15
|
-
export type { CoursesProvider } from './provider'
|
|
16
|
-
export { setCoursesProvider, getCoursesProvider, getCoursesProviderOptional } from './provider'
|
|
17
|
-
|
|
18
|
-
export { MockCoursesProvider, createMockCoursesProvider } from './mock-provider'
|
|
19
|
-
export type { MockCoursesSeed } from './mock-provider'
|
|
20
|
-
|
|
21
|
-
// Supabase-backed provider + tenant scoping (the "fayz-course" central DB).
|
|
22
|
-
export { createSupabaseCoursesProvider } from './supabase-provider'
|
|
23
|
-
export { setCoursesTenantResolver, getCoursesTenantId } from './tenant'
|
|
24
|
-
export type { TenantResolver } from './tenant'
|
|
25
|
-
export { createSafeCoursesProvider } from './safe-provider'
|
|
26
|
-
|
|
27
|
-
export {
|
|
28
|
-
buildCourseCatalog, DEFAULT_COURSE_CATALOG,
|
|
29
|
-
buildDemoLedger, DEFAULT_DEMO_LEDGER, DEMO_PLATFORM_FEE_BPS,
|
|
30
|
-
} from './seed'
|
|
31
|
-
export type { CourseCatalog, CourseSeedInput, DemoLedger } from './seed'
|
|
32
|
-
|
|
33
|
-
export { useMyCourses, useCourse, useCourseProgress } from './hooks'
|
package/src/mock-provider.ts
DELETED
|
@@ -1,369 +0,0 @@
|
|
|
1
|
-
import type { CoursesProvider } from './provider'
|
|
2
|
-
import type {
|
|
3
|
-
Course, Module, Lesson, Enrollment, Progress,
|
|
4
|
-
CreateCourseInput, UpdateCourseInput,
|
|
5
|
-
CreateModuleInput, UpdateModuleInput,
|
|
6
|
-
CreateLessonInput, UpdateLessonInput,
|
|
7
|
-
ListCoursesOptions,
|
|
8
|
-
Offer, CreateOfferInput, UpdateOfferInput,
|
|
9
|
-
Order, OrderDetail, ListOrdersOptions,
|
|
10
|
-
Subscription, ListSubscriptionsOptions,
|
|
11
|
-
Payout, CreatorAccount, FinancialSummary,
|
|
12
|
-
} from './types'
|
|
13
|
-
import {
|
|
14
|
-
DEFAULT_COURSE_CATALOG, DEFAULT_DEMO_LEDGER, DEMO_PLATFORM_FEE_BPS,
|
|
15
|
-
type CourseCatalog, type DemoLedger,
|
|
16
|
-
} from './seed'
|
|
17
|
-
|
|
18
|
-
function uid() {
|
|
19
|
-
return Math.random().toString(36).slice(2)
|
|
20
|
-
}
|
|
21
|
-
function now() {
|
|
22
|
-
return new Date().toISOString()
|
|
23
|
-
}
|
|
24
|
-
function slugify(s: string) {
|
|
25
|
-
return s.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '')
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const K = {
|
|
29
|
-
courses: 'fayz.courses.mock.courses.v1',
|
|
30
|
-
modules: 'fayz.courses.mock.modules.v1',
|
|
31
|
-
lessons: 'fayz.courses.mock.lessons.v1',
|
|
32
|
-
enrollments: 'fayz.courses.mock.enrollments.v1',
|
|
33
|
-
progress: 'fayz.courses.mock.progress.v1',
|
|
34
|
-
offers: 'fayz.courses.mock.offers.v1',
|
|
35
|
-
orders: 'fayz.courses.mock.orders.v1',
|
|
36
|
-
subscriptions: 'fayz.courses.mock.subscriptions.v1',
|
|
37
|
-
payouts: 'fayz.courses.mock.payouts.v1',
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function load<T>(key: string): T[] | null {
|
|
41
|
-
try {
|
|
42
|
-
if (typeof localStorage === 'undefined') return null
|
|
43
|
-
const raw = localStorage.getItem(key)
|
|
44
|
-
return raw ? (JSON.parse(raw) as T[]) : null
|
|
45
|
-
} catch {
|
|
46
|
-
return null
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
function save<T>(key: string, value: T[]): void {
|
|
50
|
-
try {
|
|
51
|
-
if (typeof localStorage !== 'undefined') localStorage.setItem(key, JSON.stringify(value))
|
|
52
|
-
} catch {
|
|
53
|
-
/* non-fatal in mock mode */
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export interface MockCoursesSeed {
|
|
58
|
-
catalog?: CourseCatalog
|
|
59
|
-
/** Demo sales/subscriptions/payouts for the admin commerce surfaces. */
|
|
60
|
-
ledger?: DemoLedger
|
|
61
|
-
/** Customer ids auto-enrolled in all courses (so the member area has data on first run). */
|
|
62
|
-
enrollAll?: string[]
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Mock courses provider. Courses/modules/lessons are seeded from the catalog on
|
|
67
|
-
* first run, then PERSISTED (so admin edits survive). Enrollments/progress are
|
|
68
|
-
* persisted too. Both example apps share the default catalog, so admin↔member
|
|
69
|
-
* stay consistent.
|
|
70
|
-
*/
|
|
71
|
-
export class MockCoursesProvider implements CoursesProvider {
|
|
72
|
-
constructor(seed?: MockCoursesSeed) {
|
|
73
|
-
const catalog = seed?.catalog ?? DEFAULT_COURSE_CATALOG
|
|
74
|
-
const ledger = seed?.ledger ?? DEFAULT_DEMO_LEDGER
|
|
75
|
-
if (load<Course>(K.courses) == null) {
|
|
76
|
-
save(K.courses, catalog.courses)
|
|
77
|
-
save(K.modules, catalog.modules)
|
|
78
|
-
save(K.lessons, catalog.lessons)
|
|
79
|
-
save(K.offers, catalog.offers)
|
|
80
|
-
save(K.orders, ledger.orders)
|
|
81
|
-
save(K.subscriptions, ledger.subscriptions)
|
|
82
|
-
save(K.payouts, ledger.payouts)
|
|
83
|
-
}
|
|
84
|
-
// Auto-enroll demo customers in every course (idempotent).
|
|
85
|
-
if (seed?.enrollAll?.length) {
|
|
86
|
-
const courses = load<Course>(K.courses) ?? []
|
|
87
|
-
const enrollments = load<Enrollment>(K.enrollments) ?? []
|
|
88
|
-
let changed = false
|
|
89
|
-
for (const customerId of seed.enrollAll) {
|
|
90
|
-
for (const c of courses) {
|
|
91
|
-
if (!enrollments.some((e) => e.customerId === customerId && e.courseId === c.id)) {
|
|
92
|
-
enrollments.push({ id: uid(), courseId: c.id, customerId, status: 'active', enrolledAt: now() })
|
|
93
|
-
changed = true
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
if (changed) save(K.enrollments, enrollments)
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
private courses() { return load<Course>(K.courses) ?? [] }
|
|
102
|
-
private modules() { return load<Module>(K.modules) ?? [] }
|
|
103
|
-
private lessons() { return load<Lesson>(K.lessons) ?? [] }
|
|
104
|
-
private enrollments() { return load<Enrollment>(K.enrollments) ?? [] }
|
|
105
|
-
private progress() { return load<Progress>(K.progress) ?? [] }
|
|
106
|
-
private offers() { return load<Offer>(K.offers) ?? [] }
|
|
107
|
-
private orders() { return load<Order>(K.orders) ?? [] }
|
|
108
|
-
private subscriptions() { return load<Subscription>(K.subscriptions) ?? [] }
|
|
109
|
-
private payouts() { return load<Payout>(K.payouts) ?? [] }
|
|
110
|
-
|
|
111
|
-
// Courses -----------------------------------------------------------------
|
|
112
|
-
async listCourses(opts?: ListCoursesOptions): Promise<Course[]> {
|
|
113
|
-
let list = this.courses()
|
|
114
|
-
if (opts?.status) list = list.filter((c) => c.status === opts.status)
|
|
115
|
-
if (opts?.slug) list = list.filter((c) => c.slug === opts.slug)
|
|
116
|
-
if (opts?.search) {
|
|
117
|
-
const q = opts.search.toLowerCase()
|
|
118
|
-
list = list.filter((c) => c.title.toLowerCase().includes(q) || (c.description ?? '').toLowerCase().includes(q))
|
|
119
|
-
}
|
|
120
|
-
return list.slice().sort((a, b) => a.sortOrder - b.sortOrder)
|
|
121
|
-
}
|
|
122
|
-
async getCourse(idOrSlug: string): Promise<Course | null> {
|
|
123
|
-
return this.courses().find((c) => c.id === idOrSlug || c.slug === idOrSlug) ?? null
|
|
124
|
-
}
|
|
125
|
-
async createCourse(input: CreateCourseInput): Promise<Course> {
|
|
126
|
-
const list = this.courses()
|
|
127
|
-
const course: Course = {
|
|
128
|
-
id: uid(),
|
|
129
|
-
slug: input.slug ?? slugify(input.title),
|
|
130
|
-
title: input.title,
|
|
131
|
-
subtitle: input.subtitle ?? null,
|
|
132
|
-
description: input.description ?? null,
|
|
133
|
-
thumbnailUrl: input.thumbnailUrl ?? null,
|
|
134
|
-
price: input.price ?? 0,
|
|
135
|
-
currency: input.currency ?? 'BRL',
|
|
136
|
-
status: input.status ?? 'draft',
|
|
137
|
-
deliveryType: input.deliveryType ?? 'online',
|
|
138
|
-
sortOrder: input.sortOrder ?? list.length,
|
|
139
|
-
createdAt: now(),
|
|
140
|
-
updatedAt: now(),
|
|
141
|
-
}
|
|
142
|
-
save(K.courses, [...list, course])
|
|
143
|
-
return course
|
|
144
|
-
}
|
|
145
|
-
async updateCourse(id: string, input: UpdateCourseInput): Promise<Course> {
|
|
146
|
-
const list = this.courses()
|
|
147
|
-
const idx = list.findIndex((c) => c.id === id)
|
|
148
|
-
if (idx < 0) throw new Error('Course not found')
|
|
149
|
-
const updated = { ...list[idx], ...input, updatedAt: now() } as Course
|
|
150
|
-
list[idx] = updated
|
|
151
|
-
save(K.courses, list)
|
|
152
|
-
return updated
|
|
153
|
-
}
|
|
154
|
-
async deleteCourse(id: string): Promise<void> {
|
|
155
|
-
save(K.courses, this.courses().filter((c) => c.id !== id))
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// Modules -----------------------------------------------------------------
|
|
159
|
-
async listModules(courseId: string): Promise<Module[]> {
|
|
160
|
-
return this.modules().filter((m) => m.courseId === courseId).sort((a, b) => a.sortOrder - b.sortOrder)
|
|
161
|
-
}
|
|
162
|
-
async createModule(input: CreateModuleInput): Promise<Module> {
|
|
163
|
-
const list = this.modules()
|
|
164
|
-
const mod: Module = { id: uid(), courseId: input.courseId, title: input.title, sortOrder: input.sortOrder ?? list.filter((m) => m.courseId === input.courseId).length }
|
|
165
|
-
save(K.modules, [...list, mod])
|
|
166
|
-
return mod
|
|
167
|
-
}
|
|
168
|
-
async updateModule(id: string, input: UpdateModuleInput): Promise<Module> {
|
|
169
|
-
const list = this.modules()
|
|
170
|
-
const idx = list.findIndex((m) => m.id === id)
|
|
171
|
-
if (idx < 0) throw new Error('Module not found')
|
|
172
|
-
list[idx] = { ...list[idx], ...input } as Module
|
|
173
|
-
save(K.modules, list)
|
|
174
|
-
return list[idx]
|
|
175
|
-
}
|
|
176
|
-
async deleteModule(id: string): Promise<void> {
|
|
177
|
-
save(K.modules, this.modules().filter((m) => m.id !== id))
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// Lessons -----------------------------------------------------------------
|
|
181
|
-
async listLessons(courseId: string): Promise<Lesson[]> {
|
|
182
|
-
return this.lessons().filter((l) => l.courseId === courseId).sort((a, b) => a.sortOrder - b.sortOrder)
|
|
183
|
-
}
|
|
184
|
-
async getLesson(id: string): Promise<Lesson | null> {
|
|
185
|
-
return this.lessons().find((l) => l.id === id) ?? null
|
|
186
|
-
}
|
|
187
|
-
async createLesson(input: CreateLessonInput): Promise<Lesson> {
|
|
188
|
-
const list = this.lessons()
|
|
189
|
-
const lesson: Lesson = {
|
|
190
|
-
id: uid(),
|
|
191
|
-
courseId: input.courseId,
|
|
192
|
-
moduleId: input.moduleId,
|
|
193
|
-
title: input.title,
|
|
194
|
-
description: input.description ?? null,
|
|
195
|
-
videoUrl: input.videoUrl,
|
|
196
|
-
durationSec: input.durationSec ?? 600,
|
|
197
|
-
sortOrder: input.sortOrder ?? list.filter((l) => l.moduleId === input.moduleId).length,
|
|
198
|
-
}
|
|
199
|
-
save(K.lessons, [...list, lesson])
|
|
200
|
-
return lesson
|
|
201
|
-
}
|
|
202
|
-
async updateLesson(id: string, input: UpdateLessonInput): Promise<Lesson> {
|
|
203
|
-
const list = this.lessons()
|
|
204
|
-
const idx = list.findIndex((l) => l.id === id)
|
|
205
|
-
if (idx < 0) throw new Error('Lesson not found')
|
|
206
|
-
list[idx] = { ...list[idx], ...input } as Lesson
|
|
207
|
-
save(K.lessons, list)
|
|
208
|
-
return list[idx]
|
|
209
|
-
}
|
|
210
|
-
async deleteLesson(id: string): Promise<void> {
|
|
211
|
-
save(K.lessons, this.lessons().filter((l) => l.id !== id))
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// Enrollments + progress --------------------------------------------------
|
|
215
|
-
async listEnrollments(customerId: string): Promise<Enrollment[]> {
|
|
216
|
-
return this.enrollments().filter((e) => e.customerId === customerId)
|
|
217
|
-
}
|
|
218
|
-
async enroll(courseId: string, customerId: string): Promise<Enrollment> {
|
|
219
|
-
const list = this.enrollments()
|
|
220
|
-
const existing = list.find((e) => e.courseId === courseId && e.customerId === customerId)
|
|
221
|
-
if (existing) return existing
|
|
222
|
-
const enrollment: Enrollment = { id: uid(), courseId, customerId, status: 'active', enrolledAt: now() }
|
|
223
|
-
save(K.enrollments, [...list, enrollment])
|
|
224
|
-
return enrollment
|
|
225
|
-
}
|
|
226
|
-
async listProgress(enrollmentId: string): Promise<Progress[]> {
|
|
227
|
-
return this.progress().filter((p) => p.enrollmentId === enrollmentId)
|
|
228
|
-
}
|
|
229
|
-
async setLessonProgress(enrollmentId: string, lessonId: string, patch: { completed?: boolean; lastPositionSec?: number }): Promise<Progress> {
|
|
230
|
-
const list = this.progress()
|
|
231
|
-
const idx = list.findIndex((p) => p.enrollmentId === enrollmentId && p.lessonId === lessonId)
|
|
232
|
-
if (idx >= 0) {
|
|
233
|
-
const updated: Progress = {
|
|
234
|
-
...list[idx],
|
|
235
|
-
completed: patch.completed ?? list[idx].completed,
|
|
236
|
-
lastPositionSec: patch.lastPositionSec ?? list[idx].lastPositionSec,
|
|
237
|
-
completedAt: (patch.completed ?? list[idx].completed) ? (list[idx].completedAt ?? now()) : null,
|
|
238
|
-
}
|
|
239
|
-
list[idx] = updated
|
|
240
|
-
save(K.progress, list)
|
|
241
|
-
return updated
|
|
242
|
-
}
|
|
243
|
-
const created: Progress = {
|
|
244
|
-
id: uid(),
|
|
245
|
-
enrollmentId,
|
|
246
|
-
lessonId,
|
|
247
|
-
completed: patch.completed ?? false,
|
|
248
|
-
lastPositionSec: patch.lastPositionSec ?? 0,
|
|
249
|
-
completedAt: patch.completed ? now() : null,
|
|
250
|
-
}
|
|
251
|
-
save(K.progress, [...list, created])
|
|
252
|
-
return created
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
// Offers ------------------------------------------------------------------
|
|
256
|
-
async listOffers(courseId: string): Promise<Offer[]> {
|
|
257
|
-
return this.offers().filter((o) => o.courseId === courseId).sort((a, b) => a.sortOrder - b.sortOrder)
|
|
258
|
-
}
|
|
259
|
-
async createOffer(input: CreateOfferInput): Promise<Offer> {
|
|
260
|
-
const list = this.offers()
|
|
261
|
-
const offer: Offer = {
|
|
262
|
-
id: uid(),
|
|
263
|
-
courseId: input.courseId,
|
|
264
|
-
name: input.name,
|
|
265
|
-
price: input.price,
|
|
266
|
-
currency: input.currency ?? 'BRL',
|
|
267
|
-
kind: input.kind ?? 'one_time',
|
|
268
|
-
recurringInterval: input.recurringInterval ?? (input.kind === 'subscription' ? 'month' : null),
|
|
269
|
-
isDefault: input.isDefault ?? false,
|
|
270
|
-
isOrderBump: input.isOrderBump ?? false,
|
|
271
|
-
sortOrder: input.sortOrder ?? list.filter((o) => o.courseId === input.courseId).length,
|
|
272
|
-
}
|
|
273
|
-
save(K.offers, [...list, offer])
|
|
274
|
-
return offer
|
|
275
|
-
}
|
|
276
|
-
async updateOffer(id: string, input: UpdateOfferInput): Promise<Offer> {
|
|
277
|
-
const list = this.offers()
|
|
278
|
-
const idx = list.findIndex((o) => o.id === id)
|
|
279
|
-
if (idx < 0) throw new Error('Offer not found')
|
|
280
|
-
list[idx] = { ...list[idx], ...input } as Offer
|
|
281
|
-
save(K.offers, list)
|
|
282
|
-
return list[idx]
|
|
283
|
-
}
|
|
284
|
-
async deleteOffer(id: string): Promise<void> {
|
|
285
|
-
save(K.offers, this.offers().filter((o) => o.id !== id))
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
// Sales ledger ------------------------------------------------------------
|
|
289
|
-
async listOrders(opts?: ListOrdersOptions): Promise<Order[]> {
|
|
290
|
-
let list = this.orders()
|
|
291
|
-
if (opts?.courseId) list = list.filter((o) => o.courseId === opts.courseId)
|
|
292
|
-
if (opts?.financialStatus) list = list.filter((o) => o.financialStatus === opts.financialStatus)
|
|
293
|
-
if (opts?.search) {
|
|
294
|
-
const q = opts.search.toLowerCase()
|
|
295
|
-
list = list.filter((o) =>
|
|
296
|
-
(o.customerName ?? '').toLowerCase().includes(q) ||
|
|
297
|
-
(o.customerEmail ?? '').toLowerCase().includes(q),
|
|
298
|
-
)
|
|
299
|
-
}
|
|
300
|
-
return list.slice().sort((a, b) => (a.createdAt < b.createdAt ? 1 : -1))
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
// Core-commerce course sales don't exist in the mock layer (no orders/financial).
|
|
304
|
-
async listCoreSales(): Promise<Order[]> {
|
|
305
|
-
return []
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
async getOrder(id: string): Promise<OrderDetail | null> {
|
|
309
|
-
const o = this.orders().find((x) => x.id === id)
|
|
310
|
-
if (!o) return null
|
|
311
|
-
const paid = o.financialStatus === 'paid' ? o.total : 0
|
|
312
|
-
return { ...o, courseTitle: null, offerName: null, paidAmount: paid, balance: o.total - paid, installments: [] }
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
// Subscriptions -----------------------------------------------------------
|
|
316
|
-
async listSubscriptions(opts?: ListSubscriptionsOptions): Promise<Subscription[]> {
|
|
317
|
-
let list = this.subscriptions()
|
|
318
|
-
if (opts?.courseId) list = list.filter((s) => s.courseId === opts.courseId)
|
|
319
|
-
if (opts?.status) list = list.filter((s) => s.status === opts.status)
|
|
320
|
-
return list.slice().sort((a, b) => (a.startedAt < b.startedAt ? 1 : -1))
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
// Financial / payouts / connect -------------------------------------------
|
|
324
|
-
async getFinancialSummary(): Promise<FinancialSummary> {
|
|
325
|
-
const paid = this.orders().filter((o) => o.financialStatus === 'paid')
|
|
326
|
-
const refunded = this.orders().filter((o) => o.financialStatus === 'refunded')
|
|
327
|
-
const activeSubs = this.subscriptions().filter((s) => s.status === 'active')
|
|
328
|
-
const totalRevenue = round2(paid.reduce((s, o) => s + o.total, 0))
|
|
329
|
-
const platformFeeTotal = round2(paid.reduce((s, o) => s + o.platformFee, 0))
|
|
330
|
-
const netTotal = round2(paid.reduce((s, o) => s + o.netValue, 0))
|
|
331
|
-
const paidOut = round2(this.payouts().filter((p) => p.status === 'paid').reduce((s, p) => s + p.amount, 0))
|
|
332
|
-
const mrr = round2(activeSubs.reduce((s, sub) => s + (sub.interval === 'year' ? sub.netValue / 12 : sub.netValue), 0))
|
|
333
|
-
const totalCount = paid.length + refunded.length
|
|
334
|
-
return {
|
|
335
|
-
currency: 'BRL',
|
|
336
|
-
availableBalance: round2(Math.max(netTotal - paidOut, 0)),
|
|
337
|
-
pendingBalance: round2(this.orders().filter((o) => o.financialStatus === 'pending').reduce((s, o) => s + o.netValue, 0)),
|
|
338
|
-
totalRevenue,
|
|
339
|
-
platformFeeTotal,
|
|
340
|
-
salesCount: paid.length,
|
|
341
|
-
activeSubscriptions: activeSubs.length,
|
|
342
|
-
mrr,
|
|
343
|
-
refundRate: totalCount > 0 ? refunded.length / totalCount : 0,
|
|
344
|
-
conversionRate: 0,
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
async listPayouts(): Promise<Payout[]> {
|
|
348
|
-
return this.payouts().slice().sort((a, b) => (a.createdAt < b.createdAt ? 1 : -1))
|
|
349
|
-
}
|
|
350
|
-
async getCreatorAccount(): Promise<CreatorAccount> {
|
|
351
|
-
// Mock account is not connected — the admin sees the "Connect Stripe" CTA.
|
|
352
|
-
return {
|
|
353
|
-
tenantId: 'mock-tenant',
|
|
354
|
-
stripeAccountId: null,
|
|
355
|
-
chargesEnabled: false,
|
|
356
|
-
payoutsEnabled: false,
|
|
357
|
-
defaultCurrency: 'BRL',
|
|
358
|
-
platformFeeBps: DEMO_PLATFORM_FEE_BPS,
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
function round2(n: number): number {
|
|
364
|
-
return Math.round(n * 100) / 100
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
export function createMockCoursesProvider(seed?: MockCoursesSeed): MockCoursesProvider {
|
|
368
|
-
return new MockCoursesProvider(seed)
|
|
369
|
-
}
|
package/src/provider.ts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
Course, Module, Lesson, Enrollment, Progress,
|
|
3
|
-
CreateCourseInput, UpdateCourseInput,
|
|
4
|
-
CreateModuleInput, UpdateModuleInput,
|
|
5
|
-
CreateLessonInput, UpdateLessonInput,
|
|
6
|
-
ListCoursesOptions,
|
|
7
|
-
Offer, CreateOfferInput, UpdateOfferInput,
|
|
8
|
-
Order, OrderDetail, ListOrdersOptions,
|
|
9
|
-
Subscription, ListSubscriptionsOptions,
|
|
10
|
-
Payout, CreatorAccount, FinancialSummary,
|
|
11
|
-
} from './types'
|
|
12
|
-
|
|
13
|
-
export interface CoursesProvider {
|
|
14
|
-
// Courses
|
|
15
|
-
listCourses(options?: ListCoursesOptions): Promise<Course[]>
|
|
16
|
-
getCourse(idOrSlug: string): Promise<Course | null>
|
|
17
|
-
createCourse(input: CreateCourseInput): Promise<Course>
|
|
18
|
-
updateCourse(id: string, input: UpdateCourseInput): Promise<Course>
|
|
19
|
-
deleteCourse(id: string): Promise<void>
|
|
20
|
-
|
|
21
|
-
// Modules
|
|
22
|
-
listModules(courseId: string): Promise<Module[]>
|
|
23
|
-
createModule(input: CreateModuleInput): Promise<Module>
|
|
24
|
-
updateModule(id: string, input: UpdateModuleInput): Promise<Module>
|
|
25
|
-
deleteModule(id: string): Promise<void>
|
|
26
|
-
|
|
27
|
-
// Lessons
|
|
28
|
-
listLessons(courseId: string): Promise<Lesson[]>
|
|
29
|
-
getLesson(id: string): Promise<Lesson | null>
|
|
30
|
-
createLesson(input: CreateLessonInput): Promise<Lesson>
|
|
31
|
-
updateLesson(id: string, input: UpdateLessonInput): Promise<Lesson>
|
|
32
|
-
deleteLesson(id: string): Promise<void>
|
|
33
|
-
|
|
34
|
-
// Enrollments + progress (member side)
|
|
35
|
-
listEnrollments(customerId: string): Promise<Enrollment[]>
|
|
36
|
-
enroll(courseId: string, customerId: string): Promise<Enrollment>
|
|
37
|
-
listProgress(enrollmentId: string): Promise<Progress[]>
|
|
38
|
-
setLessonProgress(
|
|
39
|
-
enrollmentId: string,
|
|
40
|
-
lessonId: string,
|
|
41
|
-
patch: { completed?: boolean; lastPositionSec?: number },
|
|
42
|
-
): Promise<Progress>
|
|
43
|
-
|
|
44
|
-
// Offers (pricing / order-bumps)
|
|
45
|
-
listOffers(courseId: string): Promise<Offer[]>
|
|
46
|
-
createOffer(input: CreateOfferInput): Promise<Offer>
|
|
47
|
-
updateOffer(id: string, input: UpdateOfferInput): Promise<Offer>
|
|
48
|
-
deleteOffer(id: string): Promise<void>
|
|
49
|
-
|
|
50
|
-
// Sales ledger
|
|
51
|
-
listOrders(options?: ListOrdersOptions): Promise<Order[]>
|
|
52
|
-
/** Course sales made through the CORE commerce (approved CRM quotes + direct
|
|
53
|
-
* financial receivables that reference a course). Mapped into the Order shape
|
|
54
|
-
* so the Sales screen shows the same money as Financeiro > Receber. */
|
|
55
|
-
listCoreSales(): Promise<Order[]>
|
|
56
|
-
/** Full detail for one order (canonical public.orders) + course/offer +
|
|
57
|
-
* the receivable's installment schedule. Powers the order-detail page. */
|
|
58
|
-
getOrder(id: string): Promise<OrderDetail | null>
|
|
59
|
-
|
|
60
|
-
// Recurring subscriptions
|
|
61
|
-
listSubscriptions(options?: ListSubscriptionsOptions): Promise<Subscription[]>
|
|
62
|
-
|
|
63
|
-
// Financial / payouts / Stripe Connect
|
|
64
|
-
getFinancialSummary(): Promise<FinancialSummary>
|
|
65
|
-
listPayouts(): Promise<Payout[]>
|
|
66
|
-
getCreatorAccount(): Promise<CreatorAccount>
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// Global provider holder (mirrors @fayz-ai/shop's setShopProvider/getShopProvider).
|
|
70
|
-
let _provider: CoursesProvider | null = null
|
|
71
|
-
|
|
72
|
-
export function setCoursesProvider(provider: CoursesProvider): void {
|
|
73
|
-
_provider = provider
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function getCoursesProvider(): CoursesProvider {
|
|
77
|
-
if (!_provider) {
|
|
78
|
-
throw new Error('Courses provider not set. Call setCoursesProvider() (e.g. createMockCoursesProvider).')
|
|
79
|
-
}
|
|
80
|
-
return _provider
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export function getCoursesProviderOptional(): CoursesProvider | null {
|
|
84
|
-
return _provider
|
|
85
|
-
}
|
package/src/safe-provider.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { createSafeDataProvider } from '@fayz-ai/core'
|
|
2
|
-
import type { CoursesProvider } from './provider'
|
|
3
|
-
import { createSupabaseCoursesProvider } from './supabase-provider'
|
|
4
|
-
import { createMockCoursesProvider } from './mock-provider'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Picks the Supabase provider when a Supabase client is initialized, else the
|
|
8
|
-
* mock — resolved lazily on first use (mirrors @fayz-ai/plugin-financial's
|
|
9
|
-
* createSafeFinancialProvider). Apps wire this and get real data with a backend,
|
|
10
|
-
* or a seeded demo without one.
|
|
11
|
-
*/
|
|
12
|
-
export function createSafeCoursesProvider(): CoursesProvider {
|
|
13
|
-
return createSafeDataProvider<CoursesProvider>(
|
|
14
|
-
() => createSupabaseCoursesProvider(),
|
|
15
|
-
() => createMockCoursesProvider(),
|
|
16
|
-
)
|
|
17
|
-
}
|