Skip to content

Commit e99ddab

Browse files
committed
refactored location of budget membership fix
1 parent 077f62b commit e99ddab

File tree

2 files changed

+83
-51
lines changed

2 files changed

+83
-51
lines changed

litellm/proxy/management_endpoints/common_utils.py

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Union
1+
from typing import Any, Union, Optional
22

33
from litellm.proxy._types import (
44
GenerateKeyRequest,
@@ -46,3 +46,77 @@ def _set_object_metadata_field(
4646
_premium_user_check()
4747
object_data.metadata = object_data.metadata or {}
4848
object_data.metadata[field_name] = value
49+
50+
51+
52+
async def _upsert_budget_and_membership(
53+
tx,
54+
*,
55+
team_id: str,
56+
user_id: str,
57+
max_budget: Optional[float],
58+
existing_budget_id: Optional[str],
59+
user_api_key_dict: UserAPIKeyAuth,
60+
):
61+
"""
62+
Helper function to Create/Update or Delete the budget within the team membership
63+
Args:
64+
tx: The transaction object
65+
team_id: The ID of the team
66+
user_id: The ID of the user
67+
max_budget: The maximum budget for the team
68+
existing_budget_id: The ID of the existing budget, if any
69+
user_api_key_dict: User API Key dictionary containing user information
70+
71+
If max_budget is None, the user's budget is removed from the team membership.
72+
If max_budget exists, a budget is updated or created and linked to the team membership.
73+
"""
74+
if max_budget is None:
75+
# disconnect the budget since max_budget is None
76+
await tx.litellm_teammembership.update(
77+
where={"user_id_team_id": {"user_id": user_id, "team_id": team_id}},
78+
data={"litellm_budget_table": {"disconnect": True}},
79+
)
80+
return
81+
82+
if existing_budget_id:
83+
# update the existing budget
84+
await tx.litellm_budgettable.update(
85+
where={"budget_id": existing_budget_id},
86+
data={"max_budget": max_budget},
87+
)
88+
return
89+
90+
# create a new budget
91+
new_budget = await tx.litellm_budgettable.create(
92+
data={
93+
"max_budget": max_budget,
94+
"created_by": user_api_key_dict.user_id or "",
95+
"updated_by": user_api_key_dict.user_id or "",
96+
},
97+
include={"team_membership": True},
98+
)
99+
# upsert the team membership with the new/updated budget
100+
await tx.litellm_teammembership.upsert(
101+
where={
102+
"user_id_team_id": {
103+
"user_id": user_id,
104+
"team_id": team_id,
105+
}
106+
},
107+
data={
108+
"create": {
109+
"user_id": user_id,
110+
"team_id": team_id,
111+
"litellm_budget_table": {
112+
"connect": {"budget_id": new_budget.budget_id},
113+
},
114+
},
115+
"update": {
116+
"litellm_budget_table": {
117+
"connect": {"budget_id": new_budget.budget_id},
118+
},
119+
},
120+
},
121+
)
122+

litellm/proxy/management_endpoints/team_endpoints.py

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
_is_user_team_admin,
6767
_set_object_metadata_field,
6868
_user_has_admin_view,
69+
_upsert_budget_and_membership,
6970
)
7071
from litellm.proxy.management_endpoints.tag_management_endpoints import (
7172
get_daily_activity,
@@ -1266,56 +1267,13 @@ async def team_member_update(
12661267

12671268
### upsert new budget
12681269
async with prisma_client.db.tx() as tx:
1269-
if data.max_budget_in_team is not None:
1270-
if identified_budget_id is None:
1271-
new_budget = await tx.litellm_budgettable.create(
1272-
data={
1273-
"max_budget": data.max_budget_in_team,
1274-
"created_by": user_api_key_dict.user_id or "",
1275-
"updated_by": user_api_key_dict.user_id or "",
1276-
},
1277-
include={"team_membership": True},
1278-
)
1279-
await tx.litellm_teammembership.upsert(
1280-
where={
1281-
"user_id_team_id": {
1282-
"user_id": received_user_id,
1283-
"team_id": data.team_id,
1284-
}
1285-
},
1286-
data={
1287-
"create": {
1288-
"user_id": received_user_id,
1289-
"team_id": data.team_id,
1290-
"litellm_budget_table": {
1291-
"connect": {"budget_id": new_budget.budget_id},
1292-
},
1293-
},
1294-
"update": {
1295-
"litellm_budget_table": {
1296-
"connect": {"budget_id": new_budget.budget_id},
1297-
},
1298-
},
1299-
},
1300-
)
1301-
elif identified_budget_id is not None:
1302-
await prisma_client.db.litellm_budgettable.update(
1303-
where={"budget_id": identified_budget_id},
1304-
data={"max_budget": data.max_budget_in_team},
1305-
)
1306-
else:
1307-
await tx.litellm_teammembership.update(
1308-
where={
1309-
"user_id_team_id": {
1310-
"user_id": received_user_id,
1311-
"team_id": data.team_id,
1312-
}
1313-
},
1314-
data={
1315-
"litellm_budget_table": {
1316-
"disconnect": True
1317-
}
1318-
},
1270+
await _upsert_budget_and_membership(
1271+
tx=tx,
1272+
team_id=data.team_id,
1273+
user_id=received_user_id,
1274+
max_budget=data.max_budget_in_team,
1275+
existing_budget_id=identified_budget_id,
1276+
user_api_key_dict=user_api_key_dict,
13191277
)
13201278

13211279
### update team member role

0 commit comments

Comments
 (0)