@@ -1428,13 +1428,27 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
1428
1428
1429
1429
protected async onTeamMemberAdded ( userId : string , teamId : string ) : Promise < void > {
1430
1430
const now = new Date ( ) ;
1431
- const teamSubscription = await this . teamSubscription2DB . findForTeam ( teamId , now . toISOString ( ) ) ;
1432
- if ( ! teamSubscription ) {
1433
- // No team subscription, nothing to do 🌴
1434
- return ;
1431
+ const ts2 = await this . teamSubscription2DB . findForTeam ( teamId , now . toISOString ( ) ) ;
1432
+ if ( ts2 ) {
1433
+ await this . updateTeamSubscriptionQuantity ( ts2 ) ;
1434
+ await this . teamSubscription2Service . addTeamMemberSubscription ( ts2 , userId ) ;
1435
+ }
1436
+ const [ teamCustomer , user ] = await Promise . all ( [
1437
+ this . stripeService . findCustomerByTeamId ( teamId ) ,
1438
+ this . userDB . findUserById ( userId ) ,
1439
+ ] ) ;
1440
+ if ( teamCustomer && user && ! user . additionalData ?. usageAttributionId ) {
1441
+ // If the user didn't explicitly choose yet where their usage should be attributed to, and
1442
+ // they join a team which accepts usage attribution (i.e. with usage-based billing enabled),
1443
+ // then we simplify the UX by automatically attributing the user's usage to that team.
1444
+ // Note: This default choice can be changed at any time by the user in their billing settings.
1445
+ const subscription = await this . stripeService . findUncancelledSubscriptionByCustomer ( teamCustomer . id ) ;
1446
+ if ( subscription ) {
1447
+ user . additionalData = user . additionalData || { } ;
1448
+ user . additionalData . usageAttributionId = `team:${ teamId } ` ;
1449
+ await this . userDB . updateUserPartial ( user ) ;
1450
+ }
1435
1451
}
1436
- await this . updateTeamSubscriptionQuantity ( teamSubscription ) ;
1437
- await this . teamSubscription2Service . addTeamMemberSubscription ( teamSubscription , userId ) ;
1438
1452
}
1439
1453
1440
1454
protected async onTeamMemberRemoved ( userId : string , teamId : string , teamMembershipId : string ) : Promise < void > {
0 commit comments