Skip to content

Commit

Permalink
Payment admin
Browse files Browse the repository at this point in the history
  • Loading branch information
armanddidierjean committed Nov 16, 2024
1 parent 685c6ec commit eeb0c61
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 1 deletion.
65 changes: 64 additions & 1 deletion app/core/myeclpay/cruds_myeclpay.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import datetime
from uuid import UUID

from sqlalchemy import or_, select, update
from sqlalchemy import delete, or_, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload

Expand All @@ -15,6 +15,69 @@
)


async def create_store(
store: models_myeclpay.Store,
db: AsyncSession,
) -> None:
db.add(store)


async def create_seller(
user_id: str,
store_id: UUID,
can_bank: bool,
can_see_historic: bool,
can_cancel: bool,
can_manage_sellers: bool,
store_admin: bool,
db: AsyncSession,
) -> None:
wallet = models_myeclpay.Seller(
user_id=user_id,
store_id=store_id,
can_bank=can_bank,
can_see_historic=can_see_historic,
can_cancel=can_cancel,
can_manage_sellers=can_manage_sellers,
store_admin=store_admin,
)
db.add(wallet)


async def get_admin_sellers(
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,
models_myeclpay.Seller.store_admin.is_(True),
),
)
return result.scalars().all()


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


async def delete_seller(
seller_id: UUID,
db: AsyncSession,
) -> models_myeclpay.Seller | None:
await db.execute(
delete(models_myeclpay.Seller).where(models_myeclpay.Seller.id == seller_id),
)


async def create_wallet(
wallet_id: UUID,
wallet_type: WalletType,
Expand Down
143 changes: 143 additions & 0 deletions app/core/myeclpay/endpoints_myeclpay.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,149 @@
hyperion_error_logger = logging.getLogger("hyperion.error")


# Members of group Payment #


@router.post(
"/myeclpay/stores",
status_code=200,
response_model=schemas_myeclpay.Store,
)
async def create_store(
store: schemas_myeclpay.StoreBase,
db: AsyncSession = Depends(get_db),
user: CoreUser = Depends(is_user_a_member_of(GroupType.payment)),
):
"""
Create a store
**The user must be a member of group Payment**
"""
# Create new wallet for user
wallet_id = uuid.uuid4()
await cruds_myeclpay.create_wallet(
wallet_id=wallet_id,
wallet_type=WalletType.USER,
balance=0,
db=db,
)

store_db = Store(
id=uuid.uuid4(),
name=store.name,
membership=store.membership,
wallet_id=wallet_id,
)
# Check if user is already registered
await cruds_myeclpay.create_store(
store=store_db,
db=db,
)

await db.commit()

return store_db


@router.post(
"/myeclpay/stores/{store_id}/admins",
status_code=204,
)
async def create_store_admin_seller(
store_id: UUID,
seller: schemas_myeclpay.SellerAdminCreation,
db: AsyncSession = Depends(get_db),
user: CoreUser = Depends(is_user_a_member_of(GroupType.payment)),
):
"""
Create a store admin seller.
This admin will have permissions:
- can_bank
- can_see_historic
- can_cancel
- can_manage_sellers
- store_admin
**The user must be a member of group Payment**
"""
await cruds_myeclpay.create_seller(
user_id=seller.user_id,
store_id=store_id,
can_bank=True,
can_see_historic=True,
can_cancel=True,
can_manage_sellers=True,
store_admin=True,
db=db,
)

await db.commit()


@router.get(
"/myeclpay/stores/{store_id}/admins",
status_code=200,
response_model=list[schemas_myeclpay.Seller],
)
async def get_store_admin_seller(
store_id: UUID,
db: AsyncSession = Depends(get_db),
user: CoreUser = Depends(is_user_a_member_of(GroupType.payment)),
):
"""
Get all sellers that have the `store_admin` permission for the given store.
**The user must be a member of group Payment**
"""
sellers = await cruds_myeclpay.get_admin_sellers(
store_id=store_id,
db=db,
)

return sellers


@router.delete(
"/myeclpay/stores/{store_id}/admins/{seller_id}",
status_code=204,
)
async def delete_store_admin_seller(
store_id: UUID,
seller_id: UUID,
db: AsyncSession = Depends(get_db),
user: CoreUser = Depends(is_user_a_member_of(GroupType.payment)),
):
"""
Delete a store admin seller.
**The user must be a member of group Payment**
"""
await cruds_myeclpay.get_seller(
seller_id=seller_id,
db=db,
)

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

if seller_id.store_id != store_id:
raise HTTPException(
status_code=400,
detail="Seller does not belong to the store",
)

await cruds_myeclpay.delete_seller(
seller_id=seller_id,
db=db,
)

await db.commit()


@router.get(
"/myeclpay/users/me/cgu",
status_code=200,
Expand Down
25 changes: 25 additions & 0 deletions app/core/myeclpay/schemas_myeclpay.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@
TransactionStatus,
WalletDeviceStatus,
)
from app.types.membership import AvailableAssociationMembership


class StoreBase(BaseModel):
name: str
membership: AvailableAssociationMembership
wallet_id: UUID


class Store(StoreBase):
id: UUID


class SellerAdminCreation(BaseModel):
user_id: str


class Seller(BaseModel):
user_id: str
store_id: UUID
can_bank: bool
can_see_historic: bool
can_cancel: bool
can_manage_sellers: bool
store_admin: bool


class CGUSignature(BaseModel):
Expand Down

0 comments on commit eeb0c61

Please sign in to comment.