@@ -210,10 +210,31 @@ export class UserService {
210
210
* Identifies the team or user to which a workspace instance's running time should be attributed to
211
211
* (e.g. for usage analytics or billing purposes).
212
212
*
213
+ * A. Billing-based attribution: If payments are enabled, we attribute all the user's usage to:
214
+ * - An explicitly selected billing account (e.g. a team with usage-based billing enabled)
215
+ * - Or, we default to the user for all usage (e.g. free tier or individual billing, regardless of project/team)
216
+ *
217
+ * B. Project-based attribution: If payments are not enabled (e.g. Self-Hosted), we attribute:
218
+ * - To the owner of the project (user or team), if the workspace is linked to a project
219
+ * - To the user, iff the workspace is not linked to a project
220
+ *
213
221
* @param user
214
222
* @param projectId
215
223
*/
216
224
async getWorkspaceUsageAttributionId ( user : User , projectId ?: string ) : Promise < string | undefined > {
225
+ // A. Billing-based attribution
226
+ if ( this . config . enablePayment ) {
227
+ if ( ! user . additionalData ?. usageAttributionId ) {
228
+ // No explicit user attribution ID yet -- attribute all usage to the user by default (regardless of project/team).
229
+ user . additionalData = user . additionalData || { } ;
230
+ user . additionalData . usageAttributionId = `user:${ user . id } ` ;
231
+ await this . userDb . updateUserPartial ( user ) ;
232
+ }
233
+ // Return the user's explicit attribution ID.
234
+ return user . additionalData . usageAttributionId ;
235
+ }
236
+
237
+ // B. Project-based attribution
217
238
if ( ! projectId ) {
218
239
// No project -- attribute to the user.
219
240
return `user:${ user . id } ` ;
0 commit comments