@fayz-ai/courses 0.7.0 → 0.8.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 +2 -0
- package/dist/index.cjs +42 -29
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +42 -29
- package/dist/index.js.map +1 -1
- package/dist/supabase-provider.d.ts.map +1 -1
- package/dist/tables.d.ts +15 -0
- package/dist/tables.d.ts.map +1 -0
- package/package.json +5 -2
- package/src/supabase-provider.ts +30 -29
- package/src/tables.ts +18 -0
package/src/supabase-provider.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { getSupabaseClientOptional } from '@fayz-ai/core'
|
|
2
2
|
import type { CoursesProvider } from './provider'
|
|
3
3
|
import { getCoursesTenantId } from './tenant'
|
|
4
|
+
import { T } from './tables'
|
|
4
5
|
import type {
|
|
5
6
|
Course, Module, Lesson, Enrollment, Progress,
|
|
6
7
|
CreateCourseInput, UpdateCourseInput,
|
|
@@ -103,7 +104,7 @@ export function createSupabaseCoursesProvider(): CoursesProvider {
|
|
|
103
104
|
return {
|
|
104
105
|
// Courses ---------------------------------------------------------------
|
|
105
106
|
async listCourses(opts?: ListCoursesOptions): Promise<Course[]> {
|
|
106
|
-
let q = scoped(getDb().from(
|
|
107
|
+
let q = scoped(getDb().from(T.courses).select('*'))
|
|
107
108
|
if (opts?.status) q = q.eq('status', opts.status)
|
|
108
109
|
if (opts?.slug) q = q.eq('slug', opts.slug)
|
|
109
110
|
if (opts?.search) q = q.ilike('title', `%${opts.search}%`)
|
|
@@ -113,12 +114,12 @@ export function createSupabaseCoursesProvider(): CoursesProvider {
|
|
|
113
114
|
},
|
|
114
115
|
async getCourse(idOrSlug: string): Promise<Course | null> {
|
|
115
116
|
const col = /^[0-9a-f-]{36}$/i.test(idOrSlug) ? 'id' : 'slug'
|
|
116
|
-
const { data, error } = await scoped(getDb().from(
|
|
117
|
+
const { data, error } = await scoped(getDb().from(T.courses).select('*').eq(col, idOrSlug)).maybeSingle()
|
|
117
118
|
if (error) throw error
|
|
118
119
|
return data ? rowToCourse(data) : null
|
|
119
120
|
},
|
|
120
121
|
async createCourse(input: CreateCourseInput): Promise<Course> {
|
|
121
|
-
const { data, error } = await getDb().from(
|
|
122
|
+
const { data, error } = await getDb().from(T.courses).insert({
|
|
122
123
|
tenant_id: getTenantId(),
|
|
123
124
|
slug: input.slug ?? slugify(input.title),
|
|
124
125
|
title: input.title, subtitle: input.subtitle ?? null, description: input.description ?? null,
|
|
@@ -139,23 +140,23 @@ export function createSupabaseCoursesProvider(): CoursesProvider {
|
|
|
139
140
|
if (input.currency !== undefined) patch.currency = input.currency
|
|
140
141
|
if (input.status !== undefined) patch.status = input.status
|
|
141
142
|
if (input.sortOrder !== undefined) patch.sort_order = input.sortOrder
|
|
142
|
-
const { data, error } = await scoped(getDb().from(
|
|
143
|
+
const { data, error } = await scoped(getDb().from(T.courses).update(patch).eq('id', id)).select('*').single()
|
|
143
144
|
if (error) throw error
|
|
144
145
|
return rowToCourse(data)
|
|
145
146
|
},
|
|
146
147
|
async deleteCourse(id: string): Promise<void> {
|
|
147
|
-
const { error } = await scoped(getDb().from(
|
|
148
|
+
const { error } = await scoped(getDb().from(T.courses).delete().eq('id', id))
|
|
148
149
|
if (error) throw error
|
|
149
150
|
},
|
|
150
151
|
|
|
151
152
|
// Modules ---------------------------------------------------------------
|
|
152
153
|
async listModules(courseId: string): Promise<Module[]> {
|
|
153
|
-
const { data, error } = await scoped(getDb().from(
|
|
154
|
+
const { data, error } = await scoped(getDb().from(T.modules).select('*').eq('course_id', courseId)).order('sort_order', { ascending: true })
|
|
154
155
|
if (error) throw error
|
|
155
156
|
return (data ?? []).map(rowToModule)
|
|
156
157
|
},
|
|
157
158
|
async createModule(input: CreateModuleInput): Promise<Module> {
|
|
158
|
-
const { data, error } = await getDb().from(
|
|
159
|
+
const { data, error } = await getDb().from(T.modules).insert({
|
|
159
160
|
tenant_id: getTenantId(), course_id: input.courseId, title: input.title, sort_order: input.sortOrder ?? 0,
|
|
160
161
|
}).select('*').single()
|
|
161
162
|
if (error) throw error
|
|
@@ -165,28 +166,28 @@ export function createSupabaseCoursesProvider(): CoursesProvider {
|
|
|
165
166
|
const patch: any = {}
|
|
166
167
|
if (input.title !== undefined) patch.title = input.title
|
|
167
168
|
if (input.sortOrder !== undefined) patch.sort_order = input.sortOrder
|
|
168
|
-
const { data, error } = await scoped(getDb().from(
|
|
169
|
+
const { data, error } = await scoped(getDb().from(T.modules).update(patch).eq('id', id)).select('*').single()
|
|
169
170
|
if (error) throw error
|
|
170
171
|
return rowToModule(data)
|
|
171
172
|
},
|
|
172
173
|
async deleteModule(id: string): Promise<void> {
|
|
173
|
-
const { error } = await scoped(getDb().from(
|
|
174
|
+
const { error } = await scoped(getDb().from(T.modules).delete().eq('id', id))
|
|
174
175
|
if (error) throw error
|
|
175
176
|
},
|
|
176
177
|
|
|
177
178
|
// Lessons ---------------------------------------------------------------
|
|
178
179
|
async listLessons(courseId: string): Promise<Lesson[]> {
|
|
179
|
-
const { data, error } = await scoped(getDb().from(
|
|
180
|
+
const { data, error } = await scoped(getDb().from(T.lessons).select('*').eq('course_id', courseId)).order('sort_order', { ascending: true })
|
|
180
181
|
if (error) throw error
|
|
181
182
|
return (data ?? []).map(rowToLesson)
|
|
182
183
|
},
|
|
183
184
|
async getLesson(id: string): Promise<Lesson | null> {
|
|
184
|
-
const { data, error } = await scoped(getDb().from(
|
|
185
|
+
const { data, error } = await scoped(getDb().from(T.lessons).select('*').eq('id', id)).maybeSingle()
|
|
185
186
|
if (error) throw error
|
|
186
187
|
return data ? rowToLesson(data) : null
|
|
187
188
|
},
|
|
188
189
|
async createLesson(input: CreateLessonInput): Promise<Lesson> {
|
|
189
|
-
const { data, error } = await getDb().from(
|
|
190
|
+
const { data, error } = await getDb().from(T.lessons).insert({
|
|
190
191
|
tenant_id: getTenantId(), course_id: input.courseId, module_id: input.moduleId,
|
|
191
192
|
title: input.title, description: input.description ?? null, video_url: input.videoUrl,
|
|
192
193
|
duration_sec: input.durationSec ?? 600, sort_order: input.sortOrder ?? 0,
|
|
@@ -201,23 +202,23 @@ export function createSupabaseCoursesProvider(): CoursesProvider {
|
|
|
201
202
|
if (input.videoUrl !== undefined) patch.video_url = input.videoUrl
|
|
202
203
|
if (input.durationSec !== undefined) patch.duration_sec = input.durationSec
|
|
203
204
|
if (input.sortOrder !== undefined) patch.sort_order = input.sortOrder
|
|
204
|
-
const { data, error } = await scoped(getDb().from(
|
|
205
|
+
const { data, error } = await scoped(getDb().from(T.lessons).update(patch).eq('id', id)).select('*').single()
|
|
205
206
|
if (error) throw error
|
|
206
207
|
return rowToLesson(data)
|
|
207
208
|
},
|
|
208
209
|
async deleteLesson(id: string): Promise<void> {
|
|
209
|
-
const { error } = await scoped(getDb().from(
|
|
210
|
+
const { error } = await scoped(getDb().from(T.lessons).delete().eq('id', id))
|
|
210
211
|
if (error) throw error
|
|
211
212
|
},
|
|
212
213
|
|
|
213
214
|
// Enrollments + progress ------------------------------------------------
|
|
214
215
|
async listEnrollments(customerId: string): Promise<Enrollment[]> {
|
|
215
|
-
const { data, error } = await getDb().from(
|
|
216
|
+
const { data, error } = await getDb().from(T.enrollments).select('*').eq('customer_id', customerId)
|
|
216
217
|
if (error) throw error
|
|
217
218
|
return (data ?? []).map(rowToEnrollment)
|
|
218
219
|
},
|
|
219
220
|
async enroll(courseId: string, customerId: string): Promise<Enrollment> {
|
|
220
|
-
const { data, error } = await getDb().from(
|
|
221
|
+
const { data, error } = await getDb().from(T.enrollments)
|
|
221
222
|
.upsert({ tenant_id: getTenantId(), course_id: courseId, customer_id: customerId, status: 'active' },
|
|
222
223
|
{ onConflict: 'course_id,customer_id' })
|
|
223
224
|
.select('*').single()
|
|
@@ -225,7 +226,7 @@ export function createSupabaseCoursesProvider(): CoursesProvider {
|
|
|
225
226
|
return rowToEnrollment(data)
|
|
226
227
|
},
|
|
227
228
|
async listProgress(enrollmentId: string): Promise<Progress[]> {
|
|
228
|
-
const { data, error } = await getDb().from(
|
|
229
|
+
const { data, error } = await getDb().from(T.progress).select('*').eq('enrollment_id', enrollmentId)
|
|
229
230
|
if (error) throw error
|
|
230
231
|
return (data ?? []).map(rowToProgress)
|
|
231
232
|
},
|
|
@@ -233,7 +234,7 @@ export function createSupabaseCoursesProvider(): CoursesProvider {
|
|
|
233
234
|
const row: any = { tenant_id: getTenantId(), enrollment_id: enrollmentId, lesson_id: lessonId }
|
|
234
235
|
if (patch.completed !== undefined) { row.completed = patch.completed; row.completed_at = patch.completed ? new Date().toISOString() : null }
|
|
235
236
|
if (patch.lastPositionSec !== undefined) row.last_position_sec = patch.lastPositionSec
|
|
236
|
-
const { data, error } = await getDb().from(
|
|
237
|
+
const { data, error } = await getDb().from(T.progress)
|
|
237
238
|
.upsert(row, { onConflict: 'enrollment_id,lesson_id' }).select('*').single()
|
|
238
239
|
if (error) throw error
|
|
239
240
|
return rowToProgress(data)
|
|
@@ -241,12 +242,12 @@ export function createSupabaseCoursesProvider(): CoursesProvider {
|
|
|
241
242
|
|
|
242
243
|
// Offers ----------------------------------------------------------------
|
|
243
244
|
async listOffers(courseId: string): Promise<Offer[]> {
|
|
244
|
-
const { data, error } = await scoped(getDb().from(
|
|
245
|
+
const { data, error } = await scoped(getDb().from(T.offers).select('*').eq('course_id', courseId)).order('sort_order', { ascending: true })
|
|
245
246
|
if (error) throw error
|
|
246
247
|
return (data ?? []).map(rowToOffer)
|
|
247
248
|
},
|
|
248
249
|
async createOffer(input: CreateOfferInput): Promise<Offer> {
|
|
249
|
-
const { data, error } = await getDb().from(
|
|
250
|
+
const { data, error } = await getDb().from(T.offers).insert({
|
|
250
251
|
tenant_id: getTenantId(), course_id: input.courseId, name: input.name, price: input.price,
|
|
251
252
|
currency: input.currency ?? 'BRL', kind: input.kind ?? 'one_time',
|
|
252
253
|
recurring_interval: input.recurringInterval ?? (input.kind === 'subscription' ? 'month' : null),
|
|
@@ -265,18 +266,18 @@ export function createSupabaseCoursesProvider(): CoursesProvider {
|
|
|
265
266
|
if (input.isDefault !== undefined) patch.is_default = input.isDefault
|
|
266
267
|
if (input.isOrderBump !== undefined) patch.is_order_bump = input.isOrderBump
|
|
267
268
|
if (input.sortOrder !== undefined) patch.sort_order = input.sortOrder
|
|
268
|
-
const { data, error } = await scoped(getDb().from(
|
|
269
|
+
const { data, error } = await scoped(getDb().from(T.offers).update(patch).eq('id', id)).select('*').single()
|
|
269
270
|
if (error) throw error
|
|
270
271
|
return rowToOffer(data)
|
|
271
272
|
},
|
|
272
273
|
async deleteOffer(id: string): Promise<void> {
|
|
273
|
-
const { error } = await scoped(getDb().from(
|
|
274
|
+
const { error } = await scoped(getDb().from(T.offers).delete().eq('id', id))
|
|
274
275
|
if (error) throw error
|
|
275
276
|
},
|
|
276
277
|
|
|
277
278
|
// Sales -----------------------------------------------------------------
|
|
278
279
|
async listOrders(opts?: ListOrdersOptions): Promise<Order[]> {
|
|
279
|
-
let q = scoped(getDb().from(
|
|
280
|
+
let q = scoped(getDb().from(T.orders).select('*'))
|
|
280
281
|
if (opts?.courseId) q = q.eq('course_id', opts.courseId)
|
|
281
282
|
if (opts?.financialStatus) q = q.eq('financial_status', opts.financialStatus)
|
|
282
283
|
if (opts?.search) q = q.or(`customer_name.ilike.%${opts.search}%,customer_email.ilike.%${opts.search}%`)
|
|
@@ -287,7 +288,7 @@ export function createSupabaseCoursesProvider(): CoursesProvider {
|
|
|
287
288
|
|
|
288
289
|
// Subscriptions ---------------------------------------------------------
|
|
289
290
|
async listSubscriptions(opts?: ListSubscriptionsOptions): Promise<Subscription[]> {
|
|
290
|
-
let q = scoped(getDb().from(
|
|
291
|
+
let q = scoped(getDb().from(T.subscriptions).select('*'))
|
|
291
292
|
if (opts?.courseId) q = q.eq('course_id', opts.courseId)
|
|
292
293
|
if (opts?.status) q = q.eq('status', opts.status)
|
|
293
294
|
const { data, error } = await q.order('started_at', { ascending: false })
|
|
@@ -298,9 +299,9 @@ export function createSupabaseCoursesProvider(): CoursesProvider {
|
|
|
298
299
|
// Financial -------------------------------------------------------------
|
|
299
300
|
async getFinancialSummary(): Promise<FinancialSummary> {
|
|
300
301
|
const [orders, subs, payouts] = await Promise.all([
|
|
301
|
-
scoped(getDb().from(
|
|
302
|
-
scoped(getDb().from(
|
|
303
|
-
scoped(getDb().from(
|
|
302
|
+
scoped(getDb().from(T.orders).select('total,platform_fee,net_value,financial_status')),
|
|
303
|
+
scoped(getDb().from(T.subscriptions).select('net_value,interval,status')),
|
|
304
|
+
scoped(getDb().from(T.payouts).select('amount,status')),
|
|
304
305
|
])
|
|
305
306
|
const orderRows: any[] = orders.data ?? []
|
|
306
307
|
const paid = orderRows.filter((o) => o.financial_status === 'paid')
|
|
@@ -324,13 +325,13 @@ export function createSupabaseCoursesProvider(): CoursesProvider {
|
|
|
324
325
|
}
|
|
325
326
|
},
|
|
326
327
|
async listPayouts(): Promise<Payout[]> {
|
|
327
|
-
const { data, error } = await scoped(getDb().from(
|
|
328
|
+
const { data, error } = await scoped(getDb().from(T.payouts).select('*')).order('created_at', { ascending: false })
|
|
328
329
|
if (error) throw error
|
|
329
330
|
return (data ?? []).map(rowToPayout)
|
|
330
331
|
},
|
|
331
332
|
async getCreatorAccount(): Promise<CreatorAccount> {
|
|
332
333
|
const tenantId = getTenantId()
|
|
333
|
-
const { data } = await scoped(getDb().from(
|
|
334
|
+
const { data } = await scoped(getDb().from(T.creatorAccounts).select('*')).maybeSingle()
|
|
334
335
|
return {
|
|
335
336
|
tenantId: tenantId ?? '',
|
|
336
337
|
stripeAccountId: data?.stripe_account_id ?? null,
|
package/src/tables.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Physical table names for @fayz-ai/courses. Renamed to the plg_courses_*
|
|
2
|
+
// industry-pool prefix (was course_*) when the core DB moved into the public
|
|
3
|
+
// schema. RPC function names (course_place_order, course_confirm_payment) are
|
|
4
|
+
// unchanged.
|
|
5
|
+
export const T = {
|
|
6
|
+
courses: 'plg_courses_courses',
|
|
7
|
+
modules: 'plg_courses_modules',
|
|
8
|
+
lessons: 'plg_courses_lessons',
|
|
9
|
+
offers: 'plg_courses_offers',
|
|
10
|
+
customers: 'plg_courses_customers',
|
|
11
|
+
enrollments: 'plg_courses_enrollments',
|
|
12
|
+
progress: 'plg_courses_progress',
|
|
13
|
+
orders: 'plg_courses_orders',
|
|
14
|
+
subscriptions: 'plg_courses_subscriptions',
|
|
15
|
+
creatorAccounts: 'plg_courses_creator_accounts',
|
|
16
|
+
payouts: 'plg_courses_payouts',
|
|
17
|
+
paymentEvents: 'plg_courses_payment_events',
|
|
18
|
+
} as const
|