@@ -210,10 +210,29 @@ 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
+ return `user:${ user . id } ` ;
230
+ }
231
+ // Return the user's explicit attribution ID.
232
+ return user . additionalData . usageAttributionId ;
233
+ }
234
+
235
+ // B. Project-based attribution
217
236
if ( ! projectId ) {
218
237
// No project -- attribute to the user.
219
238
return `user:${ user . id } ` ;
0 commit comments