briyah 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (354) hide show
  1. package/.prettierrc +7 -0
  2. package/dist/ai/LLM/anthropic.module.d.ts +2 -0
  3. package/dist/ai/LLM/anthropic.module.js +21 -0
  4. package/dist/ai/LLM/anthropic.service.d.ts +27 -0
  5. package/dist/ai/LLM/anthropic.service.js +329 -0
  6. package/dist/ai/LLM/base-ai.service.d.ts +45 -0
  7. package/dist/ai/LLM/base-ai.service.js +409 -0
  8. package/dist/ai/LLM/deepseek.module.d.ts +2 -0
  9. package/dist/ai/LLM/deepseek.module.js +21 -0
  10. package/dist/ai/LLM/deepseek.service.d.ts +15 -0
  11. package/dist/ai/LLM/deepseek.service.js +279 -0
  12. package/dist/ai/LLM/fal.module.d.ts +2 -0
  13. package/dist/ai/LLM/fal.module.js +21 -0
  14. package/dist/ai/LLM/fal.service.d.ts +17 -0
  15. package/dist/ai/LLM/fal.service.js +314 -0
  16. package/dist/ai/LLM/googleai.module.d.ts +2 -0
  17. package/dist/ai/LLM/googleai.module.js +21 -0
  18. package/dist/ai/LLM/googleai.service.d.ts +20 -0
  19. package/dist/ai/LLM/googleai.service.js +271 -0
  20. package/dist/ai/LLM/grok.module.d.ts +2 -0
  21. package/dist/ai/LLM/grok.module.js +21 -0
  22. package/dist/ai/LLM/grok.service.d.ts +14 -0
  23. package/dist/ai/LLM/grok.service.js +263 -0
  24. package/dist/ai/LLM/mock.module.d.ts +2 -0
  25. package/dist/ai/LLM/mock.module.js +21 -0
  26. package/dist/ai/LLM/mock.service.d.ts +13 -0
  27. package/dist/ai/LLM/mock.service.js +194 -0
  28. package/dist/ai/LLM/openai.module.d.ts +2 -0
  29. package/dist/ai/LLM/openai.module.js +21 -0
  30. package/dist/ai/LLM/openai.service.d.ts +21 -0
  31. package/dist/ai/LLM/openai.service.js +421 -0
  32. package/dist/ai/LLM/together.module.d.ts +2 -0
  33. package/dist/ai/LLM/together.module.js +21 -0
  34. package/dist/ai/LLM/together.service.d.ts +18 -0
  35. package/dist/ai/LLM/together.service.js +285 -0
  36. package/dist/ai/LLM/vertexai.module.d.ts +2 -0
  37. package/dist/ai/LLM/vertexai.module.js +21 -0
  38. package/dist/ai/LLM/vertexai.service.d.ts +17 -0
  39. package/dist/ai/LLM/vertexai.service.js +323 -0
  40. package/dist/ai/agent-config.d.ts +32 -0
  41. package/dist/ai/agent-config.js +2 -0
  42. package/dist/ai/agent-factory.d.ts +15 -0
  43. package/dist/ai/agent-factory.js +24 -0
  44. package/dist/ai/agent-message.service.d.ts +9 -0
  45. package/dist/ai/agent-message.service.js +56 -0
  46. package/dist/ai/agent-store.module.d.ts +2 -0
  47. package/dist/ai/agent-store.module.js +21 -0
  48. package/dist/ai/agent-store.service.d.ts +31 -0
  49. package/dist/ai/agent-store.service.js +352 -0
  50. package/dist/ai/agent.d.ts +75 -0
  51. package/dist/ai/agent.js +174 -0
  52. package/dist/ai/ai-factory.module.d.ts +2 -0
  53. package/dist/ai/ai-factory.module.js +40 -0
  54. package/dist/ai/ai-factory.service.d.ts +24 -0
  55. package/dist/ai/ai-factory.service.js +94 -0
  56. package/dist/ai/artifact.module.d.ts +2 -0
  57. package/dist/ai/artifact.module.js +22 -0
  58. package/dist/ai/artifact.service.d.ts +21 -0
  59. package/dist/ai/artifact.service.js +239 -0
  60. package/dist/ai/attached-file.module.d.ts +2 -0
  61. package/dist/ai/attached-file.module.js +22 -0
  62. package/dist/ai/attached-file.service.d.ts +35 -0
  63. package/dist/ai/attached-file.service.js +253 -0
  64. package/dist/ai/model_prices.d.ts +2 -0
  65. package/dist/ai/model_prices.js +54 -0
  66. package/dist/ai/published-agents.service.d.ts +13 -0
  67. package/dist/ai/published-agents.service.js +118 -0
  68. package/dist/app/balance-message.service.d.ts +8 -0
  69. package/dist/app/balance-message.service.js +45 -0
  70. package/dist/app/balance.module.d.ts +2 -0
  71. package/dist/app/balance.module.js +20 -0
  72. package/dist/app/balance.service.d.ts +18 -0
  73. package/dist/app/balance.service.js +158 -0
  74. package/dist/app/stripe.controller.d.ts +31 -0
  75. package/dist/app/stripe.controller.js +150 -0
  76. package/dist/app/stripe.module.d.ts +2 -0
  77. package/dist/app/stripe.module.js +22 -0
  78. package/dist/app/stripe.service.d.ts +28 -0
  79. package/dist/app/stripe.service.js +198 -0
  80. package/dist/app/transaction.service.d.ts +15 -0
  81. package/dist/app/transaction.service.js +139 -0
  82. package/dist/app/user-service-factory.d.ts +21 -0
  83. package/dist/app/user-service-factory.js +81 -0
  84. package/dist/app/user-service-manager.d.ts +28 -0
  85. package/dist/app/user-service-manager.js +102 -0
  86. package/dist/app.controller.d.ts +232 -0
  87. package/dist/app.controller.js +2536 -0
  88. package/dist/app.module.d.ts +2 -0
  89. package/dist/app.module.js +45 -0
  90. package/dist/app.service.d.ts +186 -0
  91. package/dist/app.service.js +1334 -0
  92. package/dist/auth/agent-access.decorator.d.ts +2 -0
  93. package/dist/auth/agent-access.decorator.js +10 -0
  94. package/dist/auth/auth.controller.d.ts +38 -0
  95. package/dist/auth/auth.controller.js +356 -0
  96. package/dist/auth/auth.module.d.ts +2 -0
  97. package/dist/auth/auth.module.js +55 -0
  98. package/dist/auth/auth.service.d.ts +14 -0
  99. package/dist/auth/auth.service.js +84 -0
  100. package/dist/auth/dto/bot-login.dto.d.ts +4 -0
  101. package/dist/auth/dto/bot-login.dto.js +2 -0
  102. package/dist/auth/dto/password-reset.dto.d.ts +11 -0
  103. package/dist/auth/dto/password-reset.dto.js +2 -0
  104. package/dist/auth/dto/phone-login.dto.d.ts +8 -0
  105. package/dist/auth/dto/phone-login.dto.js +2 -0
  106. package/dist/auth/dto/phone-verification.dto.d.ts +9 -0
  107. package/dist/auth/dto/phone-verification.dto.js +2 -0
  108. package/dist/auth/jwt-auth.guard.d.ts +13 -0
  109. package/dist/auth/jwt-auth.guard.js +143 -0
  110. package/dist/auth/jwt.strategy.d.ts +17 -0
  111. package/dist/auth/jwt.strategy.js +45 -0
  112. package/dist/auth/phone-validation.service.d.ts +7 -0
  113. package/dist/auth/phone-validation.service.js +50 -0
  114. package/dist/auth/public.decorator.d.ts +2 -0
  115. package/dist/auth/public.decorator.js +7 -0
  116. package/dist/auth/rate-limit.service.d.ts +16 -0
  117. package/dist/auth/rate-limit.service.js +84 -0
  118. package/dist/auth/room-access.decorator.d.ts +2 -0
  119. package/dist/auth/room-access.decorator.js +10 -0
  120. package/dist/auth/scopes.decorator.d.ts +2 -0
  121. package/dist/auth/scopes.decorator.js +7 -0
  122. package/dist/auth/scopes.guard.d.ts +9 -0
  123. package/dist/auth/scopes.guard.js +82 -0
  124. package/dist/auth/session.guard.d.ts +7 -0
  125. package/dist/auth/session.guard.js +55 -0
  126. package/dist/auth/twilio.service.d.ts +15 -0
  127. package/dist/auth/twilio.service.js +139 -0
  128. package/dist/auth/users.service.d.ts +24 -0
  129. package/dist/auth/users.service.js +149 -0
  130. package/dist/common/errors.d.ts +12 -0
  131. package/dist/common/errors.js +43 -0
  132. package/dist/config/configuration.module.d.ts +2 -0
  133. package/dist/config/configuration.module.js +20 -0
  134. package/dist/config/configuration.service.d.ts +26 -0
  135. package/dist/config/configuration.service.js +131 -0
  136. package/dist/main.js +15749 -0
  137. package/dist/room/artifact-store.service.d.ts +15 -0
  138. package/dist/room/artifact-store.service.js +226 -0
  139. package/dist/room/artifact.d.ts +6 -0
  140. package/dist/room/artifact.js +2 -0
  141. package/dist/room/message.d.ts +19 -0
  142. package/dist/room/message.js +70 -0
  143. package/dist/room/published-rooms.service.d.ts +12 -0
  144. package/dist/room/published-rooms.service.js +108 -0
  145. package/dist/room/room-config.d.ts +12 -0
  146. package/dist/room/room-config.js +2 -0
  147. package/dist/room/room-factory.d.ts +7 -0
  148. package/dist/room/room-factory.js +16 -0
  149. package/dist/room/room-message.module.d.ts +2 -0
  150. package/dist/room/room-message.module.js +21 -0
  151. package/dist/room/room-message.service.d.ts +8 -0
  152. package/dist/room/room-message.service.js +44 -0
  153. package/dist/room/room-store.module.d.ts +2 -0
  154. package/dist/room/room-store.module.js +25 -0
  155. package/dist/room/room-store.service.d.ts +38 -0
  156. package/dist/room/room-store.service.js +353 -0
  157. package/dist/room/room.d.ts +107 -0
  158. package/dist/room/room.js +833 -0
  159. package/dist/sdk/briyah-config.d.ts +13 -0
  160. package/dist/sdk/briyah-config.js +32 -0
  161. package/dist/sdk/briyah.d.ts +36 -0
  162. package/dist/sdk/briyah.js +122 -0
  163. package/dist/sdk/index.d.ts +6 -0
  164. package/dist/sdk/index.js +11 -0
  165. package/dist/server/src/ai/LLM/anthropic.module.d.ts +2 -0
  166. package/dist/server/src/ai/LLM/anthropic.module.js +21 -0
  167. package/dist/server/src/ai/LLM/anthropic.service.d.ts +27 -0
  168. package/dist/server/src/ai/LLM/anthropic.service.js +329 -0
  169. package/dist/server/src/ai/LLM/base-ai.service.d.ts +45 -0
  170. package/dist/server/src/ai/LLM/base-ai.service.js +409 -0
  171. package/dist/server/src/ai/LLM/deepseek.module.d.ts +2 -0
  172. package/dist/server/src/ai/LLM/deepseek.module.js +21 -0
  173. package/dist/server/src/ai/LLM/deepseek.service.d.ts +15 -0
  174. package/dist/server/src/ai/LLM/deepseek.service.js +279 -0
  175. package/dist/server/src/ai/LLM/fal.module.d.ts +2 -0
  176. package/dist/server/src/ai/LLM/fal.module.js +21 -0
  177. package/dist/server/src/ai/LLM/fal.service.d.ts +17 -0
  178. package/dist/server/src/ai/LLM/fal.service.js +314 -0
  179. package/dist/server/src/ai/LLM/googleai.module.d.ts +2 -0
  180. package/dist/server/src/ai/LLM/googleai.module.js +21 -0
  181. package/dist/server/src/ai/LLM/googleai.service.d.ts +20 -0
  182. package/dist/server/src/ai/LLM/googleai.service.js +271 -0
  183. package/dist/server/src/ai/LLM/grok.module.d.ts +2 -0
  184. package/dist/server/src/ai/LLM/grok.module.js +21 -0
  185. package/dist/server/src/ai/LLM/grok.service.d.ts +14 -0
  186. package/dist/server/src/ai/LLM/grok.service.js +263 -0
  187. package/dist/server/src/ai/LLM/mock.module.d.ts +2 -0
  188. package/dist/server/src/ai/LLM/mock.module.js +21 -0
  189. package/dist/server/src/ai/LLM/mock.service.d.ts +13 -0
  190. package/dist/server/src/ai/LLM/mock.service.js +194 -0
  191. package/dist/server/src/ai/LLM/openai.module.d.ts +2 -0
  192. package/dist/server/src/ai/LLM/openai.module.js +21 -0
  193. package/dist/server/src/ai/LLM/openai.service.d.ts +21 -0
  194. package/dist/server/src/ai/LLM/openai.service.js +421 -0
  195. package/dist/server/src/ai/LLM/together.module.d.ts +2 -0
  196. package/dist/server/src/ai/LLM/together.module.js +21 -0
  197. package/dist/server/src/ai/LLM/together.service.d.ts +18 -0
  198. package/dist/server/src/ai/LLM/together.service.js +285 -0
  199. package/dist/server/src/ai/LLM/vertexai.module.d.ts +2 -0
  200. package/dist/server/src/ai/LLM/vertexai.module.js +21 -0
  201. package/dist/server/src/ai/LLM/vertexai.service.d.ts +17 -0
  202. package/dist/server/src/ai/LLM/vertexai.service.js +323 -0
  203. package/dist/server/src/ai/agent-config.d.ts +32 -0
  204. package/dist/server/src/ai/agent-config.js +2 -0
  205. package/dist/server/src/ai/agent-factory.d.ts +15 -0
  206. package/dist/server/src/ai/agent-factory.js +24 -0
  207. package/dist/server/src/ai/agent-message.service.d.ts +9 -0
  208. package/dist/server/src/ai/agent-message.service.js +56 -0
  209. package/dist/server/src/ai/agent-store.module.d.ts +2 -0
  210. package/dist/server/src/ai/agent-store.module.js +21 -0
  211. package/dist/server/src/ai/agent-store.service.d.ts +31 -0
  212. package/dist/server/src/ai/agent-store.service.js +352 -0
  213. package/dist/server/src/ai/agent.d.ts +75 -0
  214. package/dist/server/src/ai/agent.js +174 -0
  215. package/dist/server/src/ai/ai-factory.module.d.ts +2 -0
  216. package/dist/server/src/ai/ai-factory.module.js +40 -0
  217. package/dist/server/src/ai/ai-factory.service.d.ts +24 -0
  218. package/dist/server/src/ai/ai-factory.service.js +94 -0
  219. package/dist/server/src/ai/artifact.module.d.ts +2 -0
  220. package/dist/server/src/ai/artifact.module.js +22 -0
  221. package/dist/server/src/ai/artifact.service.d.ts +21 -0
  222. package/dist/server/src/ai/artifact.service.js +239 -0
  223. package/dist/server/src/ai/attached-file.module.d.ts +2 -0
  224. package/dist/server/src/ai/attached-file.module.js +22 -0
  225. package/dist/server/src/ai/attached-file.service.d.ts +35 -0
  226. package/dist/server/src/ai/attached-file.service.js +253 -0
  227. package/dist/server/src/ai/model_prices.d.ts +2 -0
  228. package/dist/server/src/ai/model_prices.js +54 -0
  229. package/dist/server/src/ai/published-agents.service.d.ts +13 -0
  230. package/dist/server/src/ai/published-agents.service.js +118 -0
  231. package/dist/server/src/app/balance-message.service.d.ts +8 -0
  232. package/dist/server/src/app/balance-message.service.js +45 -0
  233. package/dist/server/src/app/balance.module.d.ts +2 -0
  234. package/dist/server/src/app/balance.module.js +20 -0
  235. package/dist/server/src/app/balance.service.d.ts +18 -0
  236. package/dist/server/src/app/balance.service.js +158 -0
  237. package/dist/server/src/app/stripe.controller.d.ts +31 -0
  238. package/dist/server/src/app/stripe.controller.js +148 -0
  239. package/dist/server/src/app/stripe.module.d.ts +2 -0
  240. package/dist/server/src/app/stripe.module.js +22 -0
  241. package/dist/server/src/app/stripe.service.d.ts +28 -0
  242. package/dist/server/src/app/stripe.service.js +198 -0
  243. package/dist/server/src/app/transaction.service.d.ts +15 -0
  244. package/dist/server/src/app/transaction.service.js +139 -0
  245. package/dist/server/src/app/user-service-factory.d.ts +21 -0
  246. package/dist/server/src/app/user-service-factory.js +81 -0
  247. package/dist/server/src/app/user-service-manager.d.ts +28 -0
  248. package/dist/server/src/app/user-service-manager.js +102 -0
  249. package/dist/server/src/app.controller.d.ts +232 -0
  250. package/dist/server/src/app.controller.js +2534 -0
  251. package/dist/server/src/app.module.d.ts +2 -0
  252. package/dist/server/src/app.module.js +45 -0
  253. package/dist/server/src/app.service.d.ts +186 -0
  254. package/dist/server/src/app.service.js +1334 -0
  255. package/dist/server/src/auth/agent-access.decorator.d.ts +2 -0
  256. package/dist/server/src/auth/agent-access.decorator.js +10 -0
  257. package/dist/server/src/auth/auth.controller.d.ts +38 -0
  258. package/dist/server/src/auth/auth.controller.js +356 -0
  259. package/dist/server/src/auth/auth.module.d.ts +2 -0
  260. package/dist/server/src/auth/auth.module.js +55 -0
  261. package/dist/server/src/auth/auth.service.d.ts +14 -0
  262. package/dist/server/src/auth/auth.service.js +84 -0
  263. package/dist/server/src/auth/dto/bot-login.dto.d.ts +4 -0
  264. package/dist/server/src/auth/dto/bot-login.dto.js +2 -0
  265. package/dist/server/src/auth/dto/password-reset.dto.d.ts +11 -0
  266. package/dist/server/src/auth/dto/password-reset.dto.js +2 -0
  267. package/dist/server/src/auth/dto/phone-login.dto.d.ts +8 -0
  268. package/dist/server/src/auth/dto/phone-login.dto.js +2 -0
  269. package/dist/server/src/auth/dto/phone-verification.dto.d.ts +9 -0
  270. package/dist/server/src/auth/dto/phone-verification.dto.js +2 -0
  271. package/dist/server/src/auth/jwt-auth.guard.d.ts +13 -0
  272. package/dist/server/src/auth/jwt-auth.guard.js +143 -0
  273. package/dist/server/src/auth/jwt.strategy.d.ts +17 -0
  274. package/dist/server/src/auth/jwt.strategy.js +45 -0
  275. package/dist/server/src/auth/phone-validation.service.d.ts +7 -0
  276. package/dist/server/src/auth/phone-validation.service.js +50 -0
  277. package/dist/server/src/auth/public.decorator.d.ts +2 -0
  278. package/dist/server/src/auth/public.decorator.js +7 -0
  279. package/dist/server/src/auth/rate-limit.service.d.ts +16 -0
  280. package/dist/server/src/auth/rate-limit.service.js +84 -0
  281. package/dist/server/src/auth/room-access.decorator.d.ts +2 -0
  282. package/dist/server/src/auth/room-access.decorator.js +10 -0
  283. package/dist/server/src/auth/scopes.decorator.d.ts +2 -0
  284. package/dist/server/src/auth/scopes.decorator.js +7 -0
  285. package/dist/server/src/auth/scopes.guard.d.ts +9 -0
  286. package/dist/server/src/auth/scopes.guard.js +82 -0
  287. package/dist/server/src/auth/session.guard.d.ts +7 -0
  288. package/dist/server/src/auth/session.guard.js +55 -0
  289. package/dist/server/src/auth/twilio.service.d.ts +15 -0
  290. package/dist/server/src/auth/twilio.service.js +139 -0
  291. package/dist/server/src/auth/users.service.d.ts +24 -0
  292. package/dist/server/src/auth/users.service.js +149 -0
  293. package/dist/server/src/common/errors.d.ts +12 -0
  294. package/dist/server/src/common/errors.js +43 -0
  295. package/dist/server/src/config/configuration.module.d.ts +2 -0
  296. package/dist/server/src/config/configuration.module.js +20 -0
  297. package/dist/server/src/config/configuration.service.d.ts +26 -0
  298. package/dist/server/src/config/configuration.service.js +131 -0
  299. package/dist/server/src/room/artifact-store.service.d.ts +15 -0
  300. package/dist/server/src/room/artifact-store.service.js +226 -0
  301. package/dist/server/src/room/artifact.d.ts +6 -0
  302. package/dist/server/src/room/artifact.js +2 -0
  303. package/dist/server/src/room/message.d.ts +19 -0
  304. package/dist/server/src/room/message.js +70 -0
  305. package/dist/server/src/room/published-rooms.service.d.ts +12 -0
  306. package/dist/server/src/room/published-rooms.service.js +108 -0
  307. package/dist/server/src/room/room-config.d.ts +12 -0
  308. package/dist/server/src/room/room-config.js +2 -0
  309. package/dist/server/src/room/room-factory.d.ts +7 -0
  310. package/dist/server/src/room/room-factory.js +16 -0
  311. package/dist/server/src/room/room-message.module.d.ts +2 -0
  312. package/dist/server/src/room/room-message.module.js +21 -0
  313. package/dist/server/src/room/room-message.service.d.ts +8 -0
  314. package/dist/server/src/room/room-message.service.js +44 -0
  315. package/dist/server/src/room/room-store.module.d.ts +2 -0
  316. package/dist/server/src/room/room-store.module.js +25 -0
  317. package/dist/server/src/room/room-store.service.d.ts +38 -0
  318. package/dist/server/src/room/room-store.service.js +353 -0
  319. package/dist/server/src/room/room.d.ts +107 -0
  320. package/dist/server/src/room/room.js +833 -0
  321. package/dist/server/src/sdk/briyah-config.d.ts +13 -0
  322. package/dist/server/src/sdk/briyah-config.js +32 -0
  323. package/dist/server/src/sdk/briyah.d.ts +36 -0
  324. package/dist/server/src/sdk/briyah.js +122 -0
  325. package/dist/server/src/sdk/index.d.ts +6 -0
  326. package/dist/server/src/sdk/index.js +11 -0
  327. package/dist/server/src/shared/shared.module.d.ts +2 -0
  328. package/dist/server/src/shared/shared.module.js +46 -0
  329. package/dist/server/src/story/story-message.service.d.ts +10 -0
  330. package/dist/server/src/story/story-message.service.js +57 -0
  331. package/dist/server/src/story/story-progress.service.d.ts +8 -0
  332. package/dist/server/src/story/story-progress.service.js +44 -0
  333. package/dist/server/src/story/story-store.module.d.ts +2 -0
  334. package/dist/server/src/story/story-store.module.js +22 -0
  335. package/dist/server/src/story/story-store.service.d.ts +23 -0
  336. package/dist/server/src/story/story-store.service.js +392 -0
  337. package/dist/server/src/story/story.service.d.ts +94 -0
  338. package/dist/server/src/story/story.service.js +1797 -0
  339. package/dist/shared/shared.module.d.ts +2 -0
  340. package/dist/shared/shared.module.js +46 -0
  341. package/dist/shared/types/app.types.d.ts +296 -0
  342. package/dist/shared/types/app.types.js +17 -0
  343. package/dist/story/story-message.service.d.ts +10 -0
  344. package/dist/story/story-message.service.js +57 -0
  345. package/dist/story/story-progress.service.d.ts +8 -0
  346. package/dist/story/story-progress.service.js +44 -0
  347. package/dist/story/story-store.module.d.ts +2 -0
  348. package/dist/story/story-store.module.js +22 -0
  349. package/dist/story/story-store.service.d.ts +23 -0
  350. package/dist/story/story-store.service.js +392 -0
  351. package/dist/story/story.service.d.ts +94 -0
  352. package/dist/story/story.service.js +1797 -0
  353. package/eslint.config.js +29 -0
  354. package/package.json +125 -0
@@ -0,0 +1,158 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.BalanceService = void 0;
37
+ const fs = __importStar(require("fs"));
38
+ const path = __importStar(require("path"));
39
+ class BalanceService {
40
+ userId;
41
+ configService;
42
+ balanceFilePath;
43
+ markupFilePath;
44
+ startingBalance;
45
+ balanceMessageService;
46
+ constructor(userId, configService, balanceMessageService) {
47
+ this.userId = userId;
48
+ this.configService = configService;
49
+ this.balanceMessageService = balanceMessageService;
50
+ if (!process.env.STARTING_BALANCE) {
51
+ throw new Error('STARTING_BALANCE environment variable is required');
52
+ }
53
+ this.startingBalance = parseFloat(process.env.STARTING_BALANCE);
54
+ if (isNaN(this.startingBalance)) {
55
+ throw new Error('STARTING_BALANCE must be a valid number');
56
+ }
57
+ if (this.startingBalance < 0) {
58
+ throw new Error('STARTING_BALANCE must be non-negative');
59
+ }
60
+ const userconfigPath = this.configService.getUserConfigDir();
61
+ this.balanceFilePath = path.join(userconfigPath, 'balance');
62
+ const commonConfigPath = this.configService.getCommonConfigDir();
63
+ this.markupFilePath = path.join(commonConfigPath, 'markup');
64
+ }
65
+ getBalance() {
66
+ try {
67
+ if (!fs.existsSync(this.balanceFilePath)) {
68
+ this.saveBalance(this.startingBalance);
69
+ return this.startingBalance;
70
+ }
71
+ const balanceStr = fs.readFileSync(this.balanceFilePath, 'utf-8').trim();
72
+ const balance = parseFloat(balanceStr);
73
+ if (isNaN(balance)) {
74
+ console.error(`Balance file corrupted for user ${this.userId}, resetting to starting balance`);
75
+ this.saveBalance(this.startingBalance);
76
+ return this.startingBalance;
77
+ }
78
+ return balance;
79
+ }
80
+ catch (error) {
81
+ console.error(`Error reading balance for user ${this.userId}:`, error);
82
+ return this.startingBalance;
83
+ }
84
+ }
85
+ getMarkup() {
86
+ try {
87
+ if (!fs.existsSync(this.markupFilePath)) {
88
+ this.saveMarkup(0);
89
+ return 0;
90
+ }
91
+ const markupStr = fs.readFileSync(this.markupFilePath, 'utf-8').trim();
92
+ const markup = parseFloat(markupStr);
93
+ if (isNaN(markup)) {
94
+ console.error(`Markup file corrupted, resetting to 0`);
95
+ this.saveMarkup(0);
96
+ return 0;
97
+ }
98
+ return markup;
99
+ }
100
+ catch (error) {
101
+ console.error(`Error reading markup:`, error);
102
+ return 0;
103
+ }
104
+ }
105
+ hasSufficientBalance() {
106
+ const balance = this.getBalance();
107
+ return balance >= 0;
108
+ }
109
+ decrementBalance(amount, markup = 0) {
110
+ if (amount < 0) {
111
+ throw new Error('Amount must be positive');
112
+ }
113
+ const currentBalance = this.getBalance();
114
+ const newBalance = currentBalance - amount;
115
+ this.saveBalance(newBalance);
116
+ const currentMarkup = this.getMarkup();
117
+ const newMarkup = currentMarkup + markup;
118
+ this.saveMarkup(newMarkup);
119
+ }
120
+ addBalance(amount) {
121
+ if (amount <= 0) {
122
+ throw new Error('Amount must be positive');
123
+ }
124
+ const currentBalance = this.getBalance();
125
+ const newBalance = currentBalance + amount;
126
+ this.saveBalance(newBalance);
127
+ }
128
+ saveBalance(balance) {
129
+ try {
130
+ const userconfigPath = this.configService.getUserConfigDir();
131
+ if (!fs.existsSync(userconfigPath)) {
132
+ fs.mkdirSync(userconfigPath, { recursive: true });
133
+ }
134
+ fs.writeFileSync(this.balanceFilePath, balance.toString(), 'utf-8');
135
+ if (this.balanceMessageService) {
136
+ this.balanceMessageService.emitUpdate(this.userId, balance);
137
+ }
138
+ }
139
+ catch (error) {
140
+ console.error(`Error saving balance for user ${this.userId}:`, error);
141
+ throw error;
142
+ }
143
+ }
144
+ saveMarkup(markup) {
145
+ try {
146
+ const commonConfigPath = this.configService.getCommonConfigDir();
147
+ if (!fs.existsSync(commonConfigPath)) {
148
+ fs.mkdirSync(commonConfigPath, { recursive: true });
149
+ }
150
+ fs.writeFileSync(this.markupFilePath, markup.toString(), 'utf-8');
151
+ }
152
+ catch (error) {
153
+ console.error(`Error saving markup:`, error);
154
+ throw error;
155
+ }
156
+ }
157
+ }
158
+ exports.BalanceService = BalanceService;
@@ -0,0 +1,31 @@
1
+ import { RawBodyRequest } from '@nestjs/common';
2
+ import { StripeService } from './stripe.service';
3
+ import { UserServiceManager } from './user-service-manager';
4
+ import type { Request as ExpressRequest } from 'express';
5
+ import { CreatePaymentIntentRequest, CreatePaymentIntentResponse, TransactionHistoryResponse, CreateCheckoutSessionRequest, CreateCheckoutSessionResponse } from '@shared/types/app.types';
6
+ export declare class StripeController {
7
+ private readonly stripeService;
8
+ private readonly userServiceManager;
9
+ constructor(stripeService: StripeService, userServiceManager: UserServiceManager);
10
+ createPaymentIntent(req: ExpressRequest & {
11
+ user: {
12
+ userId: string;
13
+ };
14
+ }, body: CreatePaymentIntentRequest): Promise<CreatePaymentIntentResponse>;
15
+ createCheckoutSession(req: ExpressRequest & {
16
+ user: {
17
+ userId: string;
18
+ };
19
+ }, body: CreateCheckoutSessionRequest): Promise<CreateCheckoutSessionResponse>;
20
+ getStripeConfig(): Promise<{
21
+ publishableKey: string;
22
+ }>;
23
+ handleWebhook(req: RawBodyRequest<ExpressRequest>, signature: string): Promise<{
24
+ received: boolean;
25
+ }>;
26
+ getTransactionHistory(req: ExpressRequest & {
27
+ user: {
28
+ userId: string;
29
+ };
30
+ }, limit?: string, offset?: string): Promise<TransactionHistoryResponse>;
31
+ }
@@ -0,0 +1,150 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ var _a, _b;
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.StripeController = void 0;
17
+ const common_1 = require("@nestjs/common");
18
+ const stripe_service_1 = require("./stripe.service");
19
+ const user_service_manager_1 = require("./user-service-manager");
20
+ const jwt_auth_guard_1 = require("../auth/jwt-auth.guard");
21
+ const public_decorator_1 = require("../auth/public.decorator");
22
+ const app_types_1 = require("@shared/types/app.types");
23
+ let StripeController = class StripeController {
24
+ stripeService;
25
+ userServiceManager;
26
+ constructor(stripeService, userServiceManager) {
27
+ this.stripeService = stripeService;
28
+ this.userServiceManager = userServiceManager;
29
+ }
30
+ async createPaymentIntent(req, body) {
31
+ const userId = req.user.userId;
32
+ const { amount } = body;
33
+ const appService = this.userServiceManager.getAppService(userId);
34
+ const transactionService = appService.getTransactionService();
35
+ const result = await this.stripeService.createPaymentIntent(userId, amount, transactionService);
36
+ return result;
37
+ }
38
+ async createCheckoutSession(req, body) {
39
+ const userId = req.user.userId;
40
+ const { amount } = body;
41
+ const appService = this.userServiceManager.getAppService(userId);
42
+ const transactionService = appService.getTransactionService();
43
+ const result = await this.stripeService.createCheckoutSession(userId, amount, transactionService);
44
+ return result;
45
+ }
46
+ async getStripeConfig() {
47
+ return this.stripeService.getStripeConfig();
48
+ }
49
+ async handleWebhook(req, signature) {
50
+ console.log('[Stripe Webhook] Received webhook request');
51
+ if (!signature) {
52
+ console.error('[Stripe Webhook] Missing stripe-signature header');
53
+ throw new common_1.BadRequestException('Missing stripe-signature header');
54
+ }
55
+ const rawBody = req.rawBody;
56
+ if (!rawBody) {
57
+ console.error('[Stripe Webhook] Missing request body');
58
+ throw new common_1.BadRequestException('Missing request body');
59
+ }
60
+ console.log('[Stripe Webhook] Verifying webhook signature...');
61
+ const event = this.stripeService.verifyWebhookSignature(rawBody, signature);
62
+ console.log(`[Stripe Webhook] Event type: ${event.type}, Event ID: ${event.id}`);
63
+ switch (event.type) {
64
+ case 'payment_intent.succeeded': {
65
+ console.log('[Stripe Webhook] Matched payment_intent.succeeded case');
66
+ const paymentIntent = event.data.object;
67
+ const userId = paymentIntent.metadata.userId;
68
+ if (!userId) {
69
+ console.error('Payment Intent missing userId in metadata');
70
+ return { received: true };
71
+ }
72
+ const appService = this.userServiceManager.getAppService(userId);
73
+ await this.stripeService.handlePaymentIntentSucceeded(paymentIntent, appService);
74
+ break;
75
+ }
76
+ case 'payment_intent.payment_failed': {
77
+ console.log('[Stripe Webhook] Matched payment_intent.payment_failed case');
78
+ const paymentIntent = event.data.object;
79
+ const userId = paymentIntent.metadata.userId;
80
+ if (!userId) {
81
+ console.error('[Stripe Webhook] Payment Intent missing userId in metadata');
82
+ return { received: true };
83
+ }
84
+ const appService = this.userServiceManager.getAppService(userId);
85
+ await this.stripeService.handlePaymentIntentFailed(paymentIntent, appService);
86
+ break;
87
+ }
88
+ default:
89
+ console.log(`[Stripe Webhook] Unhandled event type: ${event.type}`);
90
+ }
91
+ console.log('[Stripe Webhook] Webhook processed successfully');
92
+ return { received: true };
93
+ }
94
+ async getTransactionHistory(req, limit, offset) {
95
+ const userId = req.user.userId;
96
+ const parsedLimit = limit ? parseInt(limit, 10) : 50;
97
+ const parsedOffset = offset ? parseInt(offset, 10) : 0;
98
+ const appService = this.userServiceManager.getAppService(userId);
99
+ const transactionService = appService.getTransactionService();
100
+ const result = await transactionService.getTransactions(parsedLimit, parsedOffset);
101
+ return result;
102
+ }
103
+ };
104
+ exports.StripeController = StripeController;
105
+ __decorate([
106
+ (0, common_1.Post)('create-payment-intent'),
107
+ __param(0, (0, common_1.Request)()),
108
+ __param(1, (0, common_1.Body)()),
109
+ __metadata("design:type", Function),
110
+ __metadata("design:paramtypes", [Object, typeof (_a = typeof app_types_1.CreatePaymentIntentRequest !== "undefined" && app_types_1.CreatePaymentIntentRequest) === "function" ? _a : Object]),
111
+ __metadata("design:returntype", Promise)
112
+ ], StripeController.prototype, "createPaymentIntent", null);
113
+ __decorate([
114
+ (0, common_1.Post)('create-checkout-session'),
115
+ __param(0, (0, common_1.Request)()),
116
+ __param(1, (0, common_1.Body)()),
117
+ __metadata("design:type", Function),
118
+ __metadata("design:paramtypes", [Object, typeof (_b = typeof app_types_1.CreateCheckoutSessionRequest !== "undefined" && app_types_1.CreateCheckoutSessionRequest) === "function" ? _b : Object]),
119
+ __metadata("design:returntype", Promise)
120
+ ], StripeController.prototype, "createCheckoutSession", null);
121
+ __decorate([
122
+ (0, common_1.Get)('config'),
123
+ __metadata("design:type", Function),
124
+ __metadata("design:paramtypes", []),
125
+ __metadata("design:returntype", Promise)
126
+ ], StripeController.prototype, "getStripeConfig", null);
127
+ __decorate([
128
+ (0, public_decorator_1.Public)(),
129
+ (0, common_1.Post)('webhook'),
130
+ __param(0, (0, common_1.Request)()),
131
+ __param(1, (0, common_1.Headers)('stripe-signature')),
132
+ __metadata("design:type", Function),
133
+ __metadata("design:paramtypes", [Object, String]),
134
+ __metadata("design:returntype", Promise)
135
+ ], StripeController.prototype, "handleWebhook", null);
136
+ __decorate([
137
+ (0, common_1.Get)('transactions/history'),
138
+ __param(0, (0, common_1.Request)()),
139
+ __param(1, (0, common_1.Query)('limit')),
140
+ __param(2, (0, common_1.Query)('offset')),
141
+ __metadata("design:type", Function),
142
+ __metadata("design:paramtypes", [Object, String, String]),
143
+ __metadata("design:returntype", Promise)
144
+ ], StripeController.prototype, "getTransactionHistory", null);
145
+ exports.StripeController = StripeController = __decorate([
146
+ (0, common_1.Controller)('api/stripe'),
147
+ (0, common_1.UseGuards)(jwt_auth_guard_1.JwtAuthGuard),
148
+ __metadata("design:paramtypes", [stripe_service_1.StripeService,
149
+ user_service_manager_1.UserServiceManager])
150
+ ], StripeController);
@@ -0,0 +1,2 @@
1
+ export declare class StripeModule {
2
+ }
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.StripeModule = void 0;
10
+ const common_1 = require("@nestjs/common");
11
+ const stripe_service_1 = require("./stripe.service");
12
+ const stripe_controller_1 = require("./stripe.controller");
13
+ let StripeModule = class StripeModule {
14
+ };
15
+ exports.StripeModule = StripeModule;
16
+ exports.StripeModule = StripeModule = __decorate([
17
+ (0, common_1.Module)({
18
+ providers: [stripe_service_1.StripeService],
19
+ controllers: [stripe_controller_1.StripeController],
20
+ exports: [stripe_service_1.StripeService],
21
+ })
22
+ ], StripeModule);
@@ -0,0 +1,28 @@
1
+ import Stripe from 'stripe';
2
+ import { TransactionService } from './transaction.service';
3
+ import { AppService } from '../app.service';
4
+ type StripeInstance = ReturnType<typeof Stripe>;
5
+ type StripeEvent = ReturnType<StripeInstance['webhooks']['constructEvent']>;
6
+ type StripePaymentIntent = Awaited<ReturnType<StripeInstance['paymentIntents']['retrieve']>>;
7
+ export declare class StripeService {
8
+ private stripe;
9
+ private minPaymentAmount;
10
+ private maxPaymentAmount;
11
+ private publishableKey;
12
+ private webhookSecret;
13
+ constructor();
14
+ createPaymentIntent(userId: string, amount: number, transactionService: TransactionService): Promise<{
15
+ clientSecret: string;
16
+ publishableKey: string;
17
+ }>;
18
+ createCheckoutSession(userId: string, amount: number, transactionService: TransactionService): Promise<{
19
+ url: string;
20
+ }>;
21
+ getStripeConfig(): {
22
+ publishableKey: string;
23
+ };
24
+ verifyWebhookSignature(rawBody: Buffer | string, signature: string): StripeEvent;
25
+ handlePaymentIntentSucceeded(paymentIntent: StripePaymentIntent, appService: AppService): Promise<void>;
26
+ handlePaymentIntentFailed(paymentIntent: StripePaymentIntent, appService: AppService): Promise<void>;
27
+ }
28
+ export {};
@@ -0,0 +1,198 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.StripeService = void 0;
16
+ const common_1 = require("@nestjs/common");
17
+ const stripe_1 = __importDefault(require("stripe"));
18
+ let StripeService = class StripeService {
19
+ stripe;
20
+ minPaymentAmount;
21
+ maxPaymentAmount;
22
+ publishableKey;
23
+ webhookSecret;
24
+ constructor() {
25
+ if (!process.env.STRIPE_SECRET_KEY) {
26
+ throw new Error('STRIPE_SECRET_KEY environment variable is required');
27
+ }
28
+ if (!process.env.STRIPE_PUBLISHABLE_KEY) {
29
+ throw new Error('STRIPE_PUBLISHABLE_KEY environment variable is required');
30
+ }
31
+ if (!process.env.STRIPE_WEBHOOK_SECRET) {
32
+ throw new Error('STRIPE_WEBHOOK_SECRET environment variable is required');
33
+ }
34
+ this.stripe = (0, stripe_1.default)(process.env.STRIPE_SECRET_KEY, {
35
+ apiVersion: '2026-03-25.dahlia',
36
+ });
37
+ this.publishableKey = process.env.STRIPE_PUBLISHABLE_KEY;
38
+ this.webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;
39
+ this.minPaymentAmount = parseFloat(process.env.MIN_PAYMENT_AMOUNT || '5.00');
40
+ this.maxPaymentAmount = parseFloat(process.env.MAX_PAYMENT_AMOUNT || '500.00');
41
+ }
42
+ async createPaymentIntent(userId, amount, transactionService) {
43
+ if (!amount || amount <= 0) {
44
+ throw new common_1.BadRequestException('Amount must be greater than 0');
45
+ }
46
+ if (amount < this.minPaymentAmount) {
47
+ throw new common_1.BadRequestException(`Amount must be at least $${this.minPaymentAmount.toFixed(2)}`);
48
+ }
49
+ if (amount > this.maxPaymentAmount) {
50
+ throw new common_1.BadRequestException(`Amount must not exceed $${this.maxPaymentAmount.toFixed(2)}`);
51
+ }
52
+ try {
53
+ const amountInCents = Math.round(amount * 100);
54
+ const paymentIntent = await this.stripe.paymentIntents.create({
55
+ amount: amountInCents,
56
+ currency: 'usd',
57
+ metadata: {
58
+ userId,
59
+ },
60
+ automatic_payment_methods: {
61
+ enabled: true,
62
+ },
63
+ });
64
+ await transactionService.recordTransaction(amount, paymentIntent.id, 'pending');
65
+ return {
66
+ clientSecret: paymentIntent.client_secret,
67
+ publishableKey: this.publishableKey,
68
+ };
69
+ }
70
+ catch (error) {
71
+ console.error('Error creating Payment Intent:', error);
72
+ throw new common_1.InternalServerErrorException('Failed to create payment intent');
73
+ }
74
+ }
75
+ async createCheckoutSession(userId, amount, transactionService) {
76
+ if (!amount || amount <= 0) {
77
+ throw new common_1.BadRequestException('Amount must be greater than 0');
78
+ }
79
+ if (amount < this.minPaymentAmount) {
80
+ throw new common_1.BadRequestException(`Amount must be at least $${this.minPaymentAmount.toFixed(2)}`);
81
+ }
82
+ if (amount > this.maxPaymentAmount) {
83
+ throw new common_1.BadRequestException(`Amount must not exceed $${this.maxPaymentAmount.toFixed(2)}`);
84
+ }
85
+ try {
86
+ const amountInCents = Math.round(amount * 100);
87
+ const session = await this.stripe.checkout.sessions.create({
88
+ payment_method_types: ['card'],
89
+ line_items: [
90
+ {
91
+ price_data: {
92
+ currency: 'usd',
93
+ product_data: {
94
+ name: 'Briyah Credits',
95
+ description: `Add $${amount.toFixed(2)} to your Briyah balance`,
96
+ },
97
+ unit_amount: amountInCents,
98
+ },
99
+ quantity: 1,
100
+ },
101
+ ],
102
+ mode: 'payment',
103
+ success_url: `${process.env.WEB_URL || 'http://localhost:5173'}/payment-success?session_id={CHECKOUT_SESSION_ID}`,
104
+ cancel_url: `${process.env.WEB_URL || 'http://localhost:5173'}/payment-cancelled`,
105
+ metadata: {
106
+ userId,
107
+ },
108
+ payment_intent_data: {
109
+ metadata: {
110
+ userId,
111
+ },
112
+ },
113
+ });
114
+ if (session.payment_intent) {
115
+ await transactionService.recordTransaction(amount, session.payment_intent, 'pending');
116
+ }
117
+ return {
118
+ url: session.url,
119
+ };
120
+ }
121
+ catch (error) {
122
+ console.error('Error creating Checkout Session:', error);
123
+ throw new common_1.InternalServerErrorException('Failed to create checkout session');
124
+ }
125
+ }
126
+ getStripeConfig() {
127
+ return {
128
+ publishableKey: this.publishableKey,
129
+ };
130
+ }
131
+ verifyWebhookSignature(rawBody, signature) {
132
+ try {
133
+ return this.stripe.webhooks.constructEvent(rawBody, signature, this.webhookSecret);
134
+ }
135
+ catch (error) {
136
+ console.error('Webhook signature verification failed:', error);
137
+ throw new common_1.BadRequestException('Invalid webhook signature');
138
+ }
139
+ }
140
+ async handlePaymentIntentSucceeded(paymentIntent, appService) {
141
+ try {
142
+ const userId = paymentIntent.metadata.userId;
143
+ console.log(`[Stripe Webhook] Processing payment_intent.succeeded for user ${userId}, payment ${paymentIntent.id}`);
144
+ if (!userId) {
145
+ console.error('Payment Intent missing userId in metadata:', paymentIntent.id);
146
+ return;
147
+ }
148
+ const amount = paymentIntent.amount / 100;
149
+ console.log(`[Stripe Webhook] Amount: $${amount.toFixed(2)}`);
150
+ const transactionService = appService.getTransactionService();
151
+ const existingTransaction = await transactionService.getTransactionByPaymentIntent(paymentIntent.id);
152
+ console.log(`[Stripe Webhook] Existing transaction:`, existingTransaction);
153
+ if (existingTransaction?.status === 'succeeded') {
154
+ console.log(`Transaction ${paymentIntent.id} already processed, skipping`);
155
+ return;
156
+ }
157
+ const balanceBefore = await appService.getBalance();
158
+ console.log(`[Stripe Webhook] Balance before: $${balanceBefore.toFixed(2)}`);
159
+ console.log(`[Stripe Webhook] Adding $${amount.toFixed(2)} to balance`);
160
+ await appService.addBalance(amount);
161
+ const balanceAfter = await appService.getBalance();
162
+ console.log(`[Stripe Webhook] Balance after: $${balanceAfter.toFixed(2)}`);
163
+ console.log(`[Stripe Webhook] Updating transaction status to 'succeeded'`);
164
+ await transactionService.updateTransactionStatus(paymentIntent.id, 'succeeded');
165
+ console.log(`[Stripe Webhook] Checking for paused stories to resume...`);
166
+ const resumedCount = await appService.resumePausedStories();
167
+ if (resumedCount > 0) {
168
+ console.log(`[Stripe Webhook] Resumed ${resumedCount} paused story/stories`);
169
+ }
170
+ console.log(`[Stripe Webhook] Payment succeeded: $${amount.toFixed(2)} added to user ${userId} (balance: ${balanceBefore.toFixed(2)} → ${balanceAfter.toFixed(2)})`);
171
+ }
172
+ catch (error) {
173
+ console.error('[Stripe Webhook] Error handling payment success:', error);
174
+ throw error;
175
+ }
176
+ }
177
+ async handlePaymentIntentFailed(paymentIntent, appService) {
178
+ try {
179
+ const userId = paymentIntent.metadata.userId;
180
+ if (!userId) {
181
+ console.error('Payment Intent missing userId in metadata:', paymentIntent.id);
182
+ return;
183
+ }
184
+ const transactionService = appService.getTransactionService();
185
+ await transactionService.updateTransactionStatus(paymentIntent.id, 'failed');
186
+ console.log(`Payment failed for user ${userId}: ${paymentIntent.id}`);
187
+ }
188
+ catch (error) {
189
+ console.error('Error handling payment failure:', error);
190
+ throw error;
191
+ }
192
+ }
193
+ };
194
+ exports.StripeService = StripeService;
195
+ exports.StripeService = StripeService = __decorate([
196
+ (0, common_1.Injectable)(),
197
+ __metadata("design:paramtypes", [])
198
+ ], StripeService);
@@ -0,0 +1,15 @@
1
+ import { ConfigurationService } from '../config/configuration.service';
2
+ import { Transaction } from '@shared/types/app.types';
3
+ export declare class TransactionService {
4
+ private userId;
5
+ private configService;
6
+ private transactionsDir;
7
+ constructor(userId: string, configService: ConfigurationService);
8
+ recordTransaction(amount: number, stripePaymentIntentId: string, status?: 'pending' | 'succeeded' | 'failed' | 'cancelled'): Promise<Transaction>;
9
+ updateTransactionStatus(paymentIntentId: string, status: 'pending' | 'succeeded' | 'failed' | 'cancelled'): Promise<void>;
10
+ getTransactionByPaymentIntent(paymentIntentId: string): Promise<Transaction | null>;
11
+ getTransactions(limit?: number, offset?: number): Promise<{
12
+ transactions: Transaction[];
13
+ total: number;
14
+ }>;
15
+ }