@coursebuilder/adapter-drizzle 2.1.1 → 3.0.0-canary.0.0c9beb2ae

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 (296) hide show
  1. package/dist/{chunk-KIQLUFJX.js → chunk-2GW3BJE2.js} +2 -2
  2. package/dist/{chunk-M3WM5Q7W.js → chunk-33ZN7FN7.js} +2 -2
  3. package/dist/chunk-4VZJQAPV.js +74 -0
  4. package/dist/chunk-5FLS4ZBX.js +109 -0
  5. package/dist/chunk-5YR5M22S.js +147 -0
  6. package/dist/chunk-6XQ6C3IX.js +18 -0
  7. package/dist/chunk-AIDM2CH6.js +146 -0
  8. package/dist/chunk-ALTODDUX.js +173 -0
  9. package/dist/chunk-AZY2VRAE.js +63 -0
  10. package/dist/chunk-BS7T27VL.js +239 -0
  11. package/dist/chunk-CN3FCRLR.js +55 -0
  12. package/dist/chunk-EAXC4CQD.js +245 -0
  13. package/dist/chunk-EDDG2YFS.js +249 -0
  14. package/dist/chunk-EDKWCMA7.js +210 -0
  15. package/dist/{chunk-7FBSP3VD.js → chunk-EDM6DKA5.js} +2 -2
  16. package/dist/{chunk-EZEQQJNH.js → chunk-EIQBTF5J.js} +2 -2
  17. package/dist/chunk-FYT7RTVW.js +248 -0
  18. package/dist/{chunk-F56AI52P.js → chunk-INZTF6QV.js} +2 -2
  19. package/dist/{chunk-AP5HBIV4.js → chunk-JVHX4JHA.js} +2 -2
  20. package/dist/chunk-LWEKWDLC.js +300 -0
  21. package/dist/chunk-OJPEBGGE.js +139 -0
  22. package/dist/chunk-QKOSV34V.js +16268 -0
  23. package/dist/chunk-QO3QE3LE.js +330 -0
  24. package/dist/chunk-QQKNNFFB.js +399 -0
  25. package/dist/chunk-RBHCV3UW.js +20 -0
  26. package/dist/chunk-TNH2C27F.js +252 -0
  27. package/dist/chunk-TNWNW666.js +133 -0
  28. package/dist/chunk-VBEXYQ6L.js +156 -0
  29. package/dist/chunk-W3ILNMLD.js +265 -0
  30. package/dist/chunk-WJ2ZUXL3.js +71 -0
  31. package/dist/{chunk-PJORQUQP.js → chunk-WUMDFFN3.js} +2 -2
  32. package/dist/{chunk-2AW5KYAK.js → chunk-X7DRYXB7.js} +2 -2
  33. package/dist/{chunk-PNPTKVJ4.js → chunk-XUBZKYH6.js} +24 -15
  34. package/dist/{chunk-WWBSNLRC.js → chunk-ZY4AFKUQ.js} +2 -2
  35. package/dist/index.d.ts +5 -11
  36. package/dist/index.js +41 -18
  37. package/dist/lib/mysql/cms-resource-actions.cjs +15912 -4127
  38. package/dist/lib/mysql/cms-resource-actions.cjs.map +1 -1
  39. package/dist/lib/mysql/cms-resource-actions.d.cts +8 -38
  40. package/dist/lib/mysql/cms-resource-actions.d.ts +8 -38
  41. package/dist/lib/mysql/cms-resource-actions.js +2 -1
  42. package/dist/lib/mysql/context.cjs +3380 -0
  43. package/dist/lib/mysql/context.cjs.map +1 -0
  44. package/dist/lib/mysql/context.d.cts +15 -0
  45. package/dist/lib/mysql/context.d.ts +15 -0
  46. package/dist/lib/mysql/context.js +34 -0
  47. package/dist/lib/mysql/context.js.map +1 -0
  48. package/dist/lib/mysql/index.cjs +21875 -15261
  49. package/dist/lib/mysql/index.cjs.map +1 -1
  50. package/dist/lib/mysql/index.d.cts +24 -14300
  51. package/dist/lib/mysql/index.d.ts +24 -14300
  52. package/dist/lib/mysql/index.js +46 -22
  53. package/dist/lib/mysql/ports/auth-storage.cjs +84 -0
  54. package/dist/lib/mysql/ports/auth-storage.cjs.map +1 -0
  55. package/dist/lib/mysql/ports/auth-storage.d.cts +22 -0
  56. package/dist/lib/mysql/ports/auth-storage.d.ts +22 -0
  57. package/dist/lib/mysql/ports/auth-storage.js +8 -0
  58. package/dist/lib/mysql/ports/auth-storage.js.map +1 -0
  59. package/dist/lib/mysql/ports/catalog.cjs +16688 -0
  60. package/dist/lib/mysql/ports/catalog.cjs.map +1 -0
  61. package/dist/lib/mysql/ports/catalog.d.cts +24 -0
  62. package/dist/lib/mysql/ports/catalog.d.ts +24 -0
  63. package/dist/lib/mysql/ports/catalog.js +9 -0
  64. package/dist/lib/mysql/ports/catalog.js.map +1 -0
  65. package/dist/lib/mysql/ports/content-resource.cjs +321 -0
  66. package/dist/lib/mysql/ports/content-resource.cjs.map +1 -0
  67. package/dist/lib/mysql/ports/content-resource.d.cts +23 -0
  68. package/dist/lib/mysql/ports/content-resource.d.ts +23 -0
  69. package/dist/lib/mysql/ports/content-resource.js +8 -0
  70. package/dist/lib/mysql/ports/content-resource.js.map +1 -0
  71. package/dist/lib/mysql/ports/coupon.cjs +16524 -0
  72. package/dist/lib/mysql/ports/coupon.cjs.map +1 -0
  73. package/dist/lib/mysql/ports/coupon.d.cts +22 -0
  74. package/dist/lib/mysql/ports/coupon.d.ts +22 -0
  75. package/dist/lib/mysql/ports/coupon.js +9 -0
  76. package/dist/lib/mysql/ports/coupon.js.map +1 -0
  77. package/dist/lib/mysql/ports/entitlement.cjs +16350 -0
  78. package/dist/lib/mysql/ports/entitlement.cjs.map +1 -0
  79. package/dist/lib/mysql/ports/entitlement.d.cts +22 -0
  80. package/dist/lib/mysql/ports/entitlement.d.ts +22 -0
  81. package/dist/lib/mysql/ports/entitlement.js +9 -0
  82. package/dist/lib/mysql/ports/entitlement.js.map +1 -0
  83. package/dist/lib/mysql/ports/identity.cjs +154 -0
  84. package/dist/lib/mysql/ports/identity.cjs.map +1 -0
  85. package/dist/lib/mysql/ports/identity.d.cts +22 -0
  86. package/dist/lib/mysql/ports/identity.d.ts +22 -0
  87. package/dist/lib/mysql/ports/identity.js +8 -0
  88. package/dist/lib/mysql/ports/identity.js.map +1 -0
  89. package/dist/lib/mysql/ports/merchant.cjs +194 -0
  90. package/dist/lib/mysql/ports/merchant.cjs.map +1 -0
  91. package/dist/lib/mysql/ports/merchant.d.cts +22 -0
  92. package/dist/lib/mysql/ports/merchant.d.ts +22 -0
  93. package/dist/lib/mysql/ports/merchant.js +8 -0
  94. package/dist/lib/mysql/ports/merchant.js.map +1 -0
  95. package/dist/lib/mysql/ports/organization.cjs +16426 -0
  96. package/dist/lib/mysql/ports/organization.cjs.map +1 -0
  97. package/dist/lib/mysql/ports/organization.d.cts +22 -0
  98. package/dist/lib/mysql/ports/organization.d.ts +22 -0
  99. package/dist/lib/mysql/ports/organization.js +9 -0
  100. package/dist/lib/mysql/ports/organization.js.map +1 -0
  101. package/dist/lib/mysql/ports/progress.cjs +16544 -0
  102. package/dist/lib/mysql/ports/progress.cjs.map +1 -0
  103. package/dist/lib/mysql/ports/progress.d.cts +22 -0
  104. package/dist/lib/mysql/ports/progress.d.ts +22 -0
  105. package/dist/lib/mysql/ports/progress.js +9 -0
  106. package/dist/lib/mysql/ports/progress.js.map +1 -0
  107. package/dist/lib/mysql/ports/purchase.cjs +16609 -0
  108. package/dist/lib/mysql/ports/purchase.cjs.map +1 -0
  109. package/dist/lib/mysql/ports/purchase.d.cts +23 -0
  110. package/dist/lib/mysql/ports/purchase.d.ts +23 -0
  111. package/dist/lib/mysql/ports/purchase.js +9 -0
  112. package/dist/lib/mysql/ports/purchase.js.map +1 -0
  113. package/dist/lib/mysql/ports/subscription.cjs +76 -0
  114. package/dist/lib/mysql/ports/subscription.cjs.map +1 -0
  115. package/dist/lib/mysql/ports/subscription.d.cts +22 -0
  116. package/dist/lib/mysql/ports/subscription.d.ts +22 -0
  117. package/dist/lib/mysql/ports/subscription.js +8 -0
  118. package/dist/lib/mysql/ports/subscription.js.map +1 -0
  119. package/dist/lib/mysql/schema.cjs +3373 -0
  120. package/dist/lib/mysql/schema.cjs.map +1 -0
  121. package/dist/lib/mysql/schema.d.cts +14385 -0
  122. package/dist/lib/mysql/schema.d.ts +14385 -0
  123. package/dist/lib/mysql/schema.js +35 -0
  124. package/dist/lib/mysql/schema.js.map +1 -0
  125. package/dist/lib/mysql/schemas/auth/accounts.cjs.map +1 -1
  126. package/dist/lib/mysql/schemas/auth/accounts.js +4 -4
  127. package/dist/lib/mysql/schemas/auth/device-access-token.cjs.map +1 -1
  128. package/dist/lib/mysql/schemas/auth/device-access-token.js +5 -5
  129. package/dist/lib/mysql/schemas/auth/device-verification.cjs.map +1 -1
  130. package/dist/lib/mysql/schemas/auth/device-verification.js +5 -5
  131. package/dist/lib/mysql/schemas/auth/personal-access-token.cjs.map +1 -1
  132. package/dist/lib/mysql/schemas/auth/personal-access-token.js +5 -5
  133. package/dist/lib/mysql/schemas/auth/profiles.cjs.map +1 -1
  134. package/dist/lib/mysql/schemas/auth/profiles.js +4 -4
  135. package/dist/lib/mysql/schemas/auth/role-permissions.cjs.map +1 -1
  136. package/dist/lib/mysql/schemas/auth/role-permissions.js +5 -5
  137. package/dist/lib/mysql/schemas/auth/roles.cjs.map +1 -1
  138. package/dist/lib/mysql/schemas/auth/roles.js +4 -4
  139. package/dist/lib/mysql/schemas/auth/sessions.cjs.map +1 -1
  140. package/dist/lib/mysql/schemas/auth/sessions.js +5 -5
  141. package/dist/lib/mysql/schemas/auth/user-permissions.cjs.map +1 -1
  142. package/dist/lib/mysql/schemas/auth/user-permissions.js +4 -4
  143. package/dist/lib/mysql/schemas/auth/user-prefs.cjs.map +1 -1
  144. package/dist/lib/mysql/schemas/auth/user-prefs.js +4 -4
  145. package/dist/lib/mysql/schemas/auth/user-roles.cjs.map +1 -1
  146. package/dist/lib/mysql/schemas/auth/user-roles.js +4 -4
  147. package/dist/lib/mysql/schemas/auth/users.cjs +5 -1
  148. package/dist/lib/mysql/schemas/auth/users.cjs.map +1 -1
  149. package/dist/lib/mysql/schemas/auth/users.js +4 -4
  150. package/dist/lib/mysql/schemas/commerce/coupon.cjs.map +1 -1
  151. package/dist/lib/mysql/schemas/commerce/coupon.js +4 -4
  152. package/dist/lib/mysql/schemas/commerce/merchant-charge.cjs.map +1 -1
  153. package/dist/lib/mysql/schemas/commerce/merchant-charge.js +4 -4
  154. package/dist/lib/mysql/schemas/commerce/merchant-subscription.cjs.map +1 -1
  155. package/dist/lib/mysql/schemas/commerce/merchant-subscription.js +4 -4
  156. package/dist/lib/mysql/schemas/commerce/price.cjs.map +1 -1
  157. package/dist/lib/mysql/schemas/commerce/price.js +4 -4
  158. package/dist/lib/mysql/schemas/commerce/product.cjs.map +1 -1
  159. package/dist/lib/mysql/schemas/commerce/product.js +4 -4
  160. package/dist/lib/mysql/schemas/commerce/purchase-user-transfer.cjs.map +1 -1
  161. package/dist/lib/mysql/schemas/commerce/purchase-user-transfer.js +5 -5
  162. package/dist/lib/mysql/schemas/commerce/purchase.cjs +11 -2
  163. package/dist/lib/mysql/schemas/commerce/purchase.cjs.map +1 -1
  164. package/dist/lib/mysql/schemas/commerce/purchase.js +4 -4
  165. package/dist/lib/mysql/schemas/commerce/subscription.cjs +6 -1
  166. package/dist/lib/mysql/schemas/commerce/subscription.cjs.map +1 -1
  167. package/dist/lib/mysql/schemas/commerce/subscription.js +4 -4
  168. package/dist/lib/mysql/schemas/commerce/upgradable-products.cjs.map +1 -1
  169. package/dist/lib/mysql/schemas/commerce/upgradable-products.js +5 -5
  170. package/dist/lib/mysql/schemas/communication/comment.cjs +5 -1
  171. package/dist/lib/mysql/schemas/communication/comment.cjs.map +1 -1
  172. package/dist/lib/mysql/schemas/communication/comment.js +4 -4
  173. package/dist/lib/mysql/schemas/communication/communication-preferences.cjs +5 -1
  174. package/dist/lib/mysql/schemas/communication/communication-preferences.cjs.map +1 -1
  175. package/dist/lib/mysql/schemas/communication/communication-preferences.js +4 -4
  176. package/dist/lib/mysql/schemas/communication/question-response.cjs.map +1 -1
  177. package/dist/lib/mysql/schemas/communication/question-response.js +5 -5
  178. package/dist/lib/mysql/schemas/content/content-contributions.cjs +5 -1
  179. package/dist/lib/mysql/schemas/content/content-contributions.cjs.map +1 -1
  180. package/dist/lib/mysql/schemas/content/content-contributions.js +4 -4
  181. package/dist/lib/mysql/schemas/content/content-resource-product.cjs.map +1 -1
  182. package/dist/lib/mysql/schemas/content/content-resource-product.js +4 -4
  183. package/dist/lib/mysql/schemas/content/content-resource-resource.cjs.map +1 -1
  184. package/dist/lib/mysql/schemas/content/content-resource-resource.js +4 -4
  185. package/dist/lib/mysql/schemas/content/content-resource-tag.cjs.map +1 -1
  186. package/dist/lib/mysql/schemas/content/content-resource-tag.js +4 -4
  187. package/dist/lib/mysql/schemas/content/content-resource-version.cjs.map +1 -1
  188. package/dist/lib/mysql/schemas/content/content-resource-version.js +4 -4
  189. package/dist/lib/mysql/schemas/content/content-resource.cjs +5 -1
  190. package/dist/lib/mysql/schemas/content/content-resource.cjs.map +1 -1
  191. package/dist/lib/mysql/schemas/content/content-resource.js +4 -4
  192. package/dist/lib/mysql/schemas/content/tag-tag.cjs.map +1 -1
  193. package/dist/lib/mysql/schemas/content/tag-tag.js +4 -4
  194. package/dist/lib/mysql/schemas/content/tag.cjs.map +1 -1
  195. package/dist/lib/mysql/schemas/content/tag.js +4 -4
  196. package/dist/lib/mysql/schemas/entitlements/entitlement.cjs +11 -2
  197. package/dist/lib/mysql/schemas/entitlements/entitlement.cjs.map +1 -1
  198. package/dist/lib/mysql/schemas/entitlements/entitlement.js +5 -5
  199. package/dist/lib/mysql/schemas/org/organization-membership-roles.cjs +11 -2
  200. package/dist/lib/mysql/schemas/org/organization-membership-roles.cjs.map +1 -1
  201. package/dist/lib/mysql/schemas/org/organization-membership-roles.js +4 -4
  202. package/dist/lib/mysql/schemas/org/organization-memberships.cjs +11 -2
  203. package/dist/lib/mysql/schemas/org/organization-memberships.cjs.map +1 -1
  204. package/dist/lib/mysql/schemas/org/organization-memberships.d.cts +16 -0
  205. package/dist/lib/mysql/schemas/org/organization-memberships.d.ts +16 -0
  206. package/dist/lib/mysql/schemas/org/organization-memberships.js +4 -4
  207. package/dist/lib/mysql/schemas/org/organizations.cjs +11 -2
  208. package/dist/lib/mysql/schemas/org/organizations.cjs.map +1 -1
  209. package/dist/lib/mysql/schemas/org/organizations.d.cts +16 -0
  210. package/dist/lib/mysql/schemas/org/organizations.d.ts +16 -0
  211. package/dist/lib/mysql/schemas/org/organizations.js +4 -4
  212. package/dist/lib/mysql/workflows/content-resource-workflow-utils.cjs +40 -0
  213. package/dist/lib/mysql/workflows/content-resource-workflow-utils.cjs.map +1 -0
  214. package/dist/lib/mysql/workflows/content-resource-workflow-utils.d.cts +4 -0
  215. package/dist/lib/mysql/workflows/content-resource-workflow-utils.d.ts +4 -0
  216. package/dist/lib/mysql/workflows/content-resource-workflow-utils.js +10 -0
  217. package/dist/lib/mysql/workflows/content-resource-workflow-utils.js.map +1 -0
  218. package/dist/lib/mysql/workflows/create-cohort.cjs +184 -0
  219. package/dist/lib/mysql/workflows/create-cohort.cjs.map +1 -0
  220. package/dist/lib/mysql/workflows/create-cohort.d.cts +24 -0
  221. package/dist/lib/mysql/workflows/create-cohort.d.ts +24 -0
  222. package/dist/lib/mysql/workflows/create-cohort.js +9 -0
  223. package/dist/lib/mysql/workflows/create-cohort.js.map +1 -0
  224. package/dist/lib/mysql/workflows/create-event-series.cjs +283 -0
  225. package/dist/lib/mysql/workflows/create-event-series.cjs.map +1 -0
  226. package/dist/lib/mysql/workflows/create-event-series.d.cts +24 -0
  227. package/dist/lib/mysql/workflows/create-event-series.d.ts +24 -0
  228. package/dist/lib/mysql/workflows/create-event-series.js +9 -0
  229. package/dist/lib/mysql/workflows/create-event-series.js.map +1 -0
  230. package/dist/lib/mysql/workflows/create-event.cjs +194 -0
  231. package/dist/lib/mysql/workflows/create-event.cjs.map +1 -0
  232. package/dist/lib/mysql/workflows/create-event.d.cts +24 -0
  233. package/dist/lib/mysql/workflows/create-event.d.ts +24 -0
  234. package/dist/lib/mysql/workflows/create-event.js +9 -0
  235. package/dist/lib/mysql/workflows/create-event.js.map +1 -0
  236. package/dist/lib/mysql/workflows/create-merchant-charge-and-purchase.cjs +231 -0
  237. package/dist/lib/mysql/workflows/create-merchant-charge-and-purchase.cjs.map +1 -0
  238. package/dist/lib/mysql/workflows/create-merchant-charge-and-purchase.d.cts +22 -0
  239. package/dist/lib/mysql/workflows/create-merchant-charge-and-purchase.d.ts +22 -0
  240. package/dist/lib/mysql/workflows/create-merchant-charge-and-purchase.js +8 -0
  241. package/dist/lib/mysql/workflows/create-merchant-charge-and-purchase.js.map +1 -0
  242. package/dist/lib/mysql/workflows/create-workshop.cjs +286 -0
  243. package/dist/lib/mysql/workflows/create-workshop.cjs.map +1 -0
  244. package/dist/lib/mysql/workflows/create-workshop.d.cts +24 -0
  245. package/dist/lib/mysql/workflows/create-workshop.d.ts +24 -0
  246. package/dist/lib/mysql/workflows/create-workshop.js +9 -0
  247. package/dist/lib/mysql/workflows/create-workshop.js.map +1 -0
  248. package/dist/lib/mysql/workflows/redeem-full-price-coupon.cjs +130 -0
  249. package/dist/lib/mysql/workflows/redeem-full-price-coupon.cjs.map +1 -0
  250. package/dist/lib/mysql/workflows/redeem-full-price-coupon.d.cts +24 -0
  251. package/dist/lib/mysql/workflows/redeem-full-price-coupon.d.ts +24 -0
  252. package/dist/lib/mysql/workflows/redeem-full-price-coupon.js +8 -0
  253. package/dist/lib/mysql/workflows/redeem-full-price-coupon.js.map +1 -0
  254. package/dist/lib/mysql/workflows/transfer-purchase-to-user.cjs +95 -0
  255. package/dist/lib/mysql/workflows/transfer-purchase-to-user.cjs.map +1 -0
  256. package/dist/lib/mysql/workflows/transfer-purchase-to-user.d.cts +30 -0
  257. package/dist/lib/mysql/workflows/transfer-purchase-to-user.d.ts +30 -0
  258. package/dist/lib/mysql/workflows/transfer-purchase-to-user.js +8 -0
  259. package/dist/lib/mysql/workflows/transfer-purchase-to-user.js.map +1 -0
  260. package/dist/lib/utils.d.cts +1 -10
  261. package/dist/lib/utils.d.ts +1 -10
  262. package/dist/types.d.ts +14 -0
  263. package/dist/types.js +1 -0
  264. package/package.json +9 -5
  265. package/src/index.ts +3 -2
  266. package/src/lib/mysql/context.ts +20 -0
  267. package/src/lib/mysql/index.ts +118 -4873
  268. package/src/lib/mysql/ports/auth-storage.ts +139 -0
  269. package/src/lib/mysql/ports/catalog.ts +610 -0
  270. package/src/lib/mysql/ports/content-resource.ts +442 -0
  271. package/src/lib/mysql/ports/coupon.ts +387 -0
  272. package/src/lib/mysql/ports/entitlement.ts +96 -0
  273. package/src/lib/mysql/ports/identity.ts +160 -0
  274. package/src/lib/mysql/ports/merchant.ts +274 -0
  275. package/src/lib/mysql/ports/organization.ts +216 -0
  276. package/src/lib/mysql/ports/progress.ts +366 -0
  277. package/src/lib/mysql/ports/purchase.ts +517 -0
  278. package/src/lib/mysql/ports/subscription.ts +93 -0
  279. package/src/lib/mysql/schema.ts +374 -0
  280. package/src/lib/mysql/schemas/org/organization-memberships.ts +7 -0
  281. package/src/lib/mysql/schemas/org/organizations.ts +8 -0
  282. package/src/lib/mysql/workflows/content-resource-workflow-utils.ts +22 -0
  283. package/src/lib/mysql/workflows/create-cohort.ts +239 -0
  284. package/src/lib/mysql/workflows/create-event-series.ts +384 -0
  285. package/src/lib/mysql/workflows/create-event.ts +232 -0
  286. package/src/lib/mysql/workflows/create-merchant-charge-and-purchase.ts +365 -0
  287. package/src/lib/mysql/workflows/create-workshop.ts +372 -0
  288. package/src/lib/mysql/workflows/redeem-full-price-coupon.ts +179 -0
  289. package/src/lib/mysql/workflows/transfer-purchase-to-user.ts +142 -0
  290. package/src/types.ts +33 -0
  291. package/dist/authjs.d.ts +0 -27
  292. package/dist/authjs.js +0 -1
  293. package/dist/chunk-62VSIVME.js +0 -4714
  294. package/dist/chunk-RVXF3JZ7.js +0 -8732
  295. package/dist/chunk-WEJL5OFI.js +0 -8732
  296. package/src/authjs.ts +0 -34
@@ -0,0 +1,274 @@
1
+ import { and, desc, eq } from 'drizzle-orm'
2
+ import { v4 } from 'uuid'
3
+
4
+ import type { MerchantPort } from '@coursebuilder/core/ports'
5
+ import {
6
+ merchantChargeSchema,
7
+ merchantEventsSchema,
8
+ merchantPriceSchema,
9
+ merchantProductSchema,
10
+ type MerchantCharge,
11
+ type MerchantEvents,
12
+ type MerchantProduct,
13
+ type User,
14
+ } from '@coursebuilder/core/schemas'
15
+ import { merchantAccountSchema } from '@coursebuilder/core/schemas/merchant-account-schema'
16
+ import { merchantCustomerSchema } from '@coursebuilder/core/schemas/merchant-customer-schema'
17
+ import type { MerchantCustomer } from '@coursebuilder/core/schemas/merchant-customer-schema'
18
+ import {
19
+ MerchantSessionSchema,
20
+ type MerchantSession,
21
+ } from '@coursebuilder/core/schemas/merchant-session'
22
+ import { MerchantSubscriptionSchema } from '@coursebuilder/core/schemas/merchant-subscription'
23
+ import { logger as baseLogger } from '@coursebuilder/utils/logger'
24
+
25
+ import type { MySqlContext } from '../context.js'
26
+
27
+ const logger = baseLogger as unknown as {
28
+ error: (error: Error, attrs?: Record<string, unknown>) => void
29
+ }
30
+
31
+ /**
32
+ * Creates the MySQL implementation of payment-processor mirror operations.
33
+ *
34
+ * @param context - The shared MySQL client and table set.
35
+ * @returns All sixteen MerchantPort operations.
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * const merchantPort = createMerchantPort(context)
40
+ * ```
41
+ */
42
+ export function createMerchantPort({
43
+ client,
44
+ tables,
45
+ }: MySqlContext): MerchantPort {
46
+ const {
47
+ merchantAccount,
48
+ merchantCharge,
49
+ merchantCustomer,
50
+ merchantEvents,
51
+ merchantPrice,
52
+ merchantProduct,
53
+ merchantSession,
54
+ merchantSubscription: merchantSubscriptionTable,
55
+ } = tables
56
+
57
+ const merchantPort: MerchantPort = {
58
+ createMerchantCustomer: async (options) => {
59
+ await client.insert(merchantCustomer).values({
60
+ id: `mc_${v4()}`,
61
+ identifier: options.identifier,
62
+ merchantAccountId: options.merchantAccountId,
63
+ userId: options.userId,
64
+ status: 1,
65
+ })
66
+ return merchantCustomerSchema.parse(
67
+ await client.query.merchantCustomer.findFirst({
68
+ where: eq(merchantCustomer.identifier, options.identifier),
69
+ }),
70
+ )
71
+ },
72
+ getMerchantAccount: async (options) => {
73
+ return merchantAccountSchema.parse(
74
+ await client.query.merchantAccount.findFirst({
75
+ where: eq(merchantAccount.label, options.provider),
76
+ }),
77
+ )
78
+ },
79
+ getMerchantPriceForProductId: async (productId) => {
80
+ const merchantPriceData = await client.query.merchantPrice.findFirst({
81
+ where: and(
82
+ eq(merchantPrice.merchantProductId, productId),
83
+ eq(merchantPrice.status, 1),
84
+ ),
85
+ })
86
+
87
+ const parsedMerchantPrice =
88
+ merchantPriceSchema.safeParse(merchantPriceData)
89
+
90
+ if (!parsedMerchantPrice.success) {
91
+ logger.error(parsedMerchantPrice.error, {
92
+ event: 'adapter.merchantPrice.parse_failed',
93
+ productId,
94
+ })
95
+ return null
96
+ }
97
+
98
+ return parsedMerchantPrice.data
99
+ },
100
+ getMerchantProductForProductId: async (productId) => {
101
+ const merchantProductData = await client.query.merchantProduct.findFirst({
102
+ where: eq(merchantProduct.productId, productId),
103
+ })
104
+
105
+ if (!merchantProductData) return null
106
+ return merchantProductSchema.parse(merchantProductData)
107
+ },
108
+ getMerchantCustomerForUserId: async (userId) => {
109
+ const merchantCustomerData =
110
+ await client.query.merchantCustomer.findFirst({
111
+ where: eq(merchantCustomer.userId, userId),
112
+ })
113
+
114
+ if (!merchantCustomerData) return null
115
+ return merchantCustomerSchema.parse(merchantCustomerData)
116
+ },
117
+ async createMerchantSession(options): Promise<MerchantSession> {
118
+ const id = `ms_${v4()}`
119
+ await client.insert(merchantSession).values({
120
+ id,
121
+ identifier: options.identifier,
122
+ merchantAccountId: options.merchantAccountId,
123
+ ...(options.organizationId
124
+ ? { organizationId: options.organizationId }
125
+ : {}),
126
+ })
127
+
128
+ return MerchantSessionSchema.parse(
129
+ await client.query.merchantSession.findFirst({
130
+ where: eq(merchantSession.id, id),
131
+ }),
132
+ )
133
+ },
134
+ async findOrCreateMerchantCustomer(options: {
135
+ user: User
136
+ identifier: string
137
+ merchantAccountId: string
138
+ }): Promise<MerchantCustomer | null> {
139
+ const merchantCustomer = merchantCustomerSchema
140
+ .nullable()
141
+ .optional()
142
+ .parse(
143
+ await client.query.merchantCustomer.findFirst({
144
+ where: (merchantCustomer, { eq }) =>
145
+ eq(merchantCustomer.identifier, options.identifier),
146
+ }),
147
+ )
148
+
149
+ if (merchantCustomer) {
150
+ return merchantCustomer
151
+ }
152
+
153
+ return await merchantPort.createMerchantCustomer({
154
+ identifier: options.identifier,
155
+ merchantAccountId: options.merchantAccountId,
156
+ userId: options.user.id,
157
+ })
158
+ },
159
+ async createMerchantEvent(options: {
160
+ merchantAccountId: string
161
+ identifier: string
162
+ payload: Record<string, any>
163
+ }): Promise<MerchantEvents> {
164
+ const eventId = `me_${v4()}`
165
+
166
+ await client.insert(merchantEvents).values({
167
+ id: eventId,
168
+ merchantAccountId: options.merchantAccountId,
169
+ identifier: options.identifier,
170
+ payload: options.payload,
171
+ })
172
+
173
+ const createdEvent = await client.query.merchantEvents.findFirst({
174
+ where: eq(merchantEvents.id, eventId),
175
+ })
176
+
177
+ return merchantEventsSchema.parse(createdEvent)
178
+ },
179
+ async getMerchantEventByIdentifier(
180
+ identifier: string,
181
+ ): Promise<MerchantEvents | null> {
182
+ const event = await client.query.merchantEvents.findFirst({
183
+ where: eq(merchantEvents.identifier, identifier),
184
+ })
185
+
186
+ return event ? merchantEventsSchema.parse(event) : null
187
+ },
188
+ async getMerchantEventsByAccount(
189
+ merchantAccountId: string,
190
+ ): Promise<MerchantEvents[]> {
191
+ const events = await client.query.merchantEvents.findMany({
192
+ where: eq(merchantEvents.merchantAccountId, merchantAccountId),
193
+ orderBy: [desc(merchantEvents.createdAt)],
194
+ })
195
+
196
+ return events.map((event) => merchantEventsSchema.parse(event))
197
+ },
198
+ async getMerchantCharge(
199
+ merchantChargeId: string,
200
+ ): Promise<MerchantCharge | null> {
201
+ const mCharge = await client.query.merchantCharge.findFirst({
202
+ where: eq(merchantCharge.id, merchantChargeId),
203
+ })
204
+ const parsed = merchantChargeSchema.safeParse(mCharge)
205
+ if (!parsed.success) {
206
+ logger.error(parsed.error, {
207
+ event: 'adapter.merchantCharge.parse_failed',
208
+ merchantChargeId,
209
+ })
210
+ return null
211
+ }
212
+ return parsed.data
213
+ },
214
+ async getMerchantProduct(
215
+ stripeProductId: string,
216
+ ): Promise<MerchantProduct | null> {
217
+ return merchantProductSchema.nullable().parse(
218
+ await client.query.merchantProduct.findFirst({
219
+ where: eq(merchantProduct.identifier, stripeProductId),
220
+ }),
221
+ )
222
+ },
223
+ getMerchantSubscription: async (merchantSubscriptionId: string) => {
224
+ return MerchantSubscriptionSchema.parse(
225
+ await client.query.merchantSubscription.findFirst({
226
+ where: eq(merchantSubscriptionTable.id, merchantSubscriptionId),
227
+ }),
228
+ )
229
+ },
230
+ createMerchantSubscription: async (options: {
231
+ merchantAccountId: string
232
+ merchantCustomerId: string
233
+ merchantProductId: string
234
+ identifier: string
235
+ }) => {
236
+ const id = crypto.randomUUID()
237
+ await client.insert(merchantSubscriptionTable).values({
238
+ ...options,
239
+ id,
240
+ })
241
+
242
+ return MerchantSubscriptionSchema.parse(
243
+ await client.query.merchantSubscription.findFirst({
244
+ where: eq(merchantSubscriptionTable.id, id),
245
+ }),
246
+ )
247
+ },
248
+ updateMerchantSubscription: async (options: {
249
+ merchantSubscriptionId: string
250
+ status: string
251
+ }) => {
252
+ await client
253
+ .update(merchantSubscriptionTable)
254
+ .set({ status: Number(options.status) })
255
+ .where(eq(merchantSubscriptionTable.id, options.merchantSubscriptionId))
256
+
257
+ return MerchantSubscriptionSchema.parse(
258
+ await client.query.merchantSubscription.findFirst({
259
+ where: eq(
260
+ merchantSubscriptionTable.id,
261
+ options.merchantSubscriptionId,
262
+ ),
263
+ }),
264
+ )
265
+ },
266
+ deleteMerchantSubscription: async (merchantSubscriptionId: string) => {
267
+ await client
268
+ .delete(merchantSubscriptionTable)
269
+ .where(eq(merchantSubscriptionTable.id, merchantSubscriptionId))
270
+ },
271
+ }
272
+
273
+ return merchantPort
274
+ }
@@ -0,0 +1,216 @@
1
+ import { and, eq } from 'drizzle-orm'
2
+ import { z } from 'zod'
3
+
4
+ import type { OrganizationPort } from '@coursebuilder/core/ports'
5
+ import { OrganizationMemberSchema } from '@coursebuilder/core/schemas/organization-member'
6
+ import { OrganizationSchema } from '@coursebuilder/core/schemas/organization-schema'
7
+
8
+ import type { MySqlContext } from '../context.js'
9
+
10
+ /**
11
+ * Creates the MySQL implementation of the organization capability port.
12
+ *
13
+ * @param context - The shared MySQL client and table set.
14
+ * @returns All eight organization, membership, and member-role operations.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * const organizationPort = createOrganizationPort(context)
19
+ * ```
20
+ */
21
+ export function createOrganizationPort({
22
+ client,
23
+ tables,
24
+ }: MySqlContext): OrganizationPort {
25
+ const {
26
+ organization: organizationTable,
27
+ organizationMemberships: organizationMembershipTable,
28
+ organizationMembershipRoles: organizationMembershipRoleTable,
29
+ roles: roleTable,
30
+ } = tables
31
+
32
+ const getOrganization: OrganizationPort['getOrganization'] = async (
33
+ organizationId,
34
+ ) => {
35
+ return OrganizationSchema.parse(
36
+ await client.query.organization.findFirst({
37
+ where: eq(organizationTable.id, organizationId),
38
+ }),
39
+ )
40
+ }
41
+
42
+ const getPersonalOrganization: OrganizationPort['getPersonalOrganization'] =
43
+ async (userId) => {
44
+ return OrganizationSchema.nullable().parse(
45
+ (await client.query.organization.findFirst({
46
+ where: eq(organizationTable.personalOrganizationUserId, userId),
47
+ })) ?? null,
48
+ )
49
+ }
50
+
51
+ return {
52
+ createOrganization: async (options) => {
53
+ const organizationId = crypto.randomUUID()
54
+ await client.insert(organizationTable).values({
55
+ ...options,
56
+ id: organizationId,
57
+ })
58
+
59
+ return getOrganization(organizationId)
60
+ },
61
+ getOrganization,
62
+ getPersonalOrganization,
63
+ createPersonalOrganization: async ({ userId, name }) => {
64
+ await client
65
+ .insert(organizationTable)
66
+ .values({
67
+ id: crypto.randomUUID(),
68
+ name,
69
+ personalOrganizationUserId: userId,
70
+ })
71
+ .onDuplicateKeyUpdate({
72
+ set: { personalOrganizationUserId: userId },
73
+ })
74
+
75
+ const organization = await getPersonalOrganization(userId)
76
+ if (!organization) {
77
+ throw new Error('Failed to create durable personal organization')
78
+ }
79
+ return organization
80
+ },
81
+ addMemberToOrganization: async (options) => {
82
+ const membershipWhere = options.personalOrganizationUserId
83
+ ? eq(
84
+ organizationMembershipTable.personalOrganizationUserId,
85
+ options.personalOrganizationUserId,
86
+ )
87
+ : and(
88
+ eq(
89
+ organizationMembershipTable.organizationId,
90
+ options.organizationId,
91
+ ),
92
+ eq(organizationMembershipTable.userId, options.userId),
93
+ )
94
+ const currentMembership =
95
+ await client.query.organizationMemberships.findFirst({
96
+ where: membershipWhere,
97
+ with: { organization: true, user: true },
98
+ })
99
+
100
+ if (!currentMembership) {
101
+ const insert = client.insert(organizationMembershipTable).values({
102
+ ...options,
103
+ id: crypto.randomUUID(),
104
+ })
105
+ if (options.personalOrganizationUserId) {
106
+ await insert.onDuplicateKeyUpdate({
107
+ set: { userId: options.userId },
108
+ })
109
+ } else {
110
+ await insert
111
+ }
112
+ }
113
+
114
+ return OrganizationMemberSchema.parse(
115
+ await client.query.organizationMemberships.findFirst({
116
+ where: membershipWhere,
117
+ with: { organization: true, user: true },
118
+ }),
119
+ )
120
+ },
121
+ removeMemberFromOrganization: async (options) => {
122
+ await client
123
+ .delete(organizationMembershipTable)
124
+ .where(
125
+ and(
126
+ eq(
127
+ organizationMembershipTable.organizationId,
128
+ options.organizationId,
129
+ ),
130
+ eq(organizationMembershipTable.userId, options.userId),
131
+ ),
132
+ )
133
+ },
134
+ addRoleForMember: async (options) => {
135
+ await client
136
+ .insert(roleTable)
137
+ .values({
138
+ name: options.role,
139
+ organizationId: options.organizationId,
140
+ id: crypto.randomUUID(),
141
+ })
142
+ .onDuplicateKeyUpdate({ set: { name: options.role } })
143
+
144
+ const role = z.object({ id: z.string() }).parse(
145
+ await client.query.roles.findFirst({
146
+ where: and(
147
+ eq(roleTable.organizationId, options.organizationId),
148
+ eq(roleTable.name, options.role),
149
+ ),
150
+ }),
151
+ )
152
+
153
+ await client
154
+ .insert(organizationMembershipRoleTable)
155
+ .values({
156
+ organizationId: options.organizationId,
157
+ organizationMembershipId: options.memberId,
158
+ roleId: role.id,
159
+ })
160
+ .onDuplicateKeyUpdate({ set: { roleId: role.id } })
161
+ },
162
+ removeRoleForMember: async (options) => {
163
+ const existingRole = z
164
+ .object({
165
+ id: z.string(),
166
+ })
167
+ .nullable()
168
+ .parse(
169
+ await client.query.roles.findFirst({
170
+ where: and(
171
+ eq(roleTable.organizationId, options.organizationId),
172
+ eq(roleTable.name, options.role),
173
+ ),
174
+ }),
175
+ )
176
+
177
+ const roleId = existingRole?.id
178
+
179
+ if (roleId) {
180
+ await client
181
+ .delete(organizationMembershipRoleTable)
182
+ .where(
183
+ and(
184
+ eq(organizationMembershipRoleTable.roleId, roleId),
185
+ eq(
186
+ organizationMembershipRoleTable.organizationMembershipId,
187
+ options.memberId,
188
+ ),
189
+ ),
190
+ )
191
+ }
192
+ },
193
+ getMembershipsForUser: async (userId) => {
194
+ return OrganizationMemberSchema.array().parse(
195
+ (await client.query.organizationMemberships.findMany({
196
+ where: eq(organizationMembershipTable.userId, userId),
197
+ with: {
198
+ organization: true,
199
+ user: true,
200
+ },
201
+ })) || [],
202
+ )
203
+ },
204
+ getOrganizationMembers: async (organizationId) => {
205
+ return OrganizationMemberSchema.array().parse(
206
+ (await client.query.organizationMemberships.findMany({
207
+ where: eq(organizationMembershipTable.organizationId, organizationId),
208
+ with: {
209
+ organization: true,
210
+ user: true,
211
+ },
212
+ })) || [],
213
+ )
214
+ },
215
+ }
216
+ }