@@ -12,7 +12,9 @@ import GoogleProvider from "next-auth/providers/google";
1212import { updateProfilePhotoGoogle } from "@calcom/app-store/_utils/oauth/updateProfilePhotoGoogle" ;
1313import GoogleCalendarService from "@calcom/app-store/googlecalendar/lib/CalendarService" ;
1414import { LicenseKeySingleton } from "@calcom/ee/common/server/LicenseKeyService" ;
15+ import { getBillingProviderService } from "@calcom/features/ee/billing/di/containers/Billing" ;
1516import { CredentialRepository } from "@calcom/features/credentials/repositories/CredentialRepository" ;
17+ import type { TrackingData } from "@calcom/lib/tracking" ;
1618import { DeploymentRepository } from "@calcom/features/ee/deployment/repositories/DeploymentRepository" ;
1719import createUsersAndConnectToOrg from "@calcom/features/ee/dsync/lib/users/createUsersAndConnectToOrg" ;
1820import ImpersonationProvider from "@calcom/features/ee/impersonation/lib/ImpersonationProvider" ;
@@ -456,9 +458,12 @@ const mapIdentityProvider = (providerName: string) => {
456458
457459export const getOptions = ( {
458460 getDubId,
461+ getTrackingData,
459462} : {
460463 /** so we can extract the Dub cookie in both pages and app routers */
461464 getDubId : ( ) => string | undefined ;
465+ /** Ad tracking data for Stripe customer metadata */
466+ getTrackingData : ( ) => TrackingData ;
462467} ) : AuthOptions => ( {
463468 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
464469 // @ts -ignore
@@ -596,14 +601,14 @@ export const getOptions = ({
596601 org :
597602 profileOrg && ! profileOrg . isPlatform
598603 ? {
599- id : profileOrg . id ,
600- name : profileOrg . name ,
601- slug : profileOrg . slug ?? profileOrg . requestedSlug ?? "" ,
602- logoUrl : profileOrg . logoUrl ,
603- fullDomain : getOrgFullOrigin ( profileOrg . slug ?? profileOrg . requestedSlug ?? "" ) ,
604- domainSuffix : subdomainSuffix ( ) ,
605- role : orgRole as MembershipRole , // It can't be undefined if we have a profileOrg
606- }
604+ id : profileOrg . id ,
605+ name : profileOrg . name ,
606+ slug : profileOrg . slug ?? profileOrg . requestedSlug ?? "" ,
607+ logoUrl : profileOrg . logoUrl ,
608+ fullDomain : getOrgFullOrigin ( profileOrg . slug ?? profileOrg . requestedSlug ?? "" ) ,
609+ domainSuffix : subdomainSuffix ( ) ,
610+ role : orgRole as MembershipRole , // It can't be undefined if we have a profileOrg
611+ }
607612 : null ,
608613 } as JWT ;
609614 } ;
@@ -1066,11 +1071,12 @@ export const getOptions = ({
10661071 const { orgUsername, orgId } = await checkIfUserShouldBelongToOrg ( idP , user . email ) ;
10671072
10681073 try {
1074+ const newUsername = orgId ? slugify ( orgUsername ) : usernameSlug ( user . name ) ;
10691075 const newUser = await prisma . user . create ( {
10701076 data : {
10711077 // Slugify the incoming name and append a few random characters to
10721078 // prevent conflicts for users with the same name.
1073- username : orgId ? slugify ( orgUsername ) : usernameSlug ( user . name ) ,
1079+ username : newUsername ,
10741080 emailVerified : new Date ( Date . now ( ) ) ,
10751081 name : user . name ,
10761082 ...( user . image && { avatarUrl : user . image } ) ,
@@ -1094,6 +1100,40 @@ export const getOptions = ({
10941100 ) ;
10951101 await calcomAdapter . linkAccount ( linkAccountNewUserData ) ;
10961102
1103+ waitUntil (
1104+ ( async ( ) => {
1105+ try {
1106+ const tracking = getTrackingData ( ) ;
1107+ const billingService = getBillingProviderService ( ) ;
1108+ const customer = await billingService . createCustomer ( {
1109+ email : newUser . email ,
1110+ metadata : {
1111+ email : newUser . email ,
1112+ username : newUser . username ?? newUsername ,
1113+ ...( tracking . googleAds ?. gclid && {
1114+ gclid : tracking . googleAds . gclid ,
1115+ campaignId : tracking . googleAds . campaignId ,
1116+ } ) ,
1117+ ...( tracking . linkedInAds ?. liFatId && {
1118+ liFatId : tracking . linkedInAds . liFatId ,
1119+ linkedInCampaignId : tracking . linkedInAds . campaignId ,
1120+ } ) ,
1121+ } ,
1122+ } ) ;
1123+ await prisma . user . update ( {
1124+ where : { id : newUser . id } ,
1125+ data : {
1126+ metadata : {
1127+ stripeCustomerId : customer . stripeCustomerId ,
1128+ } ,
1129+ } ,
1130+ } ) ;
1131+ } catch ( err ) {
1132+ log . error ( "Failed to create Stripe customer with tracking" , err ) ;
1133+ }
1134+ } ) ( )
1135+ ) ;
1136+
10971137 if ( account . twoFactorEnabled ) {
10981138 return loginWithTotp ( newUser . email ) ;
10991139 } else {
0 commit comments