@fayz-ai/courses 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +627 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +621 -3
- package/dist/index.js.map +1 -1
- package/dist/mock-provider.d.ts +17 -2
- package/dist/mock-provider.d.ts.map +1 -1
- package/dist/provider.d.ts +10 -1
- package/dist/provider.d.ts.map +1 -1
- package/dist/safe-provider.d.ts +9 -0
- package/dist/safe-provider.d.ts.map +1 -0
- package/dist/seed.d.ts +14 -1
- package/dist/seed.d.ts.map +1 -1
- package/dist/supabase-provider.d.ts +3 -0
- package/dist/supabase-provider.d.ts.map +1 -0
- package/dist/tenant.d.ts +4 -0
- package/dist/tenant.d.ts.map +1 -0
- package/dist/types.d.ts +112 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +16 -2
- package/src/mock-provider.ts +122 -1
- package/src/provider.ts +21 -0
- package/src/safe-provider.ts +17 -0
- package/src/seed.ts +152 -2
- package/src/supabase-provider.ts +344 -0
- package/src/tenant.ts +21 -0
- package/src/types.ts +131 -0
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createSafeDataProvider, getSupabaseClientOptional } from '@fayz-ai/core';
|
|
1
2
|
import { useState, useEffect, useCallback } from 'react';
|
|
2
3
|
|
|
3
4
|
// src/provider.ts
|
|
@@ -26,8 +27,48 @@ function buildCourseCatalog(input) {
|
|
|
26
27
|
const courses = [];
|
|
27
28
|
const modules = [];
|
|
28
29
|
const lessons = [];
|
|
30
|
+
const offers = [];
|
|
29
31
|
input.forEach((c, ci) => {
|
|
30
32
|
const courseId = `course-${String(ci + 1).padStart(2, "0")}`;
|
|
33
|
+
const price = c.price ?? 197;
|
|
34
|
+
offers.push({
|
|
35
|
+
id: `${courseId}-offer-01`,
|
|
36
|
+
courseId,
|
|
37
|
+
name: c.title,
|
|
38
|
+
price,
|
|
39
|
+
currency: "BRL",
|
|
40
|
+
kind: "one_time",
|
|
41
|
+
recurringInterval: null,
|
|
42
|
+
isDefault: true,
|
|
43
|
+
isOrderBump: false,
|
|
44
|
+
sortOrder: 0
|
|
45
|
+
});
|
|
46
|
+
if (ci === 0) {
|
|
47
|
+
offers.push({
|
|
48
|
+
id: `${courseId}-offer-02`,
|
|
49
|
+
courseId,
|
|
50
|
+
name: `${c.title} + Club (mensal)`,
|
|
51
|
+
price: Math.round(price * 0.24 * 100) / 100,
|
|
52
|
+
currency: "BRL",
|
|
53
|
+
kind: "subscription",
|
|
54
|
+
recurringInterval: "month",
|
|
55
|
+
isDefault: false,
|
|
56
|
+
isOrderBump: false,
|
|
57
|
+
sortOrder: 1
|
|
58
|
+
});
|
|
59
|
+
offers.push({
|
|
60
|
+
id: `${courseId}-offer-03`,
|
|
61
|
+
courseId,
|
|
62
|
+
name: "Templates & B\xF4nus (order-bump)",
|
|
63
|
+
price: 47,
|
|
64
|
+
currency: "BRL",
|
|
65
|
+
kind: "one_time",
|
|
66
|
+
recurringInterval: null,
|
|
67
|
+
isDefault: false,
|
|
68
|
+
isOrderBump: true,
|
|
69
|
+
sortOrder: 2
|
|
70
|
+
});
|
|
71
|
+
}
|
|
31
72
|
courses.push({
|
|
32
73
|
id: courseId,
|
|
33
74
|
slug: c.slug,
|
|
@@ -59,7 +100,79 @@ function buildCourseCatalog(input) {
|
|
|
59
100
|
});
|
|
60
101
|
});
|
|
61
102
|
});
|
|
62
|
-
return { courses, modules, lessons };
|
|
103
|
+
return { courses, modules, lessons, offers };
|
|
104
|
+
}
|
|
105
|
+
var DEMO_PLATFORM_FEE_BPS = 500;
|
|
106
|
+
function daysFromBase(days) {
|
|
107
|
+
return new Date(Date.parse(t0) + days * 864e5).toISOString();
|
|
108
|
+
}
|
|
109
|
+
var DEMO_BUYERS = [
|
|
110
|
+
{ name: "William Ribeiro", email: "william@example.com" },
|
|
111
|
+
{ name: "Alan Brancalh\xE3o", email: "alan@example.com" },
|
|
112
|
+
{ name: "Luciano Rufatto", email: "luciano@example.com" },
|
|
113
|
+
{ name: "Gabriel Boni", email: "gabriel@example.com" },
|
|
114
|
+
{ name: "Leonardo Lorenzoni", email: "leonardo@example.com" }
|
|
115
|
+
];
|
|
116
|
+
var DEMO_METHODS = ["pix", "card", "boleto"];
|
|
117
|
+
function feeFor(total) {
|
|
118
|
+
return Math.round(total * DEMO_PLATFORM_FEE_BPS / 1e4 * 100) / 100;
|
|
119
|
+
}
|
|
120
|
+
function buildDemoLedger(catalog) {
|
|
121
|
+
const orders = [];
|
|
122
|
+
const subscriptions = [];
|
|
123
|
+
const oneTime = catalog.offers.filter((o) => o.kind === "one_time" && !o.isOrderBump);
|
|
124
|
+
oneTime.forEach((offer, oi) => {
|
|
125
|
+
for (let i = 0; i < 3; i++) {
|
|
126
|
+
const buyer = DEMO_BUYERS[(oi * 3 + i) % DEMO_BUYERS.length];
|
|
127
|
+
const total = offer.price;
|
|
128
|
+
const platformFee = feeFor(total);
|
|
129
|
+
orders.push({
|
|
130
|
+
id: `order-${offer.courseId}-${String(i + 1).padStart(2, "0")}`,
|
|
131
|
+
courseId: offer.courseId,
|
|
132
|
+
offerId: offer.id,
|
|
133
|
+
customerId: `cust-${buyer.email}`,
|
|
134
|
+
customerName: buyer.name,
|
|
135
|
+
customerEmail: buyer.email,
|
|
136
|
+
currency: offer.currency,
|
|
137
|
+
total,
|
|
138
|
+
platformFee,
|
|
139
|
+
netValue: Math.round((total - platformFee) * 100) / 100,
|
|
140
|
+
paymentMethod: DEMO_METHODS[(oi + i) % DEMO_METHODS.length],
|
|
141
|
+
financialStatus: "paid",
|
|
142
|
+
stripePaymentIntentId: null,
|
|
143
|
+
createdAt: daysFromBase(160 + oi * 7 + i * 2)
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
const sub = catalog.offers.find((o) => o.kind === "subscription");
|
|
148
|
+
if (sub) {
|
|
149
|
+
for (let i = 0; i < 2; i++) {
|
|
150
|
+
const buyer = DEMO_BUYERS[(3 + i) % DEMO_BUYERS.length];
|
|
151
|
+
subscriptions.push({
|
|
152
|
+
id: `sub-${sub.courseId}-${String(i + 1).padStart(2, "0")}`,
|
|
153
|
+
courseId: sub.courseId,
|
|
154
|
+
offerId: sub.id,
|
|
155
|
+
customerId: `cust-${buyer.email}`,
|
|
156
|
+
customerName: buyer.name,
|
|
157
|
+
customerEmail: buyer.email,
|
|
158
|
+
currency: sub.currency,
|
|
159
|
+
netValue: Math.round((sub.price - feeFor(sub.price)) * 100) / 100,
|
|
160
|
+
interval: sub.recurringInterval ?? "month",
|
|
161
|
+
status: "active",
|
|
162
|
+
stripeSubscriptionId: null,
|
|
163
|
+
startedAt: daysFromBase(150 + i * 2),
|
|
164
|
+
canceledAt: null
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const payouts = [200, 216.33, 196.33, 801.33].map((amount, i) => ({
|
|
169
|
+
id: `payout-${String(i + 1).padStart(2, "0")}`,
|
|
170
|
+
amount,
|
|
171
|
+
currency: "BRL",
|
|
172
|
+
status: "paid",
|
|
173
|
+
createdAt: daysFromBase(120 + i * 20)
|
|
174
|
+
}));
|
|
175
|
+
return { orders, subscriptions, payouts };
|
|
63
176
|
}
|
|
64
177
|
var DEFAULT_COURSE_CATALOG = buildCourseCatalog([
|
|
65
178
|
{
|
|
@@ -121,6 +234,7 @@ var DEFAULT_COURSE_CATALOG = buildCourseCatalog([
|
|
|
121
234
|
]
|
|
122
235
|
}
|
|
123
236
|
]);
|
|
237
|
+
var DEFAULT_DEMO_LEDGER = buildDemoLedger(DEFAULT_COURSE_CATALOG);
|
|
124
238
|
|
|
125
239
|
// src/mock-provider.ts
|
|
126
240
|
function uid() {
|
|
@@ -137,7 +251,11 @@ var K = {
|
|
|
137
251
|
modules: "fayz.courses.mock.modules.v1",
|
|
138
252
|
lessons: "fayz.courses.mock.lessons.v1",
|
|
139
253
|
enrollments: "fayz.courses.mock.enrollments.v1",
|
|
140
|
-
progress: "fayz.courses.mock.progress.v1"
|
|
254
|
+
progress: "fayz.courses.mock.progress.v1",
|
|
255
|
+
offers: "fayz.courses.mock.offers.v1",
|
|
256
|
+
orders: "fayz.courses.mock.orders.v1",
|
|
257
|
+
subscriptions: "fayz.courses.mock.subscriptions.v1",
|
|
258
|
+
payouts: "fayz.courses.mock.payouts.v1"
|
|
141
259
|
};
|
|
142
260
|
function load(key) {
|
|
143
261
|
try {
|
|
@@ -157,10 +275,15 @@ function save(key, value) {
|
|
|
157
275
|
var MockCoursesProvider = class {
|
|
158
276
|
constructor(seed) {
|
|
159
277
|
const catalog = seed?.catalog ?? DEFAULT_COURSE_CATALOG;
|
|
278
|
+
const ledger = seed?.ledger ?? DEFAULT_DEMO_LEDGER;
|
|
160
279
|
if (load(K.courses) == null) {
|
|
161
280
|
save(K.courses, catalog.courses);
|
|
162
281
|
save(K.modules, catalog.modules);
|
|
163
282
|
save(K.lessons, catalog.lessons);
|
|
283
|
+
save(K.offers, catalog.offers);
|
|
284
|
+
save(K.orders, ledger.orders);
|
|
285
|
+
save(K.subscriptions, ledger.subscriptions);
|
|
286
|
+
save(K.payouts, ledger.payouts);
|
|
164
287
|
}
|
|
165
288
|
if (seed?.enrollAll?.length) {
|
|
166
289
|
const courses = load(K.courses) ?? [];
|
|
@@ -192,6 +315,18 @@ var MockCoursesProvider = class {
|
|
|
192
315
|
progress() {
|
|
193
316
|
return load(K.progress) ?? [];
|
|
194
317
|
}
|
|
318
|
+
offers() {
|
|
319
|
+
return load(K.offers) ?? [];
|
|
320
|
+
}
|
|
321
|
+
orders() {
|
|
322
|
+
return load(K.orders) ?? [];
|
|
323
|
+
}
|
|
324
|
+
subscriptions() {
|
|
325
|
+
return load(K.subscriptions) ?? [];
|
|
326
|
+
}
|
|
327
|
+
payouts() {
|
|
328
|
+
return load(K.payouts) ?? [];
|
|
329
|
+
}
|
|
195
330
|
// Courses -----------------------------------------------------------------
|
|
196
331
|
async listCourses(opts) {
|
|
197
332
|
let list = this.courses();
|
|
@@ -331,10 +466,493 @@ var MockCoursesProvider = class {
|
|
|
331
466
|
save(K.progress, [...list, created]);
|
|
332
467
|
return created;
|
|
333
468
|
}
|
|
469
|
+
// Offers ------------------------------------------------------------------
|
|
470
|
+
async listOffers(courseId) {
|
|
471
|
+
return this.offers().filter((o) => o.courseId === courseId).sort((a, b) => a.sortOrder - b.sortOrder);
|
|
472
|
+
}
|
|
473
|
+
async createOffer(input) {
|
|
474
|
+
const list = this.offers();
|
|
475
|
+
const offer = {
|
|
476
|
+
id: uid(),
|
|
477
|
+
courseId: input.courseId,
|
|
478
|
+
name: input.name,
|
|
479
|
+
price: input.price,
|
|
480
|
+
currency: input.currency ?? "BRL",
|
|
481
|
+
kind: input.kind ?? "one_time",
|
|
482
|
+
recurringInterval: input.recurringInterval ?? (input.kind === "subscription" ? "month" : null),
|
|
483
|
+
isDefault: input.isDefault ?? false,
|
|
484
|
+
isOrderBump: input.isOrderBump ?? false,
|
|
485
|
+
sortOrder: input.sortOrder ?? list.filter((o) => o.courseId === input.courseId).length
|
|
486
|
+
};
|
|
487
|
+
save(K.offers, [...list, offer]);
|
|
488
|
+
return offer;
|
|
489
|
+
}
|
|
490
|
+
async updateOffer(id, input) {
|
|
491
|
+
const list = this.offers();
|
|
492
|
+
const idx = list.findIndex((o) => o.id === id);
|
|
493
|
+
if (idx < 0) throw new Error("Offer not found");
|
|
494
|
+
list[idx] = { ...list[idx], ...input };
|
|
495
|
+
save(K.offers, list);
|
|
496
|
+
return list[idx];
|
|
497
|
+
}
|
|
498
|
+
async deleteOffer(id) {
|
|
499
|
+
save(K.offers, this.offers().filter((o) => o.id !== id));
|
|
500
|
+
}
|
|
501
|
+
// Sales ledger ------------------------------------------------------------
|
|
502
|
+
async listOrders(opts) {
|
|
503
|
+
let list = this.orders();
|
|
504
|
+
if (opts?.courseId) list = list.filter((o) => o.courseId === opts.courseId);
|
|
505
|
+
if (opts?.financialStatus) list = list.filter((o) => o.financialStatus === opts.financialStatus);
|
|
506
|
+
if (opts?.search) {
|
|
507
|
+
const q = opts.search.toLowerCase();
|
|
508
|
+
list = list.filter(
|
|
509
|
+
(o) => (o.customerName ?? "").toLowerCase().includes(q) || (o.customerEmail ?? "").toLowerCase().includes(q)
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
return list.slice().sort((a, b) => a.createdAt < b.createdAt ? 1 : -1);
|
|
513
|
+
}
|
|
514
|
+
// Subscriptions -----------------------------------------------------------
|
|
515
|
+
async listSubscriptions(opts) {
|
|
516
|
+
let list = this.subscriptions();
|
|
517
|
+
if (opts?.courseId) list = list.filter((s) => s.courseId === opts.courseId);
|
|
518
|
+
if (opts?.status) list = list.filter((s) => s.status === opts.status);
|
|
519
|
+
return list.slice().sort((a, b) => a.startedAt < b.startedAt ? 1 : -1);
|
|
520
|
+
}
|
|
521
|
+
// Financial / payouts / connect -------------------------------------------
|
|
522
|
+
async getFinancialSummary() {
|
|
523
|
+
const paid = this.orders().filter((o) => o.financialStatus === "paid");
|
|
524
|
+
const refunded = this.orders().filter((o) => o.financialStatus === "refunded");
|
|
525
|
+
const activeSubs = this.subscriptions().filter((s) => s.status === "active");
|
|
526
|
+
const totalRevenue = round2(paid.reduce((s, o) => s + o.total, 0));
|
|
527
|
+
const platformFeeTotal = round2(paid.reduce((s, o) => s + o.platformFee, 0));
|
|
528
|
+
const netTotal = round2(paid.reduce((s, o) => s + o.netValue, 0));
|
|
529
|
+
const paidOut = round2(this.payouts().filter((p) => p.status === "paid").reduce((s, p) => s + p.amount, 0));
|
|
530
|
+
const mrr = round2(activeSubs.reduce((s, sub) => s + (sub.interval === "year" ? sub.netValue / 12 : sub.netValue), 0));
|
|
531
|
+
const totalCount = paid.length + refunded.length;
|
|
532
|
+
return {
|
|
533
|
+
currency: "BRL",
|
|
534
|
+
availableBalance: round2(Math.max(netTotal - paidOut, 0)),
|
|
535
|
+
pendingBalance: round2(this.orders().filter((o) => o.financialStatus === "pending").reduce((s, o) => s + o.netValue, 0)),
|
|
536
|
+
totalRevenue,
|
|
537
|
+
platformFeeTotal,
|
|
538
|
+
salesCount: paid.length,
|
|
539
|
+
activeSubscriptions: activeSubs.length,
|
|
540
|
+
mrr,
|
|
541
|
+
refundRate: totalCount > 0 ? refunded.length / totalCount : 0,
|
|
542
|
+
conversionRate: 0
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
async listPayouts() {
|
|
546
|
+
return this.payouts().slice().sort((a, b) => a.createdAt < b.createdAt ? 1 : -1);
|
|
547
|
+
}
|
|
548
|
+
async getCreatorAccount() {
|
|
549
|
+
return {
|
|
550
|
+
tenantId: "mock-tenant",
|
|
551
|
+
stripeAccountId: null,
|
|
552
|
+
chargesEnabled: false,
|
|
553
|
+
payoutsEnabled: false,
|
|
554
|
+
defaultCurrency: "BRL",
|
|
555
|
+
platformFeeBps: DEMO_PLATFORM_FEE_BPS
|
|
556
|
+
};
|
|
557
|
+
}
|
|
334
558
|
};
|
|
559
|
+
function round2(n) {
|
|
560
|
+
return Math.round(n * 100) / 100;
|
|
561
|
+
}
|
|
335
562
|
function createMockCoursesProvider(seed) {
|
|
336
563
|
return new MockCoursesProvider(seed);
|
|
337
564
|
}
|
|
565
|
+
|
|
566
|
+
// src/tenant.ts
|
|
567
|
+
var _resolver = null;
|
|
568
|
+
function setCoursesTenantResolver(resolver) {
|
|
569
|
+
_resolver = resolver;
|
|
570
|
+
}
|
|
571
|
+
function getCoursesTenantId() {
|
|
572
|
+
if (!_resolver) return void 0;
|
|
573
|
+
try {
|
|
574
|
+
return _resolver();
|
|
575
|
+
} catch {
|
|
576
|
+
return void 0;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
// src/supabase-provider.ts
|
|
581
|
+
function getDb() {
|
|
582
|
+
const supabase = getSupabaseClientOptional();
|
|
583
|
+
if (!supabase) throw new Error("@fayz-ai/courses: Supabase client not initialized. Call setGlobalSupabaseClient() first.");
|
|
584
|
+
return supabase;
|
|
585
|
+
}
|
|
586
|
+
var getTenantId = getCoursesTenantId;
|
|
587
|
+
function slugify2(s) {
|
|
588
|
+
return s.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
|
|
589
|
+
}
|
|
590
|
+
function round22(n) {
|
|
591
|
+
return Math.round(n * 100) / 100;
|
|
592
|
+
}
|
|
593
|
+
function rowToCourse(r) {
|
|
594
|
+
return {
|
|
595
|
+
id: r.id,
|
|
596
|
+
slug: r.slug,
|
|
597
|
+
title: r.title,
|
|
598
|
+
subtitle: r.subtitle ?? null,
|
|
599
|
+
description: r.description ?? null,
|
|
600
|
+
thumbnailUrl: r.thumbnail_url ?? null,
|
|
601
|
+
price: r.price ?? 0,
|
|
602
|
+
currency: r.currency ?? "BRL",
|
|
603
|
+
status: r.status ?? "draft",
|
|
604
|
+
sortOrder: r.sort_order ?? 0,
|
|
605
|
+
createdAt: r.created_at,
|
|
606
|
+
updatedAt: r.updated_at
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
function rowToModule(r) {
|
|
610
|
+
return { id: r.id, courseId: r.course_id, title: r.title, sortOrder: r.sort_order ?? 0 };
|
|
611
|
+
}
|
|
612
|
+
function rowToLesson(r) {
|
|
613
|
+
return {
|
|
614
|
+
id: r.id,
|
|
615
|
+
courseId: r.course_id,
|
|
616
|
+
moduleId: r.module_id,
|
|
617
|
+
title: r.title,
|
|
618
|
+
description: r.description ?? null,
|
|
619
|
+
videoUrl: r.video_url ?? "",
|
|
620
|
+
durationSec: r.duration_sec ?? 600,
|
|
621
|
+
sortOrder: r.sort_order ?? 0
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
function rowToOffer(r) {
|
|
625
|
+
return {
|
|
626
|
+
id: r.id,
|
|
627
|
+
courseId: r.course_id,
|
|
628
|
+
name: r.name,
|
|
629
|
+
price: r.price ?? 0,
|
|
630
|
+
currency: r.currency ?? "BRL",
|
|
631
|
+
kind: r.kind ?? "one_time",
|
|
632
|
+
recurringInterval: r.recurring_interval ?? null,
|
|
633
|
+
isDefault: r.is_default ?? false,
|
|
634
|
+
isOrderBump: r.is_order_bump ?? false,
|
|
635
|
+
sortOrder: r.sort_order ?? 0
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
function rowToEnrollment(r) {
|
|
639
|
+
return { id: r.id, courseId: r.course_id, customerId: r.customer_id, status: r.status ?? "active", enrolledAt: r.enrolled_at };
|
|
640
|
+
}
|
|
641
|
+
function rowToProgress(r) {
|
|
642
|
+
return {
|
|
643
|
+
id: r.id,
|
|
644
|
+
enrollmentId: r.enrollment_id,
|
|
645
|
+
lessonId: r.lesson_id,
|
|
646
|
+
completed: r.completed ?? false,
|
|
647
|
+
lastPositionSec: r.last_position_sec ?? 0,
|
|
648
|
+
completedAt: r.completed_at ?? null
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
function rowToOrder(r) {
|
|
652
|
+
return {
|
|
653
|
+
id: r.id,
|
|
654
|
+
courseId: r.course_id,
|
|
655
|
+
offerId: r.offer_id ?? null,
|
|
656
|
+
customerId: r.customer_id ?? null,
|
|
657
|
+
customerName: r.customer_name ?? null,
|
|
658
|
+
customerEmail: r.customer_email ?? null,
|
|
659
|
+
currency: r.currency ?? "BRL",
|
|
660
|
+
total: r.total ?? 0,
|
|
661
|
+
platformFee: r.platform_fee ?? 0,
|
|
662
|
+
netValue: r.net_value ?? 0,
|
|
663
|
+
paymentMethod: r.payment_method ?? null,
|
|
664
|
+
financialStatus: r.financial_status ?? "pending",
|
|
665
|
+
stripePaymentIntentId: r.stripe_payment_intent_id ?? null,
|
|
666
|
+
createdAt: r.created_at
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
function rowToSubscription(r) {
|
|
670
|
+
return {
|
|
671
|
+
id: r.id,
|
|
672
|
+
courseId: r.course_id,
|
|
673
|
+
offerId: r.offer_id ?? null,
|
|
674
|
+
customerId: r.customer_id ?? null,
|
|
675
|
+
customerName: r.customer_name ?? null,
|
|
676
|
+
customerEmail: r.customer_email ?? null,
|
|
677
|
+
currency: r.currency ?? "BRL",
|
|
678
|
+
netValue: r.net_value ?? 0,
|
|
679
|
+
interval: r.interval ?? "month",
|
|
680
|
+
status: r.status ?? "active",
|
|
681
|
+
stripeSubscriptionId: r.stripe_subscription_id ?? null,
|
|
682
|
+
startedAt: r.started_at,
|
|
683
|
+
canceledAt: r.canceled_at ?? null
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
function rowToPayout(r) {
|
|
687
|
+
return { id: r.id, amount: r.amount ?? 0, currency: r.currency ?? "BRL", status: r.status ?? "pending", createdAt: r.created_at };
|
|
688
|
+
}
|
|
689
|
+
function createSupabaseCoursesProvider() {
|
|
690
|
+
const scoped = (q) => {
|
|
691
|
+
const tenantId = getTenantId();
|
|
692
|
+
return tenantId ? q.eq("tenant_id", tenantId) : q;
|
|
693
|
+
};
|
|
694
|
+
return {
|
|
695
|
+
// Courses ---------------------------------------------------------------
|
|
696
|
+
async listCourses(opts) {
|
|
697
|
+
let q = scoped(getDb().from("course_courses").select("*"));
|
|
698
|
+
if (opts?.status) q = q.eq("status", opts.status);
|
|
699
|
+
if (opts?.slug) q = q.eq("slug", opts.slug);
|
|
700
|
+
if (opts?.search) q = q.ilike("title", `%${opts.search}%`);
|
|
701
|
+
const { data, error } = await q.order("sort_order", { ascending: true });
|
|
702
|
+
if (error) throw error;
|
|
703
|
+
return (data ?? []).map(rowToCourse);
|
|
704
|
+
},
|
|
705
|
+
async getCourse(idOrSlug) {
|
|
706
|
+
const col = /^[0-9a-f-]{36}$/i.test(idOrSlug) ? "id" : "slug";
|
|
707
|
+
const { data, error } = await scoped(getDb().from("course_courses").select("*").eq(col, idOrSlug)).maybeSingle();
|
|
708
|
+
if (error) throw error;
|
|
709
|
+
return data ? rowToCourse(data) : null;
|
|
710
|
+
},
|
|
711
|
+
async createCourse(input) {
|
|
712
|
+
const { data, error } = await getDb().from("course_courses").insert({
|
|
713
|
+
tenant_id: getTenantId(),
|
|
714
|
+
slug: input.slug ?? slugify2(input.title),
|
|
715
|
+
title: input.title,
|
|
716
|
+
subtitle: input.subtitle ?? null,
|
|
717
|
+
description: input.description ?? null,
|
|
718
|
+
thumbnail_url: input.thumbnailUrl ?? null,
|
|
719
|
+
price: input.price ?? 0,
|
|
720
|
+
currency: input.currency ?? "BRL",
|
|
721
|
+
status: input.status ?? "draft",
|
|
722
|
+
sort_order: input.sortOrder ?? 0
|
|
723
|
+
}).select("*").single();
|
|
724
|
+
if (error) throw error;
|
|
725
|
+
return rowToCourse(data);
|
|
726
|
+
},
|
|
727
|
+
async updateCourse(id, input) {
|
|
728
|
+
const patch = {};
|
|
729
|
+
if (input.title !== void 0) patch.title = input.title;
|
|
730
|
+
if (input.slug !== void 0) patch.slug = input.slug;
|
|
731
|
+
if (input.subtitle !== void 0) patch.subtitle = input.subtitle;
|
|
732
|
+
if (input.description !== void 0) patch.description = input.description;
|
|
733
|
+
if (input.thumbnailUrl !== void 0) patch.thumbnail_url = input.thumbnailUrl;
|
|
734
|
+
if (input.price !== void 0) patch.price = input.price;
|
|
735
|
+
if (input.currency !== void 0) patch.currency = input.currency;
|
|
736
|
+
if (input.status !== void 0) patch.status = input.status;
|
|
737
|
+
if (input.sortOrder !== void 0) patch.sort_order = input.sortOrder;
|
|
738
|
+
const { data, error } = await scoped(getDb().from("course_courses").update(patch).eq("id", id)).select("*").single();
|
|
739
|
+
if (error) throw error;
|
|
740
|
+
return rowToCourse(data);
|
|
741
|
+
},
|
|
742
|
+
async deleteCourse(id) {
|
|
743
|
+
const { error } = await scoped(getDb().from("course_courses").delete().eq("id", id));
|
|
744
|
+
if (error) throw error;
|
|
745
|
+
},
|
|
746
|
+
// Modules ---------------------------------------------------------------
|
|
747
|
+
async listModules(courseId) {
|
|
748
|
+
const { data, error } = await scoped(getDb().from("course_modules").select("*").eq("course_id", courseId)).order("sort_order", { ascending: true });
|
|
749
|
+
if (error) throw error;
|
|
750
|
+
return (data ?? []).map(rowToModule);
|
|
751
|
+
},
|
|
752
|
+
async createModule(input) {
|
|
753
|
+
const { data, error } = await getDb().from("course_modules").insert({
|
|
754
|
+
tenant_id: getTenantId(),
|
|
755
|
+
course_id: input.courseId,
|
|
756
|
+
title: input.title,
|
|
757
|
+
sort_order: input.sortOrder ?? 0
|
|
758
|
+
}).select("*").single();
|
|
759
|
+
if (error) throw error;
|
|
760
|
+
return rowToModule(data);
|
|
761
|
+
},
|
|
762
|
+
async updateModule(id, input) {
|
|
763
|
+
const patch = {};
|
|
764
|
+
if (input.title !== void 0) patch.title = input.title;
|
|
765
|
+
if (input.sortOrder !== void 0) patch.sort_order = input.sortOrder;
|
|
766
|
+
const { data, error } = await scoped(getDb().from("course_modules").update(patch).eq("id", id)).select("*").single();
|
|
767
|
+
if (error) throw error;
|
|
768
|
+
return rowToModule(data);
|
|
769
|
+
},
|
|
770
|
+
async deleteModule(id) {
|
|
771
|
+
const { error } = await scoped(getDb().from("course_modules").delete().eq("id", id));
|
|
772
|
+
if (error) throw error;
|
|
773
|
+
},
|
|
774
|
+
// Lessons ---------------------------------------------------------------
|
|
775
|
+
async listLessons(courseId) {
|
|
776
|
+
const { data, error } = await scoped(getDb().from("course_lessons").select("*").eq("course_id", courseId)).order("sort_order", { ascending: true });
|
|
777
|
+
if (error) throw error;
|
|
778
|
+
return (data ?? []).map(rowToLesson);
|
|
779
|
+
},
|
|
780
|
+
async getLesson(id) {
|
|
781
|
+
const { data, error } = await scoped(getDb().from("course_lessons").select("*").eq("id", id)).maybeSingle();
|
|
782
|
+
if (error) throw error;
|
|
783
|
+
return data ? rowToLesson(data) : null;
|
|
784
|
+
},
|
|
785
|
+
async createLesson(input) {
|
|
786
|
+
const { data, error } = await getDb().from("course_lessons").insert({
|
|
787
|
+
tenant_id: getTenantId(),
|
|
788
|
+
course_id: input.courseId,
|
|
789
|
+
module_id: input.moduleId,
|
|
790
|
+
title: input.title,
|
|
791
|
+
description: input.description ?? null,
|
|
792
|
+
video_url: input.videoUrl,
|
|
793
|
+
duration_sec: input.durationSec ?? 600,
|
|
794
|
+
sort_order: input.sortOrder ?? 0
|
|
795
|
+
}).select("*").single();
|
|
796
|
+
if (error) throw error;
|
|
797
|
+
return rowToLesson(data);
|
|
798
|
+
},
|
|
799
|
+
async updateLesson(id, input) {
|
|
800
|
+
const patch = {};
|
|
801
|
+
if (input.title !== void 0) patch.title = input.title;
|
|
802
|
+
if (input.description !== void 0) patch.description = input.description;
|
|
803
|
+
if (input.videoUrl !== void 0) patch.video_url = input.videoUrl;
|
|
804
|
+
if (input.durationSec !== void 0) patch.duration_sec = input.durationSec;
|
|
805
|
+
if (input.sortOrder !== void 0) patch.sort_order = input.sortOrder;
|
|
806
|
+
const { data, error } = await scoped(getDb().from("course_lessons").update(patch).eq("id", id)).select("*").single();
|
|
807
|
+
if (error) throw error;
|
|
808
|
+
return rowToLesson(data);
|
|
809
|
+
},
|
|
810
|
+
async deleteLesson(id) {
|
|
811
|
+
const { error } = await scoped(getDb().from("course_lessons").delete().eq("id", id));
|
|
812
|
+
if (error) throw error;
|
|
813
|
+
},
|
|
814
|
+
// Enrollments + progress ------------------------------------------------
|
|
815
|
+
async listEnrollments(customerId) {
|
|
816
|
+
const { data, error } = await getDb().from("course_enrollments").select("*").eq("customer_id", customerId);
|
|
817
|
+
if (error) throw error;
|
|
818
|
+
return (data ?? []).map(rowToEnrollment);
|
|
819
|
+
},
|
|
820
|
+
async enroll(courseId, customerId) {
|
|
821
|
+
const { data, error } = await getDb().from("course_enrollments").upsert(
|
|
822
|
+
{ tenant_id: getTenantId(), course_id: courseId, customer_id: customerId, status: "active" },
|
|
823
|
+
{ onConflict: "course_id,customer_id" }
|
|
824
|
+
).select("*").single();
|
|
825
|
+
if (error) throw error;
|
|
826
|
+
return rowToEnrollment(data);
|
|
827
|
+
},
|
|
828
|
+
async listProgress(enrollmentId) {
|
|
829
|
+
const { data, error } = await getDb().from("course_progress").select("*").eq("enrollment_id", enrollmentId);
|
|
830
|
+
if (error) throw error;
|
|
831
|
+
return (data ?? []).map(rowToProgress);
|
|
832
|
+
},
|
|
833
|
+
async setLessonProgress(enrollmentId, lessonId, patch) {
|
|
834
|
+
const row = { tenant_id: getTenantId(), enrollment_id: enrollmentId, lesson_id: lessonId };
|
|
835
|
+
if (patch.completed !== void 0) {
|
|
836
|
+
row.completed = patch.completed;
|
|
837
|
+
row.completed_at = patch.completed ? (/* @__PURE__ */ new Date()).toISOString() : null;
|
|
838
|
+
}
|
|
839
|
+
if (patch.lastPositionSec !== void 0) row.last_position_sec = patch.lastPositionSec;
|
|
840
|
+
const { data, error } = await getDb().from("course_progress").upsert(row, { onConflict: "enrollment_id,lesson_id" }).select("*").single();
|
|
841
|
+
if (error) throw error;
|
|
842
|
+
return rowToProgress(data);
|
|
843
|
+
},
|
|
844
|
+
// Offers ----------------------------------------------------------------
|
|
845
|
+
async listOffers(courseId) {
|
|
846
|
+
const { data, error } = await scoped(getDb().from("course_offers").select("*").eq("course_id", courseId)).order("sort_order", { ascending: true });
|
|
847
|
+
if (error) throw error;
|
|
848
|
+
return (data ?? []).map(rowToOffer);
|
|
849
|
+
},
|
|
850
|
+
async createOffer(input) {
|
|
851
|
+
const { data, error } = await getDb().from("course_offers").insert({
|
|
852
|
+
tenant_id: getTenantId(),
|
|
853
|
+
course_id: input.courseId,
|
|
854
|
+
name: input.name,
|
|
855
|
+
price: input.price,
|
|
856
|
+
currency: input.currency ?? "BRL",
|
|
857
|
+
kind: input.kind ?? "one_time",
|
|
858
|
+
recurring_interval: input.recurringInterval ?? (input.kind === "subscription" ? "month" : null),
|
|
859
|
+
is_default: input.isDefault ?? false,
|
|
860
|
+
is_order_bump: input.isOrderBump ?? false,
|
|
861
|
+
sort_order: input.sortOrder ?? 0
|
|
862
|
+
}).select("*").single();
|
|
863
|
+
if (error) throw error;
|
|
864
|
+
return rowToOffer(data);
|
|
865
|
+
},
|
|
866
|
+
async updateOffer(id, input) {
|
|
867
|
+
const patch = {};
|
|
868
|
+
if (input.name !== void 0) patch.name = input.name;
|
|
869
|
+
if (input.price !== void 0) patch.price = input.price;
|
|
870
|
+
if (input.currency !== void 0) patch.currency = input.currency;
|
|
871
|
+
if (input.kind !== void 0) patch.kind = input.kind;
|
|
872
|
+
if (input.recurringInterval !== void 0) patch.recurring_interval = input.recurringInterval;
|
|
873
|
+
if (input.isDefault !== void 0) patch.is_default = input.isDefault;
|
|
874
|
+
if (input.isOrderBump !== void 0) patch.is_order_bump = input.isOrderBump;
|
|
875
|
+
if (input.sortOrder !== void 0) patch.sort_order = input.sortOrder;
|
|
876
|
+
const { data, error } = await scoped(getDb().from("course_offers").update(patch).eq("id", id)).select("*").single();
|
|
877
|
+
if (error) throw error;
|
|
878
|
+
return rowToOffer(data);
|
|
879
|
+
},
|
|
880
|
+
async deleteOffer(id) {
|
|
881
|
+
const { error } = await scoped(getDb().from("course_offers").delete().eq("id", id));
|
|
882
|
+
if (error) throw error;
|
|
883
|
+
},
|
|
884
|
+
// Sales -----------------------------------------------------------------
|
|
885
|
+
async listOrders(opts) {
|
|
886
|
+
let q = scoped(getDb().from("course_orders").select("*"));
|
|
887
|
+
if (opts?.courseId) q = q.eq("course_id", opts.courseId);
|
|
888
|
+
if (opts?.financialStatus) q = q.eq("financial_status", opts.financialStatus);
|
|
889
|
+
if (opts?.search) q = q.or(`customer_name.ilike.%${opts.search}%,customer_email.ilike.%${opts.search}%`);
|
|
890
|
+
const { data, error } = await q.order("created_at", { ascending: false });
|
|
891
|
+
if (error) throw error;
|
|
892
|
+
return (data ?? []).map(rowToOrder);
|
|
893
|
+
},
|
|
894
|
+
// Subscriptions ---------------------------------------------------------
|
|
895
|
+
async listSubscriptions(opts) {
|
|
896
|
+
let q = scoped(getDb().from("course_subscriptions").select("*"));
|
|
897
|
+
if (opts?.courseId) q = q.eq("course_id", opts.courseId);
|
|
898
|
+
if (opts?.status) q = q.eq("status", opts.status);
|
|
899
|
+
const { data, error } = await q.order("started_at", { ascending: false });
|
|
900
|
+
if (error) throw error;
|
|
901
|
+
return (data ?? []).map(rowToSubscription);
|
|
902
|
+
},
|
|
903
|
+
// Financial -------------------------------------------------------------
|
|
904
|
+
async getFinancialSummary() {
|
|
905
|
+
const [orders, subs, payouts] = await Promise.all([
|
|
906
|
+
scoped(getDb().from("course_orders").select("total,platform_fee,net_value,financial_status")),
|
|
907
|
+
scoped(getDb().from("course_subscriptions").select("net_value,interval,status")),
|
|
908
|
+
scoped(getDb().from("course_payouts").select("amount,status"))
|
|
909
|
+
]);
|
|
910
|
+
const orderRows = orders.data ?? [];
|
|
911
|
+
const paid = orderRows.filter((o) => o.financial_status === "paid");
|
|
912
|
+
const refunded = orderRows.filter((o) => o.financial_status === "refunded");
|
|
913
|
+
const activeSubs = (subs.data ?? []).filter((s) => s.status === "active");
|
|
914
|
+
const netTotal = round22(paid.reduce((s, o) => s + Number(o.net_value ?? 0), 0));
|
|
915
|
+
const paidOut = round22((payouts.data ?? []).filter((p) => p.status === "paid").reduce((s, p) => s + Number(p.amount ?? 0), 0));
|
|
916
|
+
const mrr = round22(activeSubs.reduce((s, sub) => s + (sub.interval === "year" ? Number(sub.net_value) / 12 : Number(sub.net_value)), 0));
|
|
917
|
+
const totalCount = paid.length + refunded.length;
|
|
918
|
+
return {
|
|
919
|
+
currency: "BRL",
|
|
920
|
+
availableBalance: round22(Math.max(netTotal - paidOut, 0)),
|
|
921
|
+
pendingBalance: round22(orderRows.filter((o) => o.financial_status === "pending").reduce((s, o) => s + Number(o.net_value ?? 0), 0)),
|
|
922
|
+
totalRevenue: round22(paid.reduce((s, o) => s + Number(o.total ?? 0), 0)),
|
|
923
|
+
platformFeeTotal: round22(paid.reduce((s, o) => s + Number(o.platform_fee ?? 0), 0)),
|
|
924
|
+
salesCount: paid.length,
|
|
925
|
+
activeSubscriptions: activeSubs.length,
|
|
926
|
+
mrr,
|
|
927
|
+
refundRate: totalCount > 0 ? refunded.length / totalCount : 0,
|
|
928
|
+
conversionRate: 0
|
|
929
|
+
};
|
|
930
|
+
},
|
|
931
|
+
async listPayouts() {
|
|
932
|
+
const { data, error } = await scoped(getDb().from("course_payouts").select("*")).order("created_at", { ascending: false });
|
|
933
|
+
if (error) throw error;
|
|
934
|
+
return (data ?? []).map(rowToPayout);
|
|
935
|
+
},
|
|
936
|
+
async getCreatorAccount() {
|
|
937
|
+
const tenantId = getTenantId();
|
|
938
|
+
const { data } = await scoped(getDb().from("course_creator_accounts").select("*")).maybeSingle();
|
|
939
|
+
return {
|
|
940
|
+
tenantId: tenantId ?? "",
|
|
941
|
+
stripeAccountId: data?.stripe_account_id ?? null,
|
|
942
|
+
chargesEnabled: data?.charges_enabled ?? false,
|
|
943
|
+
payoutsEnabled: data?.payouts_enabled ?? false,
|
|
944
|
+
defaultCurrency: data?.default_currency ?? "BRL",
|
|
945
|
+
platformFeeBps: data?.platform_fee_bps ?? 500
|
|
946
|
+
};
|
|
947
|
+
}
|
|
948
|
+
};
|
|
949
|
+
}
|
|
950
|
+
function createSafeCoursesProvider() {
|
|
951
|
+
return createSafeDataProvider(
|
|
952
|
+
() => createSupabaseCoursesProvider(),
|
|
953
|
+
() => createMockCoursesProvider()
|
|
954
|
+
);
|
|
955
|
+
}
|
|
338
956
|
function useMyCourses(customerId) {
|
|
339
957
|
const [courses, setCourses] = useState([]);
|
|
340
958
|
const [loading, setLoading] = useState(false);
|
|
@@ -438,6 +1056,6 @@ function useCourseProgress(enrollmentId) {
|
|
|
438
1056
|
};
|
|
439
1057
|
}
|
|
440
1058
|
|
|
441
|
-
export { DEFAULT_COURSE_CATALOG, MockCoursesProvider, buildCourseCatalog, createMockCoursesProvider, getCoursesProvider, getCoursesProviderOptional, setCoursesProvider, useCourse, useCourseProgress, useMyCourses };
|
|
1059
|
+
export { DEFAULT_COURSE_CATALOG, DEFAULT_DEMO_LEDGER, DEMO_PLATFORM_FEE_BPS, MockCoursesProvider, buildCourseCatalog, buildDemoLedger, createMockCoursesProvider, createSafeCoursesProvider, createSupabaseCoursesProvider, getCoursesProvider, getCoursesProviderOptional, getCoursesTenantId, setCoursesProvider, setCoursesTenantResolver, useCourse, useCourseProgress, useMyCourses };
|
|
442
1060
|
//# sourceMappingURL=index.js.map
|
|
443
1061
|
//# sourceMappingURL=index.js.map
|