@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 +402 -69
- package/dist/index.mjs +402 -69
- package/package.json +1 -1
- package/src/services/auth.service.ts +161 -29
- package/src/services/clinic/utils/admin.utils.ts +155 -34
- package/src/services/clinic/utils/clinic-group.utils.ts +109 -12
package/dist/index.mjs
CHANGED
|
@@ -2543,23 +2543,54 @@ var updateClinicSchema = createClinicSchema.partial();
|
|
|
2543
2543
|
|
|
2544
2544
|
// src/services/clinic/utils/admin.utils.ts
|
|
2545
2545
|
async function createClinicAdmin(db, data, clinicGroupService) {
|
|
2546
|
+
console.log("[CLINIC_ADMIN] Starting clinic admin creation", {
|
|
2547
|
+
userRef: data.userRef
|
|
2548
|
+
});
|
|
2549
|
+
console.log("[CLINIC_ADMIN] Input data:", JSON.stringify(data, null, 2));
|
|
2550
|
+
try {
|
|
2551
|
+
const validatedData2 = createClinicAdminSchema.parse(data);
|
|
2552
|
+
console.log("[CLINIC_ADMIN] Data validation passed");
|
|
2553
|
+
} catch (validationError) {
|
|
2554
|
+
console.error("[CLINIC_ADMIN] Data validation failed:", validationError);
|
|
2555
|
+
throw validationError;
|
|
2556
|
+
}
|
|
2546
2557
|
const validatedData = createClinicAdminSchema.parse(data);
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2558
|
+
console.log("[CLINIC_ADMIN] Checking if user already has an admin profile", {
|
|
2559
|
+
userRef: validatedData.userRef
|
|
2560
|
+
});
|
|
2561
|
+
try {
|
|
2562
|
+
const existingAdmin = await getClinicAdminByUserRef(
|
|
2563
|
+
db,
|
|
2564
|
+
validatedData.userRef
|
|
2565
|
+
);
|
|
2566
|
+
if (existingAdmin) {
|
|
2567
|
+
console.error("[CLINIC_ADMIN] User already has an admin profile", {
|
|
2568
|
+
adminId: existingAdmin.id
|
|
2569
|
+
});
|
|
2570
|
+
throw new Error("User already has an admin profile");
|
|
2571
|
+
}
|
|
2572
|
+
console.log("[CLINIC_ADMIN] User does not have an existing admin profile");
|
|
2573
|
+
} catch (error) {
|
|
2574
|
+
if (error.message === "User already has an admin profile") {
|
|
2575
|
+
throw error;
|
|
2576
|
+
}
|
|
2577
|
+
console.error("[CLINIC_ADMIN] Error checking for existing admin:", error);
|
|
2578
|
+
throw error;
|
|
2553
2579
|
}
|
|
2554
2580
|
let clinicGroupId = validatedData.clinicGroupId;
|
|
2581
|
+
console.log("[CLINIC_ADMIN] Checking clinic group situation", {
|
|
2582
|
+
isGroupOwner: validatedData.isGroupOwner,
|
|
2583
|
+
hasGroupId: !!clinicGroupId
|
|
2584
|
+
});
|
|
2555
2585
|
if (validatedData.isGroupOwner && !clinicGroupId) {
|
|
2586
|
+
console.log("[CLINIC_ADMIN] Creating default group for owner");
|
|
2556
2587
|
const defaultGroup = {
|
|
2557
2588
|
name: `${validatedData.contactInfo.firstName}'s Group`,
|
|
2558
2589
|
ownerId: validatedData.userRef,
|
|
2559
2590
|
contactPerson: validatedData.contactInfo,
|
|
2560
2591
|
contactInfo: {
|
|
2561
2592
|
email: validatedData.contactInfo.email,
|
|
2562
|
-
phoneNumber: validatedData.contactInfo.phoneNumber
|
|
2593
|
+
phoneNumber: validatedData.contactInfo.phoneNumber || ""
|
|
2563
2594
|
},
|
|
2564
2595
|
hqLocation: {
|
|
2565
2596
|
address: "",
|
|
@@ -2571,24 +2602,61 @@ async function createClinicAdmin(db, data, clinicGroupService) {
|
|
|
2571
2602
|
},
|
|
2572
2603
|
isActive: true
|
|
2573
2604
|
};
|
|
2574
|
-
|
|
2575
|
-
defaultGroup
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2605
|
+
console.log("[CLINIC_ADMIN] Default group data prepared", {
|
|
2606
|
+
groupName: defaultGroup.name
|
|
2607
|
+
});
|
|
2608
|
+
try {
|
|
2609
|
+
const clinicGroup = await clinicGroupService.createClinicGroup(
|
|
2610
|
+
defaultGroup,
|
|
2611
|
+
validatedData.userRef,
|
|
2612
|
+
true
|
|
2613
|
+
);
|
|
2614
|
+
clinicGroupId = clinicGroup.id;
|
|
2615
|
+
console.log("[CLINIC_ADMIN] Default group created successfully", {
|
|
2616
|
+
groupId: clinicGroupId
|
|
2617
|
+
});
|
|
2618
|
+
} catch (groupCreationError) {
|
|
2619
|
+
console.error(
|
|
2620
|
+
"[CLINIC_ADMIN] Error creating default group:",
|
|
2621
|
+
groupCreationError
|
|
2622
|
+
);
|
|
2623
|
+
throw groupCreationError;
|
|
2624
|
+
}
|
|
2580
2625
|
} else if (!validatedData.isGroupOwner && !clinicGroupId) {
|
|
2626
|
+
console.error("[CLINIC_ADMIN] Missing clinic group ID for non-owner admin");
|
|
2581
2627
|
throw new Error("Clinic group ID is required for non-owner admins");
|
|
2582
2628
|
} else if (!validatedData.isGroupOwner && clinicGroupId) {
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2629
|
+
console.log("[CLINIC_ADMIN] Checking if specified clinic group exists", {
|
|
2630
|
+
groupId: clinicGroupId
|
|
2631
|
+
});
|
|
2632
|
+
try {
|
|
2633
|
+
const groupExists = await checkClinicGroupExists(
|
|
2634
|
+
db,
|
|
2635
|
+
clinicGroupId,
|
|
2636
|
+
clinicGroupService
|
|
2637
|
+
);
|
|
2638
|
+
if (!groupExists) {
|
|
2639
|
+
console.error("[CLINIC_ADMIN] Specified clinic group does not exist", {
|
|
2640
|
+
groupId: clinicGroupId
|
|
2641
|
+
});
|
|
2642
|
+
throw new Error("Specified clinic group does not exist");
|
|
2643
|
+
}
|
|
2644
|
+
console.log("[CLINIC_ADMIN] Specified clinic group exists");
|
|
2645
|
+
} catch (groupCheckError) {
|
|
2646
|
+
console.error(
|
|
2647
|
+
"[CLINIC_ADMIN] Error checking if clinic group exists:",
|
|
2648
|
+
groupCheckError
|
|
2649
|
+
);
|
|
2650
|
+
throw groupCheckError;
|
|
2590
2651
|
}
|
|
2591
2652
|
}
|
|
2653
|
+
console.log("[CLINIC_ADMIN] Preparing admin data object");
|
|
2654
|
+
if (!clinicGroupId) {
|
|
2655
|
+
console.error(
|
|
2656
|
+
"[CLINIC_ADMIN] clinicGroupId is undefined, which should not happen at this point"
|
|
2657
|
+
);
|
|
2658
|
+
throw new Error("clinicGroupId is required but was undefined");
|
|
2659
|
+
}
|
|
2592
2660
|
const adminData = {
|
|
2593
2661
|
id: validatedData.userRef,
|
|
2594
2662
|
userRef: validatedData.userRef,
|
|
@@ -2604,20 +2672,68 @@ async function createClinicAdmin(db, data, clinicGroupService) {
|
|
|
2604
2672
|
createdAt: serverTimestamp8(),
|
|
2605
2673
|
updatedAt: serverTimestamp8()
|
|
2606
2674
|
};
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
2675
|
+
console.log("[CLINIC_ADMIN] Validating complete admin object");
|
|
2676
|
+
try {
|
|
2677
|
+
clinicAdminSchema.parse({
|
|
2678
|
+
...adminData,
|
|
2679
|
+
createdAt: Timestamp6.now(),
|
|
2680
|
+
updatedAt: Timestamp6.now()
|
|
2681
|
+
});
|
|
2682
|
+
console.log("[CLINIC_ADMIN] Admin object validation passed");
|
|
2683
|
+
} catch (schemaError) {
|
|
2684
|
+
console.error(
|
|
2685
|
+
"[CLINIC_ADMIN] Admin object validation failed:",
|
|
2686
|
+
JSON.stringify(schemaError, null, 2)
|
|
2687
|
+
);
|
|
2688
|
+
throw schemaError;
|
|
2689
|
+
}
|
|
2690
|
+
console.log("[CLINIC_ADMIN] Saving admin to Firestore", {
|
|
2691
|
+
adminId: adminData.id
|
|
2611
2692
|
});
|
|
2612
|
-
|
|
2693
|
+
try {
|
|
2694
|
+
await setDoc6(doc4(db, CLINIC_ADMINS_COLLECTION, adminData.id), adminData);
|
|
2695
|
+
console.log("[CLINIC_ADMIN] Admin saved successfully");
|
|
2696
|
+
} catch (firestoreError) {
|
|
2697
|
+
console.error(
|
|
2698
|
+
"[CLINIC_ADMIN] Error saving admin to Firestore:",
|
|
2699
|
+
firestoreError
|
|
2700
|
+
);
|
|
2701
|
+
throw firestoreError;
|
|
2702
|
+
}
|
|
2613
2703
|
if (clinicGroupId) {
|
|
2614
|
-
|
|
2704
|
+
console.log("[CLINIC_ADMIN] Adding admin to clinic group", {
|
|
2705
|
+
adminId: adminData.id,
|
|
2706
|
+
groupId: clinicGroupId
|
|
2707
|
+
});
|
|
2708
|
+
try {
|
|
2709
|
+
await clinicGroupService.addAdminToGroup(clinicGroupId, adminData.id);
|
|
2710
|
+
console.log("[CLINIC_ADMIN] Admin added to group successfully");
|
|
2711
|
+
} catch (addToGroupError) {
|
|
2712
|
+
console.error(
|
|
2713
|
+
"[CLINIC_ADMIN] Error adding admin to group:",
|
|
2714
|
+
addToGroupError
|
|
2715
|
+
);
|
|
2716
|
+
throw addToGroupError;
|
|
2717
|
+
}
|
|
2615
2718
|
}
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2719
|
+
console.log("[CLINIC_ADMIN] Retrieving created admin");
|
|
2720
|
+
try {
|
|
2721
|
+
const createdAdmin = await getClinicAdmin(db, adminData.id);
|
|
2722
|
+
if (!createdAdmin) {
|
|
2723
|
+
console.error("[CLINIC_ADMIN] Failed to retrieve created admin");
|
|
2724
|
+
throw new Error("Failed to retrieve created admin");
|
|
2725
|
+
}
|
|
2726
|
+
console.log("[CLINIC_ADMIN] Admin creation completed successfully", {
|
|
2727
|
+
adminId: createdAdmin.id
|
|
2728
|
+
});
|
|
2729
|
+
return createdAdmin;
|
|
2730
|
+
} catch (retrieveError) {
|
|
2731
|
+
console.error(
|
|
2732
|
+
"[CLINIC_ADMIN] Error retrieving created admin:",
|
|
2733
|
+
retrieveError
|
|
2734
|
+
);
|
|
2735
|
+
throw retrieveError;
|
|
2619
2736
|
}
|
|
2620
|
-
return createdAdmin;
|
|
2621
2737
|
}
|
|
2622
2738
|
async function checkClinicGroupExists(db, groupId, clinicGroupService) {
|
|
2623
2739
|
const group = await clinicGroupService.getClinicGroup(groupId);
|
|
@@ -3600,18 +3716,63 @@ function generateId() {
|
|
|
3600
3716
|
return `${randomPart}-${timestamp}`;
|
|
3601
3717
|
}
|
|
3602
3718
|
async function createClinicGroup(db, data, ownerId, isDefault = false, clinicAdminService) {
|
|
3719
|
+
console.log("[CLINIC_GROUP] Starting clinic group creation", {
|
|
3720
|
+
ownerId,
|
|
3721
|
+
isDefault
|
|
3722
|
+
});
|
|
3723
|
+
console.log("[CLINIC_GROUP] Input data:", JSON.stringify(data, null, 2));
|
|
3724
|
+
try {
|
|
3725
|
+
const validatedData2 = createClinicGroupSchema.parse(data);
|
|
3726
|
+
console.log("[CLINIC_GROUP] Data validation passed");
|
|
3727
|
+
} catch (validationError) {
|
|
3728
|
+
console.error("[CLINIC_GROUP] Data validation failed:", validationError);
|
|
3729
|
+
throw validationError;
|
|
3730
|
+
}
|
|
3603
3731
|
const validatedData = createClinicGroupSchema.parse(data);
|
|
3604
|
-
|
|
3605
|
-
|
|
3606
|
-
|
|
3732
|
+
try {
|
|
3733
|
+
console.log("[CLINIC_GROUP] Checking if owner exists", { ownerId });
|
|
3734
|
+
if (isDefault) {
|
|
3735
|
+
console.log(
|
|
3736
|
+
"[CLINIC_GROUP] Skipping owner verification for default group creation"
|
|
3737
|
+
);
|
|
3738
|
+
} else {
|
|
3739
|
+
const owner = await clinicAdminService.getClinicAdmin(ownerId);
|
|
3740
|
+
if (!owner) {
|
|
3741
|
+
console.error(
|
|
3742
|
+
"[CLINIC_GROUP] Owner not found or is not a clinic admin",
|
|
3743
|
+
{
|
|
3744
|
+
ownerId
|
|
3745
|
+
}
|
|
3746
|
+
);
|
|
3747
|
+
throw new Error("Owner not found or is not a clinic admin");
|
|
3748
|
+
}
|
|
3749
|
+
console.log("[CLINIC_GROUP] Owner verified as clinic admin");
|
|
3750
|
+
}
|
|
3751
|
+
} catch (ownerError) {
|
|
3752
|
+
console.error("[CLINIC_GROUP] Error verifying owner:", ownerError);
|
|
3753
|
+
throw ownerError;
|
|
3607
3754
|
}
|
|
3755
|
+
console.log("[CLINIC_GROUP] Generating geohash for location");
|
|
3608
3756
|
if (validatedData.hqLocation) {
|
|
3609
|
-
|
|
3610
|
-
validatedData.hqLocation.
|
|
3611
|
-
|
|
3612
|
-
|
|
3757
|
+
try {
|
|
3758
|
+
validatedData.hqLocation.geohash = geohashForLocation2([
|
|
3759
|
+
validatedData.hqLocation.latitude,
|
|
3760
|
+
validatedData.hqLocation.longitude
|
|
3761
|
+
]);
|
|
3762
|
+
console.log("[CLINIC_GROUP] Geohash generated successfully", {
|
|
3763
|
+
geohash: validatedData.hqLocation.geohash
|
|
3764
|
+
});
|
|
3765
|
+
} catch (geohashError) {
|
|
3766
|
+
console.error("[CLINIC_GROUP] Error generating geohash:", geohashError);
|
|
3767
|
+
throw geohashError;
|
|
3768
|
+
}
|
|
3613
3769
|
}
|
|
3614
3770
|
const now = Timestamp10.now();
|
|
3771
|
+
console.log("[CLINIC_GROUP] Preparing clinic group data object");
|
|
3772
|
+
console.log("[CLINIC_GROUP] Logo value:", {
|
|
3773
|
+
logoValue: validatedData.logo,
|
|
3774
|
+
logoType: validatedData.logo === null ? "null" : typeof validatedData.logo
|
|
3775
|
+
});
|
|
3615
3776
|
const groupData = {
|
|
3616
3777
|
...validatedData,
|
|
3617
3778
|
id: doc7(collection5(db, CLINIC_GROUPS_COLLECTION)).id,
|
|
@@ -3636,17 +3797,63 @@ async function createClinicGroup(db, data, ownerId, isDefault = false, clinicAdm
|
|
|
3636
3797
|
isActive: true
|
|
3637
3798
|
};
|
|
3638
3799
|
try {
|
|
3639
|
-
|
|
3640
|
-
|
|
3641
|
-
|
|
3642
|
-
|
|
3643
|
-
|
|
3800
|
+
console.log("[CLINIC_GROUP] Validating complete clinic group object");
|
|
3801
|
+
try {
|
|
3802
|
+
clinicGroupSchema.parse(groupData);
|
|
3803
|
+
console.log("[CLINIC_GROUP] Clinic group validation passed");
|
|
3804
|
+
} catch (schemaError) {
|
|
3805
|
+
console.error(
|
|
3806
|
+
"[CLINIC_GROUP] Clinic group validation failed:",
|
|
3807
|
+
JSON.stringify(schemaError, null, 2)
|
|
3808
|
+
);
|
|
3809
|
+
throw schemaError;
|
|
3810
|
+
}
|
|
3811
|
+
console.log("[CLINIC_GROUP] Saving clinic group to Firestore", {
|
|
3812
|
+
groupId: groupData.id
|
|
3813
|
+
});
|
|
3814
|
+
try {
|
|
3815
|
+
await setDoc9(doc7(db, CLINIC_GROUPS_COLLECTION, groupData.id), groupData);
|
|
3816
|
+
console.log("[CLINIC_GROUP] Clinic group saved successfully");
|
|
3817
|
+
} catch (firestoreError) {
|
|
3818
|
+
console.error(
|
|
3819
|
+
"[CLINIC_GROUP] Error saving to Firestore:",
|
|
3820
|
+
firestoreError
|
|
3821
|
+
);
|
|
3822
|
+
throw firestoreError;
|
|
3823
|
+
}
|
|
3824
|
+
console.log("[CLINIC_GROUP] Updating clinic admin profile for owner", {
|
|
3825
|
+
ownerId
|
|
3826
|
+
});
|
|
3827
|
+
try {
|
|
3828
|
+
await clinicAdminService.updateClinicAdmin(ownerId, {
|
|
3829
|
+
clinicGroupId: groupData.id,
|
|
3830
|
+
isGroupOwner: true
|
|
3831
|
+
});
|
|
3832
|
+
console.log("[CLINIC_GROUP] Clinic admin profile updated successfully");
|
|
3833
|
+
} catch (updateError) {
|
|
3834
|
+
console.error(
|
|
3835
|
+
"[CLINIC_GROUP] Error updating clinic admin profile:",
|
|
3836
|
+
updateError
|
|
3837
|
+
);
|
|
3838
|
+
throw updateError;
|
|
3839
|
+
}
|
|
3840
|
+
console.log("[CLINIC_GROUP] Clinic group creation completed successfully", {
|
|
3841
|
+
groupId: groupData.id,
|
|
3842
|
+
groupName: groupData.name
|
|
3644
3843
|
});
|
|
3645
3844
|
return groupData;
|
|
3646
3845
|
} catch (error) {
|
|
3647
3846
|
if (error instanceof z13.ZodError) {
|
|
3847
|
+
console.error(
|
|
3848
|
+
"[CLINIC_GROUP] Zod validation error:",
|
|
3849
|
+
JSON.stringify(error.errors, null, 2)
|
|
3850
|
+
);
|
|
3648
3851
|
throw new Error("Invalid clinic group data: " + error.message);
|
|
3649
3852
|
}
|
|
3853
|
+
console.error(
|
|
3854
|
+
"[CLINIC_GROUP] Unhandled error in createClinicGroup:",
|
|
3855
|
+
error
|
|
3856
|
+
);
|
|
3650
3857
|
throw error;
|
|
3651
3858
|
}
|
|
3652
3859
|
}
|
|
@@ -4446,19 +4653,52 @@ var AuthService = class extends BaseService {
|
|
|
4446
4653
|
*/
|
|
4447
4654
|
async signUpClinicAdmin(data) {
|
|
4448
4655
|
try {
|
|
4449
|
-
|
|
4450
|
-
|
|
4451
|
-
|
|
4452
|
-
|
|
4453
|
-
data
|
|
4454
|
-
|
|
4455
|
-
|
|
4456
|
-
|
|
4457
|
-
|
|
4458
|
-
|
|
4459
|
-
|
|
4460
|
-
|
|
4461
|
-
|
|
4656
|
+
console.log("[AUTH] Starting clinic admin signup process", {
|
|
4657
|
+
email: data.email
|
|
4658
|
+
});
|
|
4659
|
+
try {
|
|
4660
|
+
await clinicAdminSignupSchema.parseAsync(data);
|
|
4661
|
+
console.log("[AUTH] Clinic admin signup data validation passed");
|
|
4662
|
+
} catch (validationError) {
|
|
4663
|
+
console.error(
|
|
4664
|
+
"[AUTH] Validation error in signUpClinicAdmin:",
|
|
4665
|
+
validationError
|
|
4666
|
+
);
|
|
4667
|
+
throw validationError;
|
|
4668
|
+
}
|
|
4669
|
+
console.log("[AUTH] Creating Firebase user");
|
|
4670
|
+
let firebaseUser;
|
|
4671
|
+
try {
|
|
4672
|
+
const result = await createUserWithEmailAndPassword(
|
|
4673
|
+
this.auth,
|
|
4674
|
+
data.email,
|
|
4675
|
+
data.password
|
|
4676
|
+
);
|
|
4677
|
+
firebaseUser = result.user;
|
|
4678
|
+
console.log("[AUTH] Firebase user created successfully", {
|
|
4679
|
+
uid: firebaseUser.uid
|
|
4680
|
+
});
|
|
4681
|
+
} catch (firebaseError) {
|
|
4682
|
+
console.error("[AUTH] Firebase user creation failed:", firebaseError);
|
|
4683
|
+
throw firebaseError;
|
|
4684
|
+
}
|
|
4685
|
+
console.log("[AUTH] Creating user with CLINIC_ADMIN role");
|
|
4686
|
+
let user;
|
|
4687
|
+
try {
|
|
4688
|
+
user = await this.userService.createUser(
|
|
4689
|
+
firebaseUser,
|
|
4690
|
+
["clinic_admin" /* CLINIC_ADMIN */],
|
|
4691
|
+
{
|
|
4692
|
+
skipProfileCreation: true
|
|
4693
|
+
}
|
|
4694
|
+
);
|
|
4695
|
+
console.log("[AUTH] User with CLINIC_ADMIN role created successfully", {
|
|
4696
|
+
userId: user.uid
|
|
4697
|
+
});
|
|
4698
|
+
} catch (userCreationError) {
|
|
4699
|
+
console.error("[AUTH] User creation failed:", userCreationError);
|
|
4700
|
+
throw userCreationError;
|
|
4701
|
+
}
|
|
4462
4702
|
const contactPerson = {
|
|
4463
4703
|
firstName: data.firstName,
|
|
4464
4704
|
lastName: data.lastName,
|
|
@@ -4466,6 +4706,8 @@ var AuthService = class extends BaseService {
|
|
|
4466
4706
|
email: data.email,
|
|
4467
4707
|
phoneNumber: data.phoneNumber
|
|
4468
4708
|
};
|
|
4709
|
+
console.log("[AUTH] Contact person object created");
|
|
4710
|
+
console.log("[AUTH] Initializing clinic services");
|
|
4469
4711
|
const clinicAdminService = new ClinicAdminService(
|
|
4470
4712
|
this.db,
|
|
4471
4713
|
this.auth,
|
|
@@ -4485,12 +4727,36 @@ var AuthService = class extends BaseService {
|
|
|
4485
4727
|
clinicAdminService
|
|
4486
4728
|
);
|
|
4487
4729
|
clinicAdminService.setServices(clinicGroupService, clinicService);
|
|
4730
|
+
console.log(
|
|
4731
|
+
"[AUTH] Services initialized and circular dependencies resolved"
|
|
4732
|
+
);
|
|
4488
4733
|
if (data.isCreatingNewGroup) {
|
|
4734
|
+
console.log("[AUTH] Creating new clinic group flow");
|
|
4489
4735
|
if (!data.clinicGroupData) {
|
|
4736
|
+
console.error("[AUTH] Clinic group data is missing");
|
|
4490
4737
|
throw new Error(
|
|
4491
4738
|
"Clinic group data is required when creating a new group"
|
|
4492
4739
|
);
|
|
4493
4740
|
}
|
|
4741
|
+
console.log("[AUTH] Creating clinic admin first");
|
|
4742
|
+
const createClinicAdminData = {
|
|
4743
|
+
userRef: firebaseUser.uid,
|
|
4744
|
+
isGroupOwner: true,
|
|
4745
|
+
clinicsManaged: [],
|
|
4746
|
+
contactInfo: contactPerson,
|
|
4747
|
+
roleTitle: data.title,
|
|
4748
|
+
isActive: true
|
|
4749
|
+
};
|
|
4750
|
+
try {
|
|
4751
|
+
await clinicAdminService.createClinicAdmin(createClinicAdminData);
|
|
4752
|
+
console.log("[AUTH] Clinic admin created successfully");
|
|
4753
|
+
} catch (adminCreationError) {
|
|
4754
|
+
console.error(
|
|
4755
|
+
"[AUTH] Clinic admin creation failed:",
|
|
4756
|
+
adminCreationError
|
|
4757
|
+
);
|
|
4758
|
+
throw adminCreationError;
|
|
4759
|
+
}
|
|
4494
4760
|
const createClinicGroupData = {
|
|
4495
4761
|
name: data.clinicGroupData.name,
|
|
4496
4762
|
hqLocation: data.clinicGroupData.hqLocation,
|
|
@@ -4501,36 +4767,79 @@ var AuthService = class extends BaseService {
|
|
|
4501
4767
|
logo: data.clinicGroupData.logo || null,
|
|
4502
4768
|
subscriptionModel: data.clinicGroupData.subscriptionModel || "no_subscription" /* NO_SUBSCRIPTION */
|
|
4503
4769
|
};
|
|
4504
|
-
|
|
4505
|
-
createClinicGroupData
|
|
4506
|
-
|
|
4507
|
-
|
|
4508
|
-
|
|
4770
|
+
console.log("[AUTH] Clinic group data prepared", {
|
|
4771
|
+
groupName: createClinicGroupData.name
|
|
4772
|
+
});
|
|
4773
|
+
try {
|
|
4774
|
+
await clinicGroupService.createClinicGroup(
|
|
4775
|
+
createClinicGroupData,
|
|
4776
|
+
firebaseUser.uid,
|
|
4777
|
+
true
|
|
4778
|
+
);
|
|
4779
|
+
console.log("[AUTH] Clinic group created successfully");
|
|
4780
|
+
} catch (groupCreationError) {
|
|
4781
|
+
console.error(
|
|
4782
|
+
"[AUTH] Clinic group creation failed:",
|
|
4783
|
+
groupCreationError
|
|
4784
|
+
);
|
|
4785
|
+
throw groupCreationError;
|
|
4786
|
+
}
|
|
4509
4787
|
} else {
|
|
4788
|
+
console.log("[AUTH] Joining existing clinic group flow");
|
|
4510
4789
|
if (!data.inviteToken) {
|
|
4790
|
+
console.error("[AUTH] Invite token is missing");
|
|
4511
4791
|
throw new Error(
|
|
4512
4792
|
"Invite token is required when joining an existing group"
|
|
4513
4793
|
);
|
|
4514
4794
|
}
|
|
4795
|
+
console.log("[AUTH] Invite token provided", {
|
|
4796
|
+
token: data.inviteToken
|
|
4797
|
+
});
|
|
4798
|
+
console.log("[AUTH] Searching for token in clinic groups");
|
|
4515
4799
|
const groupsRef = collection9(this.db, CLINIC_GROUPS_COLLECTION);
|
|
4516
4800
|
const q = query8(groupsRef);
|
|
4517
4801
|
const querySnapshot = await getDocs8(q);
|
|
4518
4802
|
let foundGroup = null;
|
|
4519
4803
|
let foundToken = null;
|
|
4804
|
+
console.log(
|
|
4805
|
+
"[AUTH] Found",
|
|
4806
|
+
querySnapshot.size,
|
|
4807
|
+
"clinic groups to check"
|
|
4808
|
+
);
|
|
4520
4809
|
for (const docSnapshot of querySnapshot.docs) {
|
|
4521
4810
|
const group = docSnapshot.data();
|
|
4522
|
-
|
|
4523
|
-
|
|
4524
|
-
|
|
4811
|
+
console.log("[AUTH] Checking group", {
|
|
4812
|
+
groupId: group.id,
|
|
4813
|
+
groupName: group.name
|
|
4814
|
+
});
|
|
4815
|
+
const token = group.adminTokens.find((t) => {
|
|
4816
|
+
const isMatch = t.token === data.inviteToken && t.status === "active" /* ACTIVE */ && new Date(t.expiresAt.toDate()) > /* @__PURE__ */ new Date();
|
|
4817
|
+
console.log("[AUTH] Checking token", {
|
|
4818
|
+
tokenId: t.id,
|
|
4819
|
+
tokenMatch: t.token === data.inviteToken,
|
|
4820
|
+
tokenStatus: t.status,
|
|
4821
|
+
tokenActive: t.status === "active" /* ACTIVE */,
|
|
4822
|
+
tokenExpiry: new Date(t.expiresAt.toDate()),
|
|
4823
|
+
tokenExpired: new Date(t.expiresAt.toDate()) <= /* @__PURE__ */ new Date(),
|
|
4824
|
+
isMatch
|
|
4825
|
+
});
|
|
4826
|
+
return isMatch;
|
|
4827
|
+
});
|
|
4525
4828
|
if (token) {
|
|
4526
4829
|
foundGroup = group;
|
|
4527
4830
|
foundToken = token;
|
|
4831
|
+
console.log("[AUTH] Found matching token in group", {
|
|
4832
|
+
groupId: group.id,
|
|
4833
|
+
tokenId: token.id
|
|
4834
|
+
});
|
|
4528
4835
|
break;
|
|
4529
4836
|
}
|
|
4530
4837
|
}
|
|
4531
4838
|
if (!foundGroup || !foundToken) {
|
|
4839
|
+
console.error("[AUTH] No valid token found in any clinic group");
|
|
4532
4840
|
throw new Error("Invalid or expired invite token");
|
|
4533
4841
|
}
|
|
4842
|
+
console.log("[AUTH] Creating clinic admin");
|
|
4534
4843
|
const createClinicAdminData = {
|
|
4535
4844
|
userRef: firebaseUser.uid,
|
|
4536
4845
|
clinicGroupId: foundGroup.id,
|
|
@@ -4540,22 +4849,46 @@ var AuthService = class extends BaseService {
|
|
|
4540
4849
|
roleTitle: data.title,
|
|
4541
4850
|
isActive: true
|
|
4542
4851
|
};
|
|
4543
|
-
|
|
4544
|
-
|
|
4545
|
-
|
|
4546
|
-
|
|
4547
|
-
|
|
4548
|
-
|
|
4852
|
+
try {
|
|
4853
|
+
await clinicAdminService.createClinicAdmin(createClinicAdminData);
|
|
4854
|
+
console.log("[AUTH] Clinic admin created successfully");
|
|
4855
|
+
} catch (adminCreationError) {
|
|
4856
|
+
console.error(
|
|
4857
|
+
"[AUTH] Clinic admin creation failed:",
|
|
4858
|
+
adminCreationError
|
|
4859
|
+
);
|
|
4860
|
+
throw adminCreationError;
|
|
4861
|
+
}
|
|
4862
|
+
try {
|
|
4863
|
+
await clinicGroupService.verifyAndUseAdminToken(
|
|
4864
|
+
foundGroup.id,
|
|
4865
|
+
data.inviteToken,
|
|
4866
|
+
firebaseUser.uid
|
|
4867
|
+
);
|
|
4868
|
+
console.log("[AUTH] Token marked as used successfully");
|
|
4869
|
+
} catch (tokenUseError) {
|
|
4870
|
+
console.error("[AUTH] Failed to mark token as used:", tokenUseError);
|
|
4871
|
+
throw tokenUseError;
|
|
4872
|
+
}
|
|
4549
4873
|
}
|
|
4874
|
+
console.log("[AUTH] Clinic admin signup completed successfully", {
|
|
4875
|
+
userId: user.uid
|
|
4876
|
+
});
|
|
4550
4877
|
return user;
|
|
4551
4878
|
} catch (error) {
|
|
4552
4879
|
if (error instanceof z15.ZodError) {
|
|
4880
|
+
console.error(
|
|
4881
|
+
"[AUTH] Zod validation error in signUpClinicAdmin:",
|
|
4882
|
+
JSON.stringify(error.errors, null, 2)
|
|
4883
|
+
);
|
|
4553
4884
|
throw AUTH_ERRORS.VALIDATION_ERROR;
|
|
4554
4885
|
}
|
|
4555
4886
|
const firebaseError = error;
|
|
4556
4887
|
if (firebaseError.code === "auth/email-already-in-use" /* EMAIL_ALREADY_IN_USE */) {
|
|
4888
|
+
console.error("[AUTH] Email already in use:", data.email);
|
|
4557
4889
|
throw AUTH_ERRORS.EMAIL_ALREADY_EXISTS;
|
|
4558
4890
|
}
|
|
4891
|
+
console.error("[AUTH] Unhandled error in signUpClinicAdmin:", error);
|
|
4559
4892
|
throw error;
|
|
4560
4893
|
}
|
|
4561
4894
|
}
|
package/package.json
CHANGED