@fayz-ai/courses 0.7.1 → 0.8.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/README.md CHANGED
@@ -5,6 +5,8 @@
5
5
  [![npm](https://img.shields.io/npm/v/@fayz-ai/courses.svg)](https://www.npmjs.com/package/@fayz-ai/courses)
6
6
  [![license](https://img.shields.io/npm/l/@fayz-ai/courses.svg)](https://github.com/FayaLabs/fayz-sdk/blob/main/LICENSE)
7
7
 
8
+ **Status:** beta — published to npm and used across Fayz dogfood apps. Pre-1.0: minor APIs may change before 1.0.
9
+
8
10
  `@fayz-ai/courses` is the headless learning engine: a `CoursesProvider` contract plus the domain types and hooks for courses, modules, lessons, enrollments, and progress tracking. It owns *what learning means* so course-admin and member surfaces only have to render it — the education-side mirror of how `@fayz-ai/shop` powers commerce.
9
11
 
10
12
  Provider-first: build on `createMockCoursesProvider`, then swap in your own `CoursesProvider` for real data without touching the UI. A catalog seed builder gets a demo running in minutes.
package/dist/index.cjs CHANGED
@@ -81,6 +81,7 @@ function buildCourseCatalog(input) {
81
81
  price: c.price ?? 197,
82
82
  currency: "BRL",
83
83
  status: "published",
84
+ deliveryType: "online",
84
85
  sortOrder: ci,
85
86
  createdAt: t0,
86
87
  updatedAt: t0
@@ -355,6 +356,7 @@ var MockCoursesProvider = class {
355
356
  price: input.price ?? 0,
356
357
  currency: input.currency ?? "BRL",
357
358
  status: input.status ?? "draft",
359
+ deliveryType: input.deliveryType ?? "online",
358
360
  sortOrder: input.sortOrder ?? list.length,
359
361
  createdAt: now(),
360
362
  updatedAt: now()
@@ -513,6 +515,16 @@ var MockCoursesProvider = class {
513
515
  }
514
516
  return list.slice().sort((a, b) => a.createdAt < b.createdAt ? 1 : -1);
515
517
  }
518
+ // Core-commerce course sales don't exist in the mock layer (no orders/financial).
519
+ async listCoreSales() {
520
+ return [];
521
+ }
522
+ async getOrder(id) {
523
+ const o = this.orders().find((x) => x.id === id);
524
+ if (!o) return null;
525
+ const paid = o.financialStatus === "paid" ? o.total : 0;
526
+ return { ...o, courseTitle: null, offerName: null, paidAmount: paid, balance: o.total - paid, installments: [] };
527
+ }
516
528
  // Subscriptions -----------------------------------------------------------
517
529
  async listSubscriptions(opts) {
518
530
  let list = this.subscriptions();
@@ -579,6 +591,19 @@ function getCoursesTenantId() {
579
591
  }
580
592
  }
581
593
 
594
+ // src/tables.ts
595
+ var T = {
596
+ courses: "plg_courses_courses",
597
+ modules: "plg_courses_modules",
598
+ lessons: "plg_courses_lessons",
599
+ offers: "plg_courses_offers",
600
+ enrollments: "plg_courses_enrollments",
601
+ progress: "plg_courses_progress",
602
+ orders: "plg_courses_orders",
603
+ subscriptions: "plg_courses_subscriptions",
604
+ creatorAccounts: "plg_courses_creator_accounts",
605
+ payouts: "plg_courses_payouts"};
606
+
582
607
  // src/supabase-provider.ts
583
608
  function getDb() {
584
609
  const supabase = core.getSupabaseClientOptional();
@@ -603,6 +628,7 @@ function rowToCourse(r) {
603
628
  price: r.price ?? 0,
604
629
  currency: r.currency ?? "BRL",
605
630
  status: r.status ?? "draft",
631
+ deliveryType: r.delivery_type ?? "online",
606
632
  sortOrder: r.sort_order ?? 0,
607
633
  createdAt: r.created_at,
608
634
  updatedAt: r.updated_at
@@ -696,7 +722,7 @@ function createSupabaseCoursesProvider() {
696
722
  return {
697
723
  // Courses ---------------------------------------------------------------
698
724
  async listCourses(opts) {
699
- let q = scoped(getDb().from("course_courses").select("*"));
725
+ let q = scoped(getDb().from(T.courses).select("*"));
700
726
  if (opts?.status) q = q.eq("status", opts.status);
701
727
  if (opts?.slug) q = q.eq("slug", opts.slug);
702
728
  if (opts?.search) q = q.ilike("title", `%${opts.search}%`);
@@ -706,12 +732,12 @@ function createSupabaseCoursesProvider() {
706
732
  },
707
733
  async getCourse(idOrSlug) {
708
734
  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();
735
+ const { data, error } = await scoped(getDb().from(T.courses).select("*").eq(col, idOrSlug)).maybeSingle();
710
736
  if (error) throw error;
711
737
  return data ? rowToCourse(data) : null;
712
738
  },
713
739
  async createCourse(input) {
714
- const { data, error } = await getDb().from("course_courses").insert({
740
+ const { data, error } = await getDb().from(T.courses).insert({
715
741
  tenant_id: getTenantId(),
716
742
  slug: input.slug ?? slugify2(input.title),
717
743
  title: input.title,
@@ -721,6 +747,7 @@ function createSupabaseCoursesProvider() {
721
747
  price: input.price ?? 0,
722
748
  currency: input.currency ?? "BRL",
723
749
  status: input.status ?? "draft",
750
+ delivery_type: input.deliveryType ?? "online",
724
751
  sort_order: input.sortOrder ?? 0
725
752
  }).select("*").single();
726
753
  if (error) throw error;
@@ -736,23 +763,24 @@ function createSupabaseCoursesProvider() {
736
763
  if (input.price !== void 0) patch.price = input.price;
737
764
  if (input.currency !== void 0) patch.currency = input.currency;
738
765
  if (input.status !== void 0) patch.status = input.status;
766
+ if (input.deliveryType !== void 0) patch.delivery_type = input.deliveryType;
739
767
  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();
768
+ const { data, error } = await scoped(getDb().from(T.courses).update(patch).eq("id", id)).select("*").single();
741
769
  if (error) throw error;
742
770
  return rowToCourse(data);
743
771
  },
744
772
  async deleteCourse(id) {
745
- const { error } = await scoped(getDb().from("course_courses").delete().eq("id", id));
773
+ const { error } = await scoped(getDb().from(T.courses).delete().eq("id", id));
746
774
  if (error) throw error;
747
775
  },
748
776
  // Modules ---------------------------------------------------------------
749
777
  async listModules(courseId) {
750
- const { data, error } = await scoped(getDb().from("course_modules").select("*").eq("course_id", courseId)).order("sort_order", { ascending: true });
778
+ const { data, error } = await scoped(getDb().from(T.modules).select("*").eq("course_id", courseId)).order("sort_order", { ascending: true });
751
779
  if (error) throw error;
752
780
  return (data ?? []).map(rowToModule);
753
781
  },
754
782
  async createModule(input) {
755
- const { data, error } = await getDb().from("course_modules").insert({
783
+ const { data, error } = await getDb().from(T.modules).insert({
756
784
  tenant_id: getTenantId(),
757
785
  course_id: input.courseId,
758
786
  title: input.title,
@@ -765,27 +793,27 @@ function createSupabaseCoursesProvider() {
765
793
  const patch = {};
766
794
  if (input.title !== void 0) patch.title = input.title;
767
795
  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();
796
+ const { data, error } = await scoped(getDb().from(T.modules).update(patch).eq("id", id)).select("*").single();
769
797
  if (error) throw error;
770
798
  return rowToModule(data);
771
799
  },
772
800
  async deleteModule(id) {
773
- const { error } = await scoped(getDb().from("course_modules").delete().eq("id", id));
801
+ const { error } = await scoped(getDb().from(T.modules).delete().eq("id", id));
774
802
  if (error) throw error;
775
803
  },
776
804
  // Lessons ---------------------------------------------------------------
777
805
  async listLessons(courseId) {
778
- const { data, error } = await scoped(getDb().from("course_lessons").select("*").eq("course_id", courseId)).order("sort_order", { ascending: true });
806
+ const { data, error } = await scoped(getDb().from(T.lessons).select("*").eq("course_id", courseId)).order("sort_order", { ascending: true });
779
807
  if (error) throw error;
780
808
  return (data ?? []).map(rowToLesson);
781
809
  },
782
810
  async getLesson(id) {
783
- const { data, error } = await scoped(getDb().from("course_lessons").select("*").eq("id", id)).maybeSingle();
811
+ const { data, error } = await scoped(getDb().from(T.lessons).select("*").eq("id", id)).maybeSingle();
784
812
  if (error) throw error;
785
813
  return data ? rowToLesson(data) : null;
786
814
  },
787
815
  async createLesson(input) {
788
- const { data, error } = await getDb().from("course_lessons").insert({
816
+ const { data, error } = await getDb().from(T.lessons).insert({
789
817
  tenant_id: getTenantId(),
790
818
  course_id: input.courseId,
791
819
  module_id: input.moduleId,
@@ -805,22 +833,22 @@ function createSupabaseCoursesProvider() {
805
833
  if (input.videoUrl !== void 0) patch.video_url = input.videoUrl;
806
834
  if (input.durationSec !== void 0) patch.duration_sec = input.durationSec;
807
835
  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();
836
+ const { data, error } = await scoped(getDb().from(T.lessons).update(patch).eq("id", id)).select("*").single();
809
837
  if (error) throw error;
810
838
  return rowToLesson(data);
811
839
  },
812
840
  async deleteLesson(id) {
813
- const { error } = await scoped(getDb().from("course_lessons").delete().eq("id", id));
841
+ const { error } = await scoped(getDb().from(T.lessons).delete().eq("id", id));
814
842
  if (error) throw error;
815
843
  },
816
844
  // Enrollments + progress ------------------------------------------------
817
845
  async listEnrollments(customerId) {
818
- const { data, error } = await getDb().from("course_enrollments").select("*").eq("customer_id", customerId);
846
+ const { data, error } = await getDb().from(T.enrollments).select("*").eq("customer_id", customerId);
819
847
  if (error) throw error;
820
848
  return (data ?? []).map(rowToEnrollment);
821
849
  },
822
850
  async enroll(courseId, customerId) {
823
- const { data, error } = await getDb().from("course_enrollments").upsert(
851
+ const { data, error } = await getDb().from(T.enrollments).upsert(
824
852
  { tenant_id: getTenantId(), course_id: courseId, customer_id: customerId, status: "active" },
825
853
  { onConflict: "course_id,customer_id" }
826
854
  ).select("*").single();
@@ -828,7 +856,7 @@ function createSupabaseCoursesProvider() {
828
856
  return rowToEnrollment(data);
829
857
  },
830
858
  async listProgress(enrollmentId) {
831
- const { data, error } = await getDb().from("course_progress").select("*").eq("enrollment_id", enrollmentId);
859
+ const { data, error } = await getDb().from(T.progress).select("*").eq("enrollment_id", enrollmentId);
832
860
  if (error) throw error;
833
861
  return (data ?? []).map(rowToProgress);
834
862
  },
@@ -839,18 +867,18 @@ function createSupabaseCoursesProvider() {
839
867
  row.completed_at = patch.completed ? (/* @__PURE__ */ new Date()).toISOString() : null;
840
868
  }
841
869
  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();
870
+ const { data, error } = await getDb().from(T.progress).upsert(row, { onConflict: "enrollment_id,lesson_id" }).select("*").single();
843
871
  if (error) throw error;
844
872
  return rowToProgress(data);
845
873
  },
846
874
  // Offers ----------------------------------------------------------------
847
875
  async listOffers(courseId) {
848
- const { data, error } = await scoped(getDb().from("course_offers").select("*").eq("course_id", courseId)).order("sort_order", { ascending: true });
876
+ const { data, error } = await scoped(getDb().from(T.offers).select("*").eq("course_id", courseId)).order("sort_order", { ascending: true });
849
877
  if (error) throw error;
850
878
  return (data ?? []).map(rowToOffer);
851
879
  },
852
880
  async createOffer(input) {
853
- const { data, error } = await getDb().from("course_offers").insert({
881
+ const { data, error } = await getDb().from(T.offers).insert({
854
882
  tenant_id: getTenantId(),
855
883
  course_id: input.courseId,
856
884
  name: input.name,
@@ -875,17 +903,17 @@ function createSupabaseCoursesProvider() {
875
903
  if (input.isDefault !== void 0) patch.is_default = input.isDefault;
876
904
  if (input.isOrderBump !== void 0) patch.is_order_bump = input.isOrderBump;
877
905
  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();
906
+ const { data, error } = await scoped(getDb().from(T.offers).update(patch).eq("id", id)).select("*").single();
879
907
  if (error) throw error;
880
908
  return rowToOffer(data);
881
909
  },
882
910
  async deleteOffer(id) {
883
- const { error } = await scoped(getDb().from("course_offers").delete().eq("id", id));
911
+ const { error } = await scoped(getDb().from(T.offers).delete().eq("id", id));
884
912
  if (error) throw error;
885
913
  },
886
914
  // Sales -----------------------------------------------------------------
887
915
  async listOrders(opts) {
888
- let q = scoped(getDb().from("course_orders").select("*"));
916
+ let q = scoped(getDb().from(T.orders).select("*"));
889
917
  if (opts?.courseId) q = q.eq("course_id", opts.courseId);
890
918
  if (opts?.financialStatus) q = q.eq("financial_status", opts.financialStatus);
891
919
  if (opts?.search) q = q.or(`customer_name.ilike.%${opts.search}%,customer_email.ilike.%${opts.search}%`);
@@ -893,9 +921,84 @@ function createSupabaseCoursesProvider() {
893
921
  if (error) throw error;
894
922
  return (data ?? []).map(rowToOrder);
895
923
  },
924
+ async listCoreSales() {
925
+ const { data, error } = await scoped(getDb().from("v_plg_course_sales").select("*")).order("created_at", { ascending: false });
926
+ if (error) throw error;
927
+ return (data ?? []).map((r) => ({
928
+ id: r.id,
929
+ courseId: r.course_id ?? "",
930
+ offerId: r.offer_id ?? null,
931
+ customerId: r.party_id ?? null,
932
+ customerName: r.customer_name ?? null,
933
+ customerEmail: r.customer_email ?? null,
934
+ currency: r.currency ?? "BRL",
935
+ total: Number(r.total ?? 0),
936
+ platformFee: Number(r.platform_fee ?? 0),
937
+ netValue: Number(r.net_value ?? r.total ?? 0),
938
+ paymentMethod: r.payment_method ?? null,
939
+ financialStatus: r.financial_status ?? "pending",
940
+ stripePaymentIntentId: null,
941
+ createdAt: r.created_at,
942
+ installmentCount: Number(r.installment_count ?? 1)
943
+ }));
944
+ },
945
+ async getOrder(id) {
946
+ const db = getDb();
947
+ const { data: v } = await scoped(db.from("v_plg_course_sales").select("*")).eq("id", id).maybeSingle();
948
+ if (!v) return null;
949
+ const [{ data: course }, { data: bills }, { data: pays }] = await Promise.all([
950
+ db.from(T.courses).select("title").eq("id", v.course_id).maybeSingle(),
951
+ db.from("plg_financial_movements").select("*").eq("invoice_id", id).eq("movement_kind", "bill").order("installment_number"),
952
+ db.from("plg_financial_movements").select("*").eq("invoice_id", id).eq("movement_kind", "payment")
953
+ ]);
954
+ let offerName = null;
955
+ if (v.offer_id) {
956
+ const { data: off } = await db.from(T.offers).select("name").eq("id", v.offer_id).maybeSingle();
957
+ offerName = off?.name ?? null;
958
+ }
959
+ const paymentsByInst = /* @__PURE__ */ new Map();
960
+ for (const p of pays ?? []) paymentsByInst.set(p.installment_number ?? 0, p);
961
+ const installments = (bills ?? []).map((b) => {
962
+ const p = paymentsByInst.get(b.installment_number ?? 0);
963
+ return {
964
+ installmentNumber: b.installment_number ?? 1,
965
+ amount: Number(b.amount ?? 0),
966
+ paidAmount: Number(b.paid_amount ?? 0),
967
+ status: b.status ?? "pending",
968
+ dueDate: b.due_date ?? null,
969
+ paymentDate: p?.payment_date ?? b.payment_date ?? null,
970
+ cardBrand: p?.card_brand ?? null,
971
+ cardInstallments: p?.card_installments ?? null
972
+ };
973
+ });
974
+ const paid = (bills ?? []).reduce((s, b) => s + Number(b.paid_amount ?? 0), 0);
975
+ const total = Number(v.total ?? 0);
976
+ return {
977
+ id: v.id,
978
+ courseId: v.course_id ?? "",
979
+ offerId: v.offer_id ?? null,
980
+ customerId: v.party_id ?? null,
981
+ customerName: v.customer_name ?? null,
982
+ customerEmail: v.customer_email ?? null,
983
+ currency: v.currency ?? "BRL",
984
+ total,
985
+ platformFee: Number(v.platform_fee ?? 0),
986
+ netValue: Number(v.net_value ?? total),
987
+ paymentMethod: v.payment_method ?? null,
988
+ financialStatus: v.financial_status ?? "pending",
989
+ stripePaymentIntentId: null,
990
+ createdAt: v.created_at,
991
+ installmentCount: Number(v.installment_count ?? installments.length ?? 1),
992
+ courseTitle: course?.title ?? null,
993
+ offerName,
994
+ paidAmount: paid,
995
+ balance: Math.max(0, total - paid),
996
+ installments
997
+ };
998
+ },
896
999
  // Subscriptions ---------------------------------------------------------
897
1000
  async listSubscriptions(opts) {
898
- let q = scoped(getDb().from("course_subscriptions").select("*"));
1001
+ let q = scoped(getDb().from(T.subscriptions).select("*"));
899
1002
  if (opts?.courseId) q = q.eq("course_id", opts.courseId);
900
1003
  if (opts?.status) q = q.eq("status", opts.status);
901
1004
  const { data, error } = await q.order("started_at", { ascending: false });
@@ -905,9 +1008,9 @@ function createSupabaseCoursesProvider() {
905
1008
  // Financial -------------------------------------------------------------
906
1009
  async getFinancialSummary() {
907
1010
  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"))
1011
+ scoped(getDb().from(T.orders).select("total,platform_fee,net_value,financial_status")),
1012
+ scoped(getDb().from(T.subscriptions).select("net_value,interval,status")),
1013
+ scoped(getDb().from(T.payouts).select("amount,status"))
911
1014
  ]);
912
1015
  const orderRows = orders.data ?? [];
913
1016
  const paid = orderRows.filter((o) => o.financial_status === "paid");
@@ -931,13 +1034,13 @@ function createSupabaseCoursesProvider() {
931
1034
  };
932
1035
  },
933
1036
  async listPayouts() {
934
- const { data, error } = await scoped(getDb().from("course_payouts").select("*")).order("created_at", { ascending: false });
1037
+ const { data, error } = await scoped(getDb().from(T.payouts).select("*")).order("created_at", { ascending: false });
935
1038
  if (error) throw error;
936
1039
  return (data ?? []).map(rowToPayout);
937
1040
  },
938
1041
  async getCreatorAccount() {
939
1042
  const tenantId = getTenantId();
940
- const { data } = await scoped(getDb().from("course_creator_accounts").select("*")).maybeSingle();
1043
+ const { data } = await scoped(getDb().from(T.creatorAccounts).select("*")).maybeSingle();
941
1044
  return {
942
1045
  tenantId: tenantId ?? "",
943
1046
  stripeAccountId: data?.stripe_account_id ?? null,