@coursebuilder/adapter-drizzle 1.0.0 → 1.0.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.
Files changed (131) hide show
  1. package/dist/{chunk-3WYG5OE7.js → chunk-3GONHHMY.js} +2 -2
  2. package/dist/{chunk-WFC55BSF.js → chunk-FPAQINSA.js} +2 -2
  3. package/dist/{chunk-RCL6HPBL.js → chunk-HWIQUI7O.js} +2 -2
  4. package/dist/{chunk-QGZ2F5SW.js → chunk-LPJ2P2KJ.js} +1162 -1027
  5. package/dist/{chunk-L5ZAWSTN.js → chunk-MOV5TUON.js} +376 -116
  6. package/dist/{chunk-GAKWXOAY.js → chunk-OBUGOZKM.js} +2 -2
  7. package/dist/{chunk-A6XPL665.js → chunk-PBTEHA7F.js} +2 -2
  8. package/dist/{chunk-5XTOCFDB.js → chunk-YQNIXCD3.js} +2 -2
  9. package/dist/index.js +9 -10
  10. package/dist/lib/mysql/index.cjs +1415 -1088
  11. package/dist/lib/mysql/index.cjs.map +1 -1
  12. package/dist/lib/mysql/index.d.cts +218 -0
  13. package/dist/lib/mysql/index.d.ts +218 -0
  14. package/dist/lib/mysql/index.js +9 -10
  15. package/dist/lib/mysql/schemas/auth/accounts.cjs +106 -98
  16. package/dist/lib/mysql/schemas/auth/accounts.cjs.map +1 -1
  17. package/dist/lib/mysql/schemas/auth/accounts.js +2 -2
  18. package/dist/lib/mysql/schemas/auth/device-access-token.cjs +99 -91
  19. package/dist/lib/mysql/schemas/auth/device-access-token.cjs.map +1 -1
  20. package/dist/lib/mysql/schemas/auth/device-access-token.js +3 -3
  21. package/dist/lib/mysql/schemas/auth/device-verification.cjs +100 -92
  22. package/dist/lib/mysql/schemas/auth/device-verification.cjs.map +1 -1
  23. package/dist/lib/mysql/schemas/auth/device-verification.js +3 -3
  24. package/dist/lib/mysql/schemas/auth/profiles.cjs +254 -0
  25. package/dist/lib/mysql/schemas/auth/profiles.cjs.map +1 -0
  26. package/dist/lib/mysql/schemas/auth/profiles.d.cts +112 -0
  27. package/dist/lib/mysql/schemas/auth/profiles.d.ts +112 -0
  28. package/dist/lib/mysql/schemas/auth/profiles.js +20 -0
  29. package/dist/lib/mysql/schemas/auth/profiles.js.map +1 -0
  30. package/dist/lib/mysql/schemas/auth/role-permissions.cjs +55 -46
  31. package/dist/lib/mysql/schemas/auth/role-permissions.cjs.map +1 -1
  32. package/dist/lib/mysql/schemas/auth/role-permissions.js +3 -3
  33. package/dist/lib/mysql/schemas/auth/roles.cjs +57 -48
  34. package/dist/lib/mysql/schemas/auth/roles.cjs.map +1 -1
  35. package/dist/lib/mysql/schemas/auth/roles.js +2 -2
  36. package/dist/lib/mysql/schemas/auth/sessions.cjs +96 -88
  37. package/dist/lib/mysql/schemas/auth/sessions.cjs.map +1 -1
  38. package/dist/lib/mysql/schemas/auth/sessions.js +3 -3
  39. package/dist/lib/mysql/schemas/auth/user-permissions.cjs +99 -91
  40. package/dist/lib/mysql/schemas/auth/user-permissions.cjs.map +1 -1
  41. package/dist/lib/mysql/schemas/auth/user-permissions.js +2 -2
  42. package/dist/lib/mysql/schemas/auth/user-prefs.cjs +104 -96
  43. package/dist/lib/mysql/schemas/auth/user-prefs.cjs.map +1 -1
  44. package/dist/lib/mysql/schemas/auth/user-prefs.js +2 -2
  45. package/dist/lib/mysql/schemas/auth/user-roles.cjs +58 -49
  46. package/dist/lib/mysql/schemas/auth/user-roles.cjs.map +1 -1
  47. package/dist/lib/mysql/schemas/auth/user-roles.js +2 -2
  48. package/dist/lib/mysql/schemas/auth/users.cjs +295 -259
  49. package/dist/lib/mysql/schemas/auth/users.cjs.map +1 -1
  50. package/dist/lib/mysql/schemas/auth/users.d.cts +1 -0
  51. package/dist/lib/mysql/schemas/auth/users.d.ts +1 -0
  52. package/dist/lib/mysql/schemas/auth/users.js +2 -2
  53. package/dist/lib/mysql/schemas/commerce/coupon.cjs +131 -123
  54. package/dist/lib/mysql/schemas/commerce/coupon.cjs.map +1 -1
  55. package/dist/lib/mysql/schemas/commerce/coupon.js +2 -2
  56. package/dist/lib/mysql/schemas/commerce/merchant-charge.cjs +96 -88
  57. package/dist/lib/mysql/schemas/commerce/merchant-charge.cjs.map +1 -1
  58. package/dist/lib/mysql/schemas/commerce/merchant-charge.js +2 -2
  59. package/dist/lib/mysql/schemas/commerce/merchant-subscription.cjs +90 -82
  60. package/dist/lib/mysql/schemas/commerce/merchant-subscription.cjs.map +1 -1
  61. package/dist/lib/mysql/schemas/commerce/merchant-subscription.js +2 -2
  62. package/dist/lib/mysql/schemas/commerce/price.cjs +98 -90
  63. package/dist/lib/mysql/schemas/commerce/price.cjs.map +1 -1
  64. package/dist/lib/mysql/schemas/commerce/price.js +2 -2
  65. package/dist/lib/mysql/schemas/commerce/product.cjs +125 -117
  66. package/dist/lib/mysql/schemas/commerce/product.cjs.map +1 -1
  67. package/dist/lib/mysql/schemas/commerce/product.js +2 -2
  68. package/dist/lib/mysql/schemas/commerce/purchase-user-transfer.cjs +131 -123
  69. package/dist/lib/mysql/schemas/commerce/purchase-user-transfer.cjs.map +1 -1
  70. package/dist/lib/mysql/schemas/commerce/purchase-user-transfer.js +3 -3
  71. package/dist/lib/mysql/schemas/commerce/purchase.cjs +184 -176
  72. package/dist/lib/mysql/schemas/commerce/purchase.cjs.map +1 -1
  73. package/dist/lib/mysql/schemas/commerce/purchase.js +2 -2
  74. package/dist/lib/mysql/schemas/commerce/subscription.cjs +117 -109
  75. package/dist/lib/mysql/schemas/commerce/subscription.cjs.map +1 -1
  76. package/dist/lib/mysql/schemas/commerce/subscription.js +2 -2
  77. package/dist/lib/mysql/schemas/commerce/upgradable-products.cjs +105 -97
  78. package/dist/lib/mysql/schemas/commerce/upgradable-products.cjs.map +1 -1
  79. package/dist/lib/mysql/schemas/commerce/upgradable-products.js +3 -3
  80. package/dist/lib/mysql/schemas/communication/comment.cjs +114 -106
  81. package/dist/lib/mysql/schemas/communication/comment.cjs.map +1 -1
  82. package/dist/lib/mysql/schemas/communication/comment.js +2 -2
  83. package/dist/lib/mysql/schemas/communication/communication-preferences.cjs +136 -128
  84. package/dist/lib/mysql/schemas/communication/communication-preferences.cjs.map +1 -1
  85. package/dist/lib/mysql/schemas/communication/communication-preferences.js +2 -2
  86. package/dist/lib/mysql/schemas/content/content-contributions.cjs +144 -136
  87. package/dist/lib/mysql/schemas/content/content-contributions.cjs.map +1 -1
  88. package/dist/lib/mysql/schemas/content/content-contributions.js +2 -2
  89. package/dist/lib/mysql/schemas/content/content-resource-product.cjs +99 -91
  90. package/dist/lib/mysql/schemas/content/content-resource-product.cjs.map +1 -1
  91. package/dist/lib/mysql/schemas/content/content-resource-product.js +2 -2
  92. package/dist/lib/mysql/schemas/content/content-resource-resource.cjs +108 -100
  93. package/dist/lib/mysql/schemas/content/content-resource-resource.cjs.map +1 -1
  94. package/dist/lib/mysql/schemas/content/content-resource-resource.js +2 -2
  95. package/dist/lib/mysql/schemas/content/content-resource-tag.cjs +118 -110
  96. package/dist/lib/mysql/schemas/content/content-resource-tag.cjs.map +1 -1
  97. package/dist/lib/mysql/schemas/content/content-resource-tag.js +2 -2
  98. package/dist/lib/mysql/schemas/content/content-resource-version.cjs +121 -113
  99. package/dist/lib/mysql/schemas/content/content-resource-version.cjs.map +1 -1
  100. package/dist/lib/mysql/schemas/content/content-resource-version.js +2 -2
  101. package/dist/lib/mysql/schemas/content/content-resource.cjs +198 -190
  102. package/dist/lib/mysql/schemas/content/content-resource.cjs.map +1 -1
  103. package/dist/lib/mysql/schemas/content/content-resource.js +2 -2
  104. package/dist/lib/mysql/schemas/content/tag-tag.cjs +103 -95
  105. package/dist/lib/mysql/schemas/content/tag-tag.cjs.map +1 -1
  106. package/dist/lib/mysql/schemas/content/tag-tag.js +2 -2
  107. package/dist/lib/mysql/schemas/content/tag.cjs +116 -108
  108. package/dist/lib/mysql/schemas/content/tag.cjs.map +1 -1
  109. package/dist/lib/mysql/schemas/content/tag.js +2 -2
  110. package/dist/lib/mysql/schemas/org/organization-membership-roles.cjs +73 -35
  111. package/dist/lib/mysql/schemas/org/organization-membership-roles.cjs.map +1 -1
  112. package/dist/lib/mysql/schemas/org/organization-membership-roles.d.cts +1 -0
  113. package/dist/lib/mysql/schemas/org/organization-membership-roles.d.ts +1 -0
  114. package/dist/lib/mysql/schemas/org/organization-membership-roles.js +2 -3
  115. package/dist/lib/mysql/schemas/org/organization-memberships.cjs +88 -38
  116. package/dist/lib/mysql/schemas/org/organization-memberships.cjs.map +1 -1
  117. package/dist/lib/mysql/schemas/org/organization-memberships.d.cts +1 -0
  118. package/dist/lib/mysql/schemas/org/organization-memberships.d.ts +1 -0
  119. package/dist/lib/mysql/schemas/org/organization-memberships.js +2 -2
  120. package/dist/lib/mysql/schemas/org/organizations.cjs +131 -121
  121. package/dist/lib/mysql/schemas/org/organizations.cjs.map +1 -1
  122. package/dist/lib/mysql/schemas/org/organizations.js +2 -2
  123. package/package.json +4 -3
  124. package/src/lib/mysql/index.ts +320 -26
  125. package/src/lib/mysql/schemas/auth/profiles.ts +50 -0
  126. package/src/lib/mysql/schemas/auth/roles.ts +6 -1
  127. package/src/lib/mysql/schemas/auth/users.ts +6 -1
  128. package/src/lib/mysql/schemas/commerce/subscription.ts +3 -5
  129. package/src/lib/mysql/schemas/org/organization-membership-roles.ts +7 -0
  130. package/src/lib/mysql/schemas/org/organization-memberships.ts +6 -0
  131. package/dist/chunk-RP66NZN3.js +0 -81
@@ -61,7 +61,15 @@ import {
61
61
  } from '@coursebuilder/core/schemas/content-resource-schema'
62
62
  import { merchantAccountSchema } from '@coursebuilder/core/schemas/merchant-account-schema'
63
63
  import { merchantCustomerSchema } from '@coursebuilder/core/schemas/merchant-customer-schema'
64
+ import {
65
+ MerchantSession,
66
+ MerchantSessionSchema,
67
+ } from '@coursebuilder/core/schemas/merchant-session'
68
+ import { MerchantSubscriptionSchema } from '@coursebuilder/core/schemas/merchant-subscription'
69
+ import { OrganizationMemberSchema } from '@coursebuilder/core/schemas/organization-member'
70
+ import { OrganizationSchema } from '@coursebuilder/core/schemas/organization-schema'
64
71
  import { type ModuleProgress } from '@coursebuilder/core/schemas/resource-progress-schema'
72
+ import { SubscriptionSchema } from '@coursebuilder/core/schemas/subscription'
65
73
  import { VideoResourceSchema } from '@coursebuilder/core/schemas/video-resource'
66
74
  import { PaymentsProviderConfig } from '@coursebuilder/core/types'
67
75
  import { logger } from '@coursebuilder/core/utils/logger'
@@ -83,6 +91,10 @@ import {
83
91
  getPermissionsRelationsSchema,
84
92
  getPermissionsSchema,
85
93
  } from './schemas/auth/permissions.js'
94
+ import {
95
+ getProfilesRelationsSchema,
96
+ getProfilesSchema,
97
+ } from './schemas/auth/profiles.js'
86
98
  import {
87
99
  getRolePermissionsRelationsSchema,
88
100
  getRolePermissionsSchema,
@@ -305,6 +317,8 @@ export function getCourseBuilderSchema(mysqlTable: MySqlTableFn) {
305
317
  getMerchantSubscriptionRelationsSchema(mysqlTable),
306
318
  subscription: getSubscriptionSchema(mysqlTable),
307
319
  subscriptionRelations: getSubscriptionRelationsSchema(mysqlTable),
320
+ profiles: getProfilesSchema(mysqlTable),
321
+ profilesRelations: getProfilesRelationsSchema(mysqlTable),
308
322
  } as const
309
323
  }
310
324
 
@@ -342,6 +356,12 @@ export function mySqlDrizzleAdapter(
342
356
  upgradableProducts,
343
357
  resourceProgress,
344
358
  comments,
359
+ organization: organizationTable,
360
+ organizationMemberships: organizationMembershipTable,
361
+ organizationMembershipRoles: organizationMembershipRoleTable,
362
+ roles: roleTable,
363
+ merchantSubscription: merchantSubscriptionTable,
364
+ subscription: subscriptionTable,
345
365
  } = createTables(tableFn)
346
366
 
347
367
  const adapter: CourseBuilderAdapter = {
@@ -655,6 +675,23 @@ export function mySqlDrizzleAdapter(
655
675
 
656
676
  return parsedCoupon.data
657
677
  },
678
+ async createMerchantSession(options): Promise<MerchantSession> {
679
+ const id = `ms_${v4()}`
680
+ await client.insert(merchantSession).values({
681
+ id,
682
+ identifier: options.identifier,
683
+ merchantAccountId: options.merchantAccountId,
684
+ ...(options.organizationId
685
+ ? { organizationId: options.organizationId }
686
+ : {}),
687
+ })
688
+
689
+ return MerchantSessionSchema.parse(
690
+ await client.query.merchantSession.findFirst({
691
+ where: eq(merchantSession.id, id),
692
+ }),
693
+ )
694
+ },
658
695
  async createMerchantChargeAndPurchase(options): Promise<Purchase> {
659
696
  const purchaseId = await client.transaction(async (trx) => {
660
697
  try {
@@ -787,15 +824,14 @@ export function mySqlDrizzleAdapter(
787
824
  }
788
825
  }
789
826
 
827
+ // create a new merchant session
790
828
  const merchantSessionId = `ms_${v4()}`
791
829
 
792
- const newMerchantSession = await client
793
- .insert(merchantSession)
794
- .values({
795
- id: merchantSessionId,
796
- identifier: checkoutSessionId,
797
- merchantAccountId,
798
- })
830
+ await client.insert(merchantSession).values({
831
+ id: merchantSessionId,
832
+ identifier: checkoutSessionId,
833
+ merchantAccountId,
834
+ })
799
835
 
800
836
  const merchantCouponUsed = stripeCouponId
801
837
  ? await client.query.merchantCoupon.findFirst({
@@ -817,7 +853,7 @@ export function mySqlDrizzleAdapter(
817
853
  ? 'Restricted'
818
854
  : 'Valid'
819
855
 
820
- const newPurchase = await client.insert(purchaseTable).values({
856
+ await client.insert(purchaseTable).values({
821
857
  id: purchaseId,
822
858
  status: newPurchaseStatus,
823
859
  userId,
@@ -833,16 +869,14 @@ export function mySqlDrizzleAdapter(
833
869
 
834
870
  const oneWeekInMilliseconds = 1000 * 60 * 60 * 24 * 7
835
871
 
836
- const newPurchaseUserTransfer = await client
837
- .insert(purchaseUserTransfer)
838
- .values({
839
- id: `put_${v4()}`,
840
- purchaseId: purchaseId as string,
841
- expiresAt: existingPurchase
842
- ? new Date()
843
- : new Date(Date.now() + oneWeekInMilliseconds),
844
- sourceUserId: userId,
845
- })
872
+ await client.insert(purchaseUserTransfer).values({
873
+ id: `put_${v4()}`,
874
+ purchaseId: purchaseId as string,
875
+ expiresAt: existingPurchase
876
+ ? new Date()
877
+ : new Date(Date.now() + oneWeekInMilliseconds),
878
+ sourceUserId: userId,
879
+ })
846
880
 
847
881
  // const result = await Promise.all([
848
882
  // newMerchantCharge,
@@ -1098,7 +1132,7 @@ export function mySqlDrizzleAdapter(
1098
1132
 
1099
1133
  const ResultRowSchema = z.object({
1100
1134
  resource_id: z.string(),
1101
- resource_type: z.enum(['lesson', 'exercise']),
1135
+ resource_type: z.enum(['lesson', 'exercise', 'post']),
1102
1136
  resource_slug: z.string().nullable(),
1103
1137
  completed_at: z
1104
1138
  .string()
@@ -1138,10 +1172,9 @@ export function mySqlDrizzleAdapter(
1138
1172
  LEFT JOIN ${resourceProgress} rp ON rp.resourceId = cr.id
1139
1173
  AND rp.userId = ${user.id}
1140
1174
  WHERE
1141
- cr.type IN ('lesson', 'exercise')
1175
+ cr.type IN ('lesson', 'exercise', 'post')
1142
1176
 
1143
1177
  `)
1144
-
1145
1178
  // Process the results
1146
1179
  const completedLessons: ResourceProgress[] = []
1147
1180
  let nextResource: Partial<ContentResource> | null = null
@@ -2441,11 +2474,39 @@ export function mySqlDrizzleAdapter(
2441
2474
 
2442
2475
  await client.insert(users).values({ ...data, id })
2443
2476
 
2444
- return await client
2445
- .select()
2446
- .from(users)
2447
- .where(eq(users.id, id))
2448
- .then((res) => res[0] as AdapterUser)
2477
+ // creating a personal organization for the user
2478
+ // every user gets an organization of their very own
2479
+ const personalOrganization = await adapter.createOrganization({
2480
+ name: `Personal (${data.email})`,
2481
+ })
2482
+
2483
+ if (!personalOrganization) {
2484
+ throw new Error('Failed to create personal organization')
2485
+ }
2486
+
2487
+ const membership = await adapter.addMemberToOrganization({
2488
+ organizationId: personalOrganization.id,
2489
+ userId: id,
2490
+ invitedById: id,
2491
+ })
2492
+
2493
+ if (!membership) {
2494
+ throw new Error('Failed to add user to personal organization')
2495
+ }
2496
+
2497
+ await adapter.addRoleForMember({
2498
+ organizationId: personalOrganization.id,
2499
+ memberId: membership.id,
2500
+ role: 'owner',
2501
+ })
2502
+
2503
+ const user = await adapter.getUser?.(id)
2504
+
2505
+ if (!user) {
2506
+ throw new Error('Failed to get user')
2507
+ }
2508
+
2509
+ return user
2449
2510
  } catch (error) {
2450
2511
  console.error(error)
2451
2512
  throw error
@@ -2631,6 +2692,239 @@ export function mySqlDrizzleAdapter(
2631
2692
 
2632
2693
  return undefined
2633
2694
  },
2695
+ createOrganization: async (options: { name: string }) => {
2696
+ const organizationId = crypto.randomUUID()
2697
+ await client.insert(organizationTable).values({
2698
+ ...options,
2699
+ id: organizationId,
2700
+ })
2701
+
2702
+ return adapter.getOrganization(organizationId)
2703
+ },
2704
+ getOrganization: async (organizationId: string) => {
2705
+ return OrganizationSchema.parse(
2706
+ await client.query.organization.findFirst({
2707
+ where: eq(organizationTable.id, organizationId),
2708
+ }),
2709
+ )
2710
+ },
2711
+ addMemberToOrganization: async (options: {
2712
+ organizationId: string
2713
+ userId: string
2714
+ invitedById: string
2715
+ }) => {
2716
+ const id = crypto.randomUUID()
2717
+ await client.insert(organizationMembershipTable).values({
2718
+ ...options,
2719
+ id,
2720
+ })
2721
+
2722
+ return OrganizationMemberSchema.parse(
2723
+ await client.query.organizationMemberships.findFirst({
2724
+ where: eq(organizationMembershipTable.id, id),
2725
+ with: {
2726
+ organization: true,
2727
+ user: true,
2728
+ },
2729
+ }),
2730
+ )
2731
+ },
2732
+ removeMemberFromOrganization: async (options: {
2733
+ organizationId: string
2734
+ userId: string
2735
+ }) => {
2736
+ await client
2737
+ .delete(organizationMembershipTable)
2738
+ .where(
2739
+ and(
2740
+ eq(
2741
+ organizationMembershipTable.organizationId,
2742
+ options.organizationId,
2743
+ ),
2744
+ eq(organizationMembershipTable.userId, options.userId),
2745
+ ),
2746
+ )
2747
+ },
2748
+ addRoleForMember: async (options: {
2749
+ organizationId: string
2750
+ memberId: string
2751
+ role: string
2752
+ }) => {
2753
+ const existingRole = z
2754
+ .object({
2755
+ id: z.string(),
2756
+ })
2757
+ .nullish()
2758
+ .parse(
2759
+ await client.query.roles.findFirst({
2760
+ where: and(
2761
+ eq(roleTable.organizationId, options.organizationId),
2762
+ eq(roleTable.name, options.role),
2763
+ ),
2764
+ }),
2765
+ )
2766
+
2767
+ const roleId = existingRole?.id || crypto.randomUUID()
2768
+
2769
+ if (!existingRole) {
2770
+ await client.insert(roleTable).values({
2771
+ name: options.role,
2772
+ organizationId: options.organizationId,
2773
+ id: roleId,
2774
+ })
2775
+ }
2776
+
2777
+ const currentOrgMembershipRole =
2778
+ await client.query.organizationMembershipRoles.findFirst({
2779
+ where: and(
2780
+ eq(
2781
+ organizationMembershipRoleTable.organizationMembershipId,
2782
+ options.memberId,
2783
+ ),
2784
+ eq(organizationMembershipRoleTable.roleId, roleId),
2785
+ ),
2786
+ })
2787
+
2788
+ if (!currentOrgMembershipRole) {
2789
+ await client.insert(organizationMembershipRoleTable).values({
2790
+ organizationId: options.organizationId,
2791
+ organizationMembershipId: options.memberId,
2792
+ roleId,
2793
+ })
2794
+ }
2795
+ },
2796
+ removeRoleForMember: async (options: {
2797
+ organizationId: string
2798
+ memberId: string
2799
+ role: string
2800
+ }) => {
2801
+ const existingRole = z
2802
+ .object({
2803
+ id: z.string(),
2804
+ })
2805
+ .nullable()
2806
+ .parse(
2807
+ await client.query.roles.findFirst({
2808
+ where: and(
2809
+ eq(roleTable.organizationId, options.organizationId),
2810
+ eq(roleTable.name, options.role),
2811
+ ),
2812
+ }),
2813
+ )
2814
+
2815
+ const roleId = existingRole?.id
2816
+
2817
+ if (roleId) {
2818
+ await client
2819
+ .delete(organizationMembershipRoleTable)
2820
+ .where(eq(organizationMembershipRoleTable.roleId, roleId))
2821
+ }
2822
+ },
2823
+ getMembershipsForUser: async (userId: string) => {
2824
+ return OrganizationMemberSchema.array().parse(
2825
+ (await client.query.organizationMemberships.findMany({
2826
+ where: eq(organizationMembershipTable.userId, userId),
2827
+ with: {
2828
+ organization: true,
2829
+ user: true,
2830
+ },
2831
+ })) || [],
2832
+ )
2833
+ },
2834
+ getOrganizationMembers: async (organizationId: string) => {
2835
+ return OrganizationMemberSchema.array().parse(
2836
+ (await client.query.organizationMemberships.findMany({
2837
+ where: eq(organizationMembershipTable.organizationId, organizationId),
2838
+ })) || [],
2839
+ )
2840
+ },
2841
+ createSubscription: async (options: {
2842
+ organizationId: string
2843
+ merchantSubscriptionId: string
2844
+ productId: string
2845
+ }) => {
2846
+ const id = `sub_${crypto.randomUUID()}`
2847
+ await client.insert(subscriptionTable).values({
2848
+ ...options,
2849
+ id,
2850
+ })
2851
+
2852
+ return SubscriptionSchema.parse(
2853
+ await client.query.subscription.findFirst({
2854
+ where: eq(subscriptionTable.id, id),
2855
+ with: {
2856
+ product: true,
2857
+ },
2858
+ }),
2859
+ )
2860
+ },
2861
+ getMerchantSubscription: async (merchantSubscriptionId: string) => {
2862
+ return MerchantSubscriptionSchema.parse(
2863
+ await client.query.merchantSubscription.findFirst({
2864
+ where: eq(merchantSubscriptionTable.id, merchantSubscriptionId),
2865
+ }),
2866
+ )
2867
+ },
2868
+ createMerchantSubscription: async (options: {
2869
+ merchantAccountId: string
2870
+ merchantCustomerId: string
2871
+ merchantProductId: string
2872
+ identifier: string
2873
+ }) => {
2874
+ const id = crypto.randomUUID()
2875
+ await client.insert(merchantSubscriptionTable).values({
2876
+ ...options,
2877
+ id,
2878
+ })
2879
+
2880
+ return MerchantSubscriptionSchema.parse(
2881
+ await client.query.merchantSubscription.findFirst({
2882
+ where: eq(merchantSubscriptionTable.id, id),
2883
+ }),
2884
+ )
2885
+ },
2886
+ updateMerchantSubscription: async (options: {
2887
+ merchantSubscriptionId: string
2888
+ status: string
2889
+ }) => {
2890
+ throw new Error('Not implemented')
2891
+ },
2892
+ deleteMerchantSubscription: async (merchantSubscriptionId: string) => {
2893
+ throw new Error('Not implemented')
2894
+ },
2895
+ getSubscriptionForStripeId: async (stripeSubscriptionId: string) => {
2896
+ const merchantSubscriptionParsed = MerchantSubscriptionSchema.safeParse(
2897
+ await client.query.merchantSubscription.findFirst({
2898
+ where: eq(merchantSubscriptionTable.identifier, stripeSubscriptionId),
2899
+ }),
2900
+ )
2901
+
2902
+ if (!merchantSubscriptionParsed.success) {
2903
+ throw new Error(
2904
+ `No merchant subscription found for stripe id ${stripeSubscriptionId} ${merchantSubscriptionParsed.error}`,
2905
+ )
2906
+ }
2907
+
2908
+ const subscriptionParsed = SubscriptionSchema.safeParse(
2909
+ await client.query.subscription.findFirst({
2910
+ where: eq(
2911
+ subscriptionTable.merchantSubscriptionId,
2912
+ merchantSubscriptionParsed.data.id,
2913
+ ),
2914
+ with: {
2915
+ product: true,
2916
+ },
2917
+ }),
2918
+ )
2919
+
2920
+ if (!subscriptionParsed.success) {
2921
+ throw new Error(
2922
+ `No subscription found for merchant subscription ${merchantSubscriptionParsed.data.id} ${subscriptionParsed.error}`,
2923
+ )
2924
+ }
2925
+
2926
+ return subscriptionParsed.data
2927
+ },
2634
2928
  }
2635
2929
 
2636
2930
  return adapter
@@ -0,0 +1,50 @@
1
+ import { relations, sql } from 'drizzle-orm'
2
+ import {
3
+ index,
4
+ json,
5
+ MySqlTableFn,
6
+ timestamp,
7
+ uniqueIndex,
8
+ varchar,
9
+ } from 'drizzle-orm/mysql-core'
10
+
11
+ import { getUsersSchema } from './users.js'
12
+
13
+ export function getProfilesSchema(mysqlTable: MySqlTableFn) {
14
+ return mysqlTable(
15
+ 'Profile',
16
+ {
17
+ id: varchar('id', { length: 255 }).notNull().primaryKey(),
18
+ userId: varchar('userId', { length: 255 }).notNull(),
19
+ type: varchar('type', { length: 255 }).notNull(),
20
+ fields: json('fields').$type<Record<string, any>>().default({}),
21
+ createdAt: timestamp('createdAt', {
22
+ mode: 'date',
23
+ fsp: 3,
24
+ }).default(sql`CURRENT_TIMESTAMP(3)`),
25
+ updatedAt: timestamp('updatedAt', {
26
+ mode: 'date',
27
+ fsp: 3,
28
+ }).default(sql`CURRENT_TIMESTAMP(3)`),
29
+ },
30
+ (profile) => ({
31
+ userIdIdx: index('userId_idx').on(profile.userId),
32
+ uniqueUserType: uniqueIndex('unique_user_type_idx').on(
33
+ profile.userId,
34
+ profile.type,
35
+ ),
36
+ }),
37
+ )
38
+ }
39
+
40
+ export function getProfilesRelationsSchema(mysqlTable: MySqlTableFn) {
41
+ const profiles = getProfilesSchema(mysqlTable)
42
+ const users = getUsersSchema(mysqlTable)
43
+ return relations(profiles, ({ one }) => ({
44
+ user: one(users, {
45
+ fields: [profiles.userId],
46
+ references: [users.id],
47
+ relationName: 'profiles',
48
+ }),
49
+ }))
50
+ }
@@ -5,6 +5,7 @@ import {
5
5
  MySqlTableFn,
6
6
  text,
7
7
  timestamp,
8
+ uniqueIndex,
8
9
  varchar,
9
10
  } from 'drizzle-orm/mysql-core'
10
11
 
@@ -16,7 +17,7 @@ export function getRolesSchema(mysqlTable: MySqlTableFn) {
16
17
  {
17
18
  id: varchar('id', { length: 255 }).notNull().primaryKey(),
18
19
  organizationId: varchar('organizationId', { length: 191 }),
19
- name: varchar('name', { length: 255 }).notNull().unique(),
20
+ name: varchar('name', { length: 255 }).notNull(),
20
21
  description: text('description'),
21
22
  active: boolean('active').notNull().default(true),
22
23
  createdAt: timestamp('createdAt', {
@@ -35,6 +36,10 @@ export function getRolesSchema(mysqlTable: MySqlTableFn) {
35
36
  (role) => ({
36
37
  nameIdx: index('name_idx').on(role.name),
37
38
  organizationIdIdx: index('organizationId_idx').on(role.organizationId),
39
+ uniqueNamePerOrg: uniqueIndex('unique_name_per_org').on(
40
+ role.organizationId,
41
+ role.name,
42
+ ),
38
43
  }),
39
44
  )
40
45
  }
@@ -15,6 +15,7 @@ import { getContentContributionsSchema } from '../content/content-contributions.
15
15
  import { getContentResourceSchema } from '../content/content-resource.js'
16
16
  import { getOrganizationMembershipsSchema } from '../org/organization-memberships.js'
17
17
  import { getAccountsSchema } from './accounts.js'
18
+ import { getProfilesSchema } from './profiles.js'
18
19
  import { getUserPermissionsSchema } from './user-permissions.js'
19
20
  import { getUserPrefsSchema } from './user-prefs.js'
20
21
  import { getUserRolesSchema } from './user-roles.js'
@@ -48,6 +49,7 @@ export function getUsersSchema(mysqlTable: MySqlTableFn) {
48
49
 
49
50
  export function getUsersRelationsSchema(mysqlTable: MySqlTableFn) {
50
51
  const users = getUsersSchema(mysqlTable)
52
+ const profiles = getProfilesSchema(mysqlTable)
51
53
  const accounts = getAccountsSchema(mysqlTable)
52
54
  const communicationPreferences = getCommunicationPreferencesSchema(mysqlTable)
53
55
  const userRoles = getUserRolesSchema(mysqlTable)
@@ -58,7 +60,10 @@ export function getUsersRelationsSchema(mysqlTable: MySqlTableFn) {
58
60
  const comments = getCommentsSchema(mysqlTable)
59
61
  const userPrefs = getUserPrefsSchema(mysqlTable)
60
62
  const organizationMemberships = getOrganizationMembershipsSchema(mysqlTable)
61
- return relations(users, ({ many }) => ({
63
+ return relations(users, ({ many, one }) => ({
64
+ profiles: many(profiles, {
65
+ relationName: 'profiles',
66
+ }),
62
67
  accounts: many(accounts, {
63
68
  relationName: 'user',
64
69
  }),
@@ -8,14 +8,12 @@ import {
8
8
  varchar,
9
9
  } from 'drizzle-orm/mysql-core'
10
10
 
11
- import { getUsersSchema } from '../auth/users.js'
12
11
  import { getOrganizationsSchema } from '../org/organizations.js'
13
- import { getCouponSchema } from './coupon.js'
14
- import { getMerchantChargeSchema } from './merchant-charge.js'
15
- import { getMerchantSessionSchema } from './merchant-session.js'
16
12
  import { getMerchantSubscriptionSchema } from './merchant-subscription.js'
17
13
  import { getProductSchema } from './product.js'
18
14
 
15
+ // TODO: do we add a renewal date here? probably just add that stuff to fields
16
+ // even status feels out of place at the top level maybe
19
17
  export function getSubscriptionSchema(mysqlTable: MySqlTableFn) {
20
18
  return mysqlTable(
21
19
  'Subscription',
@@ -29,7 +27,7 @@ export function getSubscriptionSchema(mysqlTable: MySqlTableFn) {
29
27
  merchantSubscriptionId: varchar('merchantSubscriptionId', {
30
28
  length: 191,
31
29
  }).notNull(),
32
- status: varchar('status', { length: 191 }).default('Valid').notNull(),
30
+ status: varchar('status', { length: 191 }).default('active').notNull(),
33
31
  fields: json('fields').$type<Record<string, any>>().default({}),
34
32
  },
35
33
  (table) => {
@@ -10,6 +10,7 @@ import {
10
10
 
11
11
  import { getRolesSchema } from '../auth/roles.js'
12
12
  import { getOrganizationMembershipsSchema } from './organization-memberships.js'
13
+ import { getOrganizationsSchema } from './organizations.js'
13
14
 
14
15
  export function getOrganizationMembershipRolesSchema(mysqlTable: MySqlTableFn) {
15
16
  return mysqlTable(
@@ -53,6 +54,7 @@ export function getOrganizationMembershipRolesRelationsSchema(
53
54
  getOrganizationMembershipRolesSchema(mysqlTable)
54
55
  const organizationMemberships = getOrganizationMembershipsSchema(mysqlTable)
55
56
  const roles = getRolesSchema(mysqlTable)
57
+ const organizations = getOrganizationsSchema(mysqlTable)
56
58
  return relations(organizationMembershipRoles, ({ one }) => ({
57
59
  organizationMembership: one(organizationMemberships, {
58
60
  fields: [organizationMembershipRoles.organizationMembershipId],
@@ -64,5 +66,10 @@ export function getOrganizationMembershipRolesRelationsSchema(
64
66
  references: [roles.id],
65
67
  relationName: 'role',
66
68
  }),
69
+ organization: one(organizations, {
70
+ fields: [organizationMembershipRoles.organizationId],
71
+ references: [organizations.id],
72
+ relationName: 'organization',
73
+ }),
67
74
  }))
68
75
  }
@@ -10,6 +10,7 @@ import {
10
10
 
11
11
  import { getUsersSchema } from '../auth/users.js'
12
12
  import { getPurchaseSchema } from '../commerce/purchase.js'
13
+ import { getOrganizationMembershipRolesSchema } from './organization-membership-roles.js'
13
14
  import { getOrganizationsSchema } from './organizations.js'
14
15
 
15
16
  export function getOrganizationMembershipsSchema(mysqlTable: MySqlTableFn) {
@@ -47,6 +48,8 @@ export function getOrganizationMembershipsRelationsSchema(
47
48
  const organizationMemberships = getOrganizationMembershipsSchema(mysqlTable)
48
49
  const purchases = getPurchaseSchema(mysqlTable)
49
50
  const organizations = getOrganizationsSchema(mysqlTable)
51
+ const organizationMembershipRoles =
52
+ getOrganizationMembershipRolesSchema(mysqlTable)
50
53
 
51
54
  return relations(organizationMemberships, ({ one, many }) => ({
52
55
  user: one(users, {
@@ -65,5 +68,8 @@ export function getOrganizationMembershipsRelationsSchema(
65
68
  references: [organizations.id],
66
69
  relationName: 'organization',
67
70
  }),
71
+ organizationMembershipRoles: many(organizationMembershipRoles, {
72
+ relationName: 'organizationMembership',
73
+ }),
68
74
  }))
69
75
  }
@@ -1,81 +0,0 @@
1
- import {
2
- getOrganizationMembershipsSchema,
3
- getRolesSchema
4
- } from "./chunk-QGZ2F5SW.js";
5
- import {
6
- __name
7
- } from "./chunk-H736K5TN.js";
8
-
9
- // src/lib/mysql/schemas/org/organization-membership-roles.ts
10
- import { relations } from "drizzle-orm";
11
- import { boolean, index, primaryKey, timestamp, varchar } from "drizzle-orm/mysql-core";
12
- function getOrganizationMembershipRolesSchema(mysqlTable) {
13
- return mysqlTable("OrganizationMembershipRole", {
14
- organizationMembershipId: varchar("organizationMembershipId", {
15
- length: 255
16
- }).notNull(),
17
- roleId: varchar("roleId", {
18
- length: 255
19
- }).notNull(),
20
- active: boolean("active").notNull().default(true),
21
- organizationId: varchar("organizationId", {
22
- length: 191
23
- }),
24
- createdAt: timestamp("createdAt", {
25
- mode: "date",
26
- fsp: 3
27
- }).defaultNow(),
28
- updatedAt: timestamp("updatedAt", {
29
- mode: "date",
30
- fsp: 3
31
- }).defaultNow(),
32
- deletedAt: timestamp("deletedAt", {
33
- mode: "date",
34
- fsp: 3
35
- })
36
- }, (ur) => ({
37
- pk: primaryKey({
38
- columns: [
39
- ur.organizationMembershipId,
40
- ur.roleId
41
- ],
42
- name: "pk"
43
- }),
44
- orgMemberIdIdx: index("orgMemberId_idx").on(ur.organizationMembershipId),
45
- roleIdIdx: index("roleId_idx").on(ur.roleId),
46
- organizationIdIdx: index("organizationId_idx").on(ur.organizationId)
47
- }));
48
- }
49
- __name(getOrganizationMembershipRolesSchema, "getOrganizationMembershipRolesSchema");
50
- function getOrganizationMembershipRolesRelationsSchema(mysqlTable) {
51
- const organizationMembershipRoles = getOrganizationMembershipRolesSchema(mysqlTable);
52
- const organizationMemberships = getOrganizationMembershipsSchema(mysqlTable);
53
- const roles = getRolesSchema(mysqlTable);
54
- return relations(organizationMembershipRoles, ({ one }) => ({
55
- organizationMembership: one(organizationMemberships, {
56
- fields: [
57
- organizationMembershipRoles.organizationMembershipId
58
- ],
59
- references: [
60
- organizationMemberships.id
61
- ],
62
- relationName: "organizationMembership"
63
- }),
64
- role: one(roles, {
65
- fields: [
66
- organizationMembershipRoles.roleId
67
- ],
68
- references: [
69
- roles.id
70
- ],
71
- relationName: "role"
72
- })
73
- }));
74
- }
75
- __name(getOrganizationMembershipRolesRelationsSchema, "getOrganizationMembershipRolesRelationsSchema");
76
-
77
- export {
78
- getOrganizationMembershipRolesSchema,
79
- getOrganizationMembershipRolesRelationsSchema
80
- };
81
- //# sourceMappingURL=chunk-RP66NZN3.js.map