@tech4good/angular-schematics 17.0.4 → 18.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (223) hide show
  1. package/README.md +8 -0
  2. package/package.json +4 -4
  3. package/src/collection.json +0 -16
  4. package/src/component/files/__name@dasherize__/__name@dasherize__.component.ts.template +22 -8
  5. package/src/component/index.js +2 -45
  6. package/src/component/index.js.map +1 -1
  7. package/src/component/schema.json +0 -5
  8. package/src/entity/files/__name@dasherize__/__name@dasherize__.mock.ts.template +12 -0
  9. package/src/entity/files/__name@dasherize__/__name@dasherize__.model.ts.template +1 -0
  10. package/src/entity/files/__name@dasherize__/__name@dasherize__.store.ts.template +41 -0
  11. package/src/entity/index.js +29 -50
  12. package/src/entity/index.js.map +1 -1
  13. package/src/init/files/.eslintrc.json.template +1 -1
  14. package/src/init/files/.firebaserc.template +8 -0
  15. package/src/init/files/.github/workflows/angular-github-ci.yml.template +1 -1
  16. package/src/init/files/README.md.template +13 -10
  17. package/src/init/files/angular.json.template +18 -11
  18. package/src/init/files/package.json.template +29 -30
  19. package/src/init/files/src/app/admin/{waitlist/waitlist.animations.ts.template → admin-panel/admin-panel.animations.ts.template} +1 -1
  20. package/src/init/files/src/app/admin/{waitlist/waitlist.component.html.template → admin-panel/admin-panel.component.html.template} +3 -3
  21. package/src/init/files/src/app/admin/{waitlist/waitlist.component.scss.template → admin-panel/admin-panel.component.scss.template} +1 -1
  22. package/src/init/files/src/app/admin/admin-panel/admin-panel.component.spec.ts.template +25 -0
  23. package/src/init/files/src/app/admin/admin-panel/admin-panel.component.ts.template +99 -0
  24. package/src/init/files/src/app/app.component.ts.template +12 -8
  25. package/src/init/files/src/app/app.config.ts.template +65 -0
  26. package/src/init/files/src/app/app.routes.ts.template +32 -0
  27. package/src/init/files/src/app/core/analytics/log.service.ts.template +122 -89
  28. package/src/init/files/src/app/core/analytics/time-analytics.service.ts.template +9 -17
  29. package/src/init/files/src/app/core/auth/auth.guard.ts.template +26 -23
  30. package/src/init/files/src/app/core/firebase/cached-listeners.service.ts.template +291 -0
  31. package/src/init/files/src/app/core/firebase/database.service.ts.template +56 -0
  32. package/src/init/files/src/app/core/firebase/firebase.mock.service.ts.template +276 -0
  33. package/src/init/files/src/app/core/firebase/firebase.service.ts.template +265 -215
  34. package/src/init/files/src/app/core/local-storage/session.service.ts.template +3 -4
  35. package/src/init/files/src/app/core/store/app.model.ts.template +19 -0
  36. package/src/init/files/src/app/core/store/app.store.ts.template +1074 -0
  37. package/src/init/files/src/app/core/store/auth/auth.mock.ts.template +20 -0
  38. package/src/init/files/src/app/core/store/auth/auth.store.ts.template +136 -0
  39. package/src/init/files/src/app/core/store/batch-write.firebase.service.ts.template +58 -0
  40. package/src/init/files/src/app/core/store/batch-write.mock.service.ts.template +105 -0
  41. package/src/init/files/src/app/core/store/batch-write.service.ts.template +126 -0
  42. package/src/init/files/src/app/core/store/user/user.mock.ts.template +24 -0
  43. package/src/init/files/src/app/core/store/user/user.model.ts.template +3 -6
  44. package/src/init/files/src/app/core/store/user/user.store.ts.template +41 -0
  45. package/src/init/files/src/app/core/store/user-context/user-context.mock.ts.template +24 -0
  46. package/src/init/files/src/app/core/store/user-context/user-context.model.ts.template +1 -0
  47. package/src/init/files/src/app/core/store/user-context/user-context.store.ts.template +41 -0
  48. package/src/init/files/src/app/core/utils/rand.utils.ts.template +13 -0
  49. package/src/init/files/src/app/{onboarding → first-time}/consent/consent.component.html.template +10 -14
  50. package/src/init/files/src/app/{onboarding → first-time}/consent/consent.component.scss.template +2 -2
  51. package/src/init/files/src/app/first-time/consent/consent.component.ts.template +95 -0
  52. package/src/init/files/src/app/first-time/early-access/early-access.animations.ts.template +4 -0
  53. package/src/init/files/src/app/first-time/early-access/early-access.component.html.template +65 -0
  54. package/src/init/files/src/app/{onboarding/waiting/waiting.component.scss.template → first-time/early-access/early-access.component.scss.template} +10 -3
  55. package/src/init/files/src/app/first-time/early-access/early-access.component.spec.ts.template +25 -0
  56. package/src/init/files/src/app/first-time/early-access/early-access.component.ts.template +254 -0
  57. package/src/init/files/src/app/first-time/early-access/early-access.model.ts.template +7 -0
  58. package/src/init/files/src/app/{general → first-time}/landing/landing.component.html.template +1 -1
  59. package/src/init/files/src/app/first-time/landing/landing.component.ts.template +44 -0
  60. package/src/init/files/src/app/first-time/login/login.component.html.template +10 -0
  61. package/src/init/files/src/app/first-time/login/login.component.ts.template +45 -0
  62. package/src/init/files/src/app/first-time/onboarding/onboarding.animations.ts.template +4 -0
  63. package/src/init/files/src/app/first-time/onboarding/onboarding.component.html.template +2 -0
  64. package/src/init/files/src/app/first-time/onboarding/onboarding.component.scss.template +5 -0
  65. package/src/init/files/src/app/{admin/waitlist/waitlist.component.spec.ts.template → first-time/onboarding/onboarding.component.spec.ts.template} +6 -6
  66. package/src/init/files/src/app/first-time/onboarding/onboarding.component.ts.template +41 -0
  67. package/src/init/files/src/app/main/home/home.component.html.template +6 -0
  68. package/src/init/files/src/app/{general → main}/home/home.component.scss.template +1 -0
  69. package/src/init/files/src/app/main/home/home.component.ts.template +55 -0
  70. package/src/init/files/src/app/shared/navbar/navbar.component.html.template +15 -0
  71. package/src/init/files/src/app/shared/navbar/navbar.component.scss.template +41 -0
  72. package/src/init/files/src/app/shared/navbar/navbar.component.spec.ts.template +25 -0
  73. package/src/init/files/src/app/shared/navbar/navbar.component.ts.template +40 -0
  74. package/src/init/files/src/app/shared/redeployment-warning/redeployment-warning.component.html.template +9 -0
  75. package/src/init/files/src/app/shared/{components/redeployment-warning → redeployment-warning}/redeployment-warning.component.ts.template +17 -6
  76. package/src/init/files/src/app/shared/{components/redeployment-warning → redeployment-warning}/redeployment-warning.service.ts.template +2 -2
  77. package/src/init/files/src/assets/images/close-x.svg +3 -0
  78. package/src/init/files/src/environments/environment.prod.ts.template +6 -2
  79. package/src/init/files/src/environments/environment.ts.template +4 -2
  80. package/src/init/files/src/index.html.template +1 -1
  81. package/src/init/files/src/styles.scss.template +4 -0
  82. package/src/init/files/tsconfig.json.template +0 -1
  83. package/src/container/files/__name@dasherize__/+state/__name@dasherize__.actions.ts.template +0 -26
  84. package/src/container/files/__name@dasherize__/+state/__name@dasherize__.effects.ts.template +0 -48
  85. package/src/container/files/__name@dasherize__/+state/__name@dasherize__.model.ts.template +0 -0
  86. package/src/container/files/__name@dasherize__/+state/__name@dasherize__.selectors.ts.template +0 -20
  87. package/src/container/files/__name@dasherize__/+state/__name@dasherize__.state.ts.template +0 -13
  88. package/src/container/files/__name@dasherize__/__name@dasherize__.animations.ts.template +0 -4
  89. package/src/container/files/__name@dasherize__/__name@dasherize__.component.html.template +0 -3
  90. package/src/container/files/__name@dasherize__/__name@dasherize__.component.scss.template +0 -5
  91. package/src/container/files/__name@dasherize__/__name@dasherize__.component.spec.ts.template +0 -25
  92. package/src/container/files/__name@dasherize__/__name@dasherize__.component.ts.template +0 -90
  93. package/src/container/index.d.ts +0 -2
  94. package/src/container/index.js +0 -127
  95. package/src/container/index.js.map +0 -1
  96. package/src/container/index_spec.d.ts +0 -1
  97. package/src/container/index_spec.js +0 -23
  98. package/src/container/index_spec.js.map +0 -1
  99. package/src/container/schema.json +0 -50
  100. package/src/entity/files/__name@dasherize__/__name@dasherize__.actions.ts.template +0 -249
  101. package/src/entity/files/__name@dasherize__/__name@dasherize__.effects.ts.template +0 -131
  102. package/src/entity/files/__name@dasherize__/__name@dasherize__.reducer.spec.ts.template +0 -13
  103. package/src/entity/files/__name@dasherize__/__name@dasherize__.reducer.ts.template +0 -51
  104. package/src/entity/files/__name@dasherize__/__name@dasherize__.service.ts.template +0 -50
  105. package/src/entity-in-example/files/__name@dasherize__/__name@dasherize__.actions.ts.template +0 -270
  106. package/src/entity-in-example/files/__name@dasherize__/__name@dasherize__.effects.ts.template +0 -131
  107. package/src/entity-in-example/files/__name@dasherize__/__name@dasherize__.model.ts.template +0 -3
  108. package/src/entity-in-example/files/__name@dasherize__/__name@dasherize__.reducer.spec.ts.template +0 -13
  109. package/src/entity-in-example/files/__name@dasherize__/__name@dasherize__.reducer.ts.template +0 -51
  110. package/src/entity-in-example/files/__name@dasherize__/__name@dasherize__.service.ts.template +0 -50
  111. package/src/entity-in-example/index.d.ts +0 -2
  112. package/src/entity-in-example/index.js +0 -141
  113. package/src/entity-in-example/index.js.map +0 -1
  114. package/src/entity-in-example/index_spec.d.ts +0 -1
  115. package/src/entity-in-example/index_spec.js +0 -23
  116. package/src/entity-in-example/index_spec.js.map +0 -1
  117. package/src/entity-in-example/schema.json +0 -45
  118. package/src/init/files/CHANGELOG.md.template +0 -10
  119. package/src/init/files/src/app/admin/admin-routing.module.ts.template +0 -17
  120. package/src/init/files/src/app/admin/admin.module.spec.ts.template +0 -13
  121. package/src/init/files/src/app/admin/admin.module.ts.template +0 -28
  122. package/src/init/files/src/app/admin/waitlist/+state/waitlist.actions.ts.template +0 -23
  123. package/src/init/files/src/app/admin/waitlist/+state/waitlist.effects.ts.template +0 -50
  124. package/src/init/files/src/app/admin/waitlist/+state/waitlist.model.ts.template +0 -0
  125. package/src/init/files/src/app/admin/waitlist/+state/waitlist.selectors.ts.template +0 -39
  126. package/src/init/files/src/app/admin/waitlist/+state/waitlist.state.ts.template +0 -13
  127. package/src/init/files/src/app/admin/waitlist/waitlist.component.ts.template +0 -141
  128. package/src/init/files/src/app/app-routing.module.ts.template +0 -26
  129. package/src/init/files/src/app/app.module.ts.template +0 -34
  130. package/src/init/files/src/app/core/core.module.ts.template +0 -75
  131. package/src/init/files/src/app/core/setup/firebase-setup.ts.template +0 -25
  132. package/src/init/files/src/app/core/setup/module-import-guard.ts.template +0 -5
  133. package/src/init/files/src/app/core/snackbar/snackbar.actions.ts.template +0 -24
  134. package/src/init/files/src/app/core/snackbar/snackbar.effects.ts.template +0 -41
  135. package/src/init/files/src/app/core/store/app-state/app-state.actions.ts.template +0 -249
  136. package/src/init/files/src/app/core/store/app-state/app-state.effects.ts.template +0 -131
  137. package/src/init/files/src/app/core/store/app-state/app-state.model.ts.template +0 -8
  138. package/src/init/files/src/app/core/store/app-state/app-state.reducer.spec.ts.template +0 -13
  139. package/src/init/files/src/app/core/store/app-state/app-state.reducer.ts.template +0 -51
  140. package/src/init/files/src/app/core/store/app-state/app-state.service.ts.template +0 -50
  141. package/src/init/files/src/app/core/store/app.actions.ts.template +0 -92
  142. package/src/init/files/src/app/core/store/app.effects.ts.template +0 -175
  143. package/src/init/files/src/app/core/store/app.reducer.ts.template +0 -36
  144. package/src/init/files/src/app/core/store/app.selectors.ts.template +0 -42
  145. package/src/init/files/src/app/core/store/auth/auth.actions.ts.template +0 -77
  146. package/src/init/files/src/app/core/store/auth/auth.effects.ts.template +0 -163
  147. package/src/init/files/src/app/core/store/auth/auth.reducer.spec.ts.template +0 -13
  148. package/src/init/files/src/app/core/store/auth/auth.reducer.ts.template +0 -26
  149. package/src/init/files/src/app/core/store/auth/auth.selectors.ts.template +0 -25
  150. package/src/init/files/src/app/core/store/cached-load-connections.service.ts.template +0 -405
  151. package/src/init/files/src/app/core/store/cached-selectors.service.ts.template +0 -376
  152. package/src/init/files/src/app/core/store/effects.helpers.ts.template +0 -66
  153. package/src/init/files/src/app/core/store/user/user.actions.ts.template +0 -248
  154. package/src/init/files/src/app/core/store/user/user.effects.ts.template +0 -148
  155. package/src/init/files/src/app/core/store/user/user.reducer.spec.ts.template +0 -13
  156. package/src/init/files/src/app/core/store/user/user.reducer.ts.template +0 -50
  157. package/src/init/files/src/app/core/store/user/user.service.ts.template +0 -50
  158. package/src/init/files/src/app/core/store/user-context/user-context.actions.ts.template +0 -248
  159. package/src/init/files/src/app/core/store/user-context/user-context.effects.ts.template +0 -131
  160. package/src/init/files/src/app/core/store/user-context/user-context.reducer.spec.ts.template +0 -13
  161. package/src/init/files/src/app/core/store/user-context/user-context.reducer.ts.template +0 -51
  162. package/src/init/files/src/app/core/store/user-context/user-context.service.ts.template +0 -50
  163. package/src/init/files/src/app/core/utils/string-case.utils.ts.template +0 -96
  164. package/src/init/files/src/app/general/general-routing.module.ts.template +0 -25
  165. package/src/init/files/src/app/general/general.module.spec.ts.template +0 -13
  166. package/src/init/files/src/app/general/general.module.ts.template +0 -36
  167. package/src/init/files/src/app/general/home/+state/home.actions.ts.template +0 -26
  168. package/src/init/files/src/app/general/home/+state/home.effects.ts.template +0 -48
  169. package/src/init/files/src/app/general/home/+state/home.model.ts.template +0 -0
  170. package/src/init/files/src/app/general/home/+state/home.selectors.ts.template +0 -20
  171. package/src/init/files/src/app/general/home/+state/home.state.ts.template +0 -13
  172. package/src/init/files/src/app/general/home/home.component.html.template +0 -5
  173. package/src/init/files/src/app/general/home/home.component.ts.template +0 -88
  174. package/src/init/files/src/app/general/landing/+state/landing.actions.ts.template +0 -26
  175. package/src/init/files/src/app/general/landing/+state/landing.effects.ts.template +0 -48
  176. package/src/init/files/src/app/general/landing/+state/landing.model.ts.template +0 -0
  177. package/src/init/files/src/app/general/landing/+state/landing.selectors.ts.template +0 -20
  178. package/src/init/files/src/app/general/landing/+state/landing.state.ts.template +0 -13
  179. package/src/init/files/src/app/general/landing/landing.component.ts.template +0 -98
  180. package/src/init/files/src/app/general/login/login.component.html.template +0 -11
  181. package/src/init/files/src/app/general/login/login.component.ts.template +0 -80
  182. package/src/init/files/src/app/onboarding/consent/+state/consent.actions.ts.template +0 -23
  183. package/src/init/files/src/app/onboarding/consent/+state/consent.effects.ts.template +0 -47
  184. package/src/init/files/src/app/onboarding/consent/+state/consent.model.ts.template +0 -0
  185. package/src/init/files/src/app/onboarding/consent/+state/consent.selectors.ts.template +0 -20
  186. package/src/init/files/src/app/onboarding/consent/+state/consent.state.ts.template +0 -13
  187. package/src/init/files/src/app/onboarding/consent/consent.component.ts.template +0 -151
  188. package/src/init/files/src/app/onboarding/onboarding-routing.module.ts.template +0 -19
  189. package/src/init/files/src/app/onboarding/onboarding.module.spec.ts.template +0 -13
  190. package/src/init/files/src/app/onboarding/onboarding.module.ts.template +0 -32
  191. package/src/init/files/src/app/onboarding/waiting/+state/waiting.actions.ts.template +0 -26
  192. package/src/init/files/src/app/onboarding/waiting/+state/waiting.effects.ts.template +0 -52
  193. package/src/init/files/src/app/onboarding/waiting/+state/waiting.model.ts.template +0 -9
  194. package/src/init/files/src/app/onboarding/waiting/+state/waiting.selectors.ts.template +0 -42
  195. package/src/init/files/src/app/onboarding/waiting/+state/waiting.state.ts.template +0 -13
  196. package/src/init/files/src/app/onboarding/waiting/waiting.animations.ts.template +0 -4
  197. package/src/init/files/src/app/onboarding/waiting/waiting.component.html.template +0 -57
  198. package/src/init/files/src/app/onboarding/waiting/waiting.component.spec.ts.template +0 -25
  199. package/src/init/files/src/app/onboarding/waiting/waiting.component.ts.template +0 -281
  200. package/src/init/files/src/app/shared/components/redeployment-warning/redeployment-warning.component.html.template +0 -10
  201. package/src/init/files/src/app/shared/shared.module.ts.template +0 -87
  202. package/src/init/files/src/main.ts.template +0 -12
  203. package/src/module/files/__name@dasherize__/__name@dasherize__-routing.module.ts.template +0 -15
  204. package/src/module/files/__name@dasherize__/__name@dasherize__.module.spec.ts.template +0 -13
  205. package/src/module/files/__name@dasherize__/__name@dasherize__.module.ts.template +0 -24
  206. package/src/module/index.d.ts +0 -2
  207. package/src/module/index.js +0 -97
  208. package/src/module/index.js.map +0 -1
  209. package/src/module/index_spec.d.ts +0 -1
  210. package/src/module/index_spec.js +0 -23
  211. package/src/module/index_spec.js.map +0 -1
  212. package/src/module/schema.json +0 -38
  213. /package/src/init/files/src/app/{onboarding → first-time}/consent/consent.animations.ts.template +0 -0
  214. /package/src/init/files/src/app/{onboarding → first-time}/consent/consent.component.spec.ts.template +0 -0
  215. /package/src/init/files/src/app/{general → first-time}/landing/landing.animations.ts.template +0 -0
  216. /package/src/init/files/src/app/{general → first-time}/landing/landing.component.scss.template +0 -0
  217. /package/src/init/files/src/app/{general → first-time}/landing/landing.component.spec.ts.template +0 -0
  218. /package/src/init/files/src/app/{general → first-time}/login/login.component.scss.template +0 -0
  219. /package/src/init/files/src/app/{general → first-time}/login/login.component.spec.ts.template +0 -0
  220. /package/src/init/files/src/app/{general → main}/home/home.animations.ts.template +0 -0
  221. /package/src/init/files/src/app/{general → main}/home/home.component.spec.ts.template +0 -0
  222. /package/src/init/files/src/app/shared/{components/redeployment-warning → redeployment-warning}/redeployment-warning.component.scss.template +0 -0
  223. /package/src/init/files/src/app/shared/{components/redeployment-warning → redeployment-warning}/redeployment-warning.component.spec.ts.template +0 -0
@@ -1,9 +1,9 @@
1
- import { Injectable } from '@angular/core';
1
+ import { Injectable, Inject } from '@angular/core';
2
2
  import { Router, NavigationEnd } from '@angular/router';
3
3
  import { Subject, Observable, ConnectableObservable, fromEvent, merge, combineLatest } from 'rxjs';
4
4
  import { startWith, withLatestFrom, tap, takeUntil, switchMap, debounceTime, mergeMap, publishReplay, filter, map, pairwise, scan } from 'rxjs/operators';
5
- import { FirebaseService } from '../firebase/firebase.service';
6
- import { LogService } from './log.service';
5
+ import { LOG_SERVICE, LogService } from './log.service';
6
+ import { DATABASE_SERVICE, DatabaseService } from '../firebase/database.service';
7
7
  import { SessionLocalStorageService } from '../local-storage/session-local-storage.service';
8
8
  import { SessionService } from '../local-storage/session.service';
9
9
 
@@ -75,24 +75,16 @@ export class TimeAnalyticsService {
75
75
  /** Interactions data at latest route. */
76
76
  private interactionsAtRoute: Interaction[] = null;
77
77
 
78
- /**
79
- * Helper function for getting a log id to make sure we don't save two logs
80
- * of the same session at the same time
81
- */
82
- private getLogId(data) {
83
- return `${data.__sessionId}__${data.startTime}`;
84
- }
85
-
86
78
  /** Helper function for saving log to DB given timeAtRoute, interactionsAtRoute. */
87
79
  private saveLogToDB(timeAtRoute: TimeAtRoute, interactionsAtRoute: Interaction[]) {
88
80
  if (timeAtRoute) {
89
81
  if (!interactionsAtRoute) {
90
- this.log.rawLog(timeAtRoute, 'logs', this.getLogId(timeAtRoute));
82
+ this.log.rawLog(timeAtRoute);
91
83
  } else {
92
84
  this.log.rawLog({
93
85
  ...timeAtRoute,
94
86
  interactions: interactionsAtRoute,
95
- }, 'logs', this.getLogId(timeAtRoute));
87
+ });
96
88
  }
97
89
  }
98
90
  }
@@ -195,8 +187,8 @@ export class TimeAnalyticsService {
195
187
 
196
188
  constructor(
197
189
  private router: Router,
198
- private db: FirebaseService,
199
- private log: LogService,
190
+ @Inject(DATABASE_SERVICE) private db: DatabaseService,
191
+ @Inject(LOG_SERVICE) private log: LogService,
200
192
  private local: SessionLocalStorageService,
201
193
  private session: SessionService,
202
194
  ) {
@@ -214,12 +206,12 @@ export class TimeAnalyticsService {
214
206
  ) {
215
207
  const interactionsAtRoute = this.local.getItemForSession(this.IX_KEY);
216
208
  if (!interactionsAtRoute) {
217
- this.log.rawLogOnBeaconEvent(timeAtRoute, 'logs', this.getLogId(timeAtRoute));
209
+ this.log.rawLogOnBeaconEvent(timeAtRoute);
218
210
  } else {
219
211
  this.log.rawLogOnBeaconEvent({
220
212
  ...timeAtRoute,
221
213
  interactions: interactionsAtRoute,
222
- }, 'logs', this.getLogId(timeAtRoute));
214
+ });
223
215
  }
224
216
  }
225
217
  }
@@ -1,28 +1,31 @@
1
- import { Injectable } from '@angular/core';
1
+ import { Inject, inject, Injectable } from '@angular/core';
2
2
  import { ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
3
- import { of, Observable, forkJoin } from 'rxjs';
4
- import { combineLatest, tap, filter, switchMap, map, take, pluck, withLatestFrom } from 'rxjs/operators';
5
- import { select, Store } from '@ngrx/store';
6
3
  import { User, AccessState } from '../store/user/user.model';
7
- import * as fromStore from '../store/app.reducer';
8
- import * as fromAuth from '../store/auth/auth.reducer';
9
- import { FirebaseService } from '../firebase/firebase.service';
10
- import { AuthSelectorsService } from '../store/auth/auth.selectors';
4
+ import { AuthStore } from '../store/auth/auth.store';
5
+ import { filter, switchMap, take } from 'rxjs/operators';
6
+ import { of, Observable } from 'rxjs';
7
+ import { toObservable } from '@angular/core/rxjs-interop';
8
+ import { DATABASE_SERVICE, DatabaseService } from '../firebase/database.service';
11
9
 
12
10
  @Injectable({
13
11
  providedIn: 'root',
14
12
  })
15
13
  export class AuthGuard {
14
+ readonly authStore = inject(AuthStore);
15
+ user$: Observable<User>;
16
+
16
17
  constructor(
17
- private db: FirebaseService,
18
- private store: Store<fromStore.State>,
19
18
  private router: Router,
20
- private authSelector: AuthSelectorsService,
21
- ) { }
19
+ @Inject(DATABASE_SERVICE) private db: DatabaseService,
20
+ ) {
21
+ this.user$ = toObservable(this.authStore.user).pipe(
22
+ filter((user) => user !== null),
23
+ );
24
+ }
22
25
 
23
26
  canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
24
27
  return this.db.afUser().pipe(
25
- switchMap((afUser) => afUser ? this.authSelector.selectAuthUser() : of(null)),
28
+ switchMap((afUser) => afUser ? this.user$.pipe(take(1)) : of(null)),
26
29
  switchMap((dbUser) => {
27
30
  // The overall approach is that we have a sequence of things we need to check (signed in, consent, waiting, walkthrough).
28
31
  // - For each, we want to make sure people not past that stage can't see past it,
@@ -53,24 +56,24 @@ export class AuthGuard {
53
56
 
54
57
 
55
58
  // Then handle whether users have consented. If not, go to consent, otherwise skip past consent.
56
- if ((!dbUser.consented || dbUser.accessState === AccessState.CONSENT) && state.url !== '/onboarding/consent') {
57
- this.router.navigate(['/onboarding/consent'], { queryParams: next.queryParams });
59
+ if ((!dbUser.consented || dbUser.accessState === AccessState.CONSENT) && state.url !== '/consent') {
60
+ this.router.navigate(['/consent'], { queryParams: next.queryParams });
58
61
  return of(false);
59
- } else if ((!dbUser.consented || dbUser.accessState === AccessState.CONSENT) && state.url === '/onboarding/consent') {
62
+ } else if ((!dbUser.consented || dbUser.accessState === AccessState.CONSENT) && state.url === '/consent') {
60
63
  return of(true);
61
- } else if (dbUser.consented && dbUser.accessState !== AccessState.CONSENT && state.url === '/onboarding/consent') {
62
- this.router.navigate(['/onboarding/waiting'], { queryParams: next.queryParams });
64
+ } else if (dbUser.consented && dbUser.accessState !== AccessState.CONSENT && state.url === '/consent') {
65
+ this.router.navigate(['/early-access'], { queryParams: next.queryParams });
63
66
  return of(false);
64
67
  }
65
68
 
66
69
  // Then handle whether users are past waitlist. If not, go to waiting, otherwise skip past waiting.
67
- const waitingStates = [AccessState.SUBMIT_INTEREST, AccessState.SUBMIT_DETAILED, AccessState.WAITING];
68
- if (waitingStates.includes(dbUser.accessState) && state.url !== '/onboarding/waiting') {
69
- this.router.navigate(['/onboarding/waiting'], { queryParams: next.queryParams });
70
+ const waitingStates = [AccessState.SUBMIT_INTEREST, AccessState.WAITING];
71
+ if (waitingStates.includes(dbUser.accessState) && state.url !== '/early-access') {
72
+ this.router.navigate(['/early-access'], { queryParams: next.queryParams });
70
73
  return of(false);
71
- } else if (waitingStates.includes(dbUser.accessState) && state.url === '/onboarding/waiting') {
74
+ } else if (waitingStates.includes(dbUser.accessState) && state.url === '/early-access') {
72
75
  return of(true);
73
- } else if (!waitingStates.includes(dbUser.accessState) && state.url === '/onboarding/waiting') {
76
+ } else if (!waitingStates.includes(dbUser.accessState) && state.url === '/early-access') {
74
77
  this.router.navigate(['/home'], { queryParams: next.queryParams });
75
78
  return of(false);
76
79
  }
@@ -0,0 +1,291 @@
1
+ import { Inject, Injectable, WritableSignal, inject, DestroyRef, computed } from '@angular/core';
2
+ import { toObservable } from '@angular/core/rxjs-interop';
3
+ import { Subject, Observable, merge, forkJoin, of, from, pipe } from 'rxjs';
4
+ import { startWith, pairwise, publishLast, publish, publishReplay, refCount, tap, pluck, mergeMap, filter, takeUntil, skipUntil, buffer, skip, switchMap, map, take } from 'rxjs/operators';
5
+ import { patchState } from '@ngrx/signals';
6
+ import { setEntities, setEntity, removeEntities, SelectEntityId } from '@ngrx/signals/entities';
7
+ import { EntityLoadQuery, EntityStreamQuery, entityInQueryResults } from '../store/app.store';
8
+ import { QueryParams, QueryOptions, AnyEntity } from '../store/app.model';
9
+ import { DocumentChange } from '@angular/fire/firestore';
10
+ import { DATABASE_SERVICE, DatabaseService } from './database.service';
11
+
12
+ const selectId: SelectEntityId<AnyEntity> = (entity) => entity.__id;
13
+
14
+ export interface CachedConnection {
15
+ queryKey: string;
16
+ registeredSet: Set<DestroyRef>;
17
+ }
18
+
19
+ @Injectable({
20
+ providedIn: 'root',
21
+ })
22
+ export class CachedListenersService {
23
+ /** Cached connections by loadKey. */
24
+ private connectionByQueryKey: Record<string, CachedConnection> = {};
25
+
26
+ /** Close connection events. */
27
+ private closeConnection$: Subject<string> = new Subject<string>();
28
+
29
+ /** Load state across the app by queryKey. Stores last load time in milliseconds. */
30
+ private lastLoadTime: Record<string, number> = {};
31
+
32
+ /** Define a unique key for each connection. */
33
+ private getQueryKey(collection: string, queryParams: QueryParams, queryOptions: QueryOptions): string {
34
+ let queryOptionsString = '';
35
+ if (queryOptions) {
36
+ if (queryOptions.orderBy) {
37
+ queryOptionsString = queryOptionsString.concat(`orderBy${queryOptions.orderBy}`);
38
+ }
39
+ if (queryOptions.limit) {
40
+ queryOptionsString = queryOptionsString.concat(`limit${queryOptions.limit}`);
41
+ }
42
+ if (queryOptions.startAt) {
43
+ queryOptionsString = queryOptionsString.concat(`startAt${queryOptions.startAt}`);
44
+ }
45
+ if (queryOptions.startAfter) {
46
+ queryOptionsString = queryOptionsString.concat(`startAfter${queryOptions.startAfter}`);
47
+ }
48
+ if (queryOptions.endAt) {
49
+ queryOptionsString = queryOptionsString.concat(`endAt${queryOptions.endAt}`);
50
+ }
51
+ if (queryOptions.endBefore) {
52
+ queryOptionsString = queryOptionsString.concat(`endBefore${queryOptions.endBefore}`);
53
+ }
54
+ }
55
+ return `${collection}.${queryParams.sort()}.${queryOptionsString}`;
56
+ }
57
+
58
+ /** Return whether a stream exists for this query. */
59
+ isStreamForQuery(collection: string, queryParams: QueryParams, queryOptions: QueryOptions): boolean {
60
+ const queryKey = this.getQueryKey(collection, queryParams, queryOptions);
61
+ return !!this.connectionByQueryKey[queryKey];
62
+ }
63
+
64
+ /** Get the last load time for a particular query. */
65
+ getLastLoadTime(collection: string, queryParams: QueryParams, queryOptions: QueryOptions): number {
66
+ // If there are queryOptions, we don't support saving load times
67
+ if (queryOptions && Object.keys(queryOptions).length > 0) return -1;
68
+
69
+ const queryKey = this.getQueryKey(collection, queryParams, queryOptions);
70
+ return this.lastLoadTime[queryKey] || -1;
71
+ }
72
+
73
+ /** Set the last load time for a particular query. */
74
+ setLastLoadTime(collection: string, queryParams: QueryParams, queryOptions: QueryOptions, time: number): void {
75
+ // If there are queryOptions, we don't support saving load times
76
+ if (queryOptions && Object.keys(queryOptions).length > 0) return;
77
+
78
+ const queryKey = this.getQueryKey(collection, queryParams, queryOptions);
79
+ this.lastLoadTime[queryKey] = time;
80
+ }
81
+
82
+ /** Gets a cached connection if one exists. */
83
+ private getConnection(collection: string, queryParams: QueryParams, queryOptions: QueryOptions): CachedConnection {
84
+ const queryKey = this.getQueryKey(collection, queryParams, queryOptions);
85
+ return this.connectionByQueryKey[queryKey];
86
+ }
87
+
88
+ /**
89
+ * Main function called by the outside.
90
+ */
91
+ public async processStreamQuery<T extends AnyEntity>(
92
+ collection: string,
93
+ store,
94
+ queryParams: QueryParams,
95
+ queryOptions: QueryOptions,
96
+ injector,
97
+ streamQueries?: (entity: T) => EntityStreamQuery<AnyEntity>[],
98
+ options?: {
99
+ loading?: WritableSignal<boolean>,
100
+ },
101
+ ): Promise<CachedConnection> {
102
+ // console.log("PROCESS QUERY: " + this.getQueryKey(collection, queryParams, queryOptions));
103
+
104
+ // Get destroyRef for the component that called processStreamQuery
105
+ // (which will be used to determine when to trigger unsubscribe)
106
+ const destroyRef = injector.get(DestroyRef);
107
+
108
+ // get connection and process the immediate query
109
+ let connection = this.getConnection(collection, queryParams, queryOptions);
110
+
111
+ if (connection) {
112
+ // if the connection already exists, don't need to create a new connection,
113
+ // but may need to add the DestroyRef if it doesn't already exist
114
+ if (!connection.registeredSet.has(destroyRef)) {
115
+ connection.registeredSet.add(destroyRef);
116
+ }
117
+ } else {
118
+ // if the connection does not exist, create it and initialize the
119
+ // registeredSet to include destroyRef
120
+ // this function also will subscribe and link to the store
121
+ connection = this.createConnection<T>(collection, store, queryParams, queryOptions, destroyRef);
122
+ this.connectionByQueryKey[connection.queryKey] = connection;
123
+ }
124
+
125
+ // Unregister from the connection when the component is destroyed
126
+ // Disconnects the "virtual" connection. If this was the only DestroyRef,
127
+ // then actually unsubscribe
128
+ destroyRef.onDestroy(() => {
129
+ // Add a 5-second delay so we don't have to reconnect multiple times
130
+ // if the next view happens to use the same stream
131
+ setTimeout(() => {
132
+ connection.registeredSet.delete(destroyRef);
133
+ if (connection.registeredSet.size === 0) {
134
+ this.closeConnection$.next(connection.queryKey);
135
+ this.connectionByQueryKey[connection.queryKey] = undefined;
136
+ }
137
+ }, 5000);
138
+ });
139
+
140
+ // For every new entity on the stream, process the nested streamQueries
141
+ if (streamQueries) {
142
+ // get the entities we're streaming as a signal. we do it here rather than
143
+ // using the firestore stream so that we can always get the latest version
144
+ // without needing to multicast the original stream to all future components
145
+ // querying for the stream
146
+ const streamEntities = computed(() => {
147
+ return store.selectEntities(queryParams, queryOptions);
148
+ });
149
+
150
+ // convert to an observable, convert to the changes, then subscribe
151
+ // note: we do not need takeUntil for unsubscribing because toObservable will
152
+ // unsubscribe once the injector context (passed in from the component/container) is destroyed
153
+ toObservable(streamEntities, { injector }).pipe(
154
+ // convert to entityMap to more easily compare
155
+ map((entities) => {
156
+ const entityMap = {};
157
+ for (const entity of entities) {
158
+ entityMap[entity.__id] = entity;
159
+ }
160
+ return entityMap;
161
+ }),
162
+ // compare current with prior including the first
163
+ startWith({}),
164
+ pairwise(),
165
+ // get the changedEntities
166
+ map(([prevEntities, currEntities]) => {
167
+ const changedEntities = [];
168
+ for (const key in currEntities) {
169
+ if (!(key in prevEntities)) {
170
+ // the entity was added
171
+ changedEntities.push({
172
+ type: 'added',
173
+ doc: currEntities[key],
174
+ });
175
+ } else if (currEntities[key]['_deleted']) {
176
+ // the entity was removed
177
+ changedEntities.push({
178
+ type: 'removed',
179
+ doc: currEntities[key],
180
+ });
181
+ } else if (currEntities[key]['_updatedAt'] !== prevEntities[key]['_updatedAt']) {
182
+ // the entity was modified
183
+ changedEntities.push({
184
+ type: 'modified',
185
+ doc: currEntities[key],
186
+ });
187
+ }
188
+ }
189
+
190
+ return changedEntities;
191
+ }),
192
+ // We don't need the removed ones for nested streamQueries (but I just
193
+ // included it above for debugging purposes so we can see all changes)
194
+ map((changes) => changes.filter((c) => c.type !== 'removed')),
195
+ filter((changes) => changes.length > 0),
196
+ ).subscribe(async (changes) => {
197
+ // create a matrix of queries, each row corresponding to a given entity
198
+ const queriesByEntity = changes.map((change) => streamQueries(change.doc));
199
+
200
+ // transpose the matrix so each row corresponds to all queries for the same query type
201
+ const queriesByQueryType = queriesByEntity[0].map((_, colIndex) => queriesByEntity.map((row) => row[colIndex]));
202
+
203
+ // Process all query types concurrently
204
+ await Promise.all(queriesByQueryType.map(async (queriesForOneType) => {
205
+ // Seeing if we can optimize by collapsing equality queries into 'in' queries
206
+ const optimizedQueries = EntityStreamQuery.optimize(queriesForOneType);
207
+
208
+ // Process queries concurrently using Promise.all
209
+ await Promise.all(optimizedQueries.filter((q) => !!q).map(async (query) => {
210
+ await this.processStreamQuery(query.collection, query.store, query.queryParams, query.queryOptions, injector, query.streamQueries, {
211
+ loading: options?.loading,
212
+ });
213
+ }));
214
+ }));
215
+ });
216
+ } else {
217
+ // We'll turn off the loading indicator the first time there is a query that doesn't have nested queries
218
+ // (ideally we'd want to turn if off after each of the streams have gotten their first entities, but I
219
+ // don't think we can do that easily and this won't be too off)
220
+ options?.loading?.set(false);
221
+ }
222
+
223
+ return connection;
224
+ }
225
+
226
+ private createConnection<T>(collection: string, store, queryParams: QueryParams, queryOptions: QueryOptions, destroyRef: DestroyRef): CachedConnection {
227
+ // console.log("CONNECT " + this.getQueryKey(collection, queryParams, queryOptions));
228
+
229
+ // ------------ DEFINE OBSERVABLES FOR DISCONNECTING ------------------------------
230
+
231
+ const queryKey = this.getQueryKey(collection, queryParams, queryOptions);
232
+ const disconnectObs$ = this.closeConnection$.pipe(
233
+ filter((qk) => qk === queryKey),
234
+ take(1),
235
+ // tap(a => { console.log("DISCONNECT: " + queryKey) }),
236
+ publish(),
237
+ refCount(),
238
+ );
239
+
240
+ // ------------ HANDLE PATCHING TO STORE ------------------------------------------
241
+
242
+ // Handle patching to the store
243
+ this.db.streamEntitiesChanges(collection, queryParams, queryOptions).pipe(
244
+ takeUntil(disconnectObs$),
245
+ ).subscribe((fbPayload: Array<{ type: string, docData: AnyEntity }>) => {
246
+ // Handle all the added entities
247
+ const entitiesToAdd = fbPayload
248
+ .filter((dc) => dc.type === 'added' && !dc.docData['_deleted'])
249
+ .map((dc) => dc.docData);
250
+
251
+ // Handle all the modified entities
252
+ const entitiesToModify = fbPayload
253
+ .filter((dc) => dc.type === 'modified' && !dc.docData['_deleted'])
254
+ .map((dc) => dc.docData);
255
+
256
+ // Handle all the removed entities
257
+ const entitiesToRemove = fbPayload
258
+ .filter((dc) => dc.type === 'modified' && dc.docData['_deleted'])
259
+ .map((dc) => dc.docData['__id']);
260
+
261
+ // Handle changes of the 'removed' type. In general, firebase 'removed'
262
+ // could be due to no longer matching the stream query rather than being
263
+ // deleted from the DB. this is why we use soft deletes (set _deleted to true)
264
+ // so technically we shouldn't be getting any 'removed' entries. however,
265
+ // if there were manual deletions in the database, there could still be changes
266
+ // of this type which the below code seeks to handle
267
+ const entitiesToModify2 = fbPayload
268
+ .filter((dc) => dc.type === 'removed' && !entityInQueryResults(dc.docData, queryParams, queryOptions))
269
+ .map((dc) => dc.docData);
270
+
271
+
272
+ const entitiesToRemove2 = fbPayload
273
+ .filter((dc) => dc.type === 'removed' && entityInQueryResults(dc.docData, queryParams, queryOptions))
274
+ .map((dc) => dc.docData['__id']);
275
+
276
+ // Patch all the changes to the store
277
+ patchState(store, setEntities(entitiesToAdd, { selectId }));
278
+ patchState(store, setEntities([...entitiesToModify, ...entitiesToModify2], { selectId }));
279
+ patchState(store, removeEntities([...entitiesToRemove, ...entitiesToRemove2]));
280
+ });
281
+
282
+ return {
283
+ queryKey: this.getQueryKey(collection, queryParams, queryOptions),
284
+ registeredSet: new Set<DestroyRef>([destroyRef]),
285
+ };
286
+ }
287
+
288
+ constructor(
289
+ @Inject(DATABASE_SERVICE) private db: DatabaseService,
290
+ ) { }
291
+ }
@@ -0,0 +1,56 @@
1
+ import { InjectionToken, Injector } from '@angular/core';
2
+ import { QueryParams, QueryOptions, AnyEntity } from '../store/app.model';
3
+ import { Observable } from 'rxjs';
4
+ import { User } from '@angular/fire/auth';
5
+ import { WriteBatchI } from '../store/batch-write.service';
6
+
7
+ export interface DatabaseService {
8
+ createId(): string;
9
+ afUser(): Observable<{ uid: string, email: string, displayName: string, photoURL: string }>;
10
+ login(providerId: string, scope?: string): any;
11
+ loginLink(error): void;
12
+ logout(): any;
13
+ streamEntity<T>(collectionName: string, id: string): Observable<T>;
14
+ streamEntities<T>(collectionName, queryParams, queryOptions?): Observable<T[]>;
15
+ streamEntitiesChanges<T>(collectionName, queryParams, queryOptions?): Observable<{
16
+ type: string;
17
+ docData: T;
18
+ }[]>;
19
+ getEntity<T>(collectionName: string, id: string): Promise<T>;
20
+ getEntities<S>(
21
+ collectionName: string,
22
+ queryParams: [string, string, any][],
23
+ queryOptions?: { [index: string]: any },
24
+ ): Promise<S[]>;
25
+ count(
26
+ collectionName: string,
27
+ queryParams: QueryParams,
28
+ queryOptions?: QueryOptions,
29
+ ): Promise<number>;
30
+ aggregate<S extends { [k: string]: number }>(
31
+ collectionName: string,
32
+ queryParams: QueryParams,
33
+ queryOptions: QueryOptions,
34
+ aggregateParams: { [K in keyof S]: [string] | [string, string] },
35
+ ): Promise<{ [K in keyof S]: number }>;
36
+ addEntity(
37
+ collectionName: string,
38
+ entity: AnyEntity,
39
+ batch?: WriteBatchI,
40
+ ): Promise<any>;
41
+ updateEntity(
42
+ collectionName: string,
43
+ id: string,
44
+ changes: Partial<AnyEntity>,
45
+ batch?: WriteBatchI,
46
+ ): Promise<any>;
47
+ removeEntity(
48
+ collectionName: string,
49
+ id: string,
50
+ batch?: WriteBatchI,
51
+ ): Promise<any>;
52
+ incrementEntityField(collectionName, id, field, delta): Promise<void>;
53
+ }
54
+
55
+ // InjectionToken that can be swapped out for
56
+ export const DATABASE_SERVICE = new InjectionToken<DatabaseService>('DatabaseService');