Skip to content

Commit

Permalink
Feat: sellers management
Browse files Browse the repository at this point in the history
  • Loading branch information
Rotheem committed Jan 10, 2025
1 parent 1c0b7f6 commit 15a4047
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 33 deletions.
54 changes: 26 additions & 28 deletions app/core/myeclpay/cruds_myeclpay.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,19 +225,43 @@ async def get_admin_sellers(


async def get_seller(
seller_user_id: str,
user_id: str,
store_id: UUID,
db: AsyncSession,
) -> models_myeclpay.Seller | None:
result = await db.execute(
select(models_myeclpay.Seller).where(
models_myeclpay.Seller.user_id == seller_user_id,
models_myeclpay.Seller.user_id == user_id,
models_myeclpay.Seller.store_id == store_id,
),
)
return result.scalars().first()


async def get_sellers_by_store_id(
store_id: UUID,
db: AsyncSession,
) -> Sequence[models_myeclpay.Seller]:
result = await db.execute(
select(models_myeclpay.Seller).where(
models_myeclpay.Seller.store_id == store_id,
),
)
return result.scalars().all()


async def get_sellers_by_user_id(
user_id: str,
db: AsyncSession,
) -> Sequence[models_myeclpay.Seller]:
result = await db.execute(
select(models_myeclpay.Seller).where(
models_myeclpay.Seller.user_id == user_id,
),
)
return result.scalars().all()


async def update_seller(
seller_user_id: str,
store_id: UUID,
Expand Down Expand Up @@ -530,32 +554,6 @@ async def get_transfers_by_wallet_id(
return result.scalars().all()


async def get_all_user_sellers(
user_id: str,
db: AsyncSession,
) -> Sequence[models_myeclpay.Seller]:
result = await db.execute(
select(models_myeclpay.Seller).where(
models_myeclpay.Seller.user_id == user_id,
),
)
return result.scalars().all()


async def get_seller_by_user_id_and_store_id(
store_id: UUID,
user_id: str,
db: AsyncSession,
) -> models_myeclpay.Seller | None:
result = await db.execute(
select(models_myeclpay.Seller).where(
models_myeclpay.Seller.user_id == user_id,
models_myeclpay.Seller.store_id == store_id,
),
)
return result.scalars().first()


async def get_store(
store_id: UUID,
db: AsyncSession,
Expand Down
165 changes: 160 additions & 5 deletions app/core/myeclpay/endpoints_myeclpay.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ async def update_structure_manager(
structure_id=request.structure_id,
db=db,
)
sellers = await cruds_myeclpay.get_all_user_sellers(
sellers = await cruds_myeclpay.get_sellers_by_user_id(
user_id=request.user_id,
db=db,
)
Expand Down Expand Up @@ -378,7 +378,7 @@ async def get_user_stores(
**The user must be authenticated to use this endpoint**
"""
sellers = await cruds_myeclpay.get_all_user_sellers(
sellers = await cruds_myeclpay.get_sellers_by_user_id(
user_id=user.id,
db=db,
)
Expand Down Expand Up @@ -554,7 +554,7 @@ async def delete_store_admin_seller(
)

seller = await cruds_myeclpay.get_seller(
seller_user_id=seller_user_id,
user_id=seller_user_id,
store_id=store_id,
db=db,
)
Expand Down Expand Up @@ -629,7 +629,162 @@ async def update_store(
await db.commit()


# User registration #
@router.post(
"/myeclpay/stores/{store_id}/sellers",
status_code=204,
)
async def create_store_seller(
store_id: UUID,
seller: schemas_myeclpay.SellerCreation,
db: AsyncSession = Depends(get_db),
user: CoreUser = Depends(is_user()),
):
"""
Create a store seller.
This admin will have autorized permissions among:
- can_bank
- can_see_history
- can_cancel
- can_manage_sellers
- store_admin
**The user must have the `can_manage_sellers` permission for this store**
"""
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_admin = await cruds_myeclpay.get_seller(
user_id=user.id,
store_id=store_id,
db=db,
)
if seller_admin is None or not seller_admin.can_manage_sellers:
raise HTTPException(
status_code=403,
detail="User is not a store admin",
)

await cruds_myeclpay.create_seller(
user_id=seller.user_id,
store_id=store_id,
can_bank=seller.can_bank,
can_see_history=seller.can_see_history,
can_cancel=seller.can_cancel,
can_manage_sellers=seller.can_manage_sellers,
store_admin=False,
db=db,
)

await db.commit()


@router.get(
"/myeclpay/stores/{store_id}/sellers",
status_code=200,
response_model=list[schemas_myeclpay.Seller],
)
async def get_store_sellers(
store_id: UUID,
db: AsyncSession = Depends(get_db),
user: CoreUser = Depends(is_user()),
):
"""
Get all sellers for the given store.
**The user must have the `can_manage_sellers` permission for this store**
"""
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_admin = await cruds_myeclpay.get_seller(
user_id=user.id,
store_id=store_id,
db=db,
)
if seller_admin is None or not seller_admin.can_manage_sellers:
raise HTTPException(
status_code=403,
detail="User is not a store admin",
)

sellers = await cruds_myeclpay.get_sellers_by_store_id(
store_id=store_id,
db=db,
)

return sellers


@router.delete(
"/myeclpay/stores/{store_id}/sellers/{seller_user_id}",
status_code=204,
)
async def delete_store_seller(
store_id: UUID,
seller_user_id: str,
db: AsyncSession = Depends(get_db),
user: CoreUser = Depends(is_user()),
):
"""
Delete a store seller.
**The user must have the `can_manage_sellers` permission for this store**
"""
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_admin = await cruds_myeclpay.get_seller(
user_id=user.id,
store_id=store_id,
db=db,
)
if seller_admin is None or not seller_admin.can_manage_sellers:
raise HTTPException(
status_code=403,
detail="User is not a store admin",
)

seller = await cruds_myeclpay.get_seller(
user_id=seller_user_id,
store_id=store_id,
db=db,
)

if seller is None:
raise HTTPException(
status_code=404,
detail="Seller does not exist",
)

await cruds_myeclpay.delete_seller(
seller_user_id=seller_user_id,
store_id=store_id,
db=db,
)

await db.commit()


@router.get(
Expand Down Expand Up @@ -1213,7 +1368,7 @@ async def store_scan_qrcode(
detail="Store does not exist",
)

seller = await cruds_myeclpay.get_seller_by_user_id_and_store_id(
seller = await cruds_myeclpay.get_seller(
store_id=store_id,
user_id=user.id,
db=db,
Expand Down
8 changes: 8 additions & 0 deletions app/core/myeclpay/schemas_myeclpay.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ class SellerAdminCreation(BaseModel):
user_id: str


class SellerCreation(SellerAdminCreation):
store_id: UUID
can_bank: bool
can_see_history: bool
can_cancel: bool
can_manage_sellers: bool


class Seller(BaseModel):
user_id: str
store_id: UUID
Expand Down

0 comments on commit 15a4047

Please sign in to comment.