Skip to content

Commit

Permalink
Feat: store deletion and history
Browse files Browse the repository at this point in the history
  • Loading branch information
Rotheem committed Jan 13, 2025
1 parent 15a4047 commit 2d6403d
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/core/myeclpay/cruds_myeclpay.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
112 changes: 112 additions & 0 deletions app/core/myeclpay/endpoints_myeclpay.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions app/core/myeclpay/models_myeclpay.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class Store(Base):
unique=True,
)

structure: Mapped[Structure] = relationship(init=False, lazy="joined")


class Request(Base):
__tablename__ = "myeclpay_request"
Expand Down
1 change: 1 addition & 0 deletions app/core/myeclpay/schemas_myeclpay.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Store(StoreBase):
id: UUID
structure_id: UUID
wallet_id: UUID
structure: Structure


class UserStore(Store):
Expand Down

0 comments on commit 2d6403d

Please sign in to comment.