@m5kdev/backend 0.1.0 → 0.1.2

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 (294) hide show
  1. package/.cursor/rules/backend.mdc +70 -0
  2. package/.turbo/turbo-build.log +5 -0
  3. package/.turbo/turbo-check-types.log +5 -0
  4. package/.turbo/turbo-lint$colon$fix.log +255 -0
  5. package/CHANGELOG.md +19 -0
  6. package/dist/src/lib/posthog.d.ts +3 -0
  7. package/dist/src/lib/posthog.d.ts.map +1 -0
  8. package/dist/src/lib/posthog.js +7 -0
  9. package/dist/src/lib/sentry.d.ts +2 -0
  10. package/dist/src/lib/sentry.d.ts.map +1 -0
  11. package/dist/src/lib/sentry.js +9 -0
  12. package/dist/src/modules/access/access.repository.d.ts +2348 -0
  13. package/dist/src/modules/access/access.repository.d.ts.map +1 -0
  14. package/dist/src/modules/access/access.repository.js +32 -0
  15. package/dist/src/modules/access/access.service.d.ts +22 -0
  16. package/dist/src/modules/access/access.service.d.ts.map +1 -0
  17. package/dist/src/modules/access/access.service.js +51 -0
  18. package/dist/src/modules/access/access.test.d.ts +2 -0
  19. package/dist/src/modules/access/access.test.d.ts.map +1 -0
  20. package/dist/src/modules/access/access.test.js +182 -0
  21. package/dist/src/modules/access/access.utils.d.ts +17 -0
  22. package/dist/src/modules/access/access.utils.d.ts.map +1 -0
  23. package/dist/src/modules/access/access.utils.js +20 -0
  24. package/dist/src/modules/ai/ai.db.d.ts +396 -0
  25. package/dist/src/modules/ai/ai.db.d.ts.map +1 -0
  26. package/dist/src/modules/ai/ai.db.js +39 -0
  27. package/dist/src/modules/ai/ai.prompt.d.ts +28 -0
  28. package/dist/src/modules/ai/ai.prompt.d.ts.map +1 -0
  29. package/dist/src/modules/ai/ai.prompt.js +30 -0
  30. package/dist/src/modules/ai/ai.repository.d.ts +424 -0
  31. package/dist/src/modules/ai/ai.repository.d.ts.map +1 -0
  32. package/dist/src/modules/ai/ai.repository.js +26 -0
  33. package/dist/src/modules/ai/ai.router.d.ts +2 -0
  34. package/dist/src/modules/ai/ai.router.d.ts.map +1 -0
  35. package/dist/src/modules/ai/ai.router.js +132 -0
  36. package/dist/src/modules/ai/ai.service.d.ts +115 -0
  37. package/dist/src/modules/ai/ai.service.d.ts.map +1 -0
  38. package/dist/src/modules/ai/ai.service.js +207 -0
  39. package/dist/src/modules/ai/ai.trpc.d.ts +59 -0
  40. package/dist/src/modules/ai/ai.trpc.d.ts.map +1 -0
  41. package/dist/src/modules/ai/ai.trpc.js +20 -0
  42. package/dist/src/modules/ai/ideogram/ideogram.constants.d.ts +8 -0
  43. package/dist/src/modules/ai/ideogram/ideogram.constants.d.ts.map +1 -0
  44. package/dist/src/modules/ai/ideogram/ideogram.constants.js +167 -0
  45. package/dist/src/modules/ai/ideogram/ideogram.dto.d.ts +230 -0
  46. package/dist/src/modules/ai/ideogram/ideogram.dto.d.ts.map +1 -0
  47. package/dist/src/modules/ai/ideogram/ideogram.dto.js +49 -0
  48. package/dist/src/modules/ai/ideogram/ideogram.prompt.d.ts +3 -0
  49. package/dist/src/modules/ai/ideogram/ideogram.prompt.d.ts.map +1 -0
  50. package/dist/src/modules/ai/ideogram/ideogram.prompt.js +860 -0
  51. package/dist/src/modules/ai/ideogram/ideogram.repository.d.ts +7 -0
  52. package/dist/src/modules/ai/ideogram/ideogram.repository.d.ts.map +1 -0
  53. package/dist/src/modules/ai/ideogram/ideogram.repository.js +46 -0
  54. package/dist/src/modules/ai/ideogram/ideogram.service.d.ts +10 -0
  55. package/dist/src/modules/ai/ideogram/ideogram.service.d.ts.map +1 -0
  56. package/dist/src/modules/ai/ideogram/ideogram.service.js +11 -0
  57. package/dist/src/modules/auth/auth.db.d.ts +2336 -0
  58. package/dist/src/modules/auth/auth.db.d.ts.map +1 -0
  59. package/dist/src/modules/auth/auth.db.js +215 -0
  60. package/dist/src/modules/auth/auth.dto.d.ts +66 -0
  61. package/dist/src/modules/auth/auth.dto.d.ts.map +1 -0
  62. package/dist/src/modules/auth/auth.dto.js +38 -0
  63. package/dist/src/modules/auth/auth.lib.d.ts +4874 -0
  64. package/dist/src/modules/auth/auth.lib.d.ts.map +1 -0
  65. package/dist/src/modules/auth/auth.lib.js +284 -0
  66. package/dist/src/modules/auth/auth.middleware.d.ts +615 -0
  67. package/dist/src/modules/auth/auth.middleware.d.ts.map +1 -0
  68. package/dist/src/modules/auth/auth.middleware.js +52 -0
  69. package/dist/src/modules/auth/auth.repository.d.ts +2417 -0
  70. package/dist/src/modules/auth/auth.repository.d.ts.map +1 -0
  71. package/dist/src/modules/auth/auth.repository.js +541 -0
  72. package/dist/src/modules/auth/auth.service.d.ts +104 -0
  73. package/dist/src/modules/auth/auth.service.d.ts.map +1 -0
  74. package/dist/src/modules/auth/auth.service.js +201 -0
  75. package/dist/src/modules/auth/auth.trpc.d.ts +309 -0
  76. package/dist/src/modules/auth/auth.trpc.d.ts.map +1 -0
  77. package/dist/src/modules/auth/auth.trpc.js +157 -0
  78. package/dist/src/modules/auth/auth.utils.d.ts +2352 -0
  79. package/dist/src/modules/auth/auth.utils.d.ts.map +1 -0
  80. package/dist/src/modules/auth/auth.utils.js +97 -0
  81. package/dist/src/modules/base/base.abstract.d.ts +19 -0
  82. package/dist/src/modules/base/base.abstract.d.ts.map +1 -0
  83. package/dist/src/modules/base/base.abstract.js +53 -0
  84. package/dist/src/modules/base/base.dto.d.ts +70 -0
  85. package/dist/src/modules/base/base.dto.d.ts.map +1 -0
  86. package/dist/src/modules/base/base.dto.js +112 -0
  87. package/dist/src/modules/base/base.grants.d.ts +29 -0
  88. package/dist/src/modules/base/base.grants.d.ts.map +1 -0
  89. package/dist/src/modules/base/base.grants.js +123 -0
  90. package/dist/src/modules/base/base.grants.test.d.ts +2 -0
  91. package/dist/src/modules/base/base.grants.test.d.ts.map +1 -0
  92. package/dist/src/modules/base/base.grants.test.js +668 -0
  93. package/dist/src/modules/base/base.repository.d.ts +97 -0
  94. package/dist/src/modules/base/base.repository.d.ts.map +1 -0
  95. package/dist/src/modules/base/base.repository.js +307 -0
  96. package/dist/src/modules/base/base.service.d.ts +42 -0
  97. package/dist/src/modules/base/base.service.d.ts.map +1 -0
  98. package/dist/src/modules/base/base.service.js +109 -0
  99. package/dist/src/modules/base/base.types.d.ts +2 -0
  100. package/dist/src/modules/base/base.types.d.ts.map +1 -0
  101. package/dist/src/modules/base/base.types.js +2 -0
  102. package/dist/src/modules/billing/billing.db.d.ts +366 -0
  103. package/dist/src/modules/billing/billing.db.d.ts.map +1 -0
  104. package/dist/src/modules/billing/billing.db.js +29 -0
  105. package/dist/src/modules/billing/billing.repository.d.ts +2764 -0
  106. package/dist/src/modules/billing/billing.repository.d.ts.map +1 -0
  107. package/dist/src/modules/billing/billing.repository.js +235 -0
  108. package/dist/src/modules/billing/billing.router.d.ts +5 -0
  109. package/dist/src/modules/billing/billing.router.d.ts.map +1 -0
  110. package/dist/src/modules/billing/billing.router.js +56 -0
  111. package/dist/src/modules/billing/billing.service.d.ts +60 -0
  112. package/dist/src/modules/billing/billing.service.d.ts.map +1 -0
  113. package/dist/src/modules/billing/billing.service.js +147 -0
  114. package/dist/src/modules/billing/billing.trpc.d.ts +75 -0
  115. package/dist/src/modules/billing/billing.trpc.d.ts.map +1 -0
  116. package/dist/src/modules/billing/billing.trpc.js +17 -0
  117. package/dist/src/modules/clay/clay.repository.d.ts +6 -0
  118. package/dist/src/modules/clay/clay.repository.d.ts.map +1 -0
  119. package/dist/src/modules/clay/clay.repository.js +26 -0
  120. package/dist/src/modules/clay/clay.service.d.ts +29 -0
  121. package/dist/src/modules/clay/clay.service.d.ts.map +1 -0
  122. package/dist/src/modules/clay/clay.service.js +24 -0
  123. package/dist/src/modules/connect/connect.db.d.ts +357 -0
  124. package/dist/src/modules/connect/connect.db.d.ts.map +1 -0
  125. package/dist/src/modules/connect/connect.db.js +30 -0
  126. package/dist/src/modules/connect/connect.dto.d.ts +75 -0
  127. package/dist/src/modules/connect/connect.dto.d.ts.map +1 -0
  128. package/dist/src/modules/connect/connect.dto.js +36 -0
  129. package/dist/src/modules/connect/connect.linkedin.d.ts +3 -0
  130. package/dist/src/modules/connect/connect.linkedin.d.ts.map +1 -0
  131. package/dist/src/modules/connect/connect.linkedin.js +53 -0
  132. package/dist/src/modules/connect/connect.oauth.d.ts +28 -0
  133. package/dist/src/modules/connect/connect.oauth.d.ts.map +1 -0
  134. package/dist/src/modules/connect/connect.oauth.js +198 -0
  135. package/dist/src/modules/connect/connect.repository.d.ts +414 -0
  136. package/dist/src/modules/connect/connect.repository.d.ts.map +1 -0
  137. package/dist/src/modules/connect/connect.repository.js +54 -0
  138. package/dist/src/modules/connect/connect.router.d.ts +5 -0
  139. package/dist/src/modules/connect/connect.router.d.ts.map +1 -0
  140. package/dist/src/modules/connect/connect.router.js +54 -0
  141. package/dist/src/modules/connect/connect.service.d.ts +89 -0
  142. package/dist/src/modules/connect/connect.service.d.ts.map +1 -0
  143. package/dist/src/modules/connect/connect.service.js +114 -0
  144. package/dist/src/modules/connect/connect.trpc.d.ts +81 -0
  145. package/dist/src/modules/connect/connect.trpc.d.ts.map +1 -0
  146. package/dist/src/modules/connect/connect.trpc.js +21 -0
  147. package/dist/src/modules/connect/connect.types.d.ts +26 -0
  148. package/dist/src/modules/connect/connect.types.d.ts.map +1 -0
  149. package/dist/src/modules/connect/connect.types.js +2 -0
  150. package/dist/src/modules/crypto/crypto.db.d.ts +152 -0
  151. package/dist/src/modules/crypto/crypto.db.d.ts.map +1 -0
  152. package/dist/src/modules/crypto/crypto.db.js +17 -0
  153. package/dist/src/modules/crypto/crypto.repository.d.ts +160 -0
  154. package/dist/src/modules/crypto/crypto.repository.d.ts.map +1 -0
  155. package/dist/src/modules/crypto/crypto.repository.js +10 -0
  156. package/dist/src/modules/crypto/crypto.service.d.ts +11 -0
  157. package/dist/src/modules/crypto/crypto.service.d.ts.map +1 -0
  158. package/dist/src/modules/crypto/crypto.service.js +52 -0
  159. package/dist/src/modules/email/email.service.d.ts +57 -0
  160. package/dist/src/modules/email/email.service.d.ts.map +1 -0
  161. package/dist/src/modules/email/email.service.js +107 -0
  162. package/dist/src/modules/file/file.repository.d.ts +13 -0
  163. package/dist/src/modules/file/file.repository.d.ts.map +1 -0
  164. package/dist/src/modules/file/file.repository.js +79 -0
  165. package/dist/src/modules/file/file.router.d.ts +4 -0
  166. package/dist/src/modules/file/file.router.d.ts.map +1 -0
  167. package/dist/src/modules/file/file.router.js +99 -0
  168. package/dist/src/modules/file/file.service.d.ts +25 -0
  169. package/dist/src/modules/file/file.service.d.ts.map +1 -0
  170. package/dist/src/modules/file/file.service.js +150 -0
  171. package/dist/src/modules/recurrence/recurrence.db.d.ts +563 -0
  172. package/dist/src/modules/recurrence/recurrence.db.d.ts.map +1 -0
  173. package/dist/src/modules/recurrence/recurrence.db.js +66 -0
  174. package/dist/src/modules/recurrence/recurrence.repository.d.ts +585 -0
  175. package/dist/src/modules/recurrence/recurrence.repository.d.ts.map +1 -0
  176. package/dist/src/modules/recurrence/recurrence.repository.js +39 -0
  177. package/dist/src/modules/recurrence/recurrence.service.d.ts +30 -0
  178. package/dist/src/modules/recurrence/recurrence.service.d.ts.map +1 -0
  179. package/dist/src/modules/recurrence/recurrence.service.js +70 -0
  180. package/dist/src/modules/recurrence/recurrence.trpc.d.ts +243 -0
  181. package/dist/src/modules/recurrence/recurrence.trpc.d.ts.map +1 -0
  182. package/dist/src/modules/recurrence/recurrence.trpc.js +65 -0
  183. package/dist/src/modules/social/social.dto.d.ts +35 -0
  184. package/dist/src/modules/social/social.dto.d.ts.map +1 -0
  185. package/dist/src/modules/social/social.dto.js +18 -0
  186. package/dist/src/modules/social/social.linkedin.d.ts +11 -0
  187. package/dist/src/modules/social/social.linkedin.d.ts.map +1 -0
  188. package/dist/src/modules/social/social.linkedin.js +427 -0
  189. package/dist/src/modules/social/social.linkedin.test.d.ts +2 -0
  190. package/dist/src/modules/social/social.linkedin.test.d.ts.map +1 -0
  191. package/dist/src/modules/social/social.linkedin.test.js +235 -0
  192. package/dist/src/modules/social/social.service.d.ts +29 -0
  193. package/dist/src/modules/social/social.service.d.ts.map +1 -0
  194. package/dist/src/modules/social/social.service.js +76 -0
  195. package/dist/src/modules/social/social.types.d.ts +36 -0
  196. package/dist/src/modules/social/social.types.d.ts.map +1 -0
  197. package/dist/src/modules/social/social.types.js +2 -0
  198. package/dist/src/modules/tag/tag.db.d.ts +347 -0
  199. package/dist/src/modules/tag/tag.db.d.ts.map +1 -0
  200. package/dist/src/modules/tag/tag.db.js +42 -0
  201. package/dist/src/modules/tag/tag.dto.d.ts +1019 -0
  202. package/dist/src/modules/tag/tag.dto.d.ts.map +1 -0
  203. package/dist/src/modules/tag/tag.dto.js +9 -0
  204. package/dist/src/modules/tag/tag.repository.d.ts +384 -0
  205. package/dist/src/modules/tag/tag.repository.d.ts.map +1 -0
  206. package/dist/src/modules/tag/tag.repository.js +154 -0
  207. package/dist/src/modules/tag/tag.service.d.ts +36 -0
  208. package/dist/src/modules/tag/tag.service.d.ts.map +1 -0
  209. package/dist/src/modules/tag/tag.service.js +31 -0
  210. package/dist/src/modules/tag/tag.trpc.d.ts +191 -0
  211. package/dist/src/modules/tag/tag.trpc.d.ts.map +1 -0
  212. package/dist/src/modules/tag/tag.trpc.js +47 -0
  213. package/dist/src/modules/utils/applyPagination.d.ts +7 -0
  214. package/dist/src/modules/utils/applyPagination.d.ts.map +1 -0
  215. package/dist/src/modules/utils/applyPagination.js +16 -0
  216. package/dist/src/modules/utils/applySorting.d.ts +9 -0
  217. package/dist/src/modules/utils/applySorting.d.ts.map +1 -0
  218. package/dist/src/modules/utils/applySorting.js +18 -0
  219. package/dist/src/modules/utils/getConditionsFromFilters.d.ts +5 -0
  220. package/dist/src/modules/utils/getConditionsFromFilters.d.ts.map +1 -0
  221. package/dist/src/modules/utils/getConditionsFromFilters.js +200 -0
  222. package/dist/src/modules/video/video.service.d.ts +8 -0
  223. package/dist/src/modules/video/video.service.d.ts.map +1 -0
  224. package/dist/src/modules/video/video.service.js +84 -0
  225. package/dist/src/modules/webhook/webhook.constants.d.ts +9 -0
  226. package/dist/src/modules/webhook/webhook.constants.d.ts.map +1 -0
  227. package/dist/src/modules/webhook/webhook.constants.js +10 -0
  228. package/dist/src/modules/webhook/webhook.db.d.ts +137 -0
  229. package/dist/src/modules/webhook/webhook.db.d.ts.map +1 -0
  230. package/dist/src/modules/webhook/webhook.db.js +17 -0
  231. package/dist/src/modules/webhook/webhook.dto.d.ts +395 -0
  232. package/dist/src/modules/webhook/webhook.dto.d.ts.map +1 -0
  233. package/dist/src/modules/webhook/webhook.dto.js +7 -0
  234. package/dist/src/modules/webhook/webhook.repository.d.ts +149 -0
  235. package/dist/src/modules/webhook/webhook.repository.d.ts.map +1 -0
  236. package/dist/src/modules/webhook/webhook.repository.js +56 -0
  237. package/dist/src/modules/webhook/webhook.router.d.ts +4 -0
  238. package/dist/src/modules/webhook/webhook.router.d.ts.map +1 -0
  239. package/dist/src/modules/webhook/webhook.router.js +30 -0
  240. package/dist/src/modules/webhook/webhook.service.d.ts +10 -0
  241. package/dist/src/modules/webhook/webhook.service.d.ts.map +1 -0
  242. package/dist/src/modules/webhook/webhook.service.js +68 -0
  243. package/dist/src/modules/workflow/workflow.db.d.ts +297 -0
  244. package/dist/src/modules/workflow/workflow.db.d.ts.map +1 -0
  245. package/dist/src/modules/workflow/workflow.db.js +30 -0
  246. package/dist/src/modules/workflow/workflow.repository.d.ts +344 -0
  247. package/dist/src/modules/workflow/workflow.repository.d.ts.map +1 -0
  248. package/dist/src/modules/workflow/workflow.repository.js +105 -0
  249. package/dist/src/modules/workflow/workflow.service.d.ts +22 -0
  250. package/dist/src/modules/workflow/workflow.service.d.ts.map +1 -0
  251. package/dist/src/modules/workflow/workflow.service.js +37 -0
  252. package/dist/src/modules/workflow/workflow.trpc.d.ts +93 -0
  253. package/dist/src/modules/workflow/workflow.trpc.d.ts.map +1 -0
  254. package/dist/src/modules/workflow/workflow.trpc.js +21 -0
  255. package/dist/src/modules/workflow/workflow.types.d.ts +21 -0
  256. package/dist/src/modules/workflow/workflow.types.d.ts.map +1 -0
  257. package/dist/src/modules/workflow/workflow.types.js +2 -0
  258. package/dist/src/modules/workflow/workflow.utils.d.ts +22 -0
  259. package/dist/src/modules/workflow/workflow.utils.d.ts.map +1 -0
  260. package/dist/src/modules/workflow/workflow.utils.js +173 -0
  261. package/dist/src/test/stubs/utils.d.ts +3 -0
  262. package/dist/src/test/stubs/utils.d.ts.map +1 -0
  263. package/dist/src/test/stubs/utils.js +5 -0
  264. package/dist/src/trpc/context.d.ts +42 -0
  265. package/dist/src/trpc/context.d.ts.map +1 -0
  266. package/dist/src/trpc/context.js +17 -0
  267. package/dist/src/trpc/index.d.ts +4 -0
  268. package/dist/src/trpc/index.d.ts.map +1 -0
  269. package/dist/src/trpc/index.js +6 -0
  270. package/dist/src/trpc/procedures.d.ts +234 -0
  271. package/dist/src/trpc/procedures.d.ts.map +1 -0
  272. package/dist/src/trpc/procedures.js +32 -0
  273. package/dist/src/trpc/utils.d.ts +5 -0
  274. package/dist/src/trpc/utils.d.ts.map +1 -0
  275. package/dist/src/trpc/utils.js +20 -0
  276. package/dist/src/types.d.ts +486 -0
  277. package/dist/src/types.d.ts.map +1 -0
  278. package/dist/src/types.js +13 -0
  279. package/dist/src/utils/errors.d.ts +50 -0
  280. package/dist/src/utils/errors.d.ts.map +1 -0
  281. package/dist/src/utils/errors.js +104 -0
  282. package/dist/src/utils/logger.d.ts +2 -0
  283. package/dist/src/utils/logger.d.ts.map +1 -0
  284. package/dist/src/utils/logger.js +11 -0
  285. package/dist/src/utils/posthog.d.ts +14 -0
  286. package/dist/src/utils/posthog.d.ts.map +1 -0
  287. package/dist/src/utils/posthog.js +31 -0
  288. package/dist/src/utils/types.d.ts +5 -0
  289. package/dist/src/utils/types.d.ts.map +1 -0
  290. package/dist/src/utils/types.js +2 -0
  291. package/dist/tsconfig.tsbuildinfo +1 -0
  292. package/jest.config.ts +19 -0
  293. package/package.json +3 -6
  294. package/tsconfig.json +21 -0
@@ -0,0 +1,97 @@
1
+ import type { QueryFilters, QueryInput } from "@m5kdev/commons/modules/schemas/query.schema";
2
+ import { type InferInsertModel, type InferSelectModel, type SelectedFields, type SQL } from "drizzle-orm";
3
+ import type { LibSQLDatabase } from "drizzle-orm/libsql";
4
+ import type { SQLiteColumn, SQLiteTableWithColumns } from "drizzle-orm/sqlite-core";
5
+ import { Base } from "#modules/base/base.abstract";
6
+ import { pickColumns, type ServerResultAsync } from "#modules/base/base.dto";
7
+ /** Payload for update/updateMany: id key required (string), other table fields optional. */
8
+ export type TableUpdatePayload<TTable extends SQLiteTableWithColumns<any>, TIdKey extends Extract<keyof InferSelectModel<TTable>, string> = "id"> = Record<TIdKey, string> & Partial<Omit<InferSelectModel<TTable>, TIdKey>>;
9
+ export declare class ConditionBuilder {
10
+ private conditions;
11
+ constructor(conditions?: SQL[]);
12
+ push(condition?: SQL): void;
13
+ join(type?: "and" | "or"): SQL<unknown> | undefined;
14
+ [Symbol.iterator](): ArrayIterator<SQL<unknown>>;
15
+ }
16
+ export declare class TableConditionBuilder<TTable extends SQLiteTableWithColumns<any>> extends ConditionBuilder {
17
+ private table;
18
+ constructor(table: TTable);
19
+ applyFilters({ filters }?: {
20
+ filters?: QueryFilters;
21
+ }): void;
22
+ }
23
+ export declare const arrayContains: (table: SQLiteColumn, values: string[]) => SQL<unknown> | undefined;
24
+ export declare class BaseRepository<O extends LibSQLDatabase<any>, S extends Record<string, SQLiteTableWithColumns<any>>, R extends Record<string, BaseRepository<any, any, any> | BaseExternaRepository>> extends Base {
25
+ protected orm: O;
26
+ protected schema: S;
27
+ repository?: R;
28
+ constructor(options: {
29
+ orm: O;
30
+ schema: S;
31
+ }, repository?: R);
32
+ getConditionBuilder(): ConditionBuilder;
33
+ getConditionBuilder(table: undefined): ConditionBuilder;
34
+ getConditionBuilder<TTable extends SQLiteTableWithColumns<any>>(table: TTable): TableConditionBuilder<TTable>;
35
+ withPagination<TQuery>(query: TQuery, { page, limit }: Pick<QueryInput, "page" | "limit">): TQuery;
36
+ withSorting<TTable extends SQLiteTableWithColumns<any>, TQuery>(query: TQuery, { sort, order }: Pick<QueryInput, "sort" | "order">, table?: TTable): TQuery;
37
+ withSortingAndPagination<TTable extends SQLiteTableWithColumns<any>, TQuery>(query: TQuery, { sort, order, page, limit }: Pick<QueryInput, "sort" | "order" | "page" | "limit">, table?: TTable): TQuery;
38
+ addUserIdFilter(userId: string, query?: QueryInput): QueryInput;
39
+ helpers: {
40
+ pickColumns: typeof pickColumns;
41
+ arrayContains: (table: SQLiteColumn, values: string[]) => SQL<unknown> | undefined;
42
+ ConditionBuilder: typeof ConditionBuilder;
43
+ };
44
+ }
45
+ /**
46
+ * Generic table-bound repository with typed CRUD, returning ServerResultAsync via throwableAsync.
47
+ *
48
+ * Example:
49
+ * const userRepo = new UserRepository(db, schema);
50
+ * class UserRepository extends BaseTableRepository<typeof schema.user> {
51
+ * constructor(db: LibSQLDatabase<typeof schema>, schema: typeof schema) {
52
+ * super(db, schema, schema.user);
53
+ * }
54
+ * }
55
+ */
56
+ export declare class BaseTableRepository<O extends LibSQLDatabase<any>, S extends Record<string, SQLiteTableWithColumns<any>>, R extends Record<string, BaseRepository<any, any, any> | BaseExternaRepository>, TTable extends SQLiteTableWithColumns<any>, TIdKey extends Extract<keyof InferSelectModel<TTable>, string> = "id"> extends BaseRepository<O, S, R> {
57
+ protected readonly table: TTable;
58
+ protected readonly idKey: TIdKey;
59
+ protected readonly idColumn: SQLiteColumn;
60
+ constructor(options: {
61
+ orm: O;
62
+ schema: S;
63
+ table: TTable;
64
+ idKey?: TIdKey;
65
+ }, repository?: R);
66
+ withSorting<TQuery>(query: TQuery, { sort, order }: Pick<QueryInput, "sort" | "order">, table?: SQLiteTableWithColumns<any>): TQuery;
67
+ withSortingAndPagination<MTable extends SQLiteTableWithColumns<any>, TQuery>(query: TQuery, { sort, order, page, limit }: Pick<QueryInput, "sort" | "order" | "page" | "limit">, table?: MTable): TQuery;
68
+ queryList(query?: QueryInput, options?: {
69
+ conditions?: TableConditionBuilder<TTable>;
70
+ select?: SelectedFields<SQLiteColumn, TTable>;
71
+ }, tx?: O): ServerResultAsync<{
72
+ rows: InferSelectModel<TTable>[];
73
+ total: number;
74
+ }>;
75
+ findById(id: string, tx?: O): ServerResultAsync<InferSelectModel<TTable> | undefined>;
76
+ findManyById(ids: readonly string[], tx?: O): ServerResultAsync<Array<InferSelectModel<TTable>>>;
77
+ create(data: InferInsertModel<TTable>, tx?: O): ServerResultAsync<InferSelectModel<TTable>>;
78
+ createMany(data: readonly InferInsertModel<TTable>[], tx?: O): ServerResultAsync<Array<InferSelectModel<TTable>>>;
79
+ update(data: TableUpdatePayload<TTable, TIdKey>, tx?: O): ServerResultAsync<InferSelectModel<TTable>>;
80
+ updateMany(data: readonly TableUpdatePayload<TTable, TIdKey>[], tx?: O): ServerResultAsync<Array<InferSelectModel<TTable>>>;
81
+ softDeleteById(id: string, tx?: O): ServerResultAsync<{
82
+ id: string;
83
+ }>;
84
+ softDeleteManyById(ids: readonly string[], tx?: O): ServerResultAsync<Array<{
85
+ id: string;
86
+ }>>;
87
+ deleteById(id: string, tx?: O): ServerResultAsync<{
88
+ id: string;
89
+ }>;
90
+ deleteManyById(ids: readonly string[], tx?: O): ServerResultAsync<Array<{
91
+ id: string;
92
+ }>>;
93
+ }
94
+ export declare class BaseExternaRepository extends Base {
95
+ constructor();
96
+ }
97
+ //# sourceMappingURL=base.repository.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.repository.d.ts","sourceRoot":"","sources":["../../../../src/modules/base/base.repository.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,YAAY,EACZ,UAAU,EACX,MAAM,8CAA8C,CAAC;AACtD,OAAO,EAIL,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EAIrB,KAAK,cAAc,EACnB,KAAK,GAAG,EACT,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,C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AU,CACd,IAAI,EAAE,SAAS,kBAAkB,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,EACnD,EAAE,CAAC,EAAE,CAAC,GACL,iBAAiB,CAAC,KAAK,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAC,CAAC;IA4B/C,cAAc,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,CAAC,GAAG,iBAAiB,CAAC;QAAE,EAAE,EAAE,MAAM,CAAA;KAAE,CAAC;IAkBrE,kBAAkB,CACtB,GAAG,EAAE,SAAS,MAAM,EAAE,EACtB,EAAE,CAAC,EAAE,CAAC,GACL,iBAAiB,CAAC,KAAK,CAAC;QAAE,EAAE,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAiBrC,UAAU,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,CAAC,GAAG,iBAAiB,CAAC;QAAE,EAAE,EAAE,MAAM,CAAA;KAAE,CAAC;IAgBjE,cAAc,CAAC,GAAG,EAAE,SAAS,MAAM,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,GAAG,iBAAiB,CAAC,KAAK,CAAC;QAAE,EAAE,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CAkB/F;AAED,qBAAa,qBAAsB,SAAQ,IAAI;;CAI9C"}
@@ -0,0 +1,307 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseExternaRepository = exports.BaseTableRepository = exports.BaseRepository = exports.arrayContains = exports.TableConditionBuilder = exports.ConditionBuilder = void 0;
4
+ const drizzle_orm_1 = require("drizzle-orm");
5
+ const neverthrow_1 = require("neverthrow");
6
+ const base_abstract_1 = require("#modules/base/base.abstract");
7
+ const base_dto_1 = require("#modules/base/base.dto");
8
+ const applyPagination_1 = require("#modules/utils/applyPagination");
9
+ const applySorting_1 = require("#modules/utils/applySorting");
10
+ const getConditionsFromFilters_1 = require("#modules/utils/getConditionsFromFilters");
11
+ class ConditionBuilder {
12
+ conditions;
13
+ constructor(conditions = []) {
14
+ this.conditions = conditions;
15
+ this.conditions = conditions;
16
+ }
17
+ push(condition) {
18
+ if (condition)
19
+ this.conditions.push(condition);
20
+ }
21
+ join(type = "and") {
22
+ if (this.conditions.length === 0)
23
+ return undefined;
24
+ if (this.conditions.length === 1)
25
+ return this.conditions[0];
26
+ return type === "and" ? (0, drizzle_orm_1.and)(...this.conditions) : (0, drizzle_orm_1.or)(...this.conditions);
27
+ }
28
+ [Symbol.iterator]() {
29
+ return this.conditions[Symbol.iterator]();
30
+ }
31
+ }
32
+ exports.ConditionBuilder = ConditionBuilder;
33
+ class TableConditionBuilder extends ConditionBuilder {
34
+ table;
35
+ constructor(table) {
36
+ super();
37
+ this.table = table;
38
+ }
39
+ applyFilters({ filters } = {}) {
40
+ if (filters && filters.length > 0)
41
+ (0, getConditionsFromFilters_1.getConditionsFromFilters)(this, filters, this.table);
42
+ }
43
+ }
44
+ exports.TableConditionBuilder = TableConditionBuilder;
45
+ const arrayContains = (table, values) => {
46
+ const arrayContains = [];
47
+ for (const value of values) {
48
+ arrayContains.push((0, drizzle_orm_1.like)(table, `%"${value}%"`));
49
+ }
50
+ return (0, drizzle_orm_1.or)(...arrayContains);
51
+ };
52
+ exports.arrayContains = arrayContains;
53
+ class BaseRepository extends base_abstract_1.Base {
54
+ orm;
55
+ schema;
56
+ repository;
57
+ constructor(options, repository) {
58
+ super("repository");
59
+ this.orm = options.orm;
60
+ this.schema = options.schema;
61
+ this.repository = repository;
62
+ }
63
+ getConditionBuilder(table) {
64
+ if (table === undefined) {
65
+ return new ConditionBuilder();
66
+ }
67
+ return new TableConditionBuilder(table);
68
+ }
69
+ withPagination(query, { page, limit }) {
70
+ return (0, applyPagination_1.applyPagination)(query, limit, page);
71
+ }
72
+ withSorting(query, { sort, order }, table) {
73
+ if (!table)
74
+ throw new Error("No table provided");
75
+ return (0, applySorting_1.applySorting)(query, table, sort, order);
76
+ }
77
+ withSortingAndPagination(query, { sort, order, page, limit }, table) {
78
+ if (!table)
79
+ throw new Error("No table provided");
80
+ return this.withSorting(this.withPagination(query, { page, limit }), { sort, order }, table);
81
+ }
82
+ addUserIdFilter(userId, query) {
83
+ const userIdFilter = {
84
+ columnId: "userId",
85
+ type: "string",
86
+ method: "equals",
87
+ value: userId,
88
+ };
89
+ return query
90
+ ? { ...query, filters: [...(query?.filters ?? []), userIdFilter] }
91
+ : { filters: [userIdFilter] };
92
+ }
93
+ helpers = {
94
+ pickColumns: base_dto_1.pickColumns,
95
+ arrayContains: exports.arrayContains,
96
+ ConditionBuilder,
97
+ };
98
+ }
99
+ exports.BaseRepository = BaseRepository;
100
+ /**
101
+ * Generic table-bound repository with typed CRUD, returning ServerResultAsync via throwableAsync.
102
+ *
103
+ * Example:
104
+ * const userRepo = new UserRepository(db, schema);
105
+ * class UserRepository extends BaseTableRepository<typeof schema.user> {
106
+ * constructor(db: LibSQLDatabase<typeof schema>, schema: typeof schema) {
107
+ * super(db, schema, schema.user);
108
+ * }
109
+ * }
110
+ */
111
+ class BaseTableRepository extends BaseRepository {
112
+ table;
113
+ idKey;
114
+ idColumn;
115
+ constructor(options, repository) {
116
+ super({ orm: options.orm, schema: options.schema }, repository);
117
+ this.table = options.table;
118
+ this.idKey = options.idKey ?? "id";
119
+ this.idColumn = this.table[this.idKey];
120
+ }
121
+ withSorting(query, { sort, order }, table) {
122
+ return super.withSorting(query, { sort, order }, table || this.table);
123
+ }
124
+ withSortingAndPagination(query, { sort, order, page, limit }, table) {
125
+ return super.withSortingAndPagination(query, { sort, order, page, limit }, table || this.table);
126
+ }
127
+ async queryList(query, options, tx) {
128
+ return this.throwableAsync(async () => {
129
+ const db = tx ?? this.orm;
130
+ const conditions = options?.conditions ?? this.getConditionBuilder(this.table);
131
+ conditions.applyFilters(query);
132
+ const whereClause = conditions.join();
133
+ const rowsQuery = this.withSortingAndPagination((options?.select ? db.select(options.select) : db.select())
134
+ .from(this.table)
135
+ .where(whereClause), query || {});
136
+ const countQuery = db
137
+ .select({ count: (0, drizzle_orm_1.count)() })
138
+ .from(this.table)
139
+ .where(whereClause);
140
+ const [rows, [totalResult]] = await Promise.all([rowsQuery, countQuery]);
141
+ return (0, neverthrow_1.ok)({ rows: rows, total: totalResult?.count ?? 0 });
142
+ });
143
+ }
144
+ async findById(id, tx) {
145
+ return this.throwableAsync(async () => {
146
+ const db = tx ?? this.orm;
147
+ const rows = (await db
148
+ .select()
149
+ .from(this.table)
150
+ .where((0, drizzle_orm_1.eq)(this.idColumn, id)));
151
+ return (0, neverthrow_1.ok)(rows[0]);
152
+ });
153
+ }
154
+ async findManyById(ids, tx) {
155
+ return this.throwableAsync(async () => {
156
+ const db = tx ?? this.orm;
157
+ if (ids.length === 0) {
158
+ return (0, neverthrow_1.ok)([]);
159
+ }
160
+ const rows = (await db
161
+ .select()
162
+ .from(this.table)
163
+ .where((0, drizzle_orm_1.inArray)(this.idColumn, ids)));
164
+ return (0, neverthrow_1.ok)(rows);
165
+ });
166
+ }
167
+ async create(data, tx) {
168
+ return this.throwableAsync(async () => {
169
+ const db = tx ?? this.orm;
170
+ const rows = (await db
171
+ .insert(this.table)
172
+ .values(data)
173
+ .returning());
174
+ if (rows.length === 0)
175
+ return this.error("UNPROCESSABLE_CONTENT");
176
+ return (0, neverthrow_1.ok)(rows[0]);
177
+ });
178
+ }
179
+ async createMany(data, tx) {
180
+ return this.throwableAsync(async () => {
181
+ const db = tx ?? this.orm;
182
+ if (data.length === 0) {
183
+ return (0, neverthrow_1.ok)([]);
184
+ }
185
+ const rows = (await db
186
+ .insert(this.table)
187
+ .values(data)
188
+ .returning());
189
+ return (0, neverthrow_1.ok)(rows);
190
+ });
191
+ }
192
+ async update(data, tx) {
193
+ return this.throwableAsync(async () => {
194
+ const db = tx ?? this.orm;
195
+ const single = data;
196
+ const id = String(single[this.idKey]);
197
+ const { [this.idKey]: _removed, ...rest } = single;
198
+ const update = rest;
199
+ if (this.table.updatedAt)
200
+ update.updatedAt = new Date();
201
+ const rows = (await db
202
+ .update(this.table)
203
+ .set(update)
204
+ .where((0, drizzle_orm_1.eq)(this.idColumn, id))
205
+ .returning());
206
+ const [row] = rows;
207
+ if (!row)
208
+ return this.error("NOT_FOUND");
209
+ return (0, neverthrow_1.ok)(row);
210
+ });
211
+ }
212
+ async updateMany(data, tx) {
213
+ return this.throwableAsync(async () => {
214
+ const db = tx ?? this.orm;
215
+ if (data.length === 0) {
216
+ return (0, neverthrow_1.ok)([]);
217
+ }
218
+ const results = [];
219
+ for (const item of data) {
220
+ const record = item;
221
+ const id = String(record[this.idKey]);
222
+ const { [this.idKey]: _removed, ...rest } = record;
223
+ const update = rest;
224
+ if (this.table.updatedAt)
225
+ update.updatedAt = new Date();
226
+ const rows = (await db
227
+ .update(this.table)
228
+ .set(update)
229
+ .where((0, drizzle_orm_1.eq)(this.idColumn, id))
230
+ .returning());
231
+ if (rows[0])
232
+ results.push(rows[0]);
233
+ }
234
+ return (0, neverthrow_1.ok)(results);
235
+ });
236
+ }
237
+ async softDeleteById(id, tx) {
238
+ return this.throwableAsync(async () => {
239
+ const db = tx ?? this.orm;
240
+ if (!this.table.deletedAt)
241
+ return this.error("METHOD_NOT_SUPPORTED");
242
+ const rows = await db
243
+ .update(this.table)
244
+ .set({ deletedAt: new Date() })
245
+ .where((0, drizzle_orm_1.eq)(this.idColumn, id))
246
+ .returning({
247
+ id: this.idColumn,
248
+ });
249
+ if (rows.length === 0)
250
+ return this.error("NOT_FOUND");
251
+ return (0, neverthrow_1.ok)(rows[0]);
252
+ });
253
+ }
254
+ async softDeleteManyById(ids, tx) {
255
+ return this.throwableAsync(async () => {
256
+ const db = tx ?? this.orm;
257
+ if (!this.table.deletedAt)
258
+ return this.error("METHOD_NOT_SUPPORTED");
259
+ const rows = await db
260
+ .update(this.table)
261
+ .set({ deletedAt: new Date() })
262
+ .where((0, drizzle_orm_1.inArray)(this.idColumn, ids))
263
+ .returning({
264
+ id: this.idColumn,
265
+ });
266
+ if (rows.length === 0)
267
+ return this.error("NOT_FOUND");
268
+ return (0, neverthrow_1.ok)(rows);
269
+ });
270
+ }
271
+ async deleteById(id, tx) {
272
+ return this.throwableAsync(async () => {
273
+ const db = tx ?? this.orm;
274
+ const rows = await db
275
+ .delete(this.table)
276
+ .where((0, drizzle_orm_1.eq)(this.idColumn, id))
277
+ .returning({
278
+ id: this.idColumn,
279
+ });
280
+ if (rows.length === 0)
281
+ return this.error("NOT_FOUND");
282
+ return (0, neverthrow_1.ok)(rows[0]);
283
+ });
284
+ }
285
+ async deleteManyById(ids, tx) {
286
+ return this.throwableAsync(async () => {
287
+ const db = tx ?? this.orm;
288
+ if (ids.length === 0) {
289
+ return (0, neverthrow_1.ok)([]);
290
+ }
291
+ const rows = await db
292
+ .delete(this.table)
293
+ .where((0, drizzle_orm_1.inArray)(this.idColumn, ids))
294
+ .returning({
295
+ id: this.idColumn,
296
+ });
297
+ return (0, neverthrow_1.ok)(rows);
298
+ });
299
+ }
300
+ }
301
+ exports.BaseTableRepository = BaseTableRepository;
302
+ class BaseExternaRepository extends base_abstract_1.Base {
303
+ constructor() {
304
+ super("repository");
305
+ }
306
+ }
307
+ exports.BaseExternaRepository = BaseExternaRepository;
@@ -0,0 +1,42 @@
1
+ import type { QueryFilter, QueryInput } from "@m5kdev/commons/modules/schemas/query.schema";
2
+ import type { Session, User } from "#modules/auth/auth.lib";
3
+ import { Base } from "#modules/base/base.abstract";
4
+ import type { ServerResult, ServerResultAsync } from "#modules/base/base.dto";
5
+ import { type Entity, type ResourceActionGrant, type ResourceGrant } from "#modules/base/base.grants";
6
+ import type { BaseExternaRepository, BaseRepository } from "#modules/base/base.repository";
7
+ import type { Context } from "#trpc";
8
+ export declare class BaseService<Repositories extends Record<string, BaseRepository<any, any, any> | BaseExternaRepository>, Services extends Record<string, BaseService<any, any>>> extends Base {
9
+ repository: Repositories;
10
+ service: Services;
11
+ constructor(repository?: Repositories, service?: Services);
12
+ addUserFilter(value: string, query?: QueryInput, columnId?: string, method?: QueryFilter["method"]): QueryInput;
13
+ addContextFilter(ctx: Awaited<ReturnType<Context>>, include?: {
14
+ user?: boolean;
15
+ organization?: boolean;
16
+ team?: boolean;
17
+ }, query?: QueryInput, map?: Record<string, {
18
+ columnId: string;
19
+ method: QueryFilter["method"];
20
+ }>): QueryInput;
21
+ }
22
+ export declare class BasePermissionService<Repositories extends Record<string, BaseRepository<any, any, any> | BaseExternaRepository>, Services extends Record<string, BaseService<any, any>>> extends BaseService<Repositories, Services> {
23
+ grants: ResourceGrant[];
24
+ constructor(repository: Repositories, service: Services, grants?: ResourceGrant[]);
25
+ accessGuard<T extends Entity>(ctx: {
26
+ session: Session;
27
+ user: User;
28
+ }, action: string, entities?: T | T[], grants?: ResourceActionGrant[]): ServerResult<true>;
29
+ accessGuardAsync<T extends Entity>(ctx: {
30
+ session: Session;
31
+ user: User;
32
+ }, action: string, getEntities: () => ServerResultAsync<T | T[] | undefined>, grants?: ResourceActionGrant[]): ServerResultAsync<true>;
33
+ checkPermission<T extends Entity>(ctx: {
34
+ session: Session;
35
+ user: User;
36
+ }, action: string, entities?: T | T[], grants?: ResourceActionGrant[]): boolean;
37
+ checkPermissionAsync<T extends Entity>(ctx: {
38
+ session: Session;
39
+ user: User;
40
+ }, action: string, getEntities: () => ServerResultAsync<T | T[] | undefined>, grants?: ResourceActionGrant[]): ServerResultAsync<boolean>;
41
+ }
42
+ //# sourceMappingURL=base.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.service.d.ts","sourceRoot":"","sources":["../../../../src/modules/base/base.service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,8CAA8C,CAAC;AAE5F,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,EAAE,IAAI,EAAE,MAAM,6BAA6B,CAAC;AACnD,OAAO,KAAK,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC9E,OA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C,GAAG,CAAC,EAAE,GAAG,SAAS,CAAC,EACzD,MAAM,CAAC,EAAE,mBAAmB,EAAE,GAC7B,iBAAiB,CAAC,OAAO,CAAC;CAS9B"}
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BasePermissionService = exports.BaseService = void 0;
4
+ const neverthrow_1 = require("neverthrow");
5
+ const base_abstract_1 = require("#modules/base/base.abstract");
6
+ const base_grants_1 = require("#modules/base/base.grants");
7
+ class BaseService extends base_abstract_1.Base {
8
+ repository;
9
+ service;
10
+ constructor(repository = {}, service = {}) {
11
+ super("service");
12
+ this.repository = repository;
13
+ this.service = service;
14
+ this.repository = repository;
15
+ this.service = service;
16
+ }
17
+ addUserFilter(value, query, columnId = "userId", method = "equals") {
18
+ const userFilter = {
19
+ columnId,
20
+ type: "string",
21
+ method,
22
+ value,
23
+ };
24
+ return query
25
+ ? { ...query, filters: [...(query?.filters ?? []), userFilter] }
26
+ : { filters: [userFilter] };
27
+ }
28
+ addContextFilter(ctx, include = {
29
+ user: true,
30
+ organization: false,
31
+ team: false,
32
+ }, query, map = {
33
+ userId: {
34
+ columnId: "userId",
35
+ method: "equals",
36
+ },
37
+ organizationId: {
38
+ columnId: "organizationId",
39
+ method: "equals",
40
+ },
41
+ teamId: {
42
+ columnId: "teamId",
43
+ method: "equals",
44
+ },
45
+ }) {
46
+ const filters = [];
47
+ if (include.user) {
48
+ filters.push({
49
+ columnId: map.userId.columnId,
50
+ type: "string",
51
+ method: map.userId.method,
52
+ value: ctx.user.id,
53
+ });
54
+ }
55
+ if (include.organization) {
56
+ filters.push({
57
+ columnId: map.organizationId.columnId,
58
+ type: "string",
59
+ method: map.organizationId.method,
60
+ value: ctx.session.activeOrganizationId ?? "",
61
+ });
62
+ }
63
+ if (include.team) {
64
+ filters.push({
65
+ columnId: map.teamId.columnId,
66
+ type: "string",
67
+ method: map.teamId.method,
68
+ value: ctx.session.activeTeamId ?? "",
69
+ });
70
+ }
71
+ return query ? { ...query, filters: [...(query?.filters ?? []), ...filters] } : { filters };
72
+ }
73
+ }
74
+ exports.BaseService = BaseService;
75
+ class BasePermissionService extends BaseService {
76
+ grants;
77
+ constructor(repository, service, grants = []) {
78
+ super(repository, service);
79
+ this.grants = grants;
80
+ }
81
+ accessGuard(ctx, action, entities, grants) {
82
+ const hasPermission = this.checkPermission(ctx, action, entities, grants);
83
+ if (!hasPermission)
84
+ return this.error("FORBIDDEN");
85
+ return (0, neverthrow_1.ok)(true);
86
+ }
87
+ async accessGuardAsync(ctx, action, getEntities, grants) {
88
+ const hasPermission = await this.checkPermissionAsync(ctx, action, getEntities, grants);
89
+ if (hasPermission.isErr())
90
+ return (0, neverthrow_1.err)(hasPermission.error);
91
+ if (!hasPermission.value)
92
+ return this.error("FORBIDDEN");
93
+ return (0, neverthrow_1.ok)(true);
94
+ }
95
+ checkPermission(ctx, action, entities, grants) {
96
+ const actionGrants = grants ?? this.grants.filter((grant) => grant.action === action);
97
+ return (0, base_grants_1.checkPermissionSync)(ctx, actionGrants, entities);
98
+ }
99
+ async checkPermissionAsync(ctx, action, getEntities, grants) {
100
+ const actionGrants = grants ?? this.grants.filter((grant) => grant.action === action);
101
+ const permission = await (0, base_grants_1.checkPermissionAsync)(ctx, actionGrants, getEntities);
102
+ if (permission.isErr())
103
+ return this.error("INTERNAL_SERVER_ERROR", "Failed to check permission", {
104
+ cause: permission.error,
105
+ });
106
+ return (0, neverthrow_1.ok)(permission.value);
107
+ }
108
+ }
109
+ exports.BasePermissionService = BasePermissionService;
@@ -0,0 +1,2 @@
1
+ export type ServerErrorLayer = "unknown" | "repository" | "service" | "controller" | "auth" | "workflow";
2
+ //# sourceMappingURL=base.types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.types.d.ts","sourceRoot":"","sources":["../../../../src/modules/base/base.types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,gBAAgB,GACxB,SAAS,GACT,YAAY,GACZ,SAAS,GACT,YAAY,GACZ,MAAM,GACN,UAAU,CAAC"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });