@blackcode_sa/metaestetics-api 1.4.5 → 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.d.mts +92 -92
- package/dist/index.d.ts +92 -92
- package/dist/index.js +380 -75
- package/dist/index.mjs +380 -75
- package/package.json +1 -1
- package/src/services/auth.service.ts +139 -29
- package/src/services/clinic/utils/admin.utils.ts +155 -34
- package/src/services/clinic/utils/clinic-group.utils.ts +99 -12
- package/src/types/clinic/index.ts +4 -4
- package/src/validations/clinic.schema.ts +5 -5
package/dist/index.mjs
CHANGED
|
@@ -2283,9 +2283,9 @@ var clinicTagsSchema = z9.object({
|
|
|
2283
2283
|
var contactPersonSchema = z9.object({
|
|
2284
2284
|
firstName: z9.string(),
|
|
2285
2285
|
lastName: z9.string(),
|
|
2286
|
-
title: z9.string(),
|
|
2286
|
+
title: z9.string().nullable().optional(),
|
|
2287
2287
|
email: z9.string().email(),
|
|
2288
|
-
phoneNumber: z9.string()
|
|
2288
|
+
phoneNumber: z9.string().nullable().optional()
|
|
2289
2289
|
});
|
|
2290
2290
|
var adminInfoSchema = z9.object({
|
|
2291
2291
|
id: z9.string(),
|
|
@@ -2381,7 +2381,7 @@ var clinicGroupSchema = z9.object({
|
|
|
2381
2381
|
updatedAt: z9.instanceof(Date).or(z9.instanceof(Timestamp5)),
|
|
2382
2382
|
// Timestamp
|
|
2383
2383
|
isActive: z9.boolean(),
|
|
2384
|
-
logo: z9.string().optional(),
|
|
2384
|
+
logo: z9.string().optional().nullable(),
|
|
2385
2385
|
practiceType: z9.nativeEnum(PracticeType).optional(),
|
|
2386
2386
|
languages: z9.array(z9.nativeEnum(Language)).optional(),
|
|
2387
2387
|
subscriptionModel: z9.nativeEnum(SubscriptionModel),
|
|
@@ -2453,7 +2453,7 @@ var createClinicGroupSchema = z9.object({
|
|
|
2453
2453
|
contactPerson: contactPersonSchema,
|
|
2454
2454
|
ownerId: z9.string(),
|
|
2455
2455
|
isActive: z9.boolean(),
|
|
2456
|
-
logo: z9.string().optional(),
|
|
2456
|
+
logo: z9.string().optional().nullable(),
|
|
2457
2457
|
practiceType: z9.nativeEnum(PracticeType).optional(),
|
|
2458
2458
|
languages: z9.array(z9.nativeEnum(Language)).optional(),
|
|
2459
2459
|
subscriptionModel: z9.nativeEnum(SubscriptionModel).optional().default("no_subscription" /* NO_SUBSCRIPTION */),
|
|
@@ -2490,7 +2490,7 @@ var createDefaultClinicGroupSchema = z9.object({
|
|
|
2490
2490
|
contactInfo: clinicContactInfoSchema,
|
|
2491
2491
|
hqLocation: clinicLocationSchema,
|
|
2492
2492
|
isActive: z9.boolean(),
|
|
2493
|
-
logo: z9.string().optional(),
|
|
2493
|
+
logo: z9.string().optional().nullable(),
|
|
2494
2494
|
practiceType: z9.nativeEnum(PracticeType).optional(),
|
|
2495
2495
|
languages: z9.array(z9.nativeEnum(Language)).optional(),
|
|
2496
2496
|
subscriptionModel: z9.nativeEnum(SubscriptionModel).optional().default("no_subscription" /* NO_SUBSCRIPTION */)
|
|
@@ -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,54 @@ 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
|
+
const owner = await clinicAdminService.getClinicAdmin(ownerId);
|
|
3735
|
+
if (!owner) {
|
|
3736
|
+
console.error("[CLINIC_GROUP] Owner not found or is not a clinic admin", {
|
|
3737
|
+
ownerId
|
|
3738
|
+
});
|
|
3739
|
+
throw new Error("Owner not found or is not a clinic admin");
|
|
3740
|
+
}
|
|
3741
|
+
console.log("[CLINIC_GROUP] Owner verified as clinic admin");
|
|
3742
|
+
} catch (ownerError) {
|
|
3743
|
+
console.error("[CLINIC_GROUP] Error verifying owner:", ownerError);
|
|
3744
|
+
throw ownerError;
|
|
3607
3745
|
}
|
|
3746
|
+
console.log("[CLINIC_GROUP] Generating geohash for location");
|
|
3608
3747
|
if (validatedData.hqLocation) {
|
|
3609
|
-
|
|
3610
|
-
validatedData.hqLocation.
|
|
3611
|
-
|
|
3612
|
-
|
|
3748
|
+
try {
|
|
3749
|
+
validatedData.hqLocation.geohash = geohashForLocation2([
|
|
3750
|
+
validatedData.hqLocation.latitude,
|
|
3751
|
+
validatedData.hqLocation.longitude
|
|
3752
|
+
]);
|
|
3753
|
+
console.log("[CLINIC_GROUP] Geohash generated successfully", {
|
|
3754
|
+
geohash: validatedData.hqLocation.geohash
|
|
3755
|
+
});
|
|
3756
|
+
} catch (geohashError) {
|
|
3757
|
+
console.error("[CLINIC_GROUP] Error generating geohash:", geohashError);
|
|
3758
|
+
throw geohashError;
|
|
3759
|
+
}
|
|
3613
3760
|
}
|
|
3614
3761
|
const now = Timestamp10.now();
|
|
3762
|
+
console.log("[CLINIC_GROUP] Preparing clinic group data object");
|
|
3763
|
+
console.log("[CLINIC_GROUP] Logo value:", {
|
|
3764
|
+
logoValue: validatedData.logo,
|
|
3765
|
+
logoType: validatedData.logo === null ? "null" : typeof validatedData.logo
|
|
3766
|
+
});
|
|
3615
3767
|
const groupData = {
|
|
3616
3768
|
...validatedData,
|
|
3617
3769
|
id: doc7(collection5(db, CLINIC_GROUPS_COLLECTION)).id,
|
|
@@ -3636,17 +3788,63 @@ async function createClinicGroup(db, data, ownerId, isDefault = false, clinicAdm
|
|
|
3636
3788
|
isActive: true
|
|
3637
3789
|
};
|
|
3638
3790
|
try {
|
|
3639
|
-
|
|
3640
|
-
|
|
3641
|
-
|
|
3642
|
-
|
|
3643
|
-
|
|
3791
|
+
console.log("[CLINIC_GROUP] Validating complete clinic group object");
|
|
3792
|
+
try {
|
|
3793
|
+
clinicGroupSchema.parse(groupData);
|
|
3794
|
+
console.log("[CLINIC_GROUP] Clinic group validation passed");
|
|
3795
|
+
} catch (schemaError) {
|
|
3796
|
+
console.error(
|
|
3797
|
+
"[CLINIC_GROUP] Clinic group validation failed:",
|
|
3798
|
+
JSON.stringify(schemaError, null, 2)
|
|
3799
|
+
);
|
|
3800
|
+
throw schemaError;
|
|
3801
|
+
}
|
|
3802
|
+
console.log("[CLINIC_GROUP] Saving clinic group to Firestore", {
|
|
3803
|
+
groupId: groupData.id
|
|
3804
|
+
});
|
|
3805
|
+
try {
|
|
3806
|
+
await setDoc9(doc7(db, CLINIC_GROUPS_COLLECTION, groupData.id), groupData);
|
|
3807
|
+
console.log("[CLINIC_GROUP] Clinic group saved successfully");
|
|
3808
|
+
} catch (firestoreError) {
|
|
3809
|
+
console.error(
|
|
3810
|
+
"[CLINIC_GROUP] Error saving to Firestore:",
|
|
3811
|
+
firestoreError
|
|
3812
|
+
);
|
|
3813
|
+
throw firestoreError;
|
|
3814
|
+
}
|
|
3815
|
+
console.log("[CLINIC_GROUP] Updating clinic admin profile for owner", {
|
|
3816
|
+
ownerId
|
|
3817
|
+
});
|
|
3818
|
+
try {
|
|
3819
|
+
await clinicAdminService.updateClinicAdmin(ownerId, {
|
|
3820
|
+
clinicGroupId: groupData.id,
|
|
3821
|
+
isGroupOwner: true
|
|
3822
|
+
});
|
|
3823
|
+
console.log("[CLINIC_GROUP] Clinic admin profile updated successfully");
|
|
3824
|
+
} catch (updateError) {
|
|
3825
|
+
console.error(
|
|
3826
|
+
"[CLINIC_GROUP] Error updating clinic admin profile:",
|
|
3827
|
+
updateError
|
|
3828
|
+
);
|
|
3829
|
+
throw updateError;
|
|
3830
|
+
}
|
|
3831
|
+
console.log("[CLINIC_GROUP] Clinic group creation completed successfully", {
|
|
3832
|
+
groupId: groupData.id,
|
|
3833
|
+
groupName: groupData.name
|
|
3644
3834
|
});
|
|
3645
3835
|
return groupData;
|
|
3646
3836
|
} catch (error) {
|
|
3647
3837
|
if (error instanceof z13.ZodError) {
|
|
3838
|
+
console.error(
|
|
3839
|
+
"[CLINIC_GROUP] Zod validation error:",
|
|
3840
|
+
JSON.stringify(error.errors, null, 2)
|
|
3841
|
+
);
|
|
3648
3842
|
throw new Error("Invalid clinic group data: " + error.message);
|
|
3649
3843
|
}
|
|
3844
|
+
console.error(
|
|
3845
|
+
"[CLINIC_GROUP] Unhandled error in createClinicGroup:",
|
|
3846
|
+
error
|
|
3847
|
+
);
|
|
3650
3848
|
throw error;
|
|
3651
3849
|
}
|
|
3652
3850
|
}
|
|
@@ -4446,19 +4644,52 @@ var AuthService = class extends BaseService {
|
|
|
4446
4644
|
*/
|
|
4447
4645
|
async signUpClinicAdmin(data) {
|
|
4448
4646
|
try {
|
|
4449
|
-
|
|
4450
|
-
|
|
4451
|
-
|
|
4452
|
-
|
|
4453
|
-
data
|
|
4454
|
-
|
|
4455
|
-
|
|
4456
|
-
|
|
4457
|
-
|
|
4458
|
-
|
|
4459
|
-
|
|
4460
|
-
|
|
4461
|
-
|
|
4647
|
+
console.log("[AUTH] Starting clinic admin signup process", {
|
|
4648
|
+
email: data.email
|
|
4649
|
+
});
|
|
4650
|
+
try {
|
|
4651
|
+
await clinicAdminSignupSchema.parseAsync(data);
|
|
4652
|
+
console.log("[AUTH] Clinic admin signup data validation passed");
|
|
4653
|
+
} catch (validationError) {
|
|
4654
|
+
console.error(
|
|
4655
|
+
"[AUTH] Validation error in signUpClinicAdmin:",
|
|
4656
|
+
validationError
|
|
4657
|
+
);
|
|
4658
|
+
throw validationError;
|
|
4659
|
+
}
|
|
4660
|
+
console.log("[AUTH] Creating Firebase user");
|
|
4661
|
+
let firebaseUser;
|
|
4662
|
+
try {
|
|
4663
|
+
const result = await createUserWithEmailAndPassword(
|
|
4664
|
+
this.auth,
|
|
4665
|
+
data.email,
|
|
4666
|
+
data.password
|
|
4667
|
+
);
|
|
4668
|
+
firebaseUser = result.user;
|
|
4669
|
+
console.log("[AUTH] Firebase user created successfully", {
|
|
4670
|
+
uid: firebaseUser.uid
|
|
4671
|
+
});
|
|
4672
|
+
} catch (firebaseError) {
|
|
4673
|
+
console.error("[AUTH] Firebase user creation failed:", firebaseError);
|
|
4674
|
+
throw firebaseError;
|
|
4675
|
+
}
|
|
4676
|
+
console.log("[AUTH] Creating user with CLINIC_ADMIN role");
|
|
4677
|
+
let user;
|
|
4678
|
+
try {
|
|
4679
|
+
user = await this.userService.createUser(
|
|
4680
|
+
firebaseUser,
|
|
4681
|
+
["clinic_admin" /* CLINIC_ADMIN */],
|
|
4682
|
+
{
|
|
4683
|
+
skipProfileCreation: true
|
|
4684
|
+
}
|
|
4685
|
+
);
|
|
4686
|
+
console.log("[AUTH] User with CLINIC_ADMIN role created successfully", {
|
|
4687
|
+
userId: user.uid
|
|
4688
|
+
});
|
|
4689
|
+
} catch (userCreationError) {
|
|
4690
|
+
console.error("[AUTH] User creation failed:", userCreationError);
|
|
4691
|
+
throw userCreationError;
|
|
4692
|
+
}
|
|
4462
4693
|
const contactPerson = {
|
|
4463
4694
|
firstName: data.firstName,
|
|
4464
4695
|
lastName: data.lastName,
|
|
@@ -4466,6 +4697,8 @@ var AuthService = class extends BaseService {
|
|
|
4466
4697
|
email: data.email,
|
|
4467
4698
|
phoneNumber: data.phoneNumber
|
|
4468
4699
|
};
|
|
4700
|
+
console.log("[AUTH] Contact person object created");
|
|
4701
|
+
console.log("[AUTH] Initializing clinic services");
|
|
4469
4702
|
const clinicAdminService = new ClinicAdminService(
|
|
4470
4703
|
this.db,
|
|
4471
4704
|
this.auth,
|
|
@@ -4485,8 +4718,13 @@ var AuthService = class extends BaseService {
|
|
|
4485
4718
|
clinicAdminService
|
|
4486
4719
|
);
|
|
4487
4720
|
clinicAdminService.setServices(clinicGroupService, clinicService);
|
|
4721
|
+
console.log(
|
|
4722
|
+
"[AUTH] Services initialized and circular dependencies resolved"
|
|
4723
|
+
);
|
|
4488
4724
|
if (data.isCreatingNewGroup) {
|
|
4725
|
+
console.log("[AUTH] Creating new clinic group flow");
|
|
4489
4726
|
if (!data.clinicGroupData) {
|
|
4727
|
+
console.error("[AUTH] Clinic group data is missing");
|
|
4490
4728
|
throw new Error(
|
|
4491
4729
|
"Clinic group data is required when creating a new group"
|
|
4492
4730
|
);
|
|
@@ -4498,39 +4736,82 @@ var AuthService = class extends BaseService {
|
|
|
4498
4736
|
contactPerson,
|
|
4499
4737
|
ownerId: firebaseUser.uid,
|
|
4500
4738
|
isActive: true,
|
|
4501
|
-
logo: data.clinicGroupData.logo,
|
|
4739
|
+
logo: data.clinicGroupData.logo || null,
|
|
4502
4740
|
subscriptionModel: data.clinicGroupData.subscriptionModel || "no_subscription" /* NO_SUBSCRIPTION */
|
|
4503
4741
|
};
|
|
4504
|
-
|
|
4505
|
-
createClinicGroupData
|
|
4506
|
-
|
|
4507
|
-
|
|
4508
|
-
|
|
4742
|
+
console.log("[AUTH] Clinic group data prepared", {
|
|
4743
|
+
groupName: createClinicGroupData.name
|
|
4744
|
+
});
|
|
4745
|
+
try {
|
|
4746
|
+
await clinicGroupService.createClinicGroup(
|
|
4747
|
+
createClinicGroupData,
|
|
4748
|
+
firebaseUser.uid,
|
|
4749
|
+
true
|
|
4750
|
+
);
|
|
4751
|
+
console.log("[AUTH] Clinic group created successfully");
|
|
4752
|
+
} catch (groupCreationError) {
|
|
4753
|
+
console.error(
|
|
4754
|
+
"[AUTH] Clinic group creation failed:",
|
|
4755
|
+
groupCreationError
|
|
4756
|
+
);
|
|
4757
|
+
throw groupCreationError;
|
|
4758
|
+
}
|
|
4509
4759
|
} else {
|
|
4760
|
+
console.log("[AUTH] Joining existing clinic group flow");
|
|
4510
4761
|
if (!data.inviteToken) {
|
|
4762
|
+
console.error("[AUTH] Invite token is missing");
|
|
4511
4763
|
throw new Error(
|
|
4512
4764
|
"Invite token is required when joining an existing group"
|
|
4513
4765
|
);
|
|
4514
4766
|
}
|
|
4767
|
+
console.log("[AUTH] Invite token provided", {
|
|
4768
|
+
token: data.inviteToken
|
|
4769
|
+
});
|
|
4770
|
+
console.log("[AUTH] Searching for token in clinic groups");
|
|
4515
4771
|
const groupsRef = collection9(this.db, CLINIC_GROUPS_COLLECTION);
|
|
4516
4772
|
const q = query8(groupsRef);
|
|
4517
4773
|
const querySnapshot = await getDocs8(q);
|
|
4518
4774
|
let foundGroup = null;
|
|
4519
4775
|
let foundToken = null;
|
|
4776
|
+
console.log(
|
|
4777
|
+
"[AUTH] Found",
|
|
4778
|
+
querySnapshot.size,
|
|
4779
|
+
"clinic groups to check"
|
|
4780
|
+
);
|
|
4520
4781
|
for (const docSnapshot of querySnapshot.docs) {
|
|
4521
4782
|
const group = docSnapshot.data();
|
|
4522
|
-
|
|
4523
|
-
|
|
4524
|
-
|
|
4783
|
+
console.log("[AUTH] Checking group", {
|
|
4784
|
+
groupId: group.id,
|
|
4785
|
+
groupName: group.name
|
|
4786
|
+
});
|
|
4787
|
+
const token = group.adminTokens.find((t) => {
|
|
4788
|
+
const isMatch = t.token === data.inviteToken && t.status === "active" /* ACTIVE */ && new Date(t.expiresAt.toDate()) > /* @__PURE__ */ new Date();
|
|
4789
|
+
console.log("[AUTH] Checking token", {
|
|
4790
|
+
tokenId: t.id,
|
|
4791
|
+
tokenMatch: t.token === data.inviteToken,
|
|
4792
|
+
tokenStatus: t.status,
|
|
4793
|
+
tokenActive: t.status === "active" /* ACTIVE */,
|
|
4794
|
+
tokenExpiry: new Date(t.expiresAt.toDate()),
|
|
4795
|
+
tokenExpired: new Date(t.expiresAt.toDate()) <= /* @__PURE__ */ new Date(),
|
|
4796
|
+
isMatch
|
|
4797
|
+
});
|
|
4798
|
+
return isMatch;
|
|
4799
|
+
});
|
|
4525
4800
|
if (token) {
|
|
4526
4801
|
foundGroup = group;
|
|
4527
4802
|
foundToken = token;
|
|
4803
|
+
console.log("[AUTH] Found matching token in group", {
|
|
4804
|
+
groupId: group.id,
|
|
4805
|
+
tokenId: token.id
|
|
4806
|
+
});
|
|
4528
4807
|
break;
|
|
4529
4808
|
}
|
|
4530
4809
|
}
|
|
4531
4810
|
if (!foundGroup || !foundToken) {
|
|
4811
|
+
console.error("[AUTH] No valid token found in any clinic group");
|
|
4532
4812
|
throw new Error("Invalid or expired invite token");
|
|
4533
4813
|
}
|
|
4814
|
+
console.log("[AUTH] Creating clinic admin");
|
|
4534
4815
|
const createClinicAdminData = {
|
|
4535
4816
|
userRef: firebaseUser.uid,
|
|
4536
4817
|
clinicGroupId: foundGroup.id,
|
|
@@ -4540,22 +4821,46 @@ var AuthService = class extends BaseService {
|
|
|
4540
4821
|
roleTitle: data.title,
|
|
4541
4822
|
isActive: true
|
|
4542
4823
|
};
|
|
4543
|
-
|
|
4544
|
-
|
|
4545
|
-
|
|
4546
|
-
|
|
4547
|
-
|
|
4548
|
-
|
|
4824
|
+
try {
|
|
4825
|
+
await clinicAdminService.createClinicAdmin(createClinicAdminData);
|
|
4826
|
+
console.log("[AUTH] Clinic admin created successfully");
|
|
4827
|
+
} catch (adminCreationError) {
|
|
4828
|
+
console.error(
|
|
4829
|
+
"[AUTH] Clinic admin creation failed:",
|
|
4830
|
+
adminCreationError
|
|
4831
|
+
);
|
|
4832
|
+
throw adminCreationError;
|
|
4833
|
+
}
|
|
4834
|
+
try {
|
|
4835
|
+
await clinicGroupService.verifyAndUseAdminToken(
|
|
4836
|
+
foundGroup.id,
|
|
4837
|
+
data.inviteToken,
|
|
4838
|
+
firebaseUser.uid
|
|
4839
|
+
);
|
|
4840
|
+
console.log("[AUTH] Token marked as used successfully");
|
|
4841
|
+
} catch (tokenUseError) {
|
|
4842
|
+
console.error("[AUTH] Failed to mark token as used:", tokenUseError);
|
|
4843
|
+
throw tokenUseError;
|
|
4844
|
+
}
|
|
4549
4845
|
}
|
|
4846
|
+
console.log("[AUTH] Clinic admin signup completed successfully", {
|
|
4847
|
+
userId: user.uid
|
|
4848
|
+
});
|
|
4550
4849
|
return user;
|
|
4551
4850
|
} catch (error) {
|
|
4552
4851
|
if (error instanceof z15.ZodError) {
|
|
4852
|
+
console.error(
|
|
4853
|
+
"[AUTH] Zod validation error in signUpClinicAdmin:",
|
|
4854
|
+
JSON.stringify(error.errors, null, 2)
|
|
4855
|
+
);
|
|
4553
4856
|
throw AUTH_ERRORS.VALIDATION_ERROR;
|
|
4554
4857
|
}
|
|
4555
4858
|
const firebaseError = error;
|
|
4556
4859
|
if (firebaseError.code === "auth/email-already-in-use" /* EMAIL_ALREADY_IN_USE */) {
|
|
4860
|
+
console.error("[AUTH] Email already in use:", data.email);
|
|
4557
4861
|
throw AUTH_ERRORS.EMAIL_ALREADY_EXISTS;
|
|
4558
4862
|
}
|
|
4863
|
+
console.error("[AUTH] Unhandled error in signUpClinicAdmin:", error);
|
|
4559
4864
|
throw error;
|
|
4560
4865
|
}
|
|
4561
4866
|
}
|
package/package.json
CHANGED