@blackcode_sa/metaestetics-api 1.4.6 → 1.4.7
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 +374 -69
- package/dist/index.mjs +374 -69
- package/package.json +1 -1
- package/src/services/auth.service.ts +138 -28
- package/src/services/clinic/utils/admin.utils.ts +155 -34
- package/src/services/clinic/utils/clinic-group.utils.ts +99 -12
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
|
-
|
|
2620
|
-
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
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
|
-
|
|
2647
|
-
defaultGroup
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
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
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
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
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
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,54 @@ 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
|
-
|
|
3655
|
-
|
|
3656
|
-
|
|
3782
|
+
try {
|
|
3783
|
+
console.log("[CLINIC_GROUP] Checking if owner exists", { ownerId });
|
|
3784
|
+
const owner = await clinicAdminService.getClinicAdmin(ownerId);
|
|
3785
|
+
if (!owner) {
|
|
3786
|
+
console.error("[CLINIC_GROUP] Owner not found or is not a clinic admin", {
|
|
3787
|
+
ownerId
|
|
3788
|
+
});
|
|
3789
|
+
throw new Error("Owner not found or is not a clinic admin");
|
|
3790
|
+
}
|
|
3791
|
+
console.log("[CLINIC_GROUP] Owner verified as clinic admin");
|
|
3792
|
+
} catch (ownerError) {
|
|
3793
|
+
console.error("[CLINIC_GROUP] Error verifying owner:", ownerError);
|
|
3794
|
+
throw ownerError;
|
|
3657
3795
|
}
|
|
3796
|
+
console.log("[CLINIC_GROUP] Generating geohash for location");
|
|
3658
3797
|
if (validatedData.hqLocation) {
|
|
3659
|
-
|
|
3660
|
-
validatedData.hqLocation.
|
|
3661
|
-
|
|
3662
|
-
|
|
3798
|
+
try {
|
|
3799
|
+
validatedData.hqLocation.geohash = (0, import_geofire_common2.geohashForLocation)([
|
|
3800
|
+
validatedData.hqLocation.latitude,
|
|
3801
|
+
validatedData.hqLocation.longitude
|
|
3802
|
+
]);
|
|
3803
|
+
console.log("[CLINIC_GROUP] Geohash generated successfully", {
|
|
3804
|
+
geohash: validatedData.hqLocation.geohash
|
|
3805
|
+
});
|
|
3806
|
+
} catch (geohashError) {
|
|
3807
|
+
console.error("[CLINIC_GROUP] Error generating geohash:", geohashError);
|
|
3808
|
+
throw geohashError;
|
|
3809
|
+
}
|
|
3663
3810
|
}
|
|
3664
3811
|
const now = import_firestore15.Timestamp.now();
|
|
3812
|
+
console.log("[CLINIC_GROUP] Preparing clinic group data object");
|
|
3813
|
+
console.log("[CLINIC_GROUP] Logo value:", {
|
|
3814
|
+
logoValue: validatedData.logo,
|
|
3815
|
+
logoType: validatedData.logo === null ? "null" : typeof validatedData.logo
|
|
3816
|
+
});
|
|
3665
3817
|
const groupData = {
|
|
3666
3818
|
...validatedData,
|
|
3667
3819
|
id: (0, import_firestore15.doc)((0, import_firestore15.collection)(db, CLINIC_GROUPS_COLLECTION)).id,
|
|
@@ -3686,17 +3838,63 @@ async function createClinicGroup(db, data, ownerId, isDefault = false, clinicAdm
|
|
|
3686
3838
|
isActive: true
|
|
3687
3839
|
};
|
|
3688
3840
|
try {
|
|
3689
|
-
|
|
3690
|
-
|
|
3691
|
-
|
|
3692
|
-
|
|
3693
|
-
|
|
3841
|
+
console.log("[CLINIC_GROUP] Validating complete clinic group object");
|
|
3842
|
+
try {
|
|
3843
|
+
clinicGroupSchema.parse(groupData);
|
|
3844
|
+
console.log("[CLINIC_GROUP] Clinic group validation passed");
|
|
3845
|
+
} catch (schemaError) {
|
|
3846
|
+
console.error(
|
|
3847
|
+
"[CLINIC_GROUP] Clinic group validation failed:",
|
|
3848
|
+
JSON.stringify(schemaError, null, 2)
|
|
3849
|
+
);
|
|
3850
|
+
throw schemaError;
|
|
3851
|
+
}
|
|
3852
|
+
console.log("[CLINIC_GROUP] Saving clinic group to Firestore", {
|
|
3853
|
+
groupId: groupData.id
|
|
3854
|
+
});
|
|
3855
|
+
try {
|
|
3856
|
+
await (0, import_firestore15.setDoc)((0, import_firestore15.doc)(db, CLINIC_GROUPS_COLLECTION, groupData.id), groupData);
|
|
3857
|
+
console.log("[CLINIC_GROUP] Clinic group saved successfully");
|
|
3858
|
+
} catch (firestoreError) {
|
|
3859
|
+
console.error(
|
|
3860
|
+
"[CLINIC_GROUP] Error saving to Firestore:",
|
|
3861
|
+
firestoreError
|
|
3862
|
+
);
|
|
3863
|
+
throw firestoreError;
|
|
3864
|
+
}
|
|
3865
|
+
console.log("[CLINIC_GROUP] Updating clinic admin profile for owner", {
|
|
3866
|
+
ownerId
|
|
3867
|
+
});
|
|
3868
|
+
try {
|
|
3869
|
+
await clinicAdminService.updateClinicAdmin(ownerId, {
|
|
3870
|
+
clinicGroupId: groupData.id,
|
|
3871
|
+
isGroupOwner: true
|
|
3872
|
+
});
|
|
3873
|
+
console.log("[CLINIC_GROUP] Clinic admin profile updated successfully");
|
|
3874
|
+
} catch (updateError) {
|
|
3875
|
+
console.error(
|
|
3876
|
+
"[CLINIC_GROUP] Error updating clinic admin profile:",
|
|
3877
|
+
updateError
|
|
3878
|
+
);
|
|
3879
|
+
throw updateError;
|
|
3880
|
+
}
|
|
3881
|
+
console.log("[CLINIC_GROUP] Clinic group creation completed successfully", {
|
|
3882
|
+
groupId: groupData.id,
|
|
3883
|
+
groupName: groupData.name
|
|
3694
3884
|
});
|
|
3695
3885
|
return groupData;
|
|
3696
3886
|
} catch (error) {
|
|
3697
3887
|
if (error instanceof import_zod13.z.ZodError) {
|
|
3888
|
+
console.error(
|
|
3889
|
+
"[CLINIC_GROUP] Zod validation error:",
|
|
3890
|
+
JSON.stringify(error.errors, null, 2)
|
|
3891
|
+
);
|
|
3698
3892
|
throw new Error("Invalid clinic group data: " + error.message);
|
|
3699
3893
|
}
|
|
3894
|
+
console.error(
|
|
3895
|
+
"[CLINIC_GROUP] Unhandled error in createClinicGroup:",
|
|
3896
|
+
error
|
|
3897
|
+
);
|
|
3700
3898
|
throw error;
|
|
3701
3899
|
}
|
|
3702
3900
|
}
|
|
@@ -4476,19 +4674,52 @@ var AuthService = class extends BaseService {
|
|
|
4476
4674
|
*/
|
|
4477
4675
|
async signUpClinicAdmin(data) {
|
|
4478
4676
|
try {
|
|
4479
|
-
|
|
4480
|
-
|
|
4481
|
-
|
|
4482
|
-
|
|
4483
|
-
data
|
|
4484
|
-
|
|
4485
|
-
|
|
4486
|
-
|
|
4487
|
-
|
|
4488
|
-
|
|
4489
|
-
|
|
4490
|
-
|
|
4491
|
-
|
|
4677
|
+
console.log("[AUTH] Starting clinic admin signup process", {
|
|
4678
|
+
email: data.email
|
|
4679
|
+
});
|
|
4680
|
+
try {
|
|
4681
|
+
await clinicAdminSignupSchema.parseAsync(data);
|
|
4682
|
+
console.log("[AUTH] Clinic admin signup data validation passed");
|
|
4683
|
+
} catch (validationError) {
|
|
4684
|
+
console.error(
|
|
4685
|
+
"[AUTH] Validation error in signUpClinicAdmin:",
|
|
4686
|
+
validationError
|
|
4687
|
+
);
|
|
4688
|
+
throw validationError;
|
|
4689
|
+
}
|
|
4690
|
+
console.log("[AUTH] Creating Firebase user");
|
|
4691
|
+
let firebaseUser;
|
|
4692
|
+
try {
|
|
4693
|
+
const result = await (0, import_auth5.createUserWithEmailAndPassword)(
|
|
4694
|
+
this.auth,
|
|
4695
|
+
data.email,
|
|
4696
|
+
data.password
|
|
4697
|
+
);
|
|
4698
|
+
firebaseUser = result.user;
|
|
4699
|
+
console.log("[AUTH] Firebase user created successfully", {
|
|
4700
|
+
uid: firebaseUser.uid
|
|
4701
|
+
});
|
|
4702
|
+
} catch (firebaseError) {
|
|
4703
|
+
console.error("[AUTH] Firebase user creation failed:", firebaseError);
|
|
4704
|
+
throw firebaseError;
|
|
4705
|
+
}
|
|
4706
|
+
console.log("[AUTH] Creating user with CLINIC_ADMIN role");
|
|
4707
|
+
let user;
|
|
4708
|
+
try {
|
|
4709
|
+
user = await this.userService.createUser(
|
|
4710
|
+
firebaseUser,
|
|
4711
|
+
["clinic_admin" /* CLINIC_ADMIN */],
|
|
4712
|
+
{
|
|
4713
|
+
skipProfileCreation: true
|
|
4714
|
+
}
|
|
4715
|
+
);
|
|
4716
|
+
console.log("[AUTH] User with CLINIC_ADMIN role created successfully", {
|
|
4717
|
+
userId: user.uid
|
|
4718
|
+
});
|
|
4719
|
+
} catch (userCreationError) {
|
|
4720
|
+
console.error("[AUTH] User creation failed:", userCreationError);
|
|
4721
|
+
throw userCreationError;
|
|
4722
|
+
}
|
|
4492
4723
|
const contactPerson = {
|
|
4493
4724
|
firstName: data.firstName,
|
|
4494
4725
|
lastName: data.lastName,
|
|
@@ -4496,6 +4727,8 @@ var AuthService = class extends BaseService {
|
|
|
4496
4727
|
email: data.email,
|
|
4497
4728
|
phoneNumber: data.phoneNumber
|
|
4498
4729
|
};
|
|
4730
|
+
console.log("[AUTH] Contact person object created");
|
|
4731
|
+
console.log("[AUTH] Initializing clinic services");
|
|
4499
4732
|
const clinicAdminService = new ClinicAdminService(
|
|
4500
4733
|
this.db,
|
|
4501
4734
|
this.auth,
|
|
@@ -4515,8 +4748,13 @@ var AuthService = class extends BaseService {
|
|
|
4515
4748
|
clinicAdminService
|
|
4516
4749
|
);
|
|
4517
4750
|
clinicAdminService.setServices(clinicGroupService, clinicService);
|
|
4751
|
+
console.log(
|
|
4752
|
+
"[AUTH] Services initialized and circular dependencies resolved"
|
|
4753
|
+
);
|
|
4518
4754
|
if (data.isCreatingNewGroup) {
|
|
4755
|
+
console.log("[AUTH] Creating new clinic group flow");
|
|
4519
4756
|
if (!data.clinicGroupData) {
|
|
4757
|
+
console.error("[AUTH] Clinic group data is missing");
|
|
4520
4758
|
throw new Error(
|
|
4521
4759
|
"Clinic group data is required when creating a new group"
|
|
4522
4760
|
);
|
|
@@ -4531,36 +4769,79 @@ var AuthService = class extends BaseService {
|
|
|
4531
4769
|
logo: data.clinicGroupData.logo || null,
|
|
4532
4770
|
subscriptionModel: data.clinicGroupData.subscriptionModel || "no_subscription" /* NO_SUBSCRIPTION */
|
|
4533
4771
|
};
|
|
4534
|
-
|
|
4535
|
-
createClinicGroupData
|
|
4536
|
-
|
|
4537
|
-
|
|
4538
|
-
|
|
4772
|
+
console.log("[AUTH] Clinic group data prepared", {
|
|
4773
|
+
groupName: createClinicGroupData.name
|
|
4774
|
+
});
|
|
4775
|
+
try {
|
|
4776
|
+
await clinicGroupService.createClinicGroup(
|
|
4777
|
+
createClinicGroupData,
|
|
4778
|
+
firebaseUser.uid,
|
|
4779
|
+
true
|
|
4780
|
+
);
|
|
4781
|
+
console.log("[AUTH] Clinic group created successfully");
|
|
4782
|
+
} catch (groupCreationError) {
|
|
4783
|
+
console.error(
|
|
4784
|
+
"[AUTH] Clinic group creation failed:",
|
|
4785
|
+
groupCreationError
|
|
4786
|
+
);
|
|
4787
|
+
throw groupCreationError;
|
|
4788
|
+
}
|
|
4539
4789
|
} else {
|
|
4790
|
+
console.log("[AUTH] Joining existing clinic group flow");
|
|
4540
4791
|
if (!data.inviteToken) {
|
|
4792
|
+
console.error("[AUTH] Invite token is missing");
|
|
4541
4793
|
throw new Error(
|
|
4542
4794
|
"Invite token is required when joining an existing group"
|
|
4543
4795
|
);
|
|
4544
4796
|
}
|
|
4797
|
+
console.log("[AUTH] Invite token provided", {
|
|
4798
|
+
token: data.inviteToken
|
|
4799
|
+
});
|
|
4800
|
+
console.log("[AUTH] Searching for token in clinic groups");
|
|
4545
4801
|
const groupsRef = (0, import_firestore19.collection)(this.db, CLINIC_GROUPS_COLLECTION);
|
|
4546
4802
|
const q = (0, import_firestore19.query)(groupsRef);
|
|
4547
4803
|
const querySnapshot = await (0, import_firestore19.getDocs)(q);
|
|
4548
4804
|
let foundGroup = null;
|
|
4549
4805
|
let foundToken = null;
|
|
4806
|
+
console.log(
|
|
4807
|
+
"[AUTH] Found",
|
|
4808
|
+
querySnapshot.size,
|
|
4809
|
+
"clinic groups to check"
|
|
4810
|
+
);
|
|
4550
4811
|
for (const docSnapshot of querySnapshot.docs) {
|
|
4551
4812
|
const group = docSnapshot.data();
|
|
4552
|
-
|
|
4553
|
-
|
|
4554
|
-
|
|
4813
|
+
console.log("[AUTH] Checking group", {
|
|
4814
|
+
groupId: group.id,
|
|
4815
|
+
groupName: group.name
|
|
4816
|
+
});
|
|
4817
|
+
const token = group.adminTokens.find((t) => {
|
|
4818
|
+
const isMatch = t.token === data.inviteToken && t.status === "active" /* ACTIVE */ && new Date(t.expiresAt.toDate()) > /* @__PURE__ */ new Date();
|
|
4819
|
+
console.log("[AUTH] Checking token", {
|
|
4820
|
+
tokenId: t.id,
|
|
4821
|
+
tokenMatch: t.token === data.inviteToken,
|
|
4822
|
+
tokenStatus: t.status,
|
|
4823
|
+
tokenActive: t.status === "active" /* ACTIVE */,
|
|
4824
|
+
tokenExpiry: new Date(t.expiresAt.toDate()),
|
|
4825
|
+
tokenExpired: new Date(t.expiresAt.toDate()) <= /* @__PURE__ */ new Date(),
|
|
4826
|
+
isMatch
|
|
4827
|
+
});
|
|
4828
|
+
return isMatch;
|
|
4829
|
+
});
|
|
4555
4830
|
if (token) {
|
|
4556
4831
|
foundGroup = group;
|
|
4557
4832
|
foundToken = token;
|
|
4833
|
+
console.log("[AUTH] Found matching token in group", {
|
|
4834
|
+
groupId: group.id,
|
|
4835
|
+
tokenId: token.id
|
|
4836
|
+
});
|
|
4558
4837
|
break;
|
|
4559
4838
|
}
|
|
4560
4839
|
}
|
|
4561
4840
|
if (!foundGroup || !foundToken) {
|
|
4841
|
+
console.error("[AUTH] No valid token found in any clinic group");
|
|
4562
4842
|
throw new Error("Invalid or expired invite token");
|
|
4563
4843
|
}
|
|
4844
|
+
console.log("[AUTH] Creating clinic admin");
|
|
4564
4845
|
const createClinicAdminData = {
|
|
4565
4846
|
userRef: firebaseUser.uid,
|
|
4566
4847
|
clinicGroupId: foundGroup.id,
|
|
@@ -4570,22 +4851,46 @@ var AuthService = class extends BaseService {
|
|
|
4570
4851
|
roleTitle: data.title,
|
|
4571
4852
|
isActive: true
|
|
4572
4853
|
};
|
|
4573
|
-
|
|
4574
|
-
|
|
4575
|
-
|
|
4576
|
-
|
|
4577
|
-
|
|
4578
|
-
|
|
4854
|
+
try {
|
|
4855
|
+
await clinicAdminService.createClinicAdmin(createClinicAdminData);
|
|
4856
|
+
console.log("[AUTH] Clinic admin created successfully");
|
|
4857
|
+
} catch (adminCreationError) {
|
|
4858
|
+
console.error(
|
|
4859
|
+
"[AUTH] Clinic admin creation failed:",
|
|
4860
|
+
adminCreationError
|
|
4861
|
+
);
|
|
4862
|
+
throw adminCreationError;
|
|
4863
|
+
}
|
|
4864
|
+
try {
|
|
4865
|
+
await clinicGroupService.verifyAndUseAdminToken(
|
|
4866
|
+
foundGroup.id,
|
|
4867
|
+
data.inviteToken,
|
|
4868
|
+
firebaseUser.uid
|
|
4869
|
+
);
|
|
4870
|
+
console.log("[AUTH] Token marked as used successfully");
|
|
4871
|
+
} catch (tokenUseError) {
|
|
4872
|
+
console.error("[AUTH] Failed to mark token as used:", tokenUseError);
|
|
4873
|
+
throw tokenUseError;
|
|
4874
|
+
}
|
|
4579
4875
|
}
|
|
4876
|
+
console.log("[AUTH] Clinic admin signup completed successfully", {
|
|
4877
|
+
userId: user.uid
|
|
4878
|
+
});
|
|
4580
4879
|
return user;
|
|
4581
4880
|
} catch (error) {
|
|
4582
4881
|
if (error instanceof import_zod15.z.ZodError) {
|
|
4882
|
+
console.error(
|
|
4883
|
+
"[AUTH] Zod validation error in signUpClinicAdmin:",
|
|
4884
|
+
JSON.stringify(error.errors, null, 2)
|
|
4885
|
+
);
|
|
4583
4886
|
throw AUTH_ERRORS.VALIDATION_ERROR;
|
|
4584
4887
|
}
|
|
4585
4888
|
const firebaseError = error;
|
|
4586
4889
|
if (firebaseError.code === "auth/email-already-in-use" /* EMAIL_ALREADY_IN_USE */) {
|
|
4890
|
+
console.error("[AUTH] Email already in use:", data.email);
|
|
4587
4891
|
throw AUTH_ERRORS.EMAIL_ALREADY_EXISTS;
|
|
4588
4892
|
}
|
|
4893
|
+
console.error("[AUTH] Unhandled error in signUpClinicAdmin:", error);
|
|
4589
4894
|
throw error;
|
|
4590
4895
|
}
|
|
4591
4896
|
}
|