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