11import math
22
3- from sqlalchemy import select , func , update , or_ , literal_column
3+ from sqlalchemy import select , func , update , or_ , literal_column , any_
44from sqlalchemy .ext .asyncio import AsyncSession
55from sqlalchemy .orm import Session
66
@@ -83,8 +83,6 @@ async def get_refund_data(sort_pairs: dict[str, int],
8383 filters = [username .replace ("@" , "" ) for username in filters ]
8484 filter_conditions = [User .telegram_username .icontains (name ) for name in filters ]
8585 conditions .append (or_ (* filter_conditions ))
86- # SQLITE CRUTCH 🩼
87- je = func .json_each (BuyItem .item_ids ).table_valued ("value" ).alias ("je" )
8886 stmt = (select (Buy .total_price ,
8987 BuyItem .item_ids ,
9088 Buy .id .label ("buy_id" ),
@@ -95,11 +93,7 @@ async def get_refund_data(sort_pairs: dict[str, int],
9593 User .language )
9694 .join (BuyItem , BuyItem .buy_id == Buy .id )
9795 .join (User , User .id == Buy .buyer_id )
98- # START SQLITE CRUTCH 🩼
99- .join (je , literal_column ("1" ) == literal_column ("1" ))
100- .join (Item , Item .id == je .c .value )
101- # END SQLITE CRUTCH 🩼
102- # .join(Item, Item.id.in_(BuyItem.item_ids))
96+ .join (Item , Item .id == any_ (BuyItem .item_ids ))
10397 .join (Subcategory , Subcategory .id == Item .subcategory_id )
10498 .where (* conditions )
10599 .distinct ()
@@ -112,7 +106,6 @@ async def get_refund_data(sort_pairs: dict[str, int],
112106
113107 @staticmethod
114108 async def get_refund_data_single (buy_id : int , session : Session | AsyncSession ) -> RefundDTO :
115- je = func .json_each (BuyItem .item_ids ).table_valued ("value" ).alias ("je" )
116109 stmt = (select (Buy .total_price ,
117110 BuyItem .item_ids ,
118111 Buy .id .label ("buy_id" ),
@@ -124,13 +117,9 @@ async def get_refund_data_single(buy_id: int, session: Session | AsyncSession) -
124117 User .language )
125118 .join (BuyItem , BuyItem .buy_id == Buy .id )
126119 .join (User , User .id == Buy .buyer_id )
127- # START SQLITE CRUTCH 🩼
128- .join (je , literal_column ("1" ) == literal_column ("1" ))
129- .join (Item , Item .id == je .c .value )
130- # END SQLITE CRUTCH 🩼
131- # .join(Item, Item.id.in_(BuyItem.item_ids))
120+ .join (Item , Item .id .in_ (BuyItem .item_ids ))
132121 .join (Subcategory , Subcategory .id == Item .subcategory_id )
133- .where (Buy .is_refunded == False , Buy .id == buy_id )
122+ .where (Buy .status == BuyStatus . REFUNDED , Buy .id == buy_id )
134123 .limit (1 ))
135124 refund_data = await session_execute (stmt , session )
136125 return RefundDTO .model_validate (refund_data .mappings ().one (), from_attributes = True )
@@ -155,7 +144,7 @@ async def get_by_timedelta(timedelta: StatisticsTimeDelta, session: Session | As
155144 start , end = timedelta .get_time_range ()
156145 stmt = select (Buy ).where (Buy .buy_datetime >= start ,
157146 Buy .buy_datetime <= end ,
158- Buy .is_refunded == False )
147+ Buy .status == BuyStatus . REFUNDED )
159148 buys = await session_execute (stmt , session )
160149 return [BuyDTO .model_validate (buy , from_attributes = True ) for buy in buys .scalars ().all ()]
161150
@@ -173,14 +162,14 @@ async def get_max_page_purchase_history(buyer_id: int | None, session: AsyncSess
173162 async def get_qty_by_buyer_id (buyer_id : int , session : AsyncSession | Session ) -> int :
174163 stmt = (select (func .count (Buy .id ))
175164 .where (Buy .buyer_id == buyer_id ,
176- Buy .is_refunded == False ))
165+ Buy .status == BuyStatus . REFUNDED ))
177166 qty = await session_execute (stmt , session )
178167 return qty .scalar_one ()
179168
180169 @staticmethod
181170 async def get_spent_amount (buyer_id : int , session : AsyncSession ) -> float :
182171 stmt = func .coalesce ((select (func .sum (Buy .total_price ))
183172 .where (Buy .buyer_id == buyer_id ,
184- Buy .is_refunded == False )), 0 )
173+ Buy .status == BuyStatus . REFUNDED )), 0 )
185174 qty = await session_execute (stmt , session )
186175 return qty .scalar_one ()
0 commit comments