@blackcode_sa/metaestetics-api 1.14.49 → 1.14.51

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@blackcode_sa/metaestetics-api",
3
3
  "private": false,
4
- "version": "1.14.49",
4
+ "version": "1.14.51",
5
5
  "description": "Firebase authentication service with anonymous upgrade support",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",
@@ -1,8 +1,9 @@
1
- import { Firestore, collection, doc, addDoc, deleteDoc, getDocs, query, where, serverTimestamp, getDoc } from 'firebase/firestore';
1
+ import { Firestore, collection, doc, addDoc, deleteDoc, getDocs, query, where, serverTimestamp, getDoc, updateDoc } from 'firebase/firestore';
2
2
  import {
3
3
  DocumentTemplate,
4
4
  FilledDocumentStatus,
5
5
  DOCTOR_FORMS_SUBCOLLECTION,
6
+ USER_FORMS_SUBCOLLECTION,
6
7
  DOCUMENTATION_TEMPLATES_COLLECTION,
7
8
  } from '../../../types/documentation-templates';
8
9
  import {
@@ -77,7 +78,7 @@ export async function initializeFormsForExtendedProcedure(
77
78
  }
78
79
  }
79
80
 
80
- // Initialize forms for each template (only doctor forms for extended procedures)
81
+ // Initialize forms for each template (both user forms and doctor forms for extended procedures)
81
82
  for (const templateRef of technologyTemplates) {
82
83
  const template = templatesMap.get(templateRef.templateId);
83
84
  if (!template) {
@@ -87,16 +88,11 @@ export async function initializeFormsForExtendedProcedure(
87
88
  continue;
88
89
  }
89
90
 
90
- // Skip user forms - only create doctor forms for extended procedures
91
- if (templateRef.isUserForm) {
92
- console.log(
93
- `[FormInit] Skipping user form ${templateRef.templateId} for extended procedure ${procedureId}.`
94
- );
95
- continue;
96
- }
97
-
98
91
  const isRequired = templateRef.isRequired;
99
- const formSubcollectionPath = DOCTOR_FORMS_SUBCOLLECTION;
92
+ const isUserForm = templateRef.isUserForm || false;
93
+ const formSubcollectionPath = isUserForm
94
+ ? USER_FORMS_SUBCOLLECTION
95
+ : DOCTOR_FORMS_SUBCOLLECTION;
100
96
 
101
97
  // Create form document in subcollection
102
98
  const appointmentRef = doc(db, APPOINTMENTS_COLLECTION, appointmentId);
@@ -105,7 +101,7 @@ export async function initializeFormsForExtendedProcedure(
105
101
  const filledDocumentData = {
106
102
  templateId: templateRef.templateId,
107
103
  templateVersion: template.version,
108
- isUserForm: false, // Always false for extended procedures
104
+ isUserForm: isUserForm,
109
105
  isRequired: isRequired,
110
106
  appointmentId: appointmentId,
111
107
  procedureId: procedureId,
@@ -122,7 +118,13 @@ export async function initializeFormsForExtendedProcedure(
122
118
  const docRef = await addDoc(formsCollectionRef, filledDocumentData);
123
119
  const filledDocumentId = docRef.id;
124
120
 
125
- // No user forms for extended procedures, so no pending user forms
121
+ // Update the document to include the id field in the data (for consistency)
122
+ await updateDoc(docRef, { id: filledDocumentId });
123
+
124
+ // Add to pendingUserFormsIds if it's a required user form
125
+ if (isUserForm && isRequired) {
126
+ pendingUserFormsIds.push(filledDocumentId);
127
+ }
126
128
 
127
129
  allLinkedFormIds.push(filledDocumentId);
128
130
 
@@ -131,7 +133,7 @@ export async function initializeFormsForExtendedProcedure(
131
133
  templateId: template.id,
132
134
  templateVersion: template.version,
133
135
  title: template.title,
134
- isUserForm: false, // Always false for extended procedures
136
+ isUserForm: isUserForm,
135
137
  isRequired: isRequired,
136
138
  sortingOrder: templateRef.sortingOrder,
137
139
  status: FilledDocumentStatus.PENDING,
@@ -140,7 +142,7 @@ export async function initializeFormsForExtendedProcedure(
140
142
  initializedFormsInfo.push(linkedForm);
141
143
 
142
144
  console.log(
143
- `[FormInit] Created FilledDocument ${filledDocumentId} (template: ${template.id}) for extended procedure ${procedureId} in appointment ${appointmentId}.`
145
+ `[FormInit] Created FilledDocument ${filledDocumentId} (template: ${template.id}, isUserForm: ${isUserForm}) for extended procedure ${procedureId} in appointment ${appointmentId}.`
144
146
  );
145
147
  } catch (error) {
146
148
  console.error(
@@ -155,6 +157,7 @@ export async function initializeFormsForExtendedProcedure(
155
157
 
156
158
  /**
157
159
  * Removes all forms associated with a specific procedure from an appointment
160
+ * Removes both user forms and doctor forms
158
161
  * @param db Firestore instance
159
162
  * @param appointmentId Appointment ID
160
163
  * @param procedureId Procedure ID to remove forms for
@@ -167,11 +170,10 @@ export async function removeFormsForExtendedProcedure(
167
170
  ): Promise<string[]> {
168
171
  const removedFormIds: string[] = [];
169
172
 
170
- // Only remove from doctor forms subcollection (no user forms for extended procedures)
171
173
  const appointmentRef = doc(db, APPOINTMENTS_COLLECTION, appointmentId);
172
- const doctorFormsRef = collection(appointmentRef, DOCTOR_FORMS_SUBCOLLECTION);
173
174
 
174
- // Query doctor forms for this procedure
175
+ // Remove from doctor forms subcollection
176
+ const doctorFormsRef = collection(appointmentRef, DOCTOR_FORMS_SUBCOLLECTION);
175
177
  const doctorFormsQuery = query(
176
178
  doctorFormsRef,
177
179
  where('procedureId', '==', procedureId)
@@ -193,6 +195,29 @@ export async function removeFormsForExtendedProcedure(
193
195
  }
194
196
  }
195
197
 
198
+ // Remove from user forms subcollection
199
+ const userFormsRef = collection(appointmentRef, USER_FORMS_SUBCOLLECTION);
200
+ const userFormsQuery = query(
201
+ userFormsRef,
202
+ where('procedureId', '==', procedureId)
203
+ );
204
+ const userFormsSnap = await getDocs(userFormsQuery);
205
+
206
+ for (const formDoc of userFormsSnap.docs) {
207
+ try {
208
+ await deleteDoc(formDoc.ref);
209
+ removedFormIds.push(formDoc.id);
210
+ console.log(
211
+ `[FormInit] Removed user form ${formDoc.id} for extended procedure ${procedureId} from appointment ${appointmentId}.`
212
+ );
213
+ } catch (error) {
214
+ console.error(
215
+ `[FormInit] Error removing user form ${formDoc.id}:`,
216
+ error
217
+ );
218
+ }
219
+ }
220
+
196
221
  return removedFormIds;
197
222
  }
198
223
 
@@ -211,9 +236,9 @@ export async function getFormsForExtendedProcedure(
211
236
  const formIds: string[] = [];
212
237
 
213
238
  const appointmentRef = doc(db, APPOINTMENTS_COLLECTION, appointmentId);
214
- const doctorFormsRef = collection(appointmentRef, DOCTOR_FORMS_SUBCOLLECTION);
215
239
 
216
240
  // Query doctor forms for this procedure
241
+ const doctorFormsRef = collection(appointmentRef, DOCTOR_FORMS_SUBCOLLECTION);
217
242
  const doctorFormsQuery = query(
218
243
  doctorFormsRef,
219
244
  where('procedureId', '==', procedureId)
@@ -221,5 +246,14 @@ export async function getFormsForExtendedProcedure(
221
246
  const doctorFormsSnap = await getDocs(doctorFormsQuery);
222
247
  doctorFormsSnap.docs.forEach(doc => formIds.push(doc.id));
223
248
 
249
+ // Query user forms for this procedure
250
+ const userFormsRef = collection(appointmentRef, USER_FORMS_SUBCOLLECTION);
251
+ const userFormsQuery = query(
252
+ userFormsRef,
253
+ where('procedureId', '==', procedureId)
254
+ );
255
+ const userFormsSnap = await getDocs(userFormsQuery);
256
+ userFormsSnap.docs.forEach(doc => formIds.push(doc.id));
257
+
224
258
  return formIds;
225
259
  }
@@ -144,7 +144,12 @@ export class FilledDocumentService extends BaseService {
144
144
  if (!docSnap.exists()) {
145
145
  return null;
146
146
  }
147
- return docSnap.data() as FilledDocument;
147
+ const data = docSnap.data() as FilledDocument;
148
+ // Ensure id is populated from Firestore document ID if not in data
149
+ if (!data.id) {
150
+ data.id = docSnap.id;
151
+ }
152
+ return data;
148
153
  }
149
154
 
150
155
  /**
@@ -287,9 +292,14 @@ export class FilledDocumentService extends BaseService {
287
292
  const documents: FilledDocument[] = [];
288
293
  let lastVisible: QueryDocumentSnapshot<FilledDocument> | null = null;
289
294
 
290
- querySnapshot.forEach((doc) => {
291
- documents.push(doc.data() as FilledDocument);
292
- lastVisible = doc as QueryDocumentSnapshot<FilledDocument>;
295
+ querySnapshot.forEach((docSnapshot) => {
296
+ const data = docSnapshot.data() as FilledDocument;
297
+ // Ensure id is populated from Firestore document ID if not in data
298
+ if (!data.id) {
299
+ data.id = docSnapshot.id;
300
+ }
301
+ documents.push(data);
302
+ lastVisible = docSnapshot as QueryDocumentSnapshot<FilledDocument>;
293
303
  });
294
304
 
295
305
  return {