@blackcode_sa/metaestetics-api 1.14.3 → 1.14.5

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 CHANGED
@@ -8410,6 +8410,18 @@ declare class AuthService extends BaseService {
8410
8410
  user: User;
8411
8411
  practitioner: Practitioner;
8412
8412
  }>;
8413
+ /**
8414
+ * Claims draft practitioner profiles after Google Sign-In.
8415
+ * Uses the current authenticated user (from initial Google Sign-In).
8416
+ *
8417
+ * @param idToken - The Google ID token (used to re-authenticate if needed)
8418
+ * @param practitionerIds - Array of draft practitioner profile IDs to claim
8419
+ * @returns Object containing user and claimed practitioner
8420
+ */
8421
+ claimDraftProfilesWithGoogle(idToken: string, practitionerIds: string[]): Promise<{
8422
+ user: User;
8423
+ practitioner: Practitioner;
8424
+ }>;
8413
8425
  /**
8414
8426
  * Pre-validate all signup data before any mutations
8415
8427
  * Prevents partial creation by catching issues early
package/dist/index.d.ts CHANGED
@@ -8410,6 +8410,18 @@ declare class AuthService extends BaseService {
8410
8410
  user: User;
8411
8411
  practitioner: Practitioner;
8412
8412
  }>;
8413
+ /**
8414
+ * Claims draft practitioner profiles after Google Sign-In.
8415
+ * Uses the current authenticated user (from initial Google Sign-In).
8416
+ *
8417
+ * @param idToken - The Google ID token (used to re-authenticate if needed)
8418
+ * @param practitionerIds - Array of draft practitioner profile IDs to claim
8419
+ * @returns Object containing user and claimed practitioner
8420
+ */
8421
+ claimDraftProfilesWithGoogle(idToken: string, practitionerIds: string[]): Promise<{
8422
+ user: User;
8423
+ practitioner: Practitioner;
8424
+ }>;
8413
8425
  /**
8414
8426
  * Pre-validate all signup data before any mutations
8415
8427
  * Prevents partial creation by catching issues early
package/dist/index.js CHANGED
@@ -15908,6 +15908,84 @@ var AuthService = class extends BaseService {
15908
15908
  throw handleSignupError(error);
15909
15909
  }
15910
15910
  }
15911
+ /**
15912
+ * Claims draft practitioner profiles after Google Sign-In.
15913
+ * Uses the current authenticated user (from initial Google Sign-In).
15914
+ *
15915
+ * @param idToken - The Google ID token (used to re-authenticate if needed)
15916
+ * @param practitionerIds - Array of draft practitioner profile IDs to claim
15917
+ * @returns Object containing user and claimed practitioner
15918
+ */
15919
+ async claimDraftProfilesWithGoogle(idToken, practitionerIds) {
15920
+ try {
15921
+ console.log("[AUTH] Starting claim draft profiles with Google", {
15922
+ practitionerIdsCount: practitionerIds.length,
15923
+ practitionerIds
15924
+ });
15925
+ if (practitionerIds.length === 0) {
15926
+ throw new AuthError("No practitioner profiles selected to claim", "AUTH/NO_PROFILES_SELECTED", 400);
15927
+ }
15928
+ let firebaseUser = this.auth.currentUser;
15929
+ if (!firebaseUser) {
15930
+ console.log("[AUTH] No current user, re-authenticating with Google credential");
15931
+ const credential = import_auth8.GoogleAuthProvider.credential(idToken);
15932
+ const result = await (0, import_auth8.signInWithCredential)(this.auth, credential);
15933
+ firebaseUser = result.user;
15934
+ }
15935
+ console.log("[AUTH] Using Firebase user:", firebaseUser.uid);
15936
+ console.log("[AUTH] Forcing token refresh...");
15937
+ await firebaseUser.getIdToken(true);
15938
+ console.log("[AUTH] Token refreshed successfully");
15939
+ const practitionerService = new PractitionerService(this.db, this.auth, this.app);
15940
+ let user = null;
15941
+ try {
15942
+ user = await this.userService.getUserById(firebaseUser.uid);
15943
+ console.log("[AUTH] User document already exists:", user.uid);
15944
+ } catch (userError) {
15945
+ console.log("[AUTH] User document does not exist, creating it...");
15946
+ }
15947
+ if (!user) {
15948
+ console.log("[AUTH] Creating user document for:", firebaseUser.uid);
15949
+ user = await this.userService.createUser(firebaseUser, ["practitioner" /* PRACTITIONER */], {
15950
+ skipProfileCreation: true
15951
+ });
15952
+ console.log("[AUTH] User document created successfully:", user.uid);
15953
+ }
15954
+ let practitioner;
15955
+ if (practitionerIds.length === 1) {
15956
+ console.log("[AUTH] Claiming single draft profile:", practitionerIds[0]);
15957
+ practitioner = await practitionerService.claimDraftProfileWithGoogle(
15958
+ practitionerIds[0],
15959
+ firebaseUser.uid
15960
+ );
15961
+ } else {
15962
+ console.log("[AUTH] Claiming multiple draft profiles:", practitionerIds);
15963
+ practitioner = await practitionerService.claimMultipleDraftProfilesWithGoogle(
15964
+ practitionerIds,
15965
+ firebaseUser.uid
15966
+ );
15967
+ }
15968
+ console.log("[AUTH] Draft profiles claimed:", practitioner.id);
15969
+ if (!user.practitionerProfile || user.practitionerProfile !== practitioner.id) {
15970
+ console.log("[AUTH] Linking practitioner to user");
15971
+ await this.userService.updateUser(firebaseUser.uid, {
15972
+ practitionerProfile: practitioner.id
15973
+ });
15974
+ }
15975
+ const updatedUser = await this.userService.getUserById(firebaseUser.uid);
15976
+ console.log("[AUTH] Draft profiles claimed successfully", {
15977
+ userId: updatedUser.uid,
15978
+ practitionerId: practitioner.id
15979
+ });
15980
+ return {
15981
+ user: updatedUser,
15982
+ practitioner
15983
+ };
15984
+ } catch (error) {
15985
+ console.error("[AUTH] Error claiming draft profiles with Google:", error);
15986
+ throw handleSignupError(error);
15987
+ }
15988
+ }
15911
15989
  /**
15912
15990
  * Pre-validate all signup data before any mutations
15913
15991
  * Prevents partial creation by catching issues early
package/dist/index.mjs CHANGED
@@ -15995,6 +15995,84 @@ var AuthService = class extends BaseService {
15995
15995
  throw handleSignupError(error);
15996
15996
  }
15997
15997
  }
15998
+ /**
15999
+ * Claims draft practitioner profiles after Google Sign-In.
16000
+ * Uses the current authenticated user (from initial Google Sign-In).
16001
+ *
16002
+ * @param idToken - The Google ID token (used to re-authenticate if needed)
16003
+ * @param practitionerIds - Array of draft practitioner profile IDs to claim
16004
+ * @returns Object containing user and claimed practitioner
16005
+ */
16006
+ async claimDraftProfilesWithGoogle(idToken, practitionerIds) {
16007
+ try {
16008
+ console.log("[AUTH] Starting claim draft profiles with Google", {
16009
+ practitionerIdsCount: practitionerIds.length,
16010
+ practitionerIds
16011
+ });
16012
+ if (practitionerIds.length === 0) {
16013
+ throw new AuthError("No practitioner profiles selected to claim", "AUTH/NO_PROFILES_SELECTED", 400);
16014
+ }
16015
+ let firebaseUser = this.auth.currentUser;
16016
+ if (!firebaseUser) {
16017
+ console.log("[AUTH] No current user, re-authenticating with Google credential");
16018
+ const credential = GoogleAuthProvider.credential(idToken);
16019
+ const result = await signInWithCredential(this.auth, credential);
16020
+ firebaseUser = result.user;
16021
+ }
16022
+ console.log("[AUTH] Using Firebase user:", firebaseUser.uid);
16023
+ console.log("[AUTH] Forcing token refresh...");
16024
+ await firebaseUser.getIdToken(true);
16025
+ console.log("[AUTH] Token refreshed successfully");
16026
+ const practitionerService = new PractitionerService(this.db, this.auth, this.app);
16027
+ let user = null;
16028
+ try {
16029
+ user = await this.userService.getUserById(firebaseUser.uid);
16030
+ console.log("[AUTH] User document already exists:", user.uid);
16031
+ } catch (userError) {
16032
+ console.log("[AUTH] User document does not exist, creating it...");
16033
+ }
16034
+ if (!user) {
16035
+ console.log("[AUTH] Creating user document for:", firebaseUser.uid);
16036
+ user = await this.userService.createUser(firebaseUser, ["practitioner" /* PRACTITIONER */], {
16037
+ skipProfileCreation: true
16038
+ });
16039
+ console.log("[AUTH] User document created successfully:", user.uid);
16040
+ }
16041
+ let practitioner;
16042
+ if (practitionerIds.length === 1) {
16043
+ console.log("[AUTH] Claiming single draft profile:", practitionerIds[0]);
16044
+ practitioner = await practitionerService.claimDraftProfileWithGoogle(
16045
+ practitionerIds[0],
16046
+ firebaseUser.uid
16047
+ );
16048
+ } else {
16049
+ console.log("[AUTH] Claiming multiple draft profiles:", practitionerIds);
16050
+ practitioner = await practitionerService.claimMultipleDraftProfilesWithGoogle(
16051
+ practitionerIds,
16052
+ firebaseUser.uid
16053
+ );
16054
+ }
16055
+ console.log("[AUTH] Draft profiles claimed:", practitioner.id);
16056
+ if (!user.practitionerProfile || user.practitionerProfile !== practitioner.id) {
16057
+ console.log("[AUTH] Linking practitioner to user");
16058
+ await this.userService.updateUser(firebaseUser.uid, {
16059
+ practitionerProfile: practitioner.id
16060
+ });
16061
+ }
16062
+ const updatedUser = await this.userService.getUserById(firebaseUser.uid);
16063
+ console.log("[AUTH] Draft profiles claimed successfully", {
16064
+ userId: updatedUser.uid,
16065
+ practitionerId: practitioner.id
16066
+ });
16067
+ return {
16068
+ user: updatedUser,
16069
+ practitioner
16070
+ };
16071
+ } catch (error) {
16072
+ console.error("[AUTH] Error claiming draft profiles with Google:", error);
16073
+ throw handleSignupError(error);
16074
+ }
16075
+ }
15998
16076
  /**
15999
16077
  * Pre-validate all signup data before any mutations
16000
16078
  * Prevents partial creation by catching issues early
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@blackcode_sa/metaestetics-api",
3
3
  "private": false,
4
- "version": "1.14.3",
4
+ "version": "1.14.5",
5
5
  "description": "Firebase authentication service with anonymous upgrade support",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",
@@ -811,6 +811,109 @@ export class AuthService extends BaseService {
811
811
  }
812
812
  }
813
813
 
814
+ /**
815
+ * Claims draft practitioner profiles after Google Sign-In.
816
+ * Uses the current authenticated user (from initial Google Sign-In).
817
+ *
818
+ * @param idToken - The Google ID token (used to re-authenticate if needed)
819
+ * @param practitionerIds - Array of draft practitioner profile IDs to claim
820
+ * @returns Object containing user and claimed practitioner
821
+ */
822
+ async claimDraftProfilesWithGoogle(
823
+ idToken: string,
824
+ practitionerIds: string[]
825
+ ): Promise<{
826
+ user: User;
827
+ practitioner: Practitioner;
828
+ }> {
829
+ try {
830
+ console.log('[AUTH] Starting claim draft profiles with Google', {
831
+ practitionerIdsCount: practitionerIds.length,
832
+ practitionerIds,
833
+ });
834
+
835
+ if (practitionerIds.length === 0) {
836
+ throw new AuthError('No practitioner profiles selected to claim', 'AUTH/NO_PROFILES_SELECTED', 400);
837
+ }
838
+
839
+ // Check if user is currently signed in, if not re-authenticate
840
+ let firebaseUser = this.auth.currentUser;
841
+ if (!firebaseUser) {
842
+ console.log('[AUTH] No current user, re-authenticating with Google credential');
843
+ const credential = GoogleAuthProvider.credential(idToken);
844
+ const result = await signInWithCredential(this.auth, credential);
845
+ firebaseUser = result.user;
846
+ }
847
+ console.log('[AUTH] Using Firebase user:', firebaseUser.uid);
848
+
849
+ // Force token refresh to ensure Firestore has fresh auth context
850
+ console.log('[AUTH] Forcing token refresh...');
851
+ await firebaseUser.getIdToken(true);
852
+ console.log('[AUTH] Token refreshed successfully');
853
+
854
+ const practitionerService = new PractitionerService(this.db, this.auth, this.app);
855
+
856
+ // Step 1: Check if User document already exists
857
+ let user: User | null = null;
858
+ try {
859
+ user = await this.userService.getUserById(firebaseUser.uid);
860
+ console.log('[AUTH] User document already exists:', user.uid);
861
+ } catch (userError) {
862
+ console.log('[AUTH] User document does not exist, creating it...');
863
+ }
864
+
865
+ // Step 2: Create User document if it doesn't exist (NOT in transaction - matches token flow)
866
+ if (!user) {
867
+ console.log('[AUTH] Creating user document for:', firebaseUser.uid);
868
+ user = await this.userService.createUser(firebaseUser, [UserRole.PRACTITIONER], {
869
+ skipProfileCreation: true,
870
+ });
871
+ console.log('[AUTH] User document created successfully:', user.uid);
872
+ }
873
+
874
+ // Step 3: Claim the draft profiles
875
+ let practitioner: Practitioner;
876
+ if (practitionerIds.length === 1) {
877
+ console.log('[AUTH] Claiming single draft profile:', practitionerIds[0]);
878
+ practitioner = await practitionerService.claimDraftProfileWithGoogle(
879
+ practitionerIds[0],
880
+ firebaseUser.uid
881
+ );
882
+ } else {
883
+ console.log('[AUTH] Claiming multiple draft profiles:', practitionerIds);
884
+ practitioner = await practitionerService.claimMultipleDraftProfilesWithGoogle(
885
+ practitionerIds,
886
+ firebaseUser.uid
887
+ );
888
+ }
889
+ console.log('[AUTH] Draft profiles claimed:', practitioner.id);
890
+
891
+ // Step 4: Link practitioner to user
892
+ if (!user.practitionerProfile || user.practitionerProfile !== practitioner.id) {
893
+ console.log('[AUTH] Linking practitioner to user');
894
+ await this.userService.updateUser(firebaseUser.uid, {
895
+ practitionerProfile: practitioner.id,
896
+ });
897
+ }
898
+
899
+ // Fetch updated user (with practitionerProfile reference)
900
+ const updatedUser = await this.userService.getUserById(firebaseUser.uid);
901
+
902
+ console.log('[AUTH] Draft profiles claimed successfully', {
903
+ userId: updatedUser.uid,
904
+ practitionerId: practitioner.id,
905
+ });
906
+
907
+ return {
908
+ user: updatedUser,
909
+ practitioner,
910
+ };
911
+ } catch (error: any) {
912
+ console.error('[AUTH] Error claiming draft profiles with Google:', error);
913
+ throw handleSignupError(error);
914
+ }
915
+ }
916
+
814
917
  /**
815
918
  * Pre-validate all signup data before any mutations
816
919
  * Prevents partial creation by catching issues early