@blackcode_sa/metaestetics-api 1.12.59 → 1.12.61

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 (267) hide show
  1. package/dist/admin/index.d.mts +36 -4
  2. package/dist/admin/index.d.ts +36 -4
  3. package/dist/admin/index.js +147 -26
  4. package/dist/admin/index.mjs +147 -26
  5. package/dist/index.d.mts +9 -1
  6. package/dist/index.d.ts +9 -1
  7. package/package.json +119 -119
  8. package/src/__mocks__/firstore.ts +10 -10
  9. package/src/admin/aggregation/README.md +79 -79
  10. package/src/admin/aggregation/appointment/README.md +128 -128
  11. package/src/admin/aggregation/appointment/appointment.aggregation.service.ts +1844 -1689
  12. package/src/admin/aggregation/appointment/index.ts +1 -1
  13. package/src/admin/aggregation/clinic/README.md +52 -52
  14. package/src/admin/aggregation/clinic/clinic.aggregation.service.ts +703 -703
  15. package/src/admin/aggregation/clinic/index.ts +1 -1
  16. package/src/admin/aggregation/forms/README.md +13 -13
  17. package/src/admin/aggregation/forms/filled-forms.aggregation.service.ts +322 -322
  18. package/src/admin/aggregation/forms/index.ts +1 -1
  19. package/src/admin/aggregation/index.ts +8 -8
  20. package/src/admin/aggregation/patient/README.md +27 -27
  21. package/src/admin/aggregation/patient/index.ts +1 -1
  22. package/src/admin/aggregation/patient/patient.aggregation.service.ts +141 -141
  23. package/src/admin/aggregation/practitioner/README.md +42 -42
  24. package/src/admin/aggregation/practitioner/index.ts +1 -1
  25. package/src/admin/aggregation/practitioner/practitioner.aggregation.service.ts +433 -433
  26. package/src/admin/aggregation/practitioner-invite/index.ts +1 -1
  27. package/src/admin/aggregation/practitioner-invite/practitioner-invite.aggregation.service.ts +961 -961
  28. package/src/admin/aggregation/procedure/README.md +43 -43
  29. package/src/admin/aggregation/procedure/index.ts +1 -1
  30. package/src/admin/aggregation/procedure/procedure.aggregation.service.ts +702 -702
  31. package/src/admin/aggregation/reviews/index.ts +1 -1
  32. package/src/admin/aggregation/reviews/reviews.aggregation.service.ts +641 -641
  33. package/src/admin/booking/README.md +125 -125
  34. package/src/admin/booking/booking.admin.ts +1037 -1037
  35. package/src/admin/booking/booking.calculator.ts +712 -712
  36. package/src/admin/booking/booking.types.ts +59 -59
  37. package/src/admin/booking/index.ts +3 -3
  38. package/src/admin/booking/timezones-problem.md +185 -185
  39. package/src/admin/calendar/README.md +7 -7
  40. package/src/admin/calendar/calendar.admin.service.ts +345 -345
  41. package/src/admin/calendar/index.ts +1 -1
  42. package/src/admin/documentation-templates/document-manager.admin.ts +260 -260
  43. package/src/admin/documentation-templates/index.ts +1 -1
  44. package/src/admin/free-consultation/free-consultation-utils.admin.ts +148 -148
  45. package/src/admin/free-consultation/index.ts +1 -1
  46. package/src/admin/index.ts +75 -75
  47. package/src/admin/logger/index.ts +78 -78
  48. package/src/admin/mailing/README.md +95 -95
  49. package/src/admin/mailing/appointment/appointment.mailing.service.ts +732 -732
  50. package/src/admin/mailing/appointment/index.ts +1 -1
  51. package/src/admin/mailing/appointment/templates/patient/appointment-confirmed.html +40 -40
  52. package/src/admin/mailing/base.mailing.service.ts +208 -208
  53. package/src/admin/mailing/index.ts +3 -3
  54. package/src/admin/mailing/practitionerInvite/existing-practitioner-invite.mailing.ts +611 -611
  55. package/src/admin/mailing/practitionerInvite/index.ts +2 -2
  56. package/src/admin/mailing/practitionerInvite/practitionerInvite.mailing.ts +395 -395
  57. package/src/admin/mailing/practitionerInvite/templates/existing-practitioner-invitation.template.ts +155 -155
  58. package/src/admin/mailing/practitionerInvite/templates/invitation.template.ts +101 -101
  59. package/src/admin/mailing/practitionerInvite/templates/invite-accepted-notification.template.ts +228 -228
  60. package/src/admin/mailing/practitionerInvite/templates/invite-rejected-notification.template.ts +242 -242
  61. package/src/admin/notifications/index.ts +1 -1
  62. package/src/admin/notifications/notifications.admin.ts +710 -710
  63. package/src/admin/requirements/README.md +128 -128
  64. package/src/admin/requirements/index.ts +1 -1
  65. package/src/admin/requirements/patient-requirements.admin.service.ts +475 -475
  66. package/src/admin/users/index.ts +1 -1
  67. package/src/admin/users/user-profile.admin.ts +405 -405
  68. package/src/backoffice/constants/certification.constants.ts +13 -13
  69. package/src/backoffice/constants/index.ts +1 -1
  70. package/src/backoffice/errors/backoffice.errors.ts +181 -181
  71. package/src/backoffice/errors/index.ts +1 -1
  72. package/src/backoffice/expo-safe/README.md +26 -26
  73. package/src/backoffice/expo-safe/index.ts +41 -41
  74. package/src/backoffice/index.ts +5 -5
  75. package/src/backoffice/services/FIXES_README.md +102 -102
  76. package/src/backoffice/services/README.md +40 -40
  77. package/src/backoffice/services/brand.service.ts +256 -256
  78. package/src/backoffice/services/category.service.ts +318 -318
  79. package/src/backoffice/services/constants.service.ts +385 -385
  80. package/src/backoffice/services/documentation-template.service.ts +202 -202
  81. package/src/backoffice/services/index.ts +8 -8
  82. package/src/backoffice/services/migrate-products.ts +116 -116
  83. package/src/backoffice/services/product.service.ts +553 -553
  84. package/src/backoffice/services/requirement.service.ts +235 -235
  85. package/src/backoffice/services/subcategory.service.ts +395 -395
  86. package/src/backoffice/services/technology.service.ts +1070 -1070
  87. package/src/backoffice/types/README.md +12 -12
  88. package/src/backoffice/types/admin-constants.types.ts +69 -69
  89. package/src/backoffice/types/brand.types.ts +29 -29
  90. package/src/backoffice/types/category.types.ts +62 -62
  91. package/src/backoffice/types/documentation-templates.types.ts +28 -28
  92. package/src/backoffice/types/index.ts +10 -10
  93. package/src/backoffice/types/procedure-product.types.ts +38 -38
  94. package/src/backoffice/types/product.types.ts +240 -240
  95. package/src/backoffice/types/requirement.types.ts +63 -63
  96. package/src/backoffice/types/static/README.md +18 -18
  97. package/src/backoffice/types/static/blocking-condition.types.ts +21 -21
  98. package/src/backoffice/types/static/certification.types.ts +37 -37
  99. package/src/backoffice/types/static/contraindication.types.ts +19 -19
  100. package/src/backoffice/types/static/index.ts +6 -6
  101. package/src/backoffice/types/static/pricing.types.ts +16 -16
  102. package/src/backoffice/types/static/procedure-family.types.ts +14 -14
  103. package/src/backoffice/types/static/treatment-benefit.types.ts +22 -22
  104. package/src/backoffice/types/subcategory.types.ts +34 -34
  105. package/src/backoffice/types/technology.types.ts +161 -161
  106. package/src/backoffice/validations/index.ts +1 -1
  107. package/src/backoffice/validations/schemas.ts +163 -163
  108. package/src/config/__mocks__/firebase.ts +99 -99
  109. package/src/config/firebase.ts +78 -78
  110. package/src/config/index.ts +9 -9
  111. package/src/errors/auth.error.ts +6 -6
  112. package/src/errors/auth.errors.ts +200 -200
  113. package/src/errors/clinic.errors.ts +32 -32
  114. package/src/errors/firebase.errors.ts +47 -47
  115. package/src/errors/user.errors.ts +99 -99
  116. package/src/index.backup.ts +407 -407
  117. package/src/index.ts +6 -6
  118. package/src/locales/en.ts +31 -31
  119. package/src/recommender/admin/index.ts +1 -1
  120. package/src/recommender/admin/services/recommender.service.admin.ts +5 -5
  121. package/src/recommender/front/index.ts +1 -1
  122. package/src/recommender/front/services/onboarding.service.ts +5 -5
  123. package/src/recommender/front/services/recommender.service.ts +3 -3
  124. package/src/recommender/index.ts +1 -1
  125. package/src/services/PATIENTAUTH.MD +197 -197
  126. package/src/services/README.md +106 -106
  127. package/src/services/__tests__/auth/auth.mock.test.ts +17 -17
  128. package/src/services/__tests__/auth/auth.setup.ts +293 -293
  129. package/src/services/__tests__/auth.service.test.ts +346 -346
  130. package/src/services/__tests__/base.service.test.ts +77 -77
  131. package/src/services/__tests__/user.service.test.ts +528 -528
  132. package/src/services/appointment/README.md +17 -17
  133. package/src/services/appointment/appointment.service.ts +2082 -2082
  134. package/src/services/appointment/index.ts +1 -1
  135. package/src/services/appointment/utils/appointment.utils.ts +552 -552
  136. package/src/services/appointment/utils/extended-procedure.utils.ts +314 -314
  137. package/src/services/appointment/utils/form-initialization.utils.ts +225 -225
  138. package/src/services/appointment/utils/recommended-procedure.utils.ts +195 -195
  139. package/src/services/appointment/utils/zone-management.utils.ts +353 -353
  140. package/src/services/appointment/utils/zone-photo.utils.ts +152 -152
  141. package/src/services/auth/auth.service.ts +989 -989
  142. package/src/services/auth/auth.v2.service.ts +961 -961
  143. package/src/services/auth/index.ts +7 -7
  144. package/src/services/auth/utils/error.utils.ts +90 -90
  145. package/src/services/auth/utils/firebase.utils.ts +49 -49
  146. package/src/services/auth/utils/index.ts +21 -21
  147. package/src/services/auth/utils/practitioner.utils.ts +125 -125
  148. package/src/services/base.service.ts +41 -41
  149. package/src/services/calendar/calendar.service.ts +1077 -1077
  150. package/src/services/calendar/calendar.v2.service.ts +1683 -1683
  151. package/src/services/calendar/calendar.v3.service.ts +313 -313
  152. package/src/services/calendar/externalCalendar.service.ts +178 -178
  153. package/src/services/calendar/index.ts +5 -5
  154. package/src/services/calendar/synced-calendars.service.ts +743 -743
  155. package/src/services/calendar/utils/appointment.utils.ts +265 -265
  156. package/src/services/calendar/utils/calendar-event.utils.ts +646 -646
  157. package/src/services/calendar/utils/clinic.utils.ts +237 -237
  158. package/src/services/calendar/utils/docs.utils.ts +157 -157
  159. package/src/services/calendar/utils/google-calendar.utils.ts +697 -697
  160. package/src/services/calendar/utils/index.ts +8 -8
  161. package/src/services/calendar/utils/patient.utils.ts +198 -198
  162. package/src/services/calendar/utils/practitioner.utils.ts +221 -221
  163. package/src/services/calendar/utils/synced-calendar.utils.ts +472 -472
  164. package/src/services/clinic/README.md +204 -204
  165. package/src/services/clinic/__tests__/clinic-admin.service.test.ts +287 -287
  166. package/src/services/clinic/__tests__/clinic-group.service.test.ts +352 -352
  167. package/src/services/clinic/__tests__/clinic.service.test.ts +354 -354
  168. package/src/services/clinic/billing-transactions.service.ts +217 -217
  169. package/src/services/clinic/clinic-admin.service.ts +202 -202
  170. package/src/services/clinic/clinic-group.service.ts +310 -310
  171. package/src/services/clinic/clinic.service.ts +708 -708
  172. package/src/services/clinic/index.ts +5 -5
  173. package/src/services/clinic/practitioner-invite.service.ts +519 -519
  174. package/src/services/clinic/utils/admin.utils.ts +551 -551
  175. package/src/services/clinic/utils/clinic-group.utils.ts +646 -646
  176. package/src/services/clinic/utils/clinic.utils.ts +949 -949
  177. package/src/services/clinic/utils/filter.utils.d.ts +23 -23
  178. package/src/services/clinic/utils/filter.utils.ts +446 -446
  179. package/src/services/clinic/utils/index.ts +11 -11
  180. package/src/services/clinic/utils/photos.utils.ts +188 -188
  181. package/src/services/clinic/utils/search.utils.ts +84 -84
  182. package/src/services/clinic/utils/tag.utils.ts +124 -124
  183. package/src/services/documentation-templates/documentation-template.service.ts +537 -537
  184. package/src/services/documentation-templates/filled-document.service.ts +587 -587
  185. package/src/services/documentation-templates/index.ts +2 -2
  186. package/src/services/index.ts +13 -13
  187. package/src/services/media/index.ts +1 -1
  188. package/src/services/media/media.service.ts +418 -418
  189. package/src/services/notifications/__tests__/notification.service.test.ts +242 -242
  190. package/src/services/notifications/index.ts +1 -1
  191. package/src/services/notifications/notification.service.ts +215 -215
  192. package/src/services/patient/README.md +48 -48
  193. package/src/services/patient/To-Do.md +43 -43
  194. package/src/services/patient/__tests__/patient.service.test.ts +294 -294
  195. package/src/services/patient/index.ts +2 -2
  196. package/src/services/patient/patient.service.ts +883 -883
  197. package/src/services/patient/patientRequirements.service.ts +285 -285
  198. package/src/services/patient/utils/aesthetic-analysis.utils.ts +176 -176
  199. package/src/services/patient/utils/clinic.utils.ts +80 -80
  200. package/src/services/patient/utils/docs.utils.ts +142 -142
  201. package/src/services/patient/utils/index.ts +9 -9
  202. package/src/services/patient/utils/location.utils.ts +126 -126
  203. package/src/services/patient/utils/medical-stuff.utils.ts +143 -143
  204. package/src/services/patient/utils/medical.utils.ts +458 -458
  205. package/src/services/patient/utils/practitioner.utils.ts +260 -260
  206. package/src/services/patient/utils/profile.utils.ts +510 -510
  207. package/src/services/patient/utils/sensitive.utils.ts +260 -260
  208. package/src/services/patient/utils/token.utils.ts +211 -211
  209. package/src/services/practitioner/README.md +145 -145
  210. package/src/services/practitioner/index.ts +1 -1
  211. package/src/services/practitioner/practitioner.service.ts +1742 -1742
  212. package/src/services/procedure/README.md +163 -163
  213. package/src/services/procedure/index.ts +1 -1
  214. package/src/services/procedure/procedure.service.ts +1682 -1682
  215. package/src/services/reviews/index.ts +1 -1
  216. package/src/services/reviews/reviews.service.ts +636 -636
  217. package/src/services/user/index.ts +1 -1
  218. package/src/services/user/user.service.ts +489 -489
  219. package/src/services/user/user.v2.service.ts +466 -466
  220. package/src/types/appointment/index.ts +453 -453
  221. package/src/types/calendar/index.ts +258 -258
  222. package/src/types/calendar/synced-calendar.types.ts +66 -66
  223. package/src/types/clinic/index.ts +489 -489
  224. package/src/types/clinic/practitioner-invite.types.ts +91 -91
  225. package/src/types/clinic/preferences.types.ts +159 -159
  226. package/src/types/clinic/to-do +3 -3
  227. package/src/types/documentation-templates/index.ts +308 -308
  228. package/src/types/index.ts +44 -44
  229. package/src/types/notifications/README.md +77 -77
  230. package/src/types/notifications/index.ts +265 -265
  231. package/src/types/patient/aesthetic-analysis.types.ts +66 -66
  232. package/src/types/patient/allergies.ts +58 -58
  233. package/src/types/patient/index.ts +273 -273
  234. package/src/types/patient/medical-info.types.ts +152 -152
  235. package/src/types/patient/patient-requirements.ts +92 -81
  236. package/src/types/patient/token.types.ts +61 -61
  237. package/src/types/practitioner/index.ts +206 -206
  238. package/src/types/procedure/index.ts +181 -181
  239. package/src/types/profile/index.ts +39 -39
  240. package/src/types/reviews/index.ts +130 -130
  241. package/src/types/tz-lookup.d.ts +4 -4
  242. package/src/types/user/index.ts +38 -38
  243. package/src/utils/TIMESTAMPS.md +176 -176
  244. package/src/utils/TimestampUtils.ts +241 -241
  245. package/src/utils/index.ts +1 -1
  246. package/src/validations/appointment.schema.ts +574 -574
  247. package/src/validations/calendar.schema.ts +225 -225
  248. package/src/validations/clinic.schema.ts +493 -493
  249. package/src/validations/common.schema.ts +25 -25
  250. package/src/validations/documentation-templates/index.ts +1 -1
  251. package/src/validations/documentation-templates/template.schema.ts +220 -220
  252. package/src/validations/documentation-templates.schema.ts +10 -10
  253. package/src/validations/index.ts +20 -20
  254. package/src/validations/media.schema.ts +10 -10
  255. package/src/validations/notification.schema.ts +90 -90
  256. package/src/validations/patient/aesthetic-analysis.schema.ts +55 -55
  257. package/src/validations/patient/medical-info.schema.ts +125 -125
  258. package/src/validations/patient/patient-requirements.schema.ts +84 -75
  259. package/src/validations/patient/token.schema.ts +29 -29
  260. package/src/validations/patient.schema.ts +216 -216
  261. package/src/validations/practitioner.schema.ts +222 -222
  262. package/src/validations/procedure-product.schema.ts +41 -41
  263. package/src/validations/procedure.schema.ts +124 -124
  264. package/src/validations/profile-info.schema.ts +41 -41
  265. package/src/validations/reviews.schema.ts +189 -189
  266. package/src/validations/schemas.ts +104 -104
  267. package/src/validations/shared.schema.ts +78 -78
@@ -1,587 +1,587 @@
1
- import {
2
- collection,
3
- doc,
4
- getDoc,
5
- getDocs,
6
- setDoc,
7
- updateDoc,
8
- query,
9
- where,
10
- orderBy,
11
- limit,
12
- startAfter,
13
- QueryDocumentSnapshot,
14
- } from "firebase/firestore";
15
- import { BaseService } from "../base.service";
16
- import {
17
- // DocumentTemplate, // Not directly used in this file if relying on templateService
18
- // DOCUMENTATION_TEMPLATES_COLLECTION, // No longer needed directly here if using templateService
19
- // FILLED_DOCUMENTS_COLLECTION, // This will be replaced by subcollection paths
20
- FilledDocument,
21
- FilledDocumentStatus,
22
- FilledDocumentFileValue,
23
- USER_FORMS_SUBCOLLECTION,
24
- DOCTOR_FORMS_SUBCOLLECTION,
25
- } from "../../types"; // General types
26
- import { APPOINTMENTS_COLLECTION } from "../../types/appointment"; // Specific import for the constant
27
- import { DocumentationTemplateService } from "./documentation-template.service";
28
- import {
29
- MediaService,
30
- MediaAccessLevel,
31
- MediaMetadata,
32
- } from "../media/media.service";
33
- // Import the new validation schemas if you plan to use them for input validation here
34
- // import { createFilledDocumentDataSchema, updateFilledDocumentDataSchema } from '../../validations/documentation-templates.schema';
35
-
36
- /**
37
- * Service for managing filled documents within appointment subcollections
38
- */
39
- export class FilledDocumentService extends BaseService {
40
- // No single collectionRef anymore, paths will be dynamic
41
- private readonly templateService: DocumentationTemplateService;
42
- private readonly mediaService: MediaService;
43
-
44
- constructor(...args: ConstructorParameters<typeof BaseService>) {
45
- super(...args);
46
- this.templateService = new DocumentationTemplateService(...args); // Pass db and other args
47
- this.mediaService = new MediaService(...args); // Initialize media service with the same args
48
- }
49
-
50
- private getFormSubcollectionPath(
51
- isUserForm: boolean
52
- ): typeof USER_FORMS_SUBCOLLECTION | typeof DOCTOR_FORMS_SUBCOLLECTION {
53
- return isUserForm ? USER_FORMS_SUBCOLLECTION : DOCTOR_FORMS_SUBCOLLECTION;
54
- }
55
-
56
- /**
57
- * Create a new filled document within an appointment's subcollection.
58
- * @param templateId - ID of the template to use.
59
- * @param templateVersion - Version of the template to use.
60
- * @param appointmentId - ID of the appointment this form belongs to.
61
- * @param procedureId - ID of the procedure associated with this form.
62
- * @param patientId - ID of the patient.
63
- * @param practitionerId - ID of the practitioner (can be system/generic if patient is filling).
64
- * @param clinicId - ID of the clinic.
65
- * @param initialValues - Optional initial values for the form elements.
66
- * @param initialStatus - Optional initial status for the form.
67
- * @returns The created filled document.
68
- */
69
- async createFilledDocumentForAppointment(
70
- templateId: string,
71
- templateVersion: number,
72
- appointmentId: string,
73
- procedureId: string,
74
- patientId: string,
75
- practitionerId: string, // Consider if this is always available or if a placeholder is needed
76
- clinicId: string, // Same consideration as practitionerId
77
- initialValues: { [elementId: string]: any } = {},
78
- initialStatus: FilledDocumentStatus = FilledDocumentStatus.DRAFT
79
- ): Promise<FilledDocument> {
80
- const template = await this.templateService.getTemplateById(
81
- templateId,
82
- templateVersion
83
- );
84
- if (!template) {
85
- throw new Error(`Template with ID ${templateId} not found`);
86
- }
87
-
88
- const documentId = this.generateId();
89
- const now = Date.now();
90
- const isUserForm = template.isUserForm || false;
91
- const formSubcollection = this.getFormSubcollectionPath(isUserForm);
92
-
93
- const filledDocument: FilledDocument = {
94
- id: documentId,
95
- templateId,
96
- templateVersion: template.version,
97
- isUserForm: isUserForm, // Set based on template
98
- isRequired: template.isRequired || false, // Inherit isRequired from the template
99
- appointmentId: appointmentId, // NEW
100
- procedureId: procedureId, // NEW
101
- patientId,
102
- practitionerId,
103
- clinicId,
104
- createdAt: now,
105
- updatedAt: now,
106
- values: initialValues,
107
- status: initialStatus,
108
- };
109
-
110
- const docRef = doc(
111
- this.db,
112
- APPOINTMENTS_COLLECTION, // Replaced "appointments"
113
- appointmentId,
114
- formSubcollection,
115
- documentId
116
- );
117
- await setDoc(docRef, filledDocument);
118
-
119
- return filledDocument;
120
- }
121
-
122
- /**
123
- * Get a specific filled document from an appointment's subcollection.
124
- * @param appointmentId - ID of the appointment.
125
- * @param formId - ID of the filled document.
126
- * @param isUserForm - Boolean indicating if it's a user form or doctor form.
127
- * @returns The filled document or null if not found.
128
- */
129
- async getFilledDocumentFromAppointmentById(
130
- appointmentId: string,
131
- formId: string,
132
- isUserForm: boolean
133
- ): Promise<FilledDocument | null> {
134
- const formSubcollection = this.getFormSubcollectionPath(isUserForm);
135
- const docRef = doc(
136
- this.db,
137
- APPOINTMENTS_COLLECTION, // Replaced "appointments"
138
- appointmentId,
139
- formSubcollection,
140
- formId
141
- );
142
- const docSnap = await getDoc(docRef);
143
-
144
- if (!docSnap.exists()) {
145
- return null;
146
- }
147
- return docSnap.data() as FilledDocument;
148
- }
149
-
150
- /**
151
- * Update values or status in a filled document within an appointment's subcollection.
152
- * @param appointmentId - ID of the appointment.
153
- * @param formId - ID of the filled document to update.
154
- * @param isUserForm - Boolean indicating if it's a user form or doctor form.
155
- * @param values - Updated values for elements.
156
- * @param status - Optional new status for the document.
157
- * @returns The updated filled document.
158
- */
159
- async updateFilledDocumentInAppointment(
160
- appointmentId: string,
161
- formId: string,
162
- isUserForm: boolean,
163
- values?: { [elementId: string]: any },
164
- status?: FilledDocumentStatus
165
- ): Promise<FilledDocument> {
166
- const formSubcollection = this.getFormSubcollectionPath(isUserForm);
167
- const docRef = doc(
168
- this.db,
169
- APPOINTMENTS_COLLECTION, // Replaced "appointments"
170
- appointmentId,
171
- formSubcollection,
172
- formId
173
- );
174
-
175
- const existingDoc = await this.getFilledDocumentFromAppointmentById(
176
- appointmentId,
177
- formId,
178
- isUserForm
179
- );
180
-
181
- if (!existingDoc) {
182
- throw new Error(
183
- `Filled document with ID ${formId} not found in appointment ${appointmentId} ${formSubcollection}`
184
- );
185
- }
186
-
187
- const updatePayload: Partial<FilledDocument> = {
188
- updatedAt: Date.now(),
189
- };
190
-
191
- if (values) {
192
- updatePayload.values = {
193
- ...existingDoc.values,
194
- ...values,
195
- };
196
- }
197
- if (status) {
198
- updatePayload.status = status;
199
- }
200
-
201
- if (
202
- Object.keys(updatePayload).length === 1 &&
203
- "updatedAt" in updatePayload
204
- ) {
205
- // Only updatedAt, no actual data change, so we can skip the update
206
- // or just update timestamp if that is the desired behavior.
207
- // For now, we proceed to update the timestamp at least.
208
- }
209
-
210
- await updateDoc(docRef, updatePayload);
211
-
212
- return { ...existingDoc, ...updatePayload } as FilledDocument;
213
- }
214
-
215
- /**
216
- * Get all filled user forms for a specific appointment.
217
- * @param appointmentId ID of the appointment.
218
- * @param pageSize Number of documents to retrieve.
219
- * @param lastDoc Last document from previous page for pagination.
220
- */
221
- async getFilledUserFormsForAppointment(
222
- appointmentId: string,
223
- pageSize = 20,
224
- lastDoc?: QueryDocumentSnapshot<FilledDocument>
225
- ): Promise<{
226
- documents: FilledDocument[];
227
- lastDoc: QueryDocumentSnapshot<FilledDocument> | null;
228
- }> {
229
- const subcollectionRef = collection(
230
- this.db,
231
- APPOINTMENTS_COLLECTION, // Replaced "appointments"
232
- appointmentId,
233
- USER_FORMS_SUBCOLLECTION
234
- );
235
- let q = query(
236
- subcollectionRef,
237
- orderBy("updatedAt", "desc"),
238
- limit(pageSize)
239
- );
240
-
241
- if (lastDoc) {
242
- q = query(q, startAfter(lastDoc));
243
- }
244
- return this.executeQuery(q);
245
- }
246
-
247
- /**
248
- * Get all filled doctor forms for a specific appointment.
249
- * @param appointmentId ID of the appointment.
250
- * @param pageSize Number of documents to retrieve.
251
- * @param lastDoc Last document from previous page for pagination.
252
- */
253
- async getFilledDoctorFormsForAppointment(
254
- appointmentId: string,
255
- pageSize = 20,
256
- lastDoc?: QueryDocumentSnapshot<FilledDocument>
257
- ): Promise<{
258
- documents: FilledDocument[];
259
- lastDoc: QueryDocumentSnapshot<FilledDocument> | null;
260
- }> {
261
- const subcollectionRef = collection(
262
- this.db,
263
- APPOINTMENTS_COLLECTION, // Replaced "appointments"
264
- appointmentId,
265
- DOCTOR_FORMS_SUBCOLLECTION
266
- );
267
- let q = query(
268
- subcollectionRef,
269
- orderBy("updatedAt", "desc"),
270
- limit(pageSize)
271
- );
272
-
273
- if (lastDoc) {
274
- q = query(q, startAfter(lastDoc));
275
- }
276
- return this.executeQuery(q);
277
- }
278
-
279
- // Helper to execute query and return documents + lastDoc
280
- private async executeQuery(
281
- q: any // Firestore Query type
282
- ): Promise<{
283
- documents: FilledDocument[];
284
- lastDoc: QueryDocumentSnapshot<FilledDocument> | null;
285
- }> {
286
- const querySnapshot = await getDocs(q);
287
- const documents: FilledDocument[] = [];
288
- let lastVisible: QueryDocumentSnapshot<FilledDocument> | null = null;
289
-
290
- querySnapshot.forEach((doc) => {
291
- documents.push(doc.data() as FilledDocument);
292
- lastVisible = doc as QueryDocumentSnapshot<FilledDocument>;
293
- });
294
-
295
- return {
296
- documents,
297
- lastDoc: lastVisible,
298
- };
299
- }
300
-
301
- // IMPORTANT: The following methods that query across all patients/practitioners/clinics
302
- // (e.g., getFilledDocumentsByPatient, getFilledDocumentsByPractitioner)
303
- // will NOT work correctly if FILLED_DOCUMENTS_COLLECTION is no longer a top-level collection
304
- // and data is only in appointment subcollections. You would need to use
305
- // Firestore Collection Group Queries for that, which require an index and a different query approach.
306
- // These methods are left here for now but would need significant rework or removal.
307
-
308
- /**
309
- * Get filled documents for a patient (NEEDS REWORK for subcollections or Collection Group Query)
310
- * @param patientId - ID of the patient
311
- * @param pageSize - Number of documents to retrieve
312
- * @param lastDoc - Last document from previous page for pagination
313
- * @returns Array of filled documents and the last document for pagination
314
- */
315
- async getFilledDocumentsByPatient(
316
- patientId: string,
317
- pageSize = 20,
318
- lastDoc?: QueryDocumentSnapshot<FilledDocument>
319
- ): Promise<{
320
- documents: FilledDocument[];
321
- lastDoc: QueryDocumentSnapshot<FilledDocument> | null;
322
- }> {
323
- console.warn(
324
- "getFilledDocumentsByPatient needs rework for subcollection model or Collection Group Queries."
325
- );
326
- // Original logic commented out as this.collectionRef is no longer valid for this model
327
- /*
328
- let q = query(
329
- this.collectionRef, // LINTER ERROR: this.collectionRef no longer exists
330
- where("patientId", "==", patientId),
331
- orderBy("updatedAt", "desc"),
332
- limit(pageSize)
333
- );
334
-
335
- if (lastDoc) {
336
- q = query(q, startAfter(lastDoc));
337
- }
338
-
339
- const querySnapshot = await getDocs(q);
340
- const documents: FilledDocument[] = [];
341
- let lastVisible: QueryDocumentSnapshot<FilledDocument> | null = null;
342
-
343
- querySnapshot.forEach((doc) => {
344
- documents.push(doc.data() as FilledDocument);
345
- lastVisible = doc as QueryDocumentSnapshot<FilledDocument>;
346
- });
347
-
348
- return {
349
- documents,
350
- lastDoc: lastVisible,
351
- };
352
- */
353
- return { documents: [], lastDoc: null }; // Placeholder return
354
- }
355
-
356
- /**
357
- * Get filled documents for a practitioner (NEEDS REWORK for subcollections or Collection Group Query)
358
- * @param practitionerId - ID of the practitioner
359
- * @param pageSize - Number of documents to retrieve
360
- * @param lastDoc - Last document from previous page for pagination
361
- * @returns Array of filled documents and the last document for pagination
362
- */
363
- async getFilledDocumentsByPractitioner(
364
- practitionerId: string,
365
- pageSize = 20,
366
- lastDoc?: QueryDocumentSnapshot<FilledDocument>
367
- ): Promise<{
368
- documents: FilledDocument[];
369
- lastDoc: QueryDocumentSnapshot<FilledDocument> | null;
370
- }> {
371
- console.warn(
372
- "getFilledDocumentsByPractitioner needs rework for subcollection model or Collection Group Queries."
373
- );
374
- // Original logic commented out
375
- /*
376
- let q = query(
377
- this.collectionRef, // LINTER ERROR
378
- where("practitionerId", "==", practitionerId),
379
- orderBy("updatedAt", "desc"),
380
- limit(pageSize)
381
- );
382
- // ... rest of original logic ...
383
- */
384
- return { documents: [], lastDoc: null }; // Placeholder return
385
- }
386
-
387
- /**
388
- * Get filled documents for a clinic (NEEDS REWORK for subcollections or Collection Group Query)
389
- * @param clinicId - ID of the clinic
390
- * @param pageSize - Number of documents to retrieve
391
- * @param lastDoc - Last document from previous page for pagination
392
- * @returns Array of filled documents and the last document for pagination
393
- */
394
- async getFilledDocumentsByClinic(
395
- clinicId: string,
396
- pageSize = 20,
397
- lastDoc?: QueryDocumentSnapshot<FilledDocument>
398
- ): Promise<{
399
- documents: FilledDocument[];
400
- lastDoc: QueryDocumentSnapshot<FilledDocument> | null;
401
- }> {
402
- console.warn(
403
- "getFilledDocumentsByClinic needs rework for subcollection model or Collection Group Queries."
404
- );
405
- // Original logic commented out
406
- /*
407
- let q = query(
408
- this.collectionRef, // LINTER ERROR
409
- where("clinicId", "==", clinicId),
410
- orderBy("updatedAt", "desc"),
411
- limit(pageSize)
412
- );
413
- // ... rest of original logic ...
414
- */
415
- return { documents: [], lastDoc: null }; // Placeholder return
416
- }
417
-
418
- /**
419
- * Get filled documents by template (NEEDS REWORK for subcollections or Collection Group Query)
420
- * @param templateId - ID of the template
421
- * @param pageSize - Number of documents to retrieve
422
- * @param lastDoc - Last document from previous page for pagination
423
- * @returns Array of filled documents and the last document for pagination
424
- */
425
- async getFilledDocumentsByTemplate(
426
- templateId: string,
427
- pageSize = 20,
428
- lastDoc?: QueryDocumentSnapshot<FilledDocument>
429
- ): Promise<{
430
- documents: FilledDocument[];
431
- lastDoc: QueryDocumentSnapshot<FilledDocument> | null;
432
- }> {
433
- console.warn(
434
- "getFilledDocumentsByTemplate needs rework for subcollection model or Collection Group Queries."
435
- );
436
- // Original logic commented out
437
- /*
438
- let q = query(
439
- this.collectionRef, // LINTER ERROR
440
- where("templateId", "==", templateId),
441
- orderBy("updatedAt", "desc"),
442
- limit(pageSize)
443
- );
444
- // ... rest of original logic ...
445
- */
446
- return { documents: [], lastDoc: null }; // Placeholder return
447
- }
448
-
449
- /**
450
- * Get filled documents by status (NEEDS REWORK for subcollections or Collection Group Query)
451
- * @param status - Status to filter by
452
- * @param pageSize - Number of documents to retrieve
453
- * @param lastDoc - Last document from previous page for pagination
454
- * @returns Array of filled documents and the last document for pagination
455
- */
456
- async getFilledDocumentsByStatus(
457
- status: FilledDocumentStatus,
458
- pageSize = 20,
459
- lastDoc?: QueryDocumentSnapshot<FilledDocument>
460
- ): Promise<{
461
- documents: FilledDocument[];
462
- lastDoc: QueryDocumentSnapshot<FilledDocument> | null;
463
- }> {
464
- console.warn(
465
- "getFilledDocumentsByStatus needs rework for subcollection model or Collection Group Queries."
466
- );
467
- // Original logic commented out
468
- /*
469
- let q = query(
470
- this.collectionRef, // LINTER ERROR
471
- where("status", "==", status),
472
- orderBy("updatedAt", "desc"),
473
- limit(pageSize)
474
- );
475
- // ... rest of original logic ...
476
- */
477
- return { documents: [], lastDoc: null }; // Placeholder return
478
- }
479
-
480
- /**
481
- * Upload a file for a filled document field without updating the document.
482
- * This method only handles the upload and returns the file value to be used by the UI.
483
- *
484
- * @param appointmentId - ID of the appointment
485
- * @param formId - ID of the filled document
486
- * @param isUserForm - Boolean indicating if it's a user form or doctor form
487
- * @param file - The file to upload
488
- * @returns The file value object to be stored in the document
489
- */
490
- async uploadFileForFilledDocument(
491
- appointmentId: string,
492
- formId: string,
493
- isUserForm: boolean,
494
- file: File | Blob
495
- ): Promise<FilledDocumentFileValue> {
496
- console.log(
497
- `[FilledDocumentService] Uploading file for form ${formId} in appointment ${appointmentId}`
498
- );
499
-
500
- // Generate a unique file ID
501
- const fileId = this.generateId();
502
-
503
- // Set the path according to the specified structure
504
- const formType = isUserForm ? "user-form" : "doctor-form";
505
- const collectionName = `${formType}/${formId}`;
506
-
507
- // Always use CONFIDENTIAL access level
508
- const accessLevel = MediaAccessLevel.CONFIDENTIAL;
509
-
510
- // Upload the file using MediaService
511
- const mediaMetadata = await this.mediaService.uploadMedia(
512
- file,
513
- appointmentId, // Using appointmentId as ownerId
514
- accessLevel,
515
- collectionName,
516
- file instanceof File ? file.name : `file_${fileId}`
517
- );
518
-
519
- // Create and return a file value object
520
- const fileValue: FilledDocumentFileValue = {
521
- mediaId: mediaMetadata.id,
522
- url: mediaMetadata.url,
523
- name: mediaMetadata.name,
524
- contentType: mediaMetadata.contentType,
525
- size: mediaMetadata.size,
526
- uploadedAt: Date.now(),
527
- };
528
-
529
- return fileValue;
530
- }
531
-
532
- /**
533
- * Upload a signature image for a filled document.
534
- * This is a specialized version of uploadFileForFilledDocument specifically for signatures.
535
- *
536
- * @param appointmentId - ID of the appointment
537
- * @param formId - ID of the filled document
538
- * @param isUserForm - Boolean indicating if it's a user form or doctor form
539
- * @param signatureBlob - The signature image as a Blob
540
- * @returns The file value object to be stored in the document
541
- */
542
- async uploadSignatureForFilledDocument(
543
- appointmentId: string,
544
- formId: string,
545
- isUserForm: boolean,
546
- signatureBlob: Blob
547
- ): Promise<FilledDocumentFileValue> {
548
- console.log(
549
- `[FilledDocumentService] Uploading signature for form ${formId}`
550
- );
551
-
552
- // Generate a filename for the signature
553
- const signatureId = this.generateId();
554
- const signatureFile = new File(
555
- [signatureBlob],
556
- `signature_${signatureId}.png`,
557
- { type: "image/png" }
558
- );
559
-
560
- // Use the general file upload method
561
- return this.uploadFileForFilledDocument(
562
- appointmentId,
563
- formId,
564
- isUserForm,
565
- signatureFile
566
- );
567
- }
568
-
569
- /**
570
- * Delete a file using its mediaId.
571
- *
572
- * @param mediaId - ID of the media to delete
573
- * @returns Promise resolving when the deletion is complete
574
- */
575
- async deleteFile(mediaId: string): Promise<void> {
576
- console.log(
577
- `[FilledDocumentService] Deleting file with mediaId ${mediaId}`
578
- );
579
-
580
- if (!mediaId) {
581
- throw new Error("MediaId is required to delete a file");
582
- }
583
-
584
- // Delete the file using MediaService
585
- await this.mediaService.deleteMedia(mediaId);
586
- }
587
- }
1
+ import {
2
+ collection,
3
+ doc,
4
+ getDoc,
5
+ getDocs,
6
+ setDoc,
7
+ updateDoc,
8
+ query,
9
+ where,
10
+ orderBy,
11
+ limit,
12
+ startAfter,
13
+ QueryDocumentSnapshot,
14
+ } from "firebase/firestore";
15
+ import { BaseService } from "../base.service";
16
+ import {
17
+ // DocumentTemplate, // Not directly used in this file if relying on templateService
18
+ // DOCUMENTATION_TEMPLATES_COLLECTION, // No longer needed directly here if using templateService
19
+ // FILLED_DOCUMENTS_COLLECTION, // This will be replaced by subcollection paths
20
+ FilledDocument,
21
+ FilledDocumentStatus,
22
+ FilledDocumentFileValue,
23
+ USER_FORMS_SUBCOLLECTION,
24
+ DOCTOR_FORMS_SUBCOLLECTION,
25
+ } from "../../types"; // General types
26
+ import { APPOINTMENTS_COLLECTION } from "../../types/appointment"; // Specific import for the constant
27
+ import { DocumentationTemplateService } from "./documentation-template.service";
28
+ import {
29
+ MediaService,
30
+ MediaAccessLevel,
31
+ MediaMetadata,
32
+ } from "../media/media.service";
33
+ // Import the new validation schemas if you plan to use them for input validation here
34
+ // import { createFilledDocumentDataSchema, updateFilledDocumentDataSchema } from '../../validations/documentation-templates.schema';
35
+
36
+ /**
37
+ * Service for managing filled documents within appointment subcollections
38
+ */
39
+ export class FilledDocumentService extends BaseService {
40
+ // No single collectionRef anymore, paths will be dynamic
41
+ private readonly templateService: DocumentationTemplateService;
42
+ private readonly mediaService: MediaService;
43
+
44
+ constructor(...args: ConstructorParameters<typeof BaseService>) {
45
+ super(...args);
46
+ this.templateService = new DocumentationTemplateService(...args); // Pass db and other args
47
+ this.mediaService = new MediaService(...args); // Initialize media service with the same args
48
+ }
49
+
50
+ private getFormSubcollectionPath(
51
+ isUserForm: boolean
52
+ ): typeof USER_FORMS_SUBCOLLECTION | typeof DOCTOR_FORMS_SUBCOLLECTION {
53
+ return isUserForm ? USER_FORMS_SUBCOLLECTION : DOCTOR_FORMS_SUBCOLLECTION;
54
+ }
55
+
56
+ /**
57
+ * Create a new filled document within an appointment's subcollection.
58
+ * @param templateId - ID of the template to use.
59
+ * @param templateVersion - Version of the template to use.
60
+ * @param appointmentId - ID of the appointment this form belongs to.
61
+ * @param procedureId - ID of the procedure associated with this form.
62
+ * @param patientId - ID of the patient.
63
+ * @param practitionerId - ID of the practitioner (can be system/generic if patient is filling).
64
+ * @param clinicId - ID of the clinic.
65
+ * @param initialValues - Optional initial values for the form elements.
66
+ * @param initialStatus - Optional initial status for the form.
67
+ * @returns The created filled document.
68
+ */
69
+ async createFilledDocumentForAppointment(
70
+ templateId: string,
71
+ templateVersion: number,
72
+ appointmentId: string,
73
+ procedureId: string,
74
+ patientId: string,
75
+ practitionerId: string, // Consider if this is always available or if a placeholder is needed
76
+ clinicId: string, // Same consideration as practitionerId
77
+ initialValues: { [elementId: string]: any } = {},
78
+ initialStatus: FilledDocumentStatus = FilledDocumentStatus.DRAFT
79
+ ): Promise<FilledDocument> {
80
+ const template = await this.templateService.getTemplateById(
81
+ templateId,
82
+ templateVersion
83
+ );
84
+ if (!template) {
85
+ throw new Error(`Template with ID ${templateId} not found`);
86
+ }
87
+
88
+ const documentId = this.generateId();
89
+ const now = Date.now();
90
+ const isUserForm = template.isUserForm || false;
91
+ const formSubcollection = this.getFormSubcollectionPath(isUserForm);
92
+
93
+ const filledDocument: FilledDocument = {
94
+ id: documentId,
95
+ templateId,
96
+ templateVersion: template.version,
97
+ isUserForm: isUserForm, // Set based on template
98
+ isRequired: template.isRequired || false, // Inherit isRequired from the template
99
+ appointmentId: appointmentId, // NEW
100
+ procedureId: procedureId, // NEW
101
+ patientId,
102
+ practitionerId,
103
+ clinicId,
104
+ createdAt: now,
105
+ updatedAt: now,
106
+ values: initialValues,
107
+ status: initialStatus,
108
+ };
109
+
110
+ const docRef = doc(
111
+ this.db,
112
+ APPOINTMENTS_COLLECTION, // Replaced "appointments"
113
+ appointmentId,
114
+ formSubcollection,
115
+ documentId
116
+ );
117
+ await setDoc(docRef, filledDocument);
118
+
119
+ return filledDocument;
120
+ }
121
+
122
+ /**
123
+ * Get a specific filled document from an appointment's subcollection.
124
+ * @param appointmentId - ID of the appointment.
125
+ * @param formId - ID of the filled document.
126
+ * @param isUserForm - Boolean indicating if it's a user form or doctor form.
127
+ * @returns The filled document or null if not found.
128
+ */
129
+ async getFilledDocumentFromAppointmentById(
130
+ appointmentId: string,
131
+ formId: string,
132
+ isUserForm: boolean
133
+ ): Promise<FilledDocument | null> {
134
+ const formSubcollection = this.getFormSubcollectionPath(isUserForm);
135
+ const docRef = doc(
136
+ this.db,
137
+ APPOINTMENTS_COLLECTION, // Replaced "appointments"
138
+ appointmentId,
139
+ formSubcollection,
140
+ formId
141
+ );
142
+ const docSnap = await getDoc(docRef);
143
+
144
+ if (!docSnap.exists()) {
145
+ return null;
146
+ }
147
+ return docSnap.data() as FilledDocument;
148
+ }
149
+
150
+ /**
151
+ * Update values or status in a filled document within an appointment's subcollection.
152
+ * @param appointmentId - ID of the appointment.
153
+ * @param formId - ID of the filled document to update.
154
+ * @param isUserForm - Boolean indicating if it's a user form or doctor form.
155
+ * @param values - Updated values for elements.
156
+ * @param status - Optional new status for the document.
157
+ * @returns The updated filled document.
158
+ */
159
+ async updateFilledDocumentInAppointment(
160
+ appointmentId: string,
161
+ formId: string,
162
+ isUserForm: boolean,
163
+ values?: { [elementId: string]: any },
164
+ status?: FilledDocumentStatus
165
+ ): Promise<FilledDocument> {
166
+ const formSubcollection = this.getFormSubcollectionPath(isUserForm);
167
+ const docRef = doc(
168
+ this.db,
169
+ APPOINTMENTS_COLLECTION, // Replaced "appointments"
170
+ appointmentId,
171
+ formSubcollection,
172
+ formId
173
+ );
174
+
175
+ const existingDoc = await this.getFilledDocumentFromAppointmentById(
176
+ appointmentId,
177
+ formId,
178
+ isUserForm
179
+ );
180
+
181
+ if (!existingDoc) {
182
+ throw new Error(
183
+ `Filled document with ID ${formId} not found in appointment ${appointmentId} ${formSubcollection}`
184
+ );
185
+ }
186
+
187
+ const updatePayload: Partial<FilledDocument> = {
188
+ updatedAt: Date.now(),
189
+ };
190
+
191
+ if (values) {
192
+ updatePayload.values = {
193
+ ...existingDoc.values,
194
+ ...values,
195
+ };
196
+ }
197
+ if (status) {
198
+ updatePayload.status = status;
199
+ }
200
+
201
+ if (
202
+ Object.keys(updatePayload).length === 1 &&
203
+ "updatedAt" in updatePayload
204
+ ) {
205
+ // Only updatedAt, no actual data change, so we can skip the update
206
+ // or just update timestamp if that is the desired behavior.
207
+ // For now, we proceed to update the timestamp at least.
208
+ }
209
+
210
+ await updateDoc(docRef, updatePayload);
211
+
212
+ return { ...existingDoc, ...updatePayload } as FilledDocument;
213
+ }
214
+
215
+ /**
216
+ * Get all filled user forms for a specific appointment.
217
+ * @param appointmentId ID of the appointment.
218
+ * @param pageSize Number of documents to retrieve.
219
+ * @param lastDoc Last document from previous page for pagination.
220
+ */
221
+ async getFilledUserFormsForAppointment(
222
+ appointmentId: string,
223
+ pageSize = 20,
224
+ lastDoc?: QueryDocumentSnapshot<FilledDocument>
225
+ ): Promise<{
226
+ documents: FilledDocument[];
227
+ lastDoc: QueryDocumentSnapshot<FilledDocument> | null;
228
+ }> {
229
+ const subcollectionRef = collection(
230
+ this.db,
231
+ APPOINTMENTS_COLLECTION, // Replaced "appointments"
232
+ appointmentId,
233
+ USER_FORMS_SUBCOLLECTION
234
+ );
235
+ let q = query(
236
+ subcollectionRef,
237
+ orderBy("updatedAt", "desc"),
238
+ limit(pageSize)
239
+ );
240
+
241
+ if (lastDoc) {
242
+ q = query(q, startAfter(lastDoc));
243
+ }
244
+ return this.executeQuery(q);
245
+ }
246
+
247
+ /**
248
+ * Get all filled doctor forms for a specific appointment.
249
+ * @param appointmentId ID of the appointment.
250
+ * @param pageSize Number of documents to retrieve.
251
+ * @param lastDoc Last document from previous page for pagination.
252
+ */
253
+ async getFilledDoctorFormsForAppointment(
254
+ appointmentId: string,
255
+ pageSize = 20,
256
+ lastDoc?: QueryDocumentSnapshot<FilledDocument>
257
+ ): Promise<{
258
+ documents: FilledDocument[];
259
+ lastDoc: QueryDocumentSnapshot<FilledDocument> | null;
260
+ }> {
261
+ const subcollectionRef = collection(
262
+ this.db,
263
+ APPOINTMENTS_COLLECTION, // Replaced "appointments"
264
+ appointmentId,
265
+ DOCTOR_FORMS_SUBCOLLECTION
266
+ );
267
+ let q = query(
268
+ subcollectionRef,
269
+ orderBy("updatedAt", "desc"),
270
+ limit(pageSize)
271
+ );
272
+
273
+ if (lastDoc) {
274
+ q = query(q, startAfter(lastDoc));
275
+ }
276
+ return this.executeQuery(q);
277
+ }
278
+
279
+ // Helper to execute query and return documents + lastDoc
280
+ private async executeQuery(
281
+ q: any // Firestore Query type
282
+ ): Promise<{
283
+ documents: FilledDocument[];
284
+ lastDoc: QueryDocumentSnapshot<FilledDocument> | null;
285
+ }> {
286
+ const querySnapshot = await getDocs(q);
287
+ const documents: FilledDocument[] = [];
288
+ let lastVisible: QueryDocumentSnapshot<FilledDocument> | null = null;
289
+
290
+ querySnapshot.forEach((doc) => {
291
+ documents.push(doc.data() as FilledDocument);
292
+ lastVisible = doc as QueryDocumentSnapshot<FilledDocument>;
293
+ });
294
+
295
+ return {
296
+ documents,
297
+ lastDoc: lastVisible,
298
+ };
299
+ }
300
+
301
+ // IMPORTANT: The following methods that query across all patients/practitioners/clinics
302
+ // (e.g., getFilledDocumentsByPatient, getFilledDocumentsByPractitioner)
303
+ // will NOT work correctly if FILLED_DOCUMENTS_COLLECTION is no longer a top-level collection
304
+ // and data is only in appointment subcollections. You would need to use
305
+ // Firestore Collection Group Queries for that, which require an index and a different query approach.
306
+ // These methods are left here for now but would need significant rework or removal.
307
+
308
+ /**
309
+ * Get filled documents for a patient (NEEDS REWORK for subcollections or Collection Group Query)
310
+ * @param patientId - ID of the patient
311
+ * @param pageSize - Number of documents to retrieve
312
+ * @param lastDoc - Last document from previous page for pagination
313
+ * @returns Array of filled documents and the last document for pagination
314
+ */
315
+ async getFilledDocumentsByPatient(
316
+ patientId: string,
317
+ pageSize = 20,
318
+ lastDoc?: QueryDocumentSnapshot<FilledDocument>
319
+ ): Promise<{
320
+ documents: FilledDocument[];
321
+ lastDoc: QueryDocumentSnapshot<FilledDocument> | null;
322
+ }> {
323
+ console.warn(
324
+ "getFilledDocumentsByPatient needs rework for subcollection model or Collection Group Queries."
325
+ );
326
+ // Original logic commented out as this.collectionRef is no longer valid for this model
327
+ /*
328
+ let q = query(
329
+ this.collectionRef, // LINTER ERROR: this.collectionRef no longer exists
330
+ where("patientId", "==", patientId),
331
+ orderBy("updatedAt", "desc"),
332
+ limit(pageSize)
333
+ );
334
+
335
+ if (lastDoc) {
336
+ q = query(q, startAfter(lastDoc));
337
+ }
338
+
339
+ const querySnapshot = await getDocs(q);
340
+ const documents: FilledDocument[] = [];
341
+ let lastVisible: QueryDocumentSnapshot<FilledDocument> | null = null;
342
+
343
+ querySnapshot.forEach((doc) => {
344
+ documents.push(doc.data() as FilledDocument);
345
+ lastVisible = doc as QueryDocumentSnapshot<FilledDocument>;
346
+ });
347
+
348
+ return {
349
+ documents,
350
+ lastDoc: lastVisible,
351
+ };
352
+ */
353
+ return { documents: [], lastDoc: null }; // Placeholder return
354
+ }
355
+
356
+ /**
357
+ * Get filled documents for a practitioner (NEEDS REWORK for subcollections or Collection Group Query)
358
+ * @param practitionerId - ID of the practitioner
359
+ * @param pageSize - Number of documents to retrieve
360
+ * @param lastDoc - Last document from previous page for pagination
361
+ * @returns Array of filled documents and the last document for pagination
362
+ */
363
+ async getFilledDocumentsByPractitioner(
364
+ practitionerId: string,
365
+ pageSize = 20,
366
+ lastDoc?: QueryDocumentSnapshot<FilledDocument>
367
+ ): Promise<{
368
+ documents: FilledDocument[];
369
+ lastDoc: QueryDocumentSnapshot<FilledDocument> | null;
370
+ }> {
371
+ console.warn(
372
+ "getFilledDocumentsByPractitioner needs rework for subcollection model or Collection Group Queries."
373
+ );
374
+ // Original logic commented out
375
+ /*
376
+ let q = query(
377
+ this.collectionRef, // LINTER ERROR
378
+ where("practitionerId", "==", practitionerId),
379
+ orderBy("updatedAt", "desc"),
380
+ limit(pageSize)
381
+ );
382
+ // ... rest of original logic ...
383
+ */
384
+ return { documents: [], lastDoc: null }; // Placeholder return
385
+ }
386
+
387
+ /**
388
+ * Get filled documents for a clinic (NEEDS REWORK for subcollections or Collection Group Query)
389
+ * @param clinicId - ID of the clinic
390
+ * @param pageSize - Number of documents to retrieve
391
+ * @param lastDoc - Last document from previous page for pagination
392
+ * @returns Array of filled documents and the last document for pagination
393
+ */
394
+ async getFilledDocumentsByClinic(
395
+ clinicId: string,
396
+ pageSize = 20,
397
+ lastDoc?: QueryDocumentSnapshot<FilledDocument>
398
+ ): Promise<{
399
+ documents: FilledDocument[];
400
+ lastDoc: QueryDocumentSnapshot<FilledDocument> | null;
401
+ }> {
402
+ console.warn(
403
+ "getFilledDocumentsByClinic needs rework for subcollection model or Collection Group Queries."
404
+ );
405
+ // Original logic commented out
406
+ /*
407
+ let q = query(
408
+ this.collectionRef, // LINTER ERROR
409
+ where("clinicId", "==", clinicId),
410
+ orderBy("updatedAt", "desc"),
411
+ limit(pageSize)
412
+ );
413
+ // ... rest of original logic ...
414
+ */
415
+ return { documents: [], lastDoc: null }; // Placeholder return
416
+ }
417
+
418
+ /**
419
+ * Get filled documents by template (NEEDS REWORK for subcollections or Collection Group Query)
420
+ * @param templateId - ID of the template
421
+ * @param pageSize - Number of documents to retrieve
422
+ * @param lastDoc - Last document from previous page for pagination
423
+ * @returns Array of filled documents and the last document for pagination
424
+ */
425
+ async getFilledDocumentsByTemplate(
426
+ templateId: string,
427
+ pageSize = 20,
428
+ lastDoc?: QueryDocumentSnapshot<FilledDocument>
429
+ ): Promise<{
430
+ documents: FilledDocument[];
431
+ lastDoc: QueryDocumentSnapshot<FilledDocument> | null;
432
+ }> {
433
+ console.warn(
434
+ "getFilledDocumentsByTemplate needs rework for subcollection model or Collection Group Queries."
435
+ );
436
+ // Original logic commented out
437
+ /*
438
+ let q = query(
439
+ this.collectionRef, // LINTER ERROR
440
+ where("templateId", "==", templateId),
441
+ orderBy("updatedAt", "desc"),
442
+ limit(pageSize)
443
+ );
444
+ // ... rest of original logic ...
445
+ */
446
+ return { documents: [], lastDoc: null }; // Placeholder return
447
+ }
448
+
449
+ /**
450
+ * Get filled documents by status (NEEDS REWORK for subcollections or Collection Group Query)
451
+ * @param status - Status to filter by
452
+ * @param pageSize - Number of documents to retrieve
453
+ * @param lastDoc - Last document from previous page for pagination
454
+ * @returns Array of filled documents and the last document for pagination
455
+ */
456
+ async getFilledDocumentsByStatus(
457
+ status: FilledDocumentStatus,
458
+ pageSize = 20,
459
+ lastDoc?: QueryDocumentSnapshot<FilledDocument>
460
+ ): Promise<{
461
+ documents: FilledDocument[];
462
+ lastDoc: QueryDocumentSnapshot<FilledDocument> | null;
463
+ }> {
464
+ console.warn(
465
+ "getFilledDocumentsByStatus needs rework for subcollection model or Collection Group Queries."
466
+ );
467
+ // Original logic commented out
468
+ /*
469
+ let q = query(
470
+ this.collectionRef, // LINTER ERROR
471
+ where("status", "==", status),
472
+ orderBy("updatedAt", "desc"),
473
+ limit(pageSize)
474
+ );
475
+ // ... rest of original logic ...
476
+ */
477
+ return { documents: [], lastDoc: null }; // Placeholder return
478
+ }
479
+
480
+ /**
481
+ * Upload a file for a filled document field without updating the document.
482
+ * This method only handles the upload and returns the file value to be used by the UI.
483
+ *
484
+ * @param appointmentId - ID of the appointment
485
+ * @param formId - ID of the filled document
486
+ * @param isUserForm - Boolean indicating if it's a user form or doctor form
487
+ * @param file - The file to upload
488
+ * @returns The file value object to be stored in the document
489
+ */
490
+ async uploadFileForFilledDocument(
491
+ appointmentId: string,
492
+ formId: string,
493
+ isUserForm: boolean,
494
+ file: File | Blob
495
+ ): Promise<FilledDocumentFileValue> {
496
+ console.log(
497
+ `[FilledDocumentService] Uploading file for form ${formId} in appointment ${appointmentId}`
498
+ );
499
+
500
+ // Generate a unique file ID
501
+ const fileId = this.generateId();
502
+
503
+ // Set the path according to the specified structure
504
+ const formType = isUserForm ? "user-form" : "doctor-form";
505
+ const collectionName = `${formType}/${formId}`;
506
+
507
+ // Always use CONFIDENTIAL access level
508
+ const accessLevel = MediaAccessLevel.CONFIDENTIAL;
509
+
510
+ // Upload the file using MediaService
511
+ const mediaMetadata = await this.mediaService.uploadMedia(
512
+ file,
513
+ appointmentId, // Using appointmentId as ownerId
514
+ accessLevel,
515
+ collectionName,
516
+ file instanceof File ? file.name : `file_${fileId}`
517
+ );
518
+
519
+ // Create and return a file value object
520
+ const fileValue: FilledDocumentFileValue = {
521
+ mediaId: mediaMetadata.id,
522
+ url: mediaMetadata.url,
523
+ name: mediaMetadata.name,
524
+ contentType: mediaMetadata.contentType,
525
+ size: mediaMetadata.size,
526
+ uploadedAt: Date.now(),
527
+ };
528
+
529
+ return fileValue;
530
+ }
531
+
532
+ /**
533
+ * Upload a signature image for a filled document.
534
+ * This is a specialized version of uploadFileForFilledDocument specifically for signatures.
535
+ *
536
+ * @param appointmentId - ID of the appointment
537
+ * @param formId - ID of the filled document
538
+ * @param isUserForm - Boolean indicating if it's a user form or doctor form
539
+ * @param signatureBlob - The signature image as a Blob
540
+ * @returns The file value object to be stored in the document
541
+ */
542
+ async uploadSignatureForFilledDocument(
543
+ appointmentId: string,
544
+ formId: string,
545
+ isUserForm: boolean,
546
+ signatureBlob: Blob
547
+ ): Promise<FilledDocumentFileValue> {
548
+ console.log(
549
+ `[FilledDocumentService] Uploading signature for form ${formId}`
550
+ );
551
+
552
+ // Generate a filename for the signature
553
+ const signatureId = this.generateId();
554
+ const signatureFile = new File(
555
+ [signatureBlob],
556
+ `signature_${signatureId}.png`,
557
+ { type: "image/png" }
558
+ );
559
+
560
+ // Use the general file upload method
561
+ return this.uploadFileForFilledDocument(
562
+ appointmentId,
563
+ formId,
564
+ isUserForm,
565
+ signatureFile
566
+ );
567
+ }
568
+
569
+ /**
570
+ * Delete a file using its mediaId.
571
+ *
572
+ * @param mediaId - ID of the media to delete
573
+ * @returns Promise resolving when the deletion is complete
574
+ */
575
+ async deleteFile(mediaId: string): Promise<void> {
576
+ console.log(
577
+ `[FilledDocumentService] Deleting file with mediaId ${mediaId}`
578
+ );
579
+
580
+ if (!mediaId) {
581
+ throw new Error("MediaId is required to delete a file");
582
+ }
583
+
584
+ // Delete the file using MediaService
585
+ await this.mediaService.deleteMedia(mediaId);
586
+ }
587
+ }