From 2d6403dea756af3c7467855695e152d9c86ad0e6 Mon Sep 17 00:00:00 2001 From: Thonyk Date: Tue, 14 Jan 2025 00:05:52 +0100 Subject: [PATCH] Feat: store deletion and history --- app/core/myeclpay/cruds_myeclpay.py | 9 ++ app/core/myeclpay/endpoints_myeclpay.py | 112 ++++++++++++++++++++++++ app/core/myeclpay/models_myeclpay.py | 2 + app/core/myeclpay/schemas_myeclpay.py | 1 + 4 files changed, 124 insertions(+) diff --git a/app/core/myeclpay/cruds_myeclpay.py b/app/core/myeclpay/cruds_myeclpay.py index 37250301f..4fc339fd1 100644 --- a/app/core/myeclpay/cruds_myeclpay.py +++ b/app/core/myeclpay/cruds_myeclpay.py @@ -168,6 +168,15 @@ async def update_store( await db.commit() +async def delete_store( + store_id: UUID, + db: AsyncSession, +) -> None: + await db.execute( + delete(models_myeclpay.Store).where(models_myeclpay.Store.id == store_id), + ) + + async def get_stores( db: AsyncSession, ) -> Sequence[models_myeclpay.Store]: diff --git a/app/core/myeclpay/endpoints_myeclpay.py b/app/core/myeclpay/endpoints_myeclpay.py index 9d9238fdd..347a4536c 100644 --- a/app/core/myeclpay/endpoints_myeclpay.py +++ b/app/core/myeclpay/endpoints_myeclpay.py @@ -364,6 +364,50 @@ async def create_store( return store_db +@router.get( + "/myeclpay/stores/{store_id}/history", + status_code=200, + response_model=list[schemas_myeclpay.Transaction], +) +async def get_store_history( + store_id: UUID, + db: AsyncSession = Depends(get_db), + user: CoreUser = Depends(is_user()), +): + """ + Get all transactions for the store. + + **The user must be authorized to see the store history** + """ + store = await cruds_myeclpay.get_store( + store_id=store_id, + db=db, + ) + if store is None: + raise HTTPException( + status_code=404, + detail="Store does not exist", + ) + + seller = await cruds_myeclpay.get_seller( + user_id=user.id, + store_id=store_id, + db=db, + ) + if seller is None or not seller.can_see_history: + raise HTTPException( + status_code=403, + detail="User is not authorized to see the store history", + ) + + transactions = await cruds_myeclpay.get_transactions_by_wallet_id( + wallet_id=store.wallet_id, + db=db, + ) + + return transactions + + @router.get( "/myeclpay/users/me/stores", status_code=200, @@ -629,6 +673,74 @@ async def update_store( await db.commit() +@router.delete( + "/myeclpay/stores/{store_id}", + status_code=204, +) +async def delete_store( + store_id: UUID, + db: AsyncSession = Depends(get_db), + user: CoreUser = Depends(is_user()), +): + """ + Delete a store + + **The user must be the manager for this store's structure** + """ + store = await cruds_myeclpay.get_store( + store_id=store_id, + db=db, + ) + if store is None: + raise HTTPException( + status_code=404, + detail="Store does not exist", + ) + + structure = await cruds_myeclpay.get_structure_by_id( + structure_id=store.structure_id, + db=db, + ) + if structure is None: + raise HTTPException( + status_code=404, + detail="Structure does not exist", + ) + if structure.manager_user_id != user.id: + raise HTTPException( + status_code=403, + detail="User is not the manager for this structure", + ) + + transactions = await cruds_myeclpay.get_transactions_by_wallet_id( + wallet_id=store.wallet_id, + db=db, + ) + if transactions: + raise HTTPException( + status_code=400, + detail="Store has transactions and cannot be deleted anymore", + ) + + sellers = await cruds_myeclpay.get_sellers_by_store_id( + store_id=store_id, + db=db, + ) + for seller in sellers: + await cruds_myeclpay.delete_seller( + seller_user_id=seller.user_id, + store_id=store_id, + db=db, + ) + + await cruds_myeclpay.delete_store( + store_id=store_id, + db=db, + ) + + await db.commit() + + @router.post( "/myeclpay/stores/{store_id}/sellers", status_code=204, diff --git a/app/core/myeclpay/models_myeclpay.py b/app/core/myeclpay/models_myeclpay.py index b1346443a..97d3d60d3 100644 --- a/app/core/myeclpay/models_myeclpay.py +++ b/app/core/myeclpay/models_myeclpay.py @@ -109,6 +109,8 @@ class Store(Base): unique=True, ) + structure: Mapped[Structure] = relationship(init=False, lazy="joined") + class Request(Base): __tablename__ = "myeclpay_request" diff --git a/app/core/myeclpay/schemas_myeclpay.py b/app/core/myeclpay/schemas_myeclpay.py index 867880958..9ec0756f8 100644 --- a/app/core/myeclpay/schemas_myeclpay.py +++ b/app/core/myeclpay/schemas_myeclpay.py @@ -42,6 +42,7 @@ class Store(StoreBase): id: UUID structure_id: UUID wallet_id: UUID + structure: Structure class UserStore(Store):