Skip to content

Commit 2afad51

Browse files
jankeromnesroboquat
authored andcommitted
[server] When a user with no explicit 'usageAttributionId' joins a team with usage-based billing enabled, automatically re-attribute usage to that team
1 parent bd5d3ea commit 2afad51

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

components/server/ee/src/workspace/gitpod-server-impl.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,13 +1428,27 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
14281428

14291429
protected async onTeamMemberAdded(userId: string, teamId: string): Promise<void> {
14301430
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+
}
14351451
}
1436-
await this.updateTeamSubscriptionQuantity(teamSubscription);
1437-
await this.teamSubscription2Service.addTeamMemberSubscription(teamSubscription, userId);
14381452
}
14391453

14401454
protected async onTeamMemberRemoved(userId: string, teamId: string, teamMembershipId: string): Promise<void> {

0 commit comments

Comments
 (0)