Skip to content

Commit b048cdc

Browse files
MyECLPay: increase test coverage (#731)
1 parent a80d896 commit b048cdc

File tree

4 files changed

+443
-187
lines changed

4 files changed

+443
-187
lines changed

app/core/myeclpay/endpoints_myeclpay.py

Lines changed: 30 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
format_cancel_log,
2323
format_refund_log,
2424
format_transaction_log,
25-
format_transfer_log,
2625
)
2726
from app.core.myeclpay.models_myeclpay import Store, WalletDevice
2827
from app.core.myeclpay.types_myeclpay import (
@@ -737,10 +736,18 @@ async def delete_store(
737736
wallet_id=store.wallet_id,
738737
db=db,
739738
)
740-
if transactions:
739+
transfers = await cruds_myeclpay.get_transfers_by_wallet_id(
740+
wallet_id=store.wallet_id,
741+
db=db,
742+
)
743+
refunds = await cruds_myeclpay.get_refunds_by_wallet_id(
744+
wallet_id=store.wallet_id,
745+
db=db,
746+
)
747+
if len(transactions) > 0 or len(transfers) > 0 or len(refunds) > 0:
741748
raise HTTPException(
742749
status_code=400,
743-
detail="Store has transactions and cannot be deleted anymore",
750+
detail="Store has items in history and cannot be deleted anymore",
744751
)
745752

746753
sellers = await cruds_myeclpay.get_sellers_by_store_id(
@@ -891,38 +898,32 @@ async def update_store_seller(
891898
**The user must have the `can_manage_sellers` permission for this store**
892899
"""
893900

894-
seller_admin = await cruds_myeclpay.get_seller(
895-
user_id=user.id,
901+
store = await cruds_myeclpay.get_store(
896902
store_id=store_id,
897903
db=db,
898904
)
899-
if seller_admin is None or not seller_admin.can_manage_sellers:
905+
if store is None:
900906
raise HTTPException(
901-
status_code=403,
902-
detail="User does not have the permission to manage sellers",
907+
status_code=404,
908+
detail="Store does not exist",
903909
)
904910

905-
store = await cruds_myeclpay.get_store(
911+
seller_admin = await cruds_myeclpay.get_seller(
912+
user_id=user.id,
906913
store_id=store_id,
907914
db=db,
908915
)
909-
if store is None:
916+
if seller_admin is None or not seller_admin.can_manage_sellers:
910917
raise HTTPException(
911-
status_code=404,
912-
detail="Store does not exist",
918+
status_code=403,
919+
detail="User does not have the permission to manage sellers",
913920
)
914921

915922
structure = await cruds_myeclpay.get_structure_by_id(
916923
structure_id=store.structure_id,
917924
db=db,
918925
)
919-
if structure is None:
920-
raise HTTPException(
921-
status_code=404,
922-
detail="Structure does not exist",
923-
)
924-
925-
if structure.manager_user_id == seller_user_id:
926+
if structure is None or structure.manager_user_id == seller_user_id:
926927
raise HTTPException(
927928
status_code=400,
928929
detail="User is the manager for this structure and cannot be updated as a seller",
@@ -964,37 +965,32 @@ async def delete_store_seller(
964965
965966
**The user must have the `can_manage_sellers` permission for this store**
966967
"""
967-
seller_admin = await cruds_myeclpay.get_seller(
968-
user_id=user.id,
968+
store = await cruds_myeclpay.get_store(
969969
store_id=store_id,
970970
db=db,
971971
)
972-
if seller_admin is None or not seller_admin.can_manage_sellers:
972+
if store is None:
973973
raise HTTPException(
974-
status_code=403,
975-
detail="User does not have the permission to manage sellers",
974+
status_code=404,
975+
detail="Store does not exist",
976976
)
977977

978-
store = await cruds_myeclpay.get_store(
978+
seller_admin = await cruds_myeclpay.get_seller(
979+
user_id=user.id,
979980
store_id=store_id,
980981
db=db,
981982
)
982-
if store is None:
983+
if seller_admin is None or not seller_admin.can_manage_sellers:
983984
raise HTTPException(
984-
status_code=404,
985-
detail="Store does not exist",
985+
status_code=403,
986+
detail="User does not have the permission to manage sellers",
986987
)
987988

988989
structure = await cruds_myeclpay.get_structure_by_id(
989990
structure_id=store.structure_id,
990991
db=db,
991992
)
992-
if structure is None:
993-
raise HTTPException(
994-
status_code=404,
995-
detail="Structure does not exist",
996-
)
997-
if structure.manager_user_id == seller_user_id:
993+
if structure is None or structure.manager_user_id == seller_user_id:
998994
raise HTTPException(
999995
status_code=400,
1000996
detail="User is the manager for this structure and cannot be deleted as a seller",
@@ -1637,113 +1633,6 @@ async def get_user_wallet_history(
16371633
return history
16381634

16391635

1640-
# TODO: do we keep this endpoint?
1641-
@router.post(
1642-
"/myeclpay/transfer/admin",
1643-
response_model=None,
1644-
status_code=204,
1645-
)
1646-
async def add_transfer_by_admin(
1647-
transfer_info: schemas_myeclpay.AdminTransferInfo,
1648-
db: AsyncSession = Depends(get_db),
1649-
user: CoreUser = Depends(is_user()),
1650-
settings: Settings = Depends(get_settings),
1651-
notification_tool: NotificationTool = Depends(get_notification_tool),
1652-
):
1653-
if transfer_info.transfer_type == TransferType.HELLO_ASSO:
1654-
raise HTTPException(
1655-
status_code=400,
1656-
detail="HelloAsso transfers can not be created manually",
1657-
)
1658-
1659-
if transfer_info.amount < 100:
1660-
raise HTTPException(
1661-
status_code=400,
1662-
detail="Please give an amount in cents, greater than 1€.",
1663-
)
1664-
1665-
if transfer_info.credited_user_id is None:
1666-
raise HTTPException(
1667-
status_code=400,
1668-
detail="Please provide a credited user id for this transfer type",
1669-
)
1670-
# TODO: IMPORTANT: do not let all BDE do this
1671-
if GroupType.BDE not in [group.id for group in user.groups]:
1672-
raise HTTPException(
1673-
status_code=403,
1674-
detail="User is not allowed to approve this transfer",
1675-
)
1676-
credited_user = await cruds_users.get_user_by_id(
1677-
user_id=transfer_info.credited_user_id,
1678-
db=db,
1679-
)
1680-
if credited_user is None:
1681-
raise HTTPException(
1682-
status_code=404,
1683-
detail="Receiver user does not exist",
1684-
)
1685-
1686-
user_payment = await cruds_myeclpay.get_user_payment(
1687-
user_id=credited_user.id,
1688-
db=db,
1689-
)
1690-
if user_payment is None:
1691-
raise HTTPException(
1692-
status_code=404,
1693-
detail="User is not registered for MyECL Pay",
1694-
)
1695-
1696-
if not is_user_latest_tos_signed(user_payment):
1697-
raise HTTPException(
1698-
status_code=400,
1699-
detail="User has not signed the latest TOS",
1700-
)
1701-
1702-
wallet = await cruds_myeclpay.get_wallet(
1703-
wallet_id=user_payment.wallet_id,
1704-
db=db,
1705-
)
1706-
if wallet is None:
1707-
raise HTTPException(
1708-
status_code=404,
1709-
detail="Wallet does not exist",
1710-
)
1711-
if wallet.balance + transfer_info.amount > settings.MYECLPAY_MAXIMUM_WALLET_BALANCE:
1712-
raise HTTPException(
1713-
status_code=403,
1714-
detail="Wallet balance would exceed the maximum allowed balance",
1715-
)
1716-
creation_date = datetime.now(UTC)
1717-
transfer = schemas_myeclpay.Transfer(
1718-
id=uuid.uuid4(),
1719-
type=transfer_info.transfer_type,
1720-
approver_user_id=user.id,
1721-
total=transfer_info.amount,
1722-
transfer_identifier="", # TODO: require to provide an identifier
1723-
wallet_id=user_payment.wallet_id,
1724-
creation=creation_date,
1725-
confirmed=True,
1726-
)
1727-
await cruds_myeclpay.create_transfer(transfer, db)
1728-
await cruds_myeclpay.increment_wallet_balance(
1729-
wallet_id=user_payment.wallet_id,
1730-
amount=transfer_info.amount,
1731-
db=db,
1732-
)
1733-
1734-
hyperion_myeclpay_logger.info(format_transfer_log(transfer))
1735-
1736-
message = Message(
1737-
title="💳 Paiement - transfert",
1738-
content=f"Votre compte a été crédité de {transfer_info.amount / 100} €",
1739-
action_module="MyECLPay",
1740-
)
1741-
await notification_tool.send_notification_to_user(
1742-
user_id=user_payment.user_id,
1743-
message=message,
1744-
)
1745-
1746-
17471636
@router.post(
17481637
"/myeclpay/transfer/init",
17491638
response_model=schemas_payment.PaymentUrl,

app/core/myeclpay/types_myeclpay.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ class RequestStatus(str, Enum):
4949

5050
class TransferType(str, Enum):
5151
HELLO_ASSO = "hello_asso"
52-
CHECK = "check"
53-
CASH = "cash"
54-
BANK_TRANSFER = "bank_transfer"
5552

5653

5754
class ActionType(str, Enum):

app/modules/raid/cruds_raid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ async def delete_all_invite_tokens(
208208
db: AsyncSession,
209209
) -> None:
210210
await db.execute(delete(models_raid.InviteToken))
211-
await db.commit()
211+
await db.flush()
212212

213213

214214
async def delete_team(

0 commit comments

Comments
 (0)