serene-core-server 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (315) hide show
  1. package/dist/apollo/resolvers/mutations/mailing-lists.d.ts +11 -0
  2. package/dist/apollo/resolvers/mutations/mailing-lists.js +30 -0
  3. package/dist/apollo/resolvers/mutations/user-preferences.d.ts +13 -0
  4. package/dist/apollo/resolvers/mutations/user-preferences.js +31 -0
  5. package/dist/apollo/resolvers/mutations/users.d.ts +28 -0
  6. package/dist/apollo/resolvers/mutations/users.js +44 -0
  7. package/dist/apollo/resolvers/queries/access.d.ts +11 -0
  8. package/dist/apollo/resolvers/queries/access.js +25 -0
  9. package/dist/apollo/resolvers/queries/profile.d.ts +11 -0
  10. package/dist/apollo/resolvers/queries/profile.js +25 -0
  11. package/dist/apollo/resolvers/queries/quotas.d.ts +21 -0
  12. package/dist/apollo/resolvers/queries/quotas.js +25 -0
  13. package/dist/apollo/resolvers/queries/tech.d.ts +5 -0
  14. package/dist/apollo/resolvers/queries/tech.js +35 -0
  15. package/dist/apollo/resolvers/queries/user-preferences.d.ts +5 -0
  16. package/dist/apollo/resolvers/queries/user-preferences.js +33 -0
  17. package/dist/apollo/resolvers/queries/users.d.ts +15 -0
  18. package/dist/apollo/resolvers/queries/users.js +28 -0
  19. package/dist/apollo/resolvers/resolvers.d.ts +20 -0
  20. package/dist/apollo/resolvers/resolvers.js +38 -0
  21. package/dist/apollo/typedefs/typedefs.d.ts +1 -0
  22. package/dist/apollo/typedefs/typedefs.js +142 -0
  23. package/dist/index.d.ts +52 -0
  24. package/dist/index.js +68 -0
  25. package/dist/models/chat/chat-message-created-model.d.ts +17 -0
  26. package/dist/models/chat/chat-message-created-model.js +113 -0
  27. package/dist/models/chat/chat-message-model.d.ts +44 -0
  28. package/dist/models/chat/chat-message-model.js +523 -0
  29. package/dist/models/chat/chat-participant-model.d.ts +39 -0
  30. package/dist/models/chat/chat-participant-model.js +211 -0
  31. package/dist/models/chat/chat-session-model.d.ts +92 -0
  32. package/dist/models/chat/chat-session-model.js +301 -0
  33. package/dist/models/chat/chat-settings-model.d.ts +87 -0
  34. package/dist/models/chat/chat-settings-model.js +261 -0
  35. package/dist/models/feature-flags/feature-flag-model.d.ts +40 -0
  36. package/dist/models/feature-flags/feature-flag-model.js +164 -0
  37. package/dist/models/instances/instance-model.d.ts +93 -0
  38. package/dist/models/instances/instance-model.js +432 -0
  39. package/dist/models/instances/instance-setting-model.d.ts +39 -0
  40. package/dist/models/instances/instance-setting-model.js +193 -0
  41. package/dist/models/mailing-lists/mailing-list-model.d.ts +34 -0
  42. package/dist/models/mailing-lists/mailing-list-model.js +154 -0
  43. package/dist/models/mailing-lists/mailing-list-subscriber-model.d.ts +36 -0
  44. package/dist/models/mailing-lists/mailing-list-subscriber-model.js +152 -0
  45. package/dist/models/quotas/resource-quota-total-model.d.ts +46 -0
  46. package/dist/models/quotas/resource-quota-total-model.js +200 -0
  47. package/dist/models/quotas/resource-quota-usage-model.d.ts +51 -0
  48. package/dist/models/quotas/resource-quota-usage-model.js +181 -0
  49. package/dist/models/tech/rate-limited-api-event-model.d.ts +25 -0
  50. package/dist/models/tech/rate-limited-api-event-model.js +161 -0
  51. package/dist/models/tech/rate-limited-api-model.d.ts +21 -0
  52. package/dist/models/tech/rate-limited-api-model.js +141 -0
  53. package/dist/models/tech/tech-model.d.ts +44 -0
  54. package/dist/models/tech/tech-model.js +268 -0
  55. package/dist/models/tech/tech-provider-api-key-model.d.ts +43 -0
  56. package/dist/models/tech/tech-provider-api-key-model.js +224 -0
  57. package/dist/models/tech/tech-provider-model.d.ts +28 -0
  58. package/dist/models/tech/tech-provider-model.js +197 -0
  59. package/dist/models/users/external-user-integration-model.d.ts +36 -0
  60. package/dist/models/users/external-user-integration-model.js +196 -0
  61. package/dist/models/users/user-error-model.d.ts +54 -0
  62. package/dist/models/users/user-error-model.js +173 -0
  63. package/dist/models/users/user-error-summary-model.d.ts +45 -0
  64. package/dist/models/users/user-error-summary-model.js +172 -0
  65. package/dist/models/users/user-group-member-model.d.ts +41 -0
  66. package/dist/models/users/user-group-member-model.js +193 -0
  67. package/dist/models/users/user-group-model.d.ts +27 -0
  68. package/dist/models/users/user-group-model.js +146 -0
  69. package/dist/models/users/user-model.d.ts +46 -0
  70. package/dist/models/users/user-model.js +153 -0
  71. package/dist/models/users/user-preference-model.d.ts +60 -0
  72. package/dist/models/users/user-preference-model.js +228 -0
  73. package/dist/models/users/user-profile-model.d.ts +58 -0
  74. package/dist/models/users/user-profile-model.js +173 -0
  75. package/dist/prisma.config.d.ts +3 -0
  76. package/dist/prisma.config.js +13 -0
  77. package/dist/services/access/access-service.d.ts +12 -0
  78. package/dist/services/access/access-service.js +44 -0
  79. package/dist/services/access/encrypt-service.d.ts +13 -0
  80. package/dist/services/access/encrypt-service.js +84 -0
  81. package/dist/services/api-usage/api-usage-base-service.d.ts +15 -0
  82. package/dist/services/api-usage/api-usage-base-service.js +75 -0
  83. package/dist/services/console/service.d.ts +7 -0
  84. package/dist/services/console/service.js +70 -0
  85. package/dist/services/db/pg/pg-estimate-size-service.d.ts +16 -0
  86. package/dist/services/db/pg/pg-estimate-size-service.js +90 -0
  87. package/dist/services/files/types.d.ts +6 -0
  88. package/dist/services/files/types.js +2 -0
  89. package/dist/services/files/walk-dir-service.d.ts +8 -0
  90. package/dist/services/files/walk-dir-service.js +94 -0
  91. package/dist/services/locale/countries.d.ts +6 -0
  92. package/dist/services/locale/countries.js +484 -0
  93. package/dist/services/mailing-lists/mailing-list-subscriber-service.d.ts +14 -0
  94. package/dist/services/mailing-lists/mailing-list-subscriber-service.js +45 -0
  95. package/dist/services/process/sleep.d.ts +2 -0
  96. package/dist/services/process/sleep.js +10 -0
  97. package/dist/services/quotas/mutate-service.d.ts +5 -0
  98. package/dist/services/quotas/mutate-service.js +54 -0
  99. package/dist/services/quotas/query-service.d.ts +27 -0
  100. package/dist/services/quotas/query-service.js +137 -0
  101. package/dist/services/quotas/utils-service.d.ts +4 -0
  102. package/dist/services/quotas/utils-service.js +28 -0
  103. package/dist/services/tech/tech-provider-mutate-service.d.ts +7 -0
  104. package/dist/services/tech/tech-provider-mutate-service.js +142 -0
  105. package/dist/services/tech/tech-query-service.d.ts +9 -0
  106. package/dist/services/tech/tech-query-service.js +77 -0
  107. package/dist/services/user-preference/service.d.ts +10 -0
  108. package/dist/services/user-preference/service.js +70 -0
  109. package/dist/services/users/external-user-integration-service.d.ts +9 -0
  110. package/dist/services/users/external-user-integration-service.js +42 -0
  111. package/dist/services/users/profile-service.d.ts +40 -0
  112. package/dist/services/users/profile-service.js +408 -0
  113. package/dist/services/users/service.d.ts +64 -0
  114. package/dist/services/users/service.js +187 -0
  115. package/dist/services/users/user-error-service.d.ts +4 -0
  116. package/dist/services/users/user-error-service.js +42 -0
  117. package/dist/services/users/users-group-service.d.ts +16 -0
  118. package/dist/services/users/users-group-service.js +68 -0
  119. package/dist/services/utils/string-utils-service.d.ts +5 -0
  120. package/dist/services/utils/string-utils-service.js +41 -0
  121. package/dist/src/apollo/resolvers/mutations/mailing-lists.d.ts +11 -0
  122. package/dist/src/apollo/resolvers/mutations/mailing-lists.js +19 -0
  123. package/dist/src/apollo/resolvers/mutations/user-preferences.d.ts +13 -0
  124. package/dist/src/apollo/resolvers/mutations/user-preferences.js +20 -0
  125. package/dist/src/apollo/resolvers/mutations/users.d.ts +28 -0
  126. package/dist/src/apollo/resolvers/mutations/users.js +33 -0
  127. package/dist/src/apollo/resolvers/queries/access.d.ts +11 -0
  128. package/dist/src/apollo/resolvers/queries/access.js +14 -0
  129. package/dist/src/apollo/resolvers/queries/profile.d.ts +11 -0
  130. package/dist/src/apollo/resolvers/queries/profile.js +14 -0
  131. package/dist/src/apollo/resolvers/queries/quotas.d.ts +21 -0
  132. package/dist/src/apollo/resolvers/queries/quotas.js +14 -0
  133. package/dist/src/apollo/resolvers/queries/tech.d.ts +5 -0
  134. package/dist/src/apollo/resolvers/queries/tech.js +24 -0
  135. package/dist/src/apollo/resolvers/queries/user-preferences.d.ts +5 -0
  136. package/dist/src/apollo/resolvers/queries/user-preferences.js +22 -0
  137. package/dist/src/apollo/resolvers/queries/users.d.ts +15 -0
  138. package/dist/src/apollo/resolvers/queries/users.js +17 -0
  139. package/dist/src/apollo/typedefs/typedefs.d.ts +1 -0
  140. package/dist/src/apollo/typedefs/typedefs.js +139 -0
  141. package/dist/src/db.d.ts +3 -0
  142. package/dist/src/db.js +11 -0
  143. package/dist/src/index.d.ts +52 -0
  144. package/dist/src/index.js +52 -0
  145. package/dist/src/models/chat/chat-message-created-model.d.ts +17 -0
  146. package/dist/src/models/chat/chat-message-created-model.js +92 -0
  147. package/dist/src/models/chat/chat-message-model.d.ts +44 -0
  148. package/dist/src/models/chat/chat-message-model.js +484 -0
  149. package/dist/src/models/chat/chat-participant-model.d.ts +39 -0
  150. package/dist/src/models/chat/chat-participant-model.js +178 -0
  151. package/dist/src/models/chat/chat-session-model.d.ts +92 -0
  152. package/dist/src/models/chat/chat-session-model.js +266 -0
  153. package/dist/src/models/chat/chat-settings-model.d.ts +87 -0
  154. package/dist/src/models/chat/chat-settings-model.js +230 -0
  155. package/dist/src/models/feature-flags/feature-flag-model.d.ts +40 -0
  156. package/dist/src/models/feature-flags/feature-flag-model.js +137 -0
  157. package/dist/src/models/instances/instance-model.d.ts +100 -0
  158. package/dist/src/models/instances/instance-model.js +466 -0
  159. package/dist/src/models/instances/instance-setting-model.d.ts +39 -0
  160. package/dist/src/models/instances/instance-setting-model.js +164 -0
  161. package/dist/src/models/mailing-lists/mailing-list-model.d.ts +34 -0
  162. package/dist/src/models/mailing-lists/mailing-list-model.js +127 -0
  163. package/dist/src/models/mailing-lists/mailing-list-subscriber-model.d.ts +36 -0
  164. package/dist/src/models/mailing-lists/mailing-list-subscriber-model.js +127 -0
  165. package/dist/src/models/quotas/resource-quota-total-model.d.ts +46 -0
  166. package/dist/src/models/quotas/resource-quota-total-model.js +170 -0
  167. package/dist/src/models/quotas/resource-quota-usage-model.d.ts +51 -0
  168. package/dist/src/models/quotas/resource-quota-usage-model.js +151 -0
  169. package/dist/src/models/tech/rate-limited-api-event-model.d.ts +25 -0
  170. package/dist/src/models/tech/rate-limited-api-event-model.js +134 -0
  171. package/dist/src/models/tech/rate-limited-api-model.d.ts +21 -0
  172. package/dist/src/models/tech/rate-limited-api-model.js +116 -0
  173. package/dist/src/models/tech/tech-model.d.ts +44 -0
  174. package/dist/src/models/tech/tech-model.js +239 -0
  175. package/dist/src/models/tech/tech-provider-api-key-model.d.ts +43 -0
  176. package/dist/src/models/tech/tech-provider-api-key-model.js +195 -0
  177. package/dist/src/models/tech/tech-provider-model.d.ts +28 -0
  178. package/dist/src/models/tech/tech-provider-model.js +170 -0
  179. package/dist/src/models/users/external-user-integration-model.d.ts +36 -0
  180. package/dist/src/models/users/external-user-integration-model.js +167 -0
  181. package/dist/src/models/users/user-error-model.d.ts +54 -0
  182. package/dist/src/models/users/user-error-model.js +146 -0
  183. package/dist/src/models/users/user-error-summary-model.d.ts +45 -0
  184. package/dist/src/models/users/user-error-summary-model.js +145 -0
  185. package/dist/src/models/users/user-group-member-model.d.ts +41 -0
  186. package/dist/src/models/users/user-group-member-model.js +164 -0
  187. package/dist/src/models/users/user-group-model.d.ts +27 -0
  188. package/dist/src/models/users/user-group-model.js +121 -0
  189. package/dist/src/models/users/user-model.d.ts +46 -0
  190. package/dist/src/models/users/user-model.js +126 -0
  191. package/dist/src/models/users/user-preference-model.d.ts +60 -0
  192. package/dist/src/models/users/user-preference-model.js +197 -0
  193. package/dist/src/models/users/user-profile-model.d.ts +58 -0
  194. package/dist/src/models/users/user-profile-model.js +146 -0
  195. package/dist/src/services/access/access-service.d.ts +12 -0
  196. package/dist/src/services/access/access-service.js +27 -0
  197. package/dist/src/services/access/encrypt-service.d.ts +13 -0
  198. package/dist/src/services/access/encrypt-service.js +79 -0
  199. package/dist/src/services/api-usage/api-usage-base-service.d.ts +15 -0
  200. package/dist/src/services/api-usage/api-usage-base-service.js +58 -0
  201. package/dist/src/services/console/service.d.ts +7 -0
  202. package/dist/src/services/console/service.js +18 -0
  203. package/dist/src/services/db/pg/pg-estimate-size-service.d.ts +16 -0
  204. package/dist/src/services/db/pg/pg-estimate-size-service.js +83 -0
  205. package/dist/src/services/files/types.d.ts +6 -0
  206. package/dist/src/services/files/types.js +1 -0
  207. package/dist/src/services/files/walk-dir-service.d.ts +8 -0
  208. package/dist/src/services/files/walk-dir-service.js +77 -0
  209. package/dist/src/services/locale/countries.d.ts +6 -0
  210. package/dist/src/services/locale/countries.js +481 -0
  211. package/dist/src/services/mailing-lists/mailing-list-subscriber-service.d.ts +14 -0
  212. package/dist/src/services/mailing-lists/mailing-list-subscriber-service.js +28 -0
  213. package/dist/src/services/process/sleep.d.ts +2 -0
  214. package/dist/src/services/process/sleep.js +6 -0
  215. package/dist/src/services/quotas/mutate-service.d.ts +5 -0
  216. package/dist/src/services/quotas/mutate-service.js +37 -0
  217. package/dist/src/services/quotas/query-service.d.ts +27 -0
  218. package/dist/src/services/quotas/query-service.js +116 -0
  219. package/dist/src/services/quotas/utils-service.d.ts +4 -0
  220. package/dist/src/services/quotas/utils-service.js +22 -0
  221. package/dist/src/services/tech/tech-provider-mutate-service.d.ts +7 -0
  222. package/dist/src/services/tech/tech-provider-mutate-service.js +85 -0
  223. package/dist/src/services/tech/tech-query-service.d.ts +9 -0
  224. package/dist/src/services/tech/tech-query-service.js +58 -0
  225. package/dist/src/services/user-preference/service.d.ts +10 -0
  226. package/dist/src/services/user-preference/service.js +49 -0
  227. package/dist/src/services/users/external-user-integration-service.d.ts +9 -0
  228. package/dist/src/services/users/external-user-integration-service.js +25 -0
  229. package/dist/src/services/users/profile-service.d.ts +40 -0
  230. package/dist/src/services/users/profile-service.js +385 -0
  231. package/dist/src/services/users/service.d.ts +64 -0
  232. package/dist/src/services/users/service.js +152 -0
  233. package/dist/src/services/users/user-error-service.d.ts +4 -0
  234. package/dist/src/services/users/user-error-service.js +27 -0
  235. package/dist/src/services/users/users-group-service.d.ts +16 -0
  236. package/dist/src/services/users/users-group-service.js +51 -0
  237. package/dist/src/services/utils/field-naming-service.d.ts +20 -0
  238. package/dist/src/services/utils/field-naming-service.js +59 -0
  239. package/dist/src/services/utils/string-utils-service.d.ts +5 -0
  240. package/dist/src/services/utils/string-utils-service.js +37 -0
  241. package/dist/src/types/errors.d.ts +9 -0
  242. package/dist/src/types/errors.js +15 -0
  243. package/dist/src/types/user-types.d.ts +11 -0
  244. package/dist/src/types/user-types.js +14 -0
  245. package/dist/types/errors.d.ts +9 -0
  246. package/dist/types/errors.js +20 -0
  247. package/dist/types/user-types.d.ts +11 -0
  248. package/dist/types/user-types.js +19 -0
  249. package/package.json +21 -0
  250. package/prisma/schema.prisma +572 -0
  251. package/prisma.config.ts +14 -0
  252. package/src/apollo/README.md +4 -0
  253. package/src/apollo/resolvers/mutations/mailing-lists.ts +30 -0
  254. package/src/apollo/resolvers/mutations/user-preferences.ts +33 -0
  255. package/src/apollo/resolvers/mutations/users.ts +61 -0
  256. package/src/apollo/resolvers/queries/access.ts +23 -0
  257. package/src/apollo/resolvers/queries/profile.ts +24 -0
  258. package/src/apollo/resolvers/queries/quotas.ts +25 -0
  259. package/src/apollo/resolvers/queries/tech.ts +42 -0
  260. package/src/apollo/resolvers/queries/user-preferences.ts +34 -0
  261. package/src/apollo/resolvers/queries/users.ts +33 -0
  262. package/src/apollo/typedefs/typedefs.ts +139 -0
  263. package/src/db.ts +19 -0
  264. package/src/index.ts +52 -0
  265. package/src/models/chat/chat-message-created-model.ts +126 -0
  266. package/src/models/chat/chat-message-model.ts +678 -0
  267. package/src/models/chat/chat-participant-model.ts +248 -0
  268. package/src/models/chat/chat-session-model.ts +397 -0
  269. package/src/models/chat/chat-settings-model.ts +336 -0
  270. package/src/models/feature-flags/feature-flag-model.ts +198 -0
  271. package/src/models/instances/instance-model.ts +673 -0
  272. package/src/models/instances/instance-setting-model.ts +235 -0
  273. package/src/models/mailing-lists/mailing-list-model.ts +177 -0
  274. package/src/models/mailing-lists/mailing-list-subscriber-model.ts +194 -0
  275. package/src/models/quotas/resource-quota-total-model.ts +243 -0
  276. package/src/models/quotas/resource-quota-usage-model.ts +219 -0
  277. package/src/models/tech/rate-limited-api-event-model.ts +190 -0
  278. package/src/models/tech/rate-limited-api-model.ts +171 -0
  279. package/src/models/tech/tech-model.ts +360 -0
  280. package/src/models/tech/tech-provider-api-key-model.ts +289 -0
  281. package/src/models/tech/tech-provider-model.ts +247 -0
  282. package/src/models/users/external-user-integration-model.ts +236 -0
  283. package/src/models/users/user-error-model.ts +219 -0
  284. package/src/models/users/user-error-summary-model.ts +216 -0
  285. package/src/models/users/user-group-member-model.ts +233 -0
  286. package/src/models/users/user-group-model.ts +174 -0
  287. package/src/models/users/user-model.ts +172 -0
  288. package/src/models/users/user-preference-model.ts +285 -0
  289. package/src/models/users/user-profile-model.ts +207 -0
  290. package/src/services/access/access-service.ts +36 -0
  291. package/src/services/access/encrypt-service.ts +122 -0
  292. package/src/services/api-usage/api-usage-base-service.ts +95 -0
  293. package/src/services/console/service.ts +24 -0
  294. package/src/services/db/pg/pg-estimate-size-service.ts +119 -0
  295. package/src/services/files/types.ts +7 -0
  296. package/src/services/files/walk-dir-service.ts +135 -0
  297. package/src/services/locale/countries.ts +483 -0
  298. package/src/services/mailing-lists/mailing-list-subscriber-service.ts +54 -0
  299. package/src/services/process/sleep.ts +7 -0
  300. package/src/services/quotas/mutate-service.ts +66 -0
  301. package/src/services/quotas/query-service.ts +197 -0
  302. package/src/services/quotas/utils-service.ts +29 -0
  303. package/src/services/tech/tech-provider-mutate-service.ts +136 -0
  304. package/src/services/tech/tech-query-service.ts +93 -0
  305. package/src/services/user-preference/service.ts +110 -0
  306. package/src/services/users/external-user-integration-service.ts +51 -0
  307. package/src/services/users/profile-service.ts +513 -0
  308. package/src/services/users/service.ts +293 -0
  309. package/src/services/users/user-error-service.ts +74 -0
  310. package/src/services/users/users-group-service.ts +85 -0
  311. package/src/services/utils/field-naming-service.ts +78 -0
  312. package/src/services/utils/string-utils-service.ts +48 -0
  313. package/src/types/errors.ts +18 -0
  314. package/src/types/user-types.ts +19 -0
  315. package/tsconfig.json +25 -0
@@ -0,0 +1,285 @@
1
+ import { PrismaClient } from '@/prisma/client'
2
+
3
+ export class UserPreferenceModel {
4
+
5
+ // Consts
6
+ clName = 'UserPreferenceModel'
7
+
8
+ // Code
9
+ async create(
10
+ prisma: PrismaClient,
11
+ userProfileId: string,
12
+ category: string,
13
+ key: string,
14
+ value: string | null,
15
+ values: string[] | null) {
16
+
17
+ // Debug
18
+ const fnName = `${this.clName}.create()`
19
+
20
+ // Values can't be null
21
+ if (values == null) {
22
+
23
+ values = []
24
+ }
25
+
26
+ // Create record
27
+ try {
28
+ return await prisma.userPreference.create({
29
+ data: {
30
+ userProfileId: userProfileId,
31
+ category: category,
32
+ key: key,
33
+ value: value,
34
+ values: values
35
+ }
36
+ })
37
+ } catch(error) {
38
+ console.error(`${fnName}: error: ${error}`)
39
+ throw 'Prisma error'
40
+ }
41
+ }
42
+
43
+ async deleteById(
44
+ prisma: PrismaClient,
45
+ id: string) {
46
+
47
+ // Debug
48
+ const fnName = `${this.clName}.deleteById()`
49
+
50
+ // Delete
51
+ try {
52
+ return await prisma.userPreference.delete({
53
+ where: {
54
+ id: id
55
+ }
56
+ })
57
+ } catch(error: any) {
58
+ if (!(error instanceof error.NotFound)) {
59
+ console.error(`${fnName}: error: ${error}`)
60
+ throw 'Prisma error'
61
+ }
62
+ }
63
+ }
64
+
65
+ async filter(
66
+ prisma: PrismaClient,
67
+ userProfileId: string | undefined,
68
+ category: string | undefined,
69
+ key: string | undefined) {
70
+
71
+ // Debug
72
+ const fnName = `${this.clName}.filter()`
73
+
74
+ // Query
75
+ try {
76
+ return await prisma.userPreference.findMany({
77
+ where: {
78
+ userProfileId: userProfileId,
79
+ category: category,
80
+ key: key
81
+ }
82
+ })
83
+ } catch(error: any) {
84
+ console.error(`${fnName}: error: ${error}`)
85
+ throw 'Prisma error'
86
+ }
87
+ }
88
+
89
+ async filterManyKeys(
90
+ prisma: PrismaClient,
91
+ userProfileId: string | undefined,
92
+ keys: string[] | undefined) {
93
+
94
+ // Debug
95
+ const fnName = `${this.clName}.filter()`
96
+
97
+ // Query
98
+ try {
99
+ return await prisma.userPreference.findMany({
100
+ where: {
101
+ userProfileId: userProfileId,
102
+ key: {
103
+ in: keys
104
+ }
105
+ }
106
+ })
107
+ } catch(error: any) {
108
+ console.error(`${fnName}: error: ${error}`)
109
+ throw 'Prisma error'
110
+ }
111
+ }
112
+
113
+ async getById(
114
+ prisma: PrismaClient,
115
+ id: string) {
116
+
117
+ // Debug
118
+ const fnName = `${this.clName}.getById()`
119
+
120
+ // Query
121
+ var userpreference: any = null
122
+
123
+ try {
124
+ userpreference = await prisma.userPreference.findUnique({
125
+ where: {
126
+ id: id
127
+ }
128
+ })
129
+ } catch(error: any) {
130
+ if (!(error instanceof error.NotFound)) {
131
+ console.error(`${fnName}: error: ${error}`)
132
+ throw 'Prisma error'
133
+ }
134
+ }
135
+
136
+ // Return
137
+ return userpreference
138
+ }
139
+
140
+ async getByUniqueKey(
141
+ prisma: PrismaClient,
142
+ userProfileId: string,
143
+ key: string) {
144
+
145
+ // Debug
146
+ const fnName = `${this.clName}.getByUniqueKey()`
147
+
148
+ // Query
149
+ var userpreference: any = null
150
+
151
+ try {
152
+ userpreference = await prisma.userPreference.findFirst({
153
+ where: {
154
+ userProfileId: userProfileId,
155
+ key: key
156
+ }
157
+ })
158
+ } catch(error: any) {
159
+ if (!(error instanceof error.NotFound)) {
160
+ console.error(`${fnName}: error: ${error}`)
161
+ throw 'Prisma error'
162
+ }
163
+ }
164
+ // Return
165
+ return userpreference
166
+ }
167
+
168
+ async update(
169
+ prisma: PrismaClient,
170
+ id: string,
171
+ userProfileId: string | undefined,
172
+ category: string | undefined,
173
+ key: string | undefined,
174
+ value: string | null | undefined,
175
+ values: string[] | null | undefined) {
176
+
177
+ // Debug
178
+ const fnName = `${this.clName}.update()`
179
+
180
+ // Values can't be null
181
+ if (values == null) {
182
+
183
+ values = []
184
+ }
185
+
186
+ // Update record
187
+ try {
188
+ return await prisma.userPreference.update({
189
+ data: {
190
+ userProfileId: userProfileId,
191
+ category: category,
192
+ key: key,
193
+ value: value,
194
+ values: values
195
+ },
196
+ where: {
197
+ id: id
198
+ }
199
+ })
200
+ } catch(error) {
201
+ console.error(`${fnName}: error: ${error}`)
202
+ throw 'Prisma error'
203
+ }
204
+ }
205
+
206
+ async upsert(prisma: PrismaClient,
207
+ id: string | undefined,
208
+ userProfileId: string | undefined,
209
+ category: string | undefined,
210
+ key: string | undefined,
211
+ value: string | null | undefined,
212
+ values: string[] | null | undefined) {
213
+
214
+ // Debug
215
+ const fnName = `${this.clName}.upsert()`
216
+
217
+ // If id isn't specified, but the unique keys are, try to get the record
218
+ if (id == null &&
219
+ userProfileId != null &&
220
+ key != null) {
221
+
222
+ const userPreference = await
223
+ this.getByUniqueKey(
224
+ prisma,
225
+ userProfileId,
226
+ key)
227
+
228
+ if (userPreference != null) {
229
+ id = userPreference.id
230
+ }
231
+ }
232
+
233
+ // Upsert
234
+ if (id == null) {
235
+
236
+ // Validate for create (mainly for type validation of the create call)
237
+ if (userProfileId == null) {
238
+ console.error(`${fnName}: id is null and userProfileId is null`)
239
+ throw 'Prisma error'
240
+ }
241
+
242
+ if (category == null) {
243
+ console.error(`${fnName}: id is null and category is null`)
244
+ throw 'Prisma error'
245
+ }
246
+
247
+ if (key == null) {
248
+ console.error(`${fnName}: id is null and key is null`)
249
+ throw 'Prisma error'
250
+ }
251
+
252
+ if (value === undefined) {
253
+ console.error(`${fnName}: id is null and value is undefined`)
254
+ throw 'Prisma error'
255
+ }
256
+
257
+ if (values === undefined) {
258
+ console.error(`${fnName}: id is null and values is undefined`)
259
+ throw 'Prisma error'
260
+ }
261
+
262
+ // Create
263
+ return await
264
+ this.create(
265
+ prisma,
266
+ userProfileId,
267
+ category,
268
+ key,
269
+ value,
270
+ values)
271
+ } else {
272
+
273
+ // Update
274
+ return await
275
+ this.update(
276
+ prisma,
277
+ id,
278
+ userProfileId,
279
+ category,
280
+ key,
281
+ value,
282
+ values)
283
+ }
284
+ }
285
+ }
@@ -0,0 +1,207 @@
1
+ import { PrismaClient } from '@/prisma/client'
2
+ import { UserTypes } from '../../types/user-types'
3
+
4
+ export class UserProfileModel {
5
+
6
+ // Consts
7
+ clName = 'UserProfileModel'
8
+
9
+ // Code
10
+ async create(prisma: PrismaClient,
11
+ userId: string | null,
12
+ isAdmin: boolean,
13
+ deletePending: Date | null) {
14
+
15
+ // Debug
16
+ const fnName = `${this.clName}.create()`
17
+
18
+ // Create record
19
+ try {
20
+ return await prisma.userProfile.create({
21
+ data: {
22
+ userId: userId,
23
+ isAdmin: isAdmin,
24
+ deletePending: deletePending
25
+ }
26
+ })
27
+ } catch(error: any) {
28
+ console.error(`${fnName}: error: ${error}`)
29
+ throw 'Prisma error'
30
+ }
31
+ }
32
+
33
+ async getById(prisma: PrismaClient,
34
+ id: string) {
35
+
36
+ // Debug
37
+ const fnName = `${this.clName}.getById()`
38
+
39
+ // Get record
40
+ try {
41
+ return await prisma.userProfile.findUnique({
42
+ where: {
43
+ id: id
44
+ }
45
+ })
46
+ } catch(error: any) {
47
+ if (!(error instanceof error.NotFound)) {
48
+ console.error(`${fnName}: error: ${error}`)
49
+ throw 'Prisma error'
50
+ }
51
+ }
52
+ }
53
+
54
+ async getByUserId(
55
+ prisma: PrismaClient,
56
+ userId: string) {
57
+
58
+ // Debug
59
+ const fnName = `${this.clName}.getByUserId()`
60
+
61
+ // Get record
62
+ try {
63
+ return await prisma.userProfile.findFirst({
64
+ where: {
65
+ userId: userId
66
+ }
67
+ })
68
+ } catch(error: any) {
69
+ if (!(error instanceof error.NotFound)) {
70
+ console.error(`${fnName}: error: ${error}`)
71
+ throw 'Prisma error'
72
+ }
73
+ }
74
+ }
75
+
76
+ async setOwnerType(
77
+ prisma: PrismaClient,
78
+ userProfile: any) {
79
+
80
+ // Debug
81
+ const fnName = `${this.clName}.setOwnerType()`
82
+
83
+ console.log(`${fnName}: starting with userProfile: ` +
84
+ JSON.stringify(userProfile))
85
+
86
+ // Return immediately if ownerType is set
87
+ if (userProfile.ownerType != null) {
88
+
89
+ return {
90
+ status: true,
91
+ userProfile: userProfile
92
+ }
93
+ }
94
+
95
+ // Set to human ownerType if the userId field is set
96
+ var ownerType: string = ''
97
+
98
+ if (userProfile.ownerType == null) {
99
+
100
+ ownerType = UserTypes.humanRoleOwnerType
101
+ }
102
+
103
+ // Update record
104
+ userProfile = await prisma.userProfile.update({
105
+ data: {
106
+ ownerType: ownerType
107
+ },
108
+ where: {
109
+ id: userProfile.id
110
+ }
111
+ })
112
+
113
+ // Return
114
+ return {
115
+ status: true,
116
+ userProfile: userProfile
117
+ }
118
+ }
119
+
120
+ async update(prisma: PrismaClient,
121
+ id: string,
122
+ userId: string | null | undefined,
123
+ isAdmin: boolean | undefined,
124
+ deletePending: Date | null | undefined) {
125
+
126
+ // Debug
127
+ const fnName = `${this.clName}.update()`
128
+
129
+ // Create record
130
+ try {
131
+ return await prisma.userProfile.update({
132
+ data: {
133
+ userId: userId,
134
+ isAdmin: isAdmin
135
+ },
136
+ where: {
137
+ id: id
138
+ }
139
+ })
140
+ } catch(error: any) {
141
+ console.error(`${fnName}: error: ${error}`)
142
+ throw 'Prisma error'
143
+ }
144
+ }
145
+
146
+ async upsert(prisma: PrismaClient,
147
+ id: string | undefined,
148
+ userId: string | null | undefined,
149
+ isAdmin: boolean | undefined,
150
+ deletePending: Date | null | undefined) {
151
+
152
+ // Debug
153
+ const fnName = `${this.clName}.upsert()`
154
+
155
+ // If id isn't specified, but the unique keys are, try to get the record
156
+ if (id == null &&
157
+ userId != null) {
158
+
159
+ const userProfile = await
160
+ this.getByUserId(
161
+ prisma,
162
+ userId)
163
+
164
+ if (userProfile != null) {
165
+ id = userProfile.id
166
+ }
167
+ }
168
+
169
+ // Upsert
170
+ if (id == null) {
171
+
172
+ // Validate for create (mainly for type validation of the create call)
173
+ if (userId === undefined) {
174
+ console.error(`${fnName}: id is null and userId is undefined`)
175
+ throw 'Prisma error'
176
+ }
177
+
178
+ if (isAdmin == null) {
179
+ console.error(`${fnName}: id is null and isAdmin is null`)
180
+ throw 'Prisma error'
181
+ }
182
+
183
+ if (deletePending === undefined) {
184
+ console.error(`${fnName}: id is null and deletePending is undefined`)
185
+ throw 'Prisma error'
186
+ }
187
+
188
+ // Create
189
+ return await
190
+ this.create(
191
+ prisma,
192
+ userId,
193
+ isAdmin,
194
+ deletePending)
195
+ } else {
196
+
197
+ // Update
198
+ return await
199
+ this.update(
200
+ prisma,
201
+ id,
202
+ userId,
203
+ isAdmin,
204
+ deletePending)
205
+ }
206
+ }
207
+ }
@@ -0,0 +1,36 @@
1
+ import { PrismaClient } from '@/prisma/client'
2
+ import { UsersService } from '../users/service'
3
+
4
+ export class AccessService {
5
+
6
+ usersService = new UsersService()
7
+
8
+ async isAdminUser(
9
+ prisma: PrismaClient,
10
+ userProfileId: string) {
11
+
12
+ // Get UserProfile record
13
+ var userProfile: any = null
14
+
15
+ try {
16
+ userProfile = await prisma.userProfile.findUnique({
17
+ where: {
18
+ id: userProfileId
19
+ }
20
+ })
21
+ } catch(NotFound) {}
22
+
23
+ if (userProfile == null) {
24
+ return {
25
+ status: false,
26
+ message: 'User not found'
27
+ }
28
+ }
29
+
30
+ // Return isAdmin value
31
+ return {
32
+ status: userProfile.isAdmin,
33
+ message: null
34
+ }
35
+ }
36
+ }
@@ -0,0 +1,122 @@
1
+ // Source: https://stackoverflow.com/a/66476430
2
+
3
+ import crypto, { BinaryLike, CipherGCMTypes, CipherKey } from 'crypto'
4
+ import { Buffer } from 'buffer'
5
+
6
+ export class Encrypter {
7
+
8
+ // Consts
9
+ clName = 'Encrypter'
10
+ header = '!enc%'
11
+ algorithm: CipherGCMTypes = 'aes-256-gcm'
12
+
13
+ // Vars
14
+ key: Buffer
15
+
16
+ // Code
17
+ constructor(encryptionKey: string | undefined) {
18
+
19
+ // Debug
20
+ const fnName = `${this.clName}.constructor()`
21
+
22
+ // Validate
23
+ if (encryptionKey == null) {
24
+
25
+ throw new Error(`${fnName}: encryptionKey == null`)
26
+ }
27
+
28
+ // Set key
29
+ this.key = crypto.scryptSync(encryptionKey, 'salt', 32)
30
+ }
31
+
32
+ encrypt(clearText: string) {
33
+
34
+ return this.encryptAes256Gcm(clearText)
35
+ }
36
+
37
+ encryptAes256Gcm(clearText: string) {
38
+
39
+ // Get IV
40
+ const iv: Buffer = crypto.randomBytes(16)
41
+
42
+ // Get cipher
43
+ const cipher =
44
+ crypto.createCipheriv(
45
+ this.algorithm,
46
+ this.key as unknown as CipherKey,
47
+ iv as unknown as BinaryLike)
48
+
49
+ // Encrypt string
50
+ const encrypted = cipher.update(clearText, 'utf8', 'hex') + cipher.final('hex')
51
+
52
+ // Get the authTag
53
+ const authTag = cipher.getAuthTag().toString('hex')
54
+
55
+ // Return final string with metadata
56
+ return [
57
+ this.header,
58
+ this.algorithm,
59
+ encrypted,
60
+ iv.toString('hex'),
61
+ authTag
62
+ ].join('|')
63
+ }
64
+
65
+ decrypt(encryptedText: string) {
66
+
67
+ return this.decryptAes256Gcm(encryptedText)
68
+ }
69
+
70
+ decryptAes256Gcm(encryptedText: string) {
71
+
72
+ // Debug
73
+ const fnName = `${this.clName}.decryptAes256Gcm()`
74
+
75
+ // console.log(`${fnName}: starting with encryptedText: ${encryptedText}`)
76
+
77
+ // Split string with metadata into separate vars
78
+ const [header, algorithm, encrypted, iv, authTag] = encryptedText.split('|')
79
+
80
+ // Validate
81
+ if (header !== this.header) {
82
+ throw new Error('Unexpected header, string is likely not encrypted')
83
+ }
84
+
85
+ if (algorithm !== this.algorithm) {
86
+ throw new Error('Unexpected/invalid encryption algorithm')
87
+ }
88
+
89
+ if (!iv) {
90
+ throw new Error('IV not found')
91
+ }
92
+
93
+ if (!authTag) {
94
+ throw new Error('authTag not found')
95
+ }
96
+
97
+ // Debug
98
+ // console.log(`${fnName}: decipher encrypted text..`)
99
+
100
+ // Decipher encrypted string
101
+ const decipher =
102
+ crypto.createDecipheriv(
103
+ this.algorithm,
104
+ this.key as unknown as CipherKey,
105
+ Buffer.from(iv, 'hex') as unknown as BinaryLike)
106
+
107
+ decipher.setAuthTag(new Uint8Array(Buffer.from(authTag, 'hex')))
108
+
109
+ // Debug
110
+ // console.log(`${fnName}: decipher: ` + JSON.stringify(decipher))
111
+
112
+ // Get the final decrypted string
113
+ const decryptedText =
114
+ decipher.update(encrypted, 'hex', 'utf8') + decipher.final('utf8')
115
+
116
+ // Debug
117
+ // console.log(`${fnName}: returning decrypted text..`)
118
+
119
+ // Return final decrypted string
120
+ return decryptedText
121
+ }
122
+ }