@blackcode_sa/metaestetics-api 1.4.6 → 1.4.8

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
@@ -2615,23 +2615,54 @@ var updateClinicSchema = createClinicSchema.partial();
2615
2615
 
2616
2616
  // src/services/clinic/utils/admin.utils.ts
2617
2617
  async function createClinicAdmin(db, data, clinicGroupService) {
2618
+ console.log("[CLINIC_ADMIN] Starting clinic admin creation", {
2619
+ userRef: data.userRef
2620
+ });
2621
+ console.log("[CLINIC_ADMIN] Input data:", JSON.stringify(data, null, 2));
2622
+ try {
2623
+ const validatedData2 = createClinicAdminSchema.parse(data);
2624
+ console.log("[CLINIC_ADMIN] Data validation passed");
2625
+ } catch (validationError) {
2626
+ console.error("[CLINIC_ADMIN] Data validation failed:", validationError);
2627
+ throw validationError;
2628
+ }
2618
2629
  const validatedData = createClinicAdminSchema.parse(data);
2619
- const existingAdmin = await getClinicAdminByUserRef(
2620
- db,
2621
- validatedData.userRef
2622
- );
2623
- if (existingAdmin) {
2624
- throw new Error("User already has an admin profile");
2630
+ console.log("[CLINIC_ADMIN] Checking if user already has an admin profile", {
2631
+ userRef: validatedData.userRef
2632
+ });
2633
+ try {
2634
+ const existingAdmin = await getClinicAdminByUserRef(
2635
+ db,
2636
+ validatedData.userRef
2637
+ );
2638
+ if (existingAdmin) {
2639
+ console.error("[CLINIC_ADMIN] User already has an admin profile", {
2640
+ adminId: existingAdmin.id
2641
+ });
2642
+ throw new Error("User already has an admin profile");
2643
+ }
2644
+ console.log("[CLINIC_ADMIN] User does not have an existing admin profile");
2645
+ } catch (error) {
2646
+ if (error.message === "User already has an admin profile") {
2647
+ throw error;
2648
+ }
2649
+ console.error("[CLINIC_ADMIN] Error checking for existing admin:", error);
2650
+ throw error;
2625
2651
  }
2626
2652
  let clinicGroupId = validatedData.clinicGroupId;
2653
+ console.log("[CLINIC_ADMIN] Checking clinic group situation", {
2654
+ isGroupOwner: validatedData.isGroupOwner,
2655
+ hasGroupId: !!clinicGroupId
2656
+ });
2627
2657
  if (validatedData.isGroupOwner && !clinicGroupId) {
2658
+ console.log("[CLINIC_ADMIN] Creating default group for owner");
2628
2659
  const defaultGroup = {
2629
2660
  name: `${validatedData.contactInfo.firstName}'s Group`,
2630
2661
  ownerId: validatedData.userRef,
2631
2662
  contactPerson: validatedData.contactInfo,
2632
2663
  contactInfo: {
2633
2664
  email: validatedData.contactInfo.email,
2634
- phoneNumber: validatedData.contactInfo.phoneNumber
2665
+ phoneNumber: validatedData.contactInfo.phoneNumber || ""
2635
2666
  },
2636
2667
  hqLocation: {
2637
2668
  address: "",
@@ -2643,24 +2674,61 @@ async function createClinicAdmin(db, data, clinicGroupService) {
2643
2674
  },
2644
2675
  isActive: true
2645
2676
  };
2646
- const clinicGroup = await clinicGroupService.createClinicGroup(
2647
- defaultGroup,
2648
- validatedData.userRef,
2649
- true
2650
- );
2651
- clinicGroupId = clinicGroup.id;
2677
+ console.log("[CLINIC_ADMIN] Default group data prepared", {
2678
+ groupName: defaultGroup.name
2679
+ });
2680
+ try {
2681
+ const clinicGroup = await clinicGroupService.createClinicGroup(
2682
+ defaultGroup,
2683
+ validatedData.userRef,
2684
+ true
2685
+ );
2686
+ clinicGroupId = clinicGroup.id;
2687
+ console.log("[CLINIC_ADMIN] Default group created successfully", {
2688
+ groupId: clinicGroupId
2689
+ });
2690
+ } catch (groupCreationError) {
2691
+ console.error(
2692
+ "[CLINIC_ADMIN] Error creating default group:",
2693
+ groupCreationError
2694
+ );
2695
+ throw groupCreationError;
2696
+ }
2652
2697
  } else if (!validatedData.isGroupOwner && !clinicGroupId) {
2698
+ console.error("[CLINIC_ADMIN] Missing clinic group ID for non-owner admin");
2653
2699
  throw new Error("Clinic group ID is required for non-owner admins");
2654
2700
  } else if (!validatedData.isGroupOwner && clinicGroupId) {
2655
- const groupExists = await checkClinicGroupExists(
2656
- db,
2657
- clinicGroupId,
2658
- clinicGroupService
2659
- );
2660
- if (!groupExists) {
2661
- throw new Error("Specified clinic group does not exist");
2701
+ console.log("[CLINIC_ADMIN] Checking if specified clinic group exists", {
2702
+ groupId: clinicGroupId
2703
+ });
2704
+ try {
2705
+ const groupExists = await checkClinicGroupExists(
2706
+ db,
2707
+ clinicGroupId,
2708
+ clinicGroupService
2709
+ );
2710
+ if (!groupExists) {
2711
+ console.error("[CLINIC_ADMIN] Specified clinic group does not exist", {
2712
+ groupId: clinicGroupId
2713
+ });
2714
+ throw new Error("Specified clinic group does not exist");
2715
+ }
2716
+ console.log("[CLINIC_ADMIN] Specified clinic group exists");
2717
+ } catch (groupCheckError) {
2718
+ console.error(
2719
+ "[CLINIC_ADMIN] Error checking if clinic group exists:",
2720
+ groupCheckError
2721
+ );
2722
+ throw groupCheckError;
2662
2723
  }
2663
2724
  }
2725
+ console.log("[CLINIC_ADMIN] Preparing admin data object");
2726
+ if (!clinicGroupId) {
2727
+ console.error(
2728
+ "[CLINIC_ADMIN] clinicGroupId is undefined, which should not happen at this point"
2729
+ );
2730
+ throw new Error("clinicGroupId is required but was undefined");
2731
+ }
2664
2732
  const adminData = {
2665
2733
  id: validatedData.userRef,
2666
2734
  userRef: validatedData.userRef,
@@ -2676,20 +2744,68 @@ async function createClinicAdmin(db, data, clinicGroupService) {
2676
2744
  createdAt: (0, import_firestore11.serverTimestamp)(),
2677
2745
  updatedAt: (0, import_firestore11.serverTimestamp)()
2678
2746
  };
2679
- clinicAdminSchema.parse({
2680
- ...adminData,
2681
- createdAt: import_firestore11.Timestamp.now(),
2682
- updatedAt: import_firestore11.Timestamp.now()
2747
+ console.log("[CLINIC_ADMIN] Validating complete admin object");
2748
+ try {
2749
+ clinicAdminSchema.parse({
2750
+ ...adminData,
2751
+ createdAt: import_firestore11.Timestamp.now(),
2752
+ updatedAt: import_firestore11.Timestamp.now()
2753
+ });
2754
+ console.log("[CLINIC_ADMIN] Admin object validation passed");
2755
+ } catch (schemaError) {
2756
+ console.error(
2757
+ "[CLINIC_ADMIN] Admin object validation failed:",
2758
+ JSON.stringify(schemaError, null, 2)
2759
+ );
2760
+ throw schemaError;
2761
+ }
2762
+ console.log("[CLINIC_ADMIN] Saving admin to Firestore", {
2763
+ adminId: adminData.id
2683
2764
  });
2684
- await (0, import_firestore11.setDoc)((0, import_firestore11.doc)(db, CLINIC_ADMINS_COLLECTION, adminData.id), adminData);
2765
+ try {
2766
+ await (0, import_firestore11.setDoc)((0, import_firestore11.doc)(db, CLINIC_ADMINS_COLLECTION, adminData.id), adminData);
2767
+ console.log("[CLINIC_ADMIN] Admin saved successfully");
2768
+ } catch (firestoreError) {
2769
+ console.error(
2770
+ "[CLINIC_ADMIN] Error saving admin to Firestore:",
2771
+ firestoreError
2772
+ );
2773
+ throw firestoreError;
2774
+ }
2685
2775
  if (clinicGroupId) {
2686
- await clinicGroupService.addAdminToGroup(clinicGroupId, adminData.id);
2776
+ console.log("[CLINIC_ADMIN] Adding admin to clinic group", {
2777
+ adminId: adminData.id,
2778
+ groupId: clinicGroupId
2779
+ });
2780
+ try {
2781
+ await clinicGroupService.addAdminToGroup(clinicGroupId, adminData.id);
2782
+ console.log("[CLINIC_ADMIN] Admin added to group successfully");
2783
+ } catch (addToGroupError) {
2784
+ console.error(
2785
+ "[CLINIC_ADMIN] Error adding admin to group:",
2786
+ addToGroupError
2787
+ );
2788
+ throw addToGroupError;
2789
+ }
2687
2790
  }
2688
- const createdAdmin = await getClinicAdmin(db, adminData.id);
2689
- if (!createdAdmin) {
2690
- throw new Error("Failed to retrieve created admin");
2791
+ console.log("[CLINIC_ADMIN] Retrieving created admin");
2792
+ try {
2793
+ const createdAdmin = await getClinicAdmin(db, adminData.id);
2794
+ if (!createdAdmin) {
2795
+ console.error("[CLINIC_ADMIN] Failed to retrieve created admin");
2796
+ throw new Error("Failed to retrieve created admin");
2797
+ }
2798
+ console.log("[CLINIC_ADMIN] Admin creation completed successfully", {
2799
+ adminId: createdAdmin.id
2800
+ });
2801
+ return createdAdmin;
2802
+ } catch (retrieveError) {
2803
+ console.error(
2804
+ "[CLINIC_ADMIN] Error retrieving created admin:",
2805
+ retrieveError
2806
+ );
2807
+ throw retrieveError;
2691
2808
  }
2692
- return createdAdmin;
2693
2809
  }
2694
2810
  async function checkClinicGroupExists(db, groupId, clinicGroupService) {
2695
2811
  const group = await clinicGroupService.getClinicGroup(groupId);
@@ -3650,18 +3766,63 @@ function generateId() {
3650
3766
  return `${randomPart}-${timestamp}`;
3651
3767
  }
3652
3768
  async function createClinicGroup(db, data, ownerId, isDefault = false, clinicAdminService) {
3769
+ console.log("[CLINIC_GROUP] Starting clinic group creation", {
3770
+ ownerId,
3771
+ isDefault
3772
+ });
3773
+ console.log("[CLINIC_GROUP] Input data:", JSON.stringify(data, null, 2));
3774
+ try {
3775
+ const validatedData2 = createClinicGroupSchema.parse(data);
3776
+ console.log("[CLINIC_GROUP] Data validation passed");
3777
+ } catch (validationError) {
3778
+ console.error("[CLINIC_GROUP] Data validation failed:", validationError);
3779
+ throw validationError;
3780
+ }
3653
3781
  const validatedData = createClinicGroupSchema.parse(data);
3654
- const owner = await clinicAdminService.getClinicAdmin(ownerId);
3655
- if (!owner) {
3656
- throw new Error("Owner not found or is not a clinic admin");
3782
+ try {
3783
+ console.log("[CLINIC_GROUP] Checking if owner exists", { ownerId });
3784
+ if (isDefault) {
3785
+ console.log(
3786
+ "[CLINIC_GROUP] Skipping owner verification for default group creation"
3787
+ );
3788
+ } else {
3789
+ const owner = await clinicAdminService.getClinicAdmin(ownerId);
3790
+ if (!owner) {
3791
+ console.error(
3792
+ "[CLINIC_GROUP] Owner not found or is not a clinic admin",
3793
+ {
3794
+ ownerId
3795
+ }
3796
+ );
3797
+ throw new Error("Owner not found or is not a clinic admin");
3798
+ }
3799
+ console.log("[CLINIC_GROUP] Owner verified as clinic admin");
3800
+ }
3801
+ } catch (ownerError) {
3802
+ console.error("[CLINIC_GROUP] Error verifying owner:", ownerError);
3803
+ throw ownerError;
3657
3804
  }
3805
+ console.log("[CLINIC_GROUP] Generating geohash for location");
3658
3806
  if (validatedData.hqLocation) {
3659
- validatedData.hqLocation.geohash = (0, import_geofire_common2.geohashForLocation)([
3660
- validatedData.hqLocation.latitude,
3661
- validatedData.hqLocation.longitude
3662
- ]);
3807
+ try {
3808
+ validatedData.hqLocation.geohash = (0, import_geofire_common2.geohashForLocation)([
3809
+ validatedData.hqLocation.latitude,
3810
+ validatedData.hqLocation.longitude
3811
+ ]);
3812
+ console.log("[CLINIC_GROUP] Geohash generated successfully", {
3813
+ geohash: validatedData.hqLocation.geohash
3814
+ });
3815
+ } catch (geohashError) {
3816
+ console.error("[CLINIC_GROUP] Error generating geohash:", geohashError);
3817
+ throw geohashError;
3818
+ }
3663
3819
  }
3664
3820
  const now = import_firestore15.Timestamp.now();
3821
+ console.log("[CLINIC_GROUP] Preparing clinic group data object");
3822
+ console.log("[CLINIC_GROUP] Logo value:", {
3823
+ logoValue: validatedData.logo,
3824
+ logoType: validatedData.logo === null ? "null" : typeof validatedData.logo
3825
+ });
3665
3826
  const groupData = {
3666
3827
  ...validatedData,
3667
3828
  id: (0, import_firestore15.doc)((0, import_firestore15.collection)(db, CLINIC_GROUPS_COLLECTION)).id,
@@ -3686,17 +3847,63 @@ async function createClinicGroup(db, data, ownerId, isDefault = false, clinicAdm
3686
3847
  isActive: true
3687
3848
  };
3688
3849
  try {
3689
- clinicGroupSchema.parse(groupData);
3690
- await (0, import_firestore15.setDoc)((0, import_firestore15.doc)(db, CLINIC_GROUPS_COLLECTION, groupData.id), groupData);
3691
- await clinicAdminService.updateClinicAdmin(ownerId, {
3692
- clinicGroupId: groupData.id,
3693
- isGroupOwner: true
3850
+ console.log("[CLINIC_GROUP] Validating complete clinic group object");
3851
+ try {
3852
+ clinicGroupSchema.parse(groupData);
3853
+ console.log("[CLINIC_GROUP] Clinic group validation passed");
3854
+ } catch (schemaError) {
3855
+ console.error(
3856
+ "[CLINIC_GROUP] Clinic group validation failed:",
3857
+ JSON.stringify(schemaError, null, 2)
3858
+ );
3859
+ throw schemaError;
3860
+ }
3861
+ console.log("[CLINIC_GROUP] Saving clinic group to Firestore", {
3862
+ groupId: groupData.id
3863
+ });
3864
+ try {
3865
+ await (0, import_firestore15.setDoc)((0, import_firestore15.doc)(db, CLINIC_GROUPS_COLLECTION, groupData.id), groupData);
3866
+ console.log("[CLINIC_GROUP] Clinic group saved successfully");
3867
+ } catch (firestoreError) {
3868
+ console.error(
3869
+ "[CLINIC_GROUP] Error saving to Firestore:",
3870
+ firestoreError
3871
+ );
3872
+ throw firestoreError;
3873
+ }
3874
+ console.log("[CLINIC_GROUP] Updating clinic admin profile for owner", {
3875
+ ownerId
3876
+ });
3877
+ try {
3878
+ await clinicAdminService.updateClinicAdmin(ownerId, {
3879
+ clinicGroupId: groupData.id,
3880
+ isGroupOwner: true
3881
+ });
3882
+ console.log("[CLINIC_GROUP] Clinic admin profile updated successfully");
3883
+ } catch (updateError) {
3884
+ console.error(
3885
+ "[CLINIC_GROUP] Error updating clinic admin profile:",
3886
+ updateError
3887
+ );
3888
+ throw updateError;
3889
+ }
3890
+ console.log("[CLINIC_GROUP] Clinic group creation completed successfully", {
3891
+ groupId: groupData.id,
3892
+ groupName: groupData.name
3694
3893
  });
3695
3894
  return groupData;
3696
3895
  } catch (error) {
3697
3896
  if (error instanceof import_zod13.z.ZodError) {
3897
+ console.error(
3898
+ "[CLINIC_GROUP] Zod validation error:",
3899
+ JSON.stringify(error.errors, null, 2)
3900
+ );
3698
3901
  throw new Error("Invalid clinic group data: " + error.message);
3699
3902
  }
3903
+ console.error(
3904
+ "[CLINIC_GROUP] Unhandled error in createClinicGroup:",
3905
+ error
3906
+ );
3700
3907
  throw error;
3701
3908
  }
3702
3909
  }
@@ -4476,19 +4683,52 @@ var AuthService = class extends BaseService {
4476
4683
  */
4477
4684
  async signUpClinicAdmin(data) {
4478
4685
  try {
4479
- await clinicAdminSignupSchema.parseAsync(data);
4480
- const { user: firebaseUser } = await (0, import_auth5.createUserWithEmailAndPassword)(
4481
- this.auth,
4482
- data.email,
4483
- data.password
4484
- );
4485
- const user = await this.userService.createUser(
4486
- firebaseUser,
4487
- ["clinic_admin" /* CLINIC_ADMIN */],
4488
- {
4489
- skipProfileCreation: true
4490
- }
4491
- );
4686
+ console.log("[AUTH] Starting clinic admin signup process", {
4687
+ email: data.email
4688
+ });
4689
+ try {
4690
+ await clinicAdminSignupSchema.parseAsync(data);
4691
+ console.log("[AUTH] Clinic admin signup data validation passed");
4692
+ } catch (validationError) {
4693
+ console.error(
4694
+ "[AUTH] Validation error in signUpClinicAdmin:",
4695
+ validationError
4696
+ );
4697
+ throw validationError;
4698
+ }
4699
+ console.log("[AUTH] Creating Firebase user");
4700
+ let firebaseUser;
4701
+ try {
4702
+ const result = await (0, import_auth5.createUserWithEmailAndPassword)(
4703
+ this.auth,
4704
+ data.email,
4705
+ data.password
4706
+ );
4707
+ firebaseUser = result.user;
4708
+ console.log("[AUTH] Firebase user created successfully", {
4709
+ uid: firebaseUser.uid
4710
+ });
4711
+ } catch (firebaseError) {
4712
+ console.error("[AUTH] Firebase user creation failed:", firebaseError);
4713
+ throw firebaseError;
4714
+ }
4715
+ console.log("[AUTH] Creating user with CLINIC_ADMIN role");
4716
+ let user;
4717
+ try {
4718
+ user = await this.userService.createUser(
4719
+ firebaseUser,
4720
+ ["clinic_admin" /* CLINIC_ADMIN */],
4721
+ {
4722
+ skipProfileCreation: true
4723
+ }
4724
+ );
4725
+ console.log("[AUTH] User with CLINIC_ADMIN role created successfully", {
4726
+ userId: user.uid
4727
+ });
4728
+ } catch (userCreationError) {
4729
+ console.error("[AUTH] User creation failed:", userCreationError);
4730
+ throw userCreationError;
4731
+ }
4492
4732
  const contactPerson = {
4493
4733
  firstName: data.firstName,
4494
4734
  lastName: data.lastName,
@@ -4496,6 +4736,8 @@ var AuthService = class extends BaseService {
4496
4736
  email: data.email,
4497
4737
  phoneNumber: data.phoneNumber
4498
4738
  };
4739
+ console.log("[AUTH] Contact person object created");
4740
+ console.log("[AUTH] Initializing clinic services");
4499
4741
  const clinicAdminService = new ClinicAdminService(
4500
4742
  this.db,
4501
4743
  this.auth,
@@ -4515,12 +4757,36 @@ var AuthService = class extends BaseService {
4515
4757
  clinicAdminService
4516
4758
  );
4517
4759
  clinicAdminService.setServices(clinicGroupService, clinicService);
4760
+ console.log(
4761
+ "[AUTH] Services initialized and circular dependencies resolved"
4762
+ );
4518
4763
  if (data.isCreatingNewGroup) {
4764
+ console.log("[AUTH] Creating new clinic group flow");
4519
4765
  if (!data.clinicGroupData) {
4766
+ console.error("[AUTH] Clinic group data is missing");
4520
4767
  throw new Error(
4521
4768
  "Clinic group data is required when creating a new group"
4522
4769
  );
4523
4770
  }
4771
+ console.log("[AUTH] Creating clinic admin first");
4772
+ const createClinicAdminData = {
4773
+ userRef: firebaseUser.uid,
4774
+ isGroupOwner: true,
4775
+ clinicsManaged: [],
4776
+ contactInfo: contactPerson,
4777
+ roleTitle: data.title,
4778
+ isActive: true
4779
+ };
4780
+ try {
4781
+ await clinicAdminService.createClinicAdmin(createClinicAdminData);
4782
+ console.log("[AUTH] Clinic admin created successfully");
4783
+ } catch (adminCreationError) {
4784
+ console.error(
4785
+ "[AUTH] Clinic admin creation failed:",
4786
+ adminCreationError
4787
+ );
4788
+ throw adminCreationError;
4789
+ }
4524
4790
  const createClinicGroupData = {
4525
4791
  name: data.clinicGroupData.name,
4526
4792
  hqLocation: data.clinicGroupData.hqLocation,
@@ -4531,36 +4797,79 @@ var AuthService = class extends BaseService {
4531
4797
  logo: data.clinicGroupData.logo || null,
4532
4798
  subscriptionModel: data.clinicGroupData.subscriptionModel || "no_subscription" /* NO_SUBSCRIPTION */
4533
4799
  };
4534
- await clinicGroupService.createClinicGroup(
4535
- createClinicGroupData,
4536
- firebaseUser.uid,
4537
- true
4538
- );
4800
+ console.log("[AUTH] Clinic group data prepared", {
4801
+ groupName: createClinicGroupData.name
4802
+ });
4803
+ try {
4804
+ await clinicGroupService.createClinicGroup(
4805
+ createClinicGroupData,
4806
+ firebaseUser.uid,
4807
+ true
4808
+ );
4809
+ console.log("[AUTH] Clinic group created successfully");
4810
+ } catch (groupCreationError) {
4811
+ console.error(
4812
+ "[AUTH] Clinic group creation failed:",
4813
+ groupCreationError
4814
+ );
4815
+ throw groupCreationError;
4816
+ }
4539
4817
  } else {
4818
+ console.log("[AUTH] Joining existing clinic group flow");
4540
4819
  if (!data.inviteToken) {
4820
+ console.error("[AUTH] Invite token is missing");
4541
4821
  throw new Error(
4542
4822
  "Invite token is required when joining an existing group"
4543
4823
  );
4544
4824
  }
4825
+ console.log("[AUTH] Invite token provided", {
4826
+ token: data.inviteToken
4827
+ });
4828
+ console.log("[AUTH] Searching for token in clinic groups");
4545
4829
  const groupsRef = (0, import_firestore19.collection)(this.db, CLINIC_GROUPS_COLLECTION);
4546
4830
  const q = (0, import_firestore19.query)(groupsRef);
4547
4831
  const querySnapshot = await (0, import_firestore19.getDocs)(q);
4548
4832
  let foundGroup = null;
4549
4833
  let foundToken = null;
4834
+ console.log(
4835
+ "[AUTH] Found",
4836
+ querySnapshot.size,
4837
+ "clinic groups to check"
4838
+ );
4550
4839
  for (const docSnapshot of querySnapshot.docs) {
4551
4840
  const group = docSnapshot.data();
4552
- const token = group.adminTokens.find(
4553
- (t) => t.token === data.inviteToken && t.status === "active" /* ACTIVE */ && new Date(t.expiresAt.toDate()) > /* @__PURE__ */ new Date()
4554
- );
4841
+ console.log("[AUTH] Checking group", {
4842
+ groupId: group.id,
4843
+ groupName: group.name
4844
+ });
4845
+ const token = group.adminTokens.find((t) => {
4846
+ const isMatch = t.token === data.inviteToken && t.status === "active" /* ACTIVE */ && new Date(t.expiresAt.toDate()) > /* @__PURE__ */ new Date();
4847
+ console.log("[AUTH] Checking token", {
4848
+ tokenId: t.id,
4849
+ tokenMatch: t.token === data.inviteToken,
4850
+ tokenStatus: t.status,
4851
+ tokenActive: t.status === "active" /* ACTIVE */,
4852
+ tokenExpiry: new Date(t.expiresAt.toDate()),
4853
+ tokenExpired: new Date(t.expiresAt.toDate()) <= /* @__PURE__ */ new Date(),
4854
+ isMatch
4855
+ });
4856
+ return isMatch;
4857
+ });
4555
4858
  if (token) {
4556
4859
  foundGroup = group;
4557
4860
  foundToken = token;
4861
+ console.log("[AUTH] Found matching token in group", {
4862
+ groupId: group.id,
4863
+ tokenId: token.id
4864
+ });
4558
4865
  break;
4559
4866
  }
4560
4867
  }
4561
4868
  if (!foundGroup || !foundToken) {
4869
+ console.error("[AUTH] No valid token found in any clinic group");
4562
4870
  throw new Error("Invalid or expired invite token");
4563
4871
  }
4872
+ console.log("[AUTH] Creating clinic admin");
4564
4873
  const createClinicAdminData = {
4565
4874
  userRef: firebaseUser.uid,
4566
4875
  clinicGroupId: foundGroup.id,
@@ -4570,22 +4879,46 @@ var AuthService = class extends BaseService {
4570
4879
  roleTitle: data.title,
4571
4880
  isActive: true
4572
4881
  };
4573
- await clinicAdminService.createClinicAdmin(createClinicAdminData);
4574
- await clinicGroupService.verifyAndUseAdminToken(
4575
- foundGroup.id,
4576
- data.inviteToken,
4577
- firebaseUser.uid
4578
- );
4882
+ try {
4883
+ await clinicAdminService.createClinicAdmin(createClinicAdminData);
4884
+ console.log("[AUTH] Clinic admin created successfully");
4885
+ } catch (adminCreationError) {
4886
+ console.error(
4887
+ "[AUTH] Clinic admin creation failed:",
4888
+ adminCreationError
4889
+ );
4890
+ throw adminCreationError;
4891
+ }
4892
+ try {
4893
+ await clinicGroupService.verifyAndUseAdminToken(
4894
+ foundGroup.id,
4895
+ data.inviteToken,
4896
+ firebaseUser.uid
4897
+ );
4898
+ console.log("[AUTH] Token marked as used successfully");
4899
+ } catch (tokenUseError) {
4900
+ console.error("[AUTH] Failed to mark token as used:", tokenUseError);
4901
+ throw tokenUseError;
4902
+ }
4579
4903
  }
4904
+ console.log("[AUTH] Clinic admin signup completed successfully", {
4905
+ userId: user.uid
4906
+ });
4580
4907
  return user;
4581
4908
  } catch (error) {
4582
4909
  if (error instanceof import_zod15.z.ZodError) {
4910
+ console.error(
4911
+ "[AUTH] Zod validation error in signUpClinicAdmin:",
4912
+ JSON.stringify(error.errors, null, 2)
4913
+ );
4583
4914
  throw AUTH_ERRORS.VALIDATION_ERROR;
4584
4915
  }
4585
4916
  const firebaseError = error;
4586
4917
  if (firebaseError.code === "auth/email-already-in-use" /* EMAIL_ALREADY_IN_USE */) {
4918
+ console.error("[AUTH] Email already in use:", data.email);
4587
4919
  throw AUTH_ERRORS.EMAIL_ALREADY_EXISTS;
4588
4920
  }
4921
+ console.error("[AUTH] Unhandled error in signUpClinicAdmin:", error);
4589
4922
  throw error;
4590
4923
  }
4591
4924
  }