@@ -2099,41 +2099,42 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
2099
2099
return this . findStripeSubscriptionId ( ctx , AttributionId . render ( attributionId ) ) ;
2100
2100
}
2101
2101
2102
- async createOrUpdateStripeCustomerForTeam ( ctx : TraceContext , teamId : string , currency : string ) : Promise < void > {
2103
- const user = this . checkAndBlockUser ( "createOrUpdateStripeCustomerForTeam" ) ;
2104
- const team = await this . guardTeamOperation ( teamId , "update" ) ;
2105
- await this . ensureStripeApiIsAllowed ( { team } ) ;
2106
- try {
2107
- let customerId = await this . stripeService . findCustomerByTeamId ( team ! . id ) ;
2108
- if ( ! customerId ) {
2109
- log . info ( "Creating customer for team" , { teamId } ) ;
2110
- customerId = await this . stripeService . createCustomerForTeam ( user , team ! ) ;
2102
+ async createStripeCustomer ( ctx : TraceContext , attributionId : string , currency : string ) : Promise < void > {
2103
+ const user = this . checkAndBlockUser ( "createStripeCustomer" ) ;
2104
+ const attrId = AttributionId . parse ( attributionId ) ;
2105
+ if ( ! attrId ) {
2106
+ throw new ResponseError ( ErrorCodes . BAD_REQUEST , `Invalid attributionId '${ attributionId } '` ) ;
2107
+ }
2108
+ let team : Team | undefined ;
2109
+ if ( attrId . kind === "team" ) {
2110
+ team = await this . guardTeamOperation ( attrId . teamId , "update" ) ;
2111
+ await this . ensureStripeApiIsAllowed ( { team } ) ;
2112
+ } else {
2113
+ if ( attrId . userId !== user . id ) {
2114
+ throw new ResponseError (
2115
+ ErrorCodes . PERMISSION_DENIED ,
2116
+ "Cannot create Stripe customer profile for another user" ,
2117
+ ) ;
2111
2118
}
2112
- log . info ( "Setting preferred currency for team" , { teamId, currency } ) ;
2113
- await this . stripeService . setPreferredCurrencyForCustomer ( customerId , currency ) ;
2114
- } catch ( error ) {
2115
- log . error ( `Failed to update Stripe customer profile for team '${ teamId } '` , error ) ;
2116
- throw new ResponseError (
2117
- ErrorCodes . INTERNAL_SERVER_ERROR ,
2118
- `Failed to update Stripe customer profile for team '${ teamId } '` ,
2119
- ) ;
2119
+ await this . ensureStripeApiIsAllowed ( { user } ) ;
2120
2120
}
2121
- }
2122
-
2123
- async createOrUpdateStripeCustomerForUser ( ctx : TraceContext , currency : string ) : Promise < void > {
2124
- const user = this . checkAndBlockUser ( "createOrUpdateStripeCustomerForUser" ) ;
2125
- await this . ensureStripeApiIsAllowed ( { user } ) ;
2126
2121
try {
2127
- let customerId = await this . stripeService . findCustomerByUserId ( user . id ) ;
2128
- if ( ! customerId ) {
2129
- customerId = await this . stripeService . createCustomerForUser ( user ) ;
2122
+ if ( await this . stripeService . findCustomerByAttributionId ( attributionId ) ) {
2123
+ throw new ResponseError (
2124
+ ErrorCodes . BAD_REQUEST ,
2125
+ "A Stripe customer profile already exists for this attributionId" ,
2126
+ ) ;
2130
2127
}
2128
+ const customerId =
2129
+ attrId . kind === "team"
2130
+ ? await this . stripeService . createCustomerForTeam ( user , team ! )
2131
+ : await this . stripeService . createCustomerForUser ( user ) ;
2131
2132
await this . stripeService . setPreferredCurrencyForCustomer ( customerId , currency ) ;
2132
2133
} catch ( error ) {
2133
- log . error ( `Failed to update Stripe customer profile for user '${ user . id } '` , error ) ;
2134
+ log . error ( `Failed to create Stripe customer profile for '${ attributionId } '` , error ) ;
2134
2135
throw new ResponseError (
2135
2136
ErrorCodes . INTERNAL_SERVER_ERROR ,
2136
- `Failed to update Stripe customer profile for user '${ user . id } '` ,
2137
+ `Failed to create Stripe customer profile for '${ attributionId } '` ,
2137
2138
) ;
2138
2139
}
2139
2140
}
0 commit comments