@blackcode_sa/metaestetics-api 1.14.50 → 1.14.52

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/dist/index.js CHANGED
@@ -4957,6 +4957,45 @@ var import_firestore10 = require("firebase/firestore");
4957
4957
 
4958
4958
  // src/services/appointment/utils/form-initialization.utils.ts
4959
4959
  var import_firestore9 = require("firebase/firestore");
4960
+ function isProcedureSpecificForm(template) {
4961
+ const tags = template.tags || [];
4962
+ const titleLower = template.title.toLowerCase();
4963
+ if (tags.includes("procedure-specific")) {
4964
+ return true;
4965
+ }
4966
+ if (tags.includes("shared")) {
4967
+ return false;
4968
+ }
4969
+ if (tags.some((tag) => {
4970
+ const tagLower = tag.toLowerCase();
4971
+ return tagLower.includes("consent") || tagLower === "consent-form";
4972
+ })) {
4973
+ return true;
4974
+ }
4975
+ if (titleLower.includes("consent")) {
4976
+ return true;
4977
+ }
4978
+ return false;
4979
+ }
4980
+ async function findExistingFormByTemplate(db, appointmentId, templateId, isUserForm) {
4981
+ const formSubcollection = isUserForm ? USER_FORMS_SUBCOLLECTION : DOCTOR_FORMS_SUBCOLLECTION;
4982
+ const appointmentRef = (0, import_firestore9.doc)(db, APPOINTMENTS_COLLECTION, appointmentId);
4983
+ const formsCollectionRef = (0, import_firestore9.collection)(appointmentRef, formSubcollection);
4984
+ const q = (0, import_firestore9.query)(
4985
+ formsCollectionRef,
4986
+ (0, import_firestore9.where)("templateId", "==", templateId)
4987
+ );
4988
+ const querySnapshot = await (0, import_firestore9.getDocs)(q);
4989
+ if (querySnapshot.empty) {
4990
+ return null;
4991
+ }
4992
+ const docSnap = querySnapshot.docs[0];
4993
+ const data = docSnap.data();
4994
+ if (!data.id) {
4995
+ data.id = docSnap.id;
4996
+ }
4997
+ return data;
4998
+ }
4960
4999
  async function initializeFormsForExtendedProcedure(db, appointmentId, procedureId, technologyTemplates, patientId, practitionerId, clinicId) {
4961
5000
  const initializedFormsInfo = [];
4962
5001
  const pendingUserFormsIds = [];
@@ -4999,6 +5038,49 @@ async function initializeFormsForExtendedProcedure(db, appointmentId, procedureI
4999
5038
  const isRequired = templateRef.isRequired;
5000
5039
  const isUserForm = templateRef.isUserForm || false;
5001
5040
  const formSubcollectionPath = isUserForm ? USER_FORMS_SUBCOLLECTION : DOCTOR_FORMS_SUBCOLLECTION;
5041
+ const isProcedureSpecific = isProcedureSpecificForm(template);
5042
+ let existingForm = null;
5043
+ if (!isProcedureSpecific) {
5044
+ try {
5045
+ existingForm = await findExistingFormByTemplate(
5046
+ db,
5047
+ appointmentId,
5048
+ templateRef.templateId,
5049
+ isUserForm
5050
+ );
5051
+ if (existingForm) {
5052
+ console.log(
5053
+ `[FormInit] Found existing shared form ${existingForm.id} (template: ${template.id}) for appointment ${appointmentId}. Reusing instead of creating duplicate.`
5054
+ );
5055
+ const linkedForm = {
5056
+ formId: existingForm.id,
5057
+ templateId: template.id,
5058
+ templateVersion: template.version,
5059
+ title: template.title,
5060
+ isUserForm,
5061
+ isRequired,
5062
+ sortingOrder: templateRef.sortingOrder,
5063
+ status: existingForm.status || "pending" /* PENDING */,
5064
+ path: `${APPOINTMENTS_COLLECTION}/${appointmentId}/${formSubcollectionPath}/${existingForm.id}`
5065
+ };
5066
+ initializedFormsInfo.push(linkedForm);
5067
+ if (!allLinkedFormIds.includes(existingForm.id)) {
5068
+ allLinkedFormIds.push(existingForm.id);
5069
+ }
5070
+ if (isUserForm && isRequired && existingForm.status === "pending" /* PENDING */) {
5071
+ if (!pendingUserFormsIds.includes(existingForm.id)) {
5072
+ pendingUserFormsIds.push(existingForm.id);
5073
+ }
5074
+ }
5075
+ continue;
5076
+ }
5077
+ } catch (error) {
5078
+ console.warn(
5079
+ `[FormInit] Error checking for existing form (template: ${templateRef.templateId}):`,
5080
+ error
5081
+ );
5082
+ }
5083
+ }
5002
5084
  const appointmentRef = (0, import_firestore9.doc)(db, APPOINTMENTS_COLLECTION, appointmentId);
5003
5085
  const formsCollectionRef = (0, import_firestore9.collection)(appointmentRef, formSubcollectionPath);
5004
5086
  const filledDocumentData = {
@@ -5019,6 +5101,7 @@ async function initializeFormsForExtendedProcedure(db, appointmentId, procedureI
5019
5101
  try {
5020
5102
  const docRef = await (0, import_firestore9.addDoc)(formsCollectionRef, filledDocumentData);
5021
5103
  const filledDocumentId = docRef.id;
5104
+ await (0, import_firestore9.updateDoc)(docRef, { id: filledDocumentId });
5022
5105
  if (isUserForm && isRequired) {
5023
5106
  pendingUserFormsIds.push(filledDocumentId);
5024
5107
  }
@@ -5035,8 +5118,9 @@ async function initializeFormsForExtendedProcedure(db, appointmentId, procedureI
5035
5118
  path: docRef.path
5036
5119
  };
5037
5120
  initializedFormsInfo.push(linkedForm);
5121
+ const formType = isProcedureSpecific ? "procedure-specific" : "general/shared";
5038
5122
  console.log(
5039
- `[FormInit] Created FilledDocument ${filledDocumentId} (template: ${template.id}, isUserForm: ${isUserForm}) for extended procedure ${procedureId} in appointment ${appointmentId}.`
5123
+ `[FormInit] Created ${formType} FilledDocument ${filledDocumentId} (template: ${template.id}, isUserForm: ${isUserForm}) for extended procedure ${procedureId} in appointment ${appointmentId}.`
5040
5124
  );
5041
5125
  } catch (error) {
5042
5126
  console.error(
@@ -5048,8 +5132,28 @@ async function initializeFormsForExtendedProcedure(db, appointmentId, procedureI
5048
5132
  return { initializedFormsInfo, pendingUserFormsIds, allLinkedFormIds };
5049
5133
  }
5050
5134
  async function removeFormsForExtendedProcedure(db, appointmentId, procedureId) {
5135
+ var _a, _b;
5051
5136
  const removedFormIds = [];
5052
5137
  const appointmentRef = (0, import_firestore9.doc)(db, APPOINTMENTS_COLLECTION, appointmentId);
5138
+ const appointmentSnap = await (0, import_firestore9.getDoc)(appointmentRef);
5139
+ if (!appointmentSnap.exists()) {
5140
+ console.warn(
5141
+ `[FormInit] Appointment ${appointmentId} not found when removing forms for procedure ${procedureId}.`
5142
+ );
5143
+ return removedFormIds;
5144
+ }
5145
+ const appointment = appointmentSnap.data();
5146
+ const linkedForms = appointment.linkedForms || [];
5147
+ const mainProcedureId = appointment.procedureId;
5148
+ const extendedProcedureIds = ((_b = (_a = appointment.metadata) == null ? void 0 : _a.extendedProcedures) == null ? void 0 : _b.map(
5149
+ (ep) => ep.procedureId
5150
+ )) || [];
5151
+ const allProcedureIds = [mainProcedureId, ...extendedProcedureIds].filter(Boolean);
5152
+ const remainingProcedureIds = allProcedureIds.filter((id) => id !== procedureId);
5153
+ const isFormSharedAndReferenced = (formId) => {
5154
+ const formEntries = linkedForms.filter((form) => form.formId === formId);
5155
+ return formEntries.length > 1;
5156
+ };
5053
5157
  const doctorFormsRef = (0, import_firestore9.collection)(appointmentRef, DOCTOR_FORMS_SUBCOLLECTION);
5054
5158
  const doctorFormsQuery = (0, import_firestore9.query)(
5055
5159
  doctorFormsRef,
@@ -5058,11 +5162,42 @@ async function removeFormsForExtendedProcedure(db, appointmentId, procedureId) {
5058
5162
  const doctorFormsSnap = await (0, import_firestore9.getDocs)(doctorFormsQuery);
5059
5163
  for (const formDoc of doctorFormsSnap.docs) {
5060
5164
  try {
5061
- await (0, import_firestore9.deleteDoc)(formDoc.ref);
5062
- removedFormIds.push(formDoc.id);
5063
- console.log(
5064
- `[FormInit] Removed doctor form ${formDoc.id} for extended procedure ${procedureId} from appointment ${appointmentId}.`
5065
- );
5165
+ const formData = formDoc.data();
5166
+ let isShared = false;
5167
+ if (formData.templateId) {
5168
+ try {
5169
+ const templateDoc = (0, import_firestore9.doc)(db, DOCUMENTATION_TEMPLATES_COLLECTION, formData.templateId);
5170
+ const templateSnap = await (0, import_firestore9.getDoc)(templateDoc);
5171
+ if (templateSnap.exists()) {
5172
+ const template = templateSnap.data();
5173
+ isShared = !isProcedureSpecificForm(template);
5174
+ }
5175
+ } catch (error) {
5176
+ console.warn(
5177
+ `[FormInit] Could not check template for form ${formDoc.id}, assuming procedure-specific:`,
5178
+ error
5179
+ );
5180
+ }
5181
+ }
5182
+ if (!isShared) {
5183
+ await (0, import_firestore9.deleteDoc)(formDoc.ref);
5184
+ removedFormIds.push(formDoc.id);
5185
+ console.log(
5186
+ `[FormInit] Removed procedure-specific doctor form ${formDoc.id} for extended procedure ${procedureId} from appointment ${appointmentId}.`
5187
+ );
5188
+ } else {
5189
+ if (isFormSharedAndReferenced(formDoc.id)) {
5190
+ console.log(
5191
+ `[FormInit] Skipped deletion of shared doctor form ${formDoc.id} - still referenced by other procedures.`
5192
+ );
5193
+ } else {
5194
+ await (0, import_firestore9.deleteDoc)(formDoc.ref);
5195
+ removedFormIds.push(formDoc.id);
5196
+ console.log(
5197
+ `[FormInit] Removed shared doctor form ${formDoc.id} - no longer referenced by other procedures.`
5198
+ );
5199
+ }
5200
+ }
5066
5201
  } catch (error) {
5067
5202
  console.error(
5068
5203
  `[FormInit] Error removing doctor form ${formDoc.id}:`,
@@ -5078,11 +5213,42 @@ async function removeFormsForExtendedProcedure(db, appointmentId, procedureId) {
5078
5213
  const userFormsSnap = await (0, import_firestore9.getDocs)(userFormsQuery);
5079
5214
  for (const formDoc of userFormsSnap.docs) {
5080
5215
  try {
5081
- await (0, import_firestore9.deleteDoc)(formDoc.ref);
5082
- removedFormIds.push(formDoc.id);
5083
- console.log(
5084
- `[FormInit] Removed user form ${formDoc.id} for extended procedure ${procedureId} from appointment ${appointmentId}.`
5085
- );
5216
+ const formData = formDoc.data();
5217
+ let isShared = false;
5218
+ if (formData.templateId) {
5219
+ try {
5220
+ const templateDoc = (0, import_firestore9.doc)(db, DOCUMENTATION_TEMPLATES_COLLECTION, formData.templateId);
5221
+ const templateSnap = await (0, import_firestore9.getDoc)(templateDoc);
5222
+ if (templateSnap.exists()) {
5223
+ const template = templateSnap.data();
5224
+ isShared = !isProcedureSpecificForm(template);
5225
+ }
5226
+ } catch (error) {
5227
+ console.warn(
5228
+ `[FormInit] Could not check template for form ${formDoc.id}, assuming procedure-specific:`,
5229
+ error
5230
+ );
5231
+ }
5232
+ }
5233
+ if (!isShared) {
5234
+ await (0, import_firestore9.deleteDoc)(formDoc.ref);
5235
+ removedFormIds.push(formDoc.id);
5236
+ console.log(
5237
+ `[FormInit] Removed procedure-specific user form ${formDoc.id} for extended procedure ${procedureId} from appointment ${appointmentId}.`
5238
+ );
5239
+ } else {
5240
+ if (isFormSharedAndReferenced(formDoc.id)) {
5241
+ console.log(
5242
+ `[FormInit] Skipped deletion of shared user form ${formDoc.id} - still referenced by other procedures.`
5243
+ );
5244
+ } else {
5245
+ await (0, import_firestore9.deleteDoc)(formDoc.ref);
5246
+ removedFormIds.push(formDoc.id);
5247
+ console.log(
5248
+ `[FormInit] Removed shared user form ${formDoc.id} - no longer referenced by other procedures.`
5249
+ );
5250
+ }
5251
+ }
5086
5252
  } catch (error) {
5087
5253
  console.error(
5088
5254
  `[FormInit] Error removing user form ${formDoc.id}:`,
@@ -20471,7 +20637,11 @@ var FilledDocumentService = class extends BaseService {
20471
20637
  if (!docSnap.exists()) {
20472
20638
  return null;
20473
20639
  }
20474
- return docSnap.data();
20640
+ const data = docSnap.data();
20641
+ if (!data.id) {
20642
+ data.id = docSnap.id;
20643
+ }
20644
+ return data;
20475
20645
  }
20476
20646
  /**
20477
20647
  * Update values or status in a filled document within an appointment's subcollection.
@@ -20572,9 +20742,13 @@ var FilledDocumentService = class extends BaseService {
20572
20742
  const querySnapshot = await (0, import_firestore55.getDocs)(q);
20573
20743
  const documents = [];
20574
20744
  let lastVisible = null;
20575
- querySnapshot.forEach((doc47) => {
20576
- documents.push(doc47.data());
20577
- lastVisible = doc47;
20745
+ querySnapshot.forEach((docSnapshot) => {
20746
+ const data = docSnapshot.data();
20747
+ if (!data.id) {
20748
+ data.id = docSnapshot.id;
20749
+ }
20750
+ documents.push(data);
20751
+ lastVisible = docSnapshot;
20578
20752
  });
20579
20753
  return {
20580
20754
  documents,