Skip to content

Commit 59681ae

Browse files
committed
refactored location of budget membership fix
1 parent a8671af commit 59681ae

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,
@@ -1189,56 +1190,13 @@ async def team_member_update(
11891190

11901191
### upsert new budget
11911192
async with prisma_client.db.tx() as tx:
1192-
if data.max_budget_in_team is not None:
1193-
if identified_budget_id is None:
1194-
new_budget = await tx.litellm_budgettable.create(
1195-
data={
1196-
"max_budget": data.max_budget_in_team,
1197-
"created_by": user_api_key_dict.user_id or "",
1198-
"updated_by": user_api_key_dict.user_id or "",
1199-
},
1200-
include={"team_membership": True},
1201-
)
1202-
await tx.litellm_teammembership.upsert(
1203-
where={
1204-
"user_id_team_id": {
1205-
"user_id": received_user_id,
1206-
"team_id": data.team_id,
1207-
}
1208-
},
1209-
data={
1210-
"create": {
1211-
"user_id": received_user_id,
1212-
"team_id": data.team_id,
1213-
"litellm_budget_table": {
1214-
"connect": {"budget_id": new_budget.budget_id},
1215-
},
1216-
},
1217-
"update": {
1218-
"litellm_budget_table": {
1219-
"connect": {"budget_id": new_budget.budget_id},
1220-
},
1221-
},
1222-
},
1223-
)
1224-
elif identified_budget_id is not None:
1225-
await prisma_client.db.litellm_budgettable.update(
1226-
where={"budget_id": identified_budget_id},
1227-
data={"max_budget": data.max_budget_in_team},
1228-
)
1229-
else:
1230-
await tx.litellm_teammembership.update(
1231-
where={
1232-
"user_id_team_id": {
1233-
"user_id": received_user_id,
1234-
"team_id": data.team_id,
1235-
}
1236-
},
1237-
data={
1238-
"litellm_budget_table": {
1239-
"disconnect": True
1240-
}
1241-
},
1193+
await _upsert_budget_and_membership(
1194+
tx=tx,
1195+
team_id=data.team_id,
1196+
user_id=received_user_id,
1197+
max_budget=data.max_budget_in_team,
1198+
existing_budget_id=identified_budget_id,
1199+
user_api_key_dict=user_api_key_dict,
12421200
)
12431201

12441202
### update team member role

0 commit comments

Comments
 (0)