|
22 | 22 | format_cancel_log, |
23 | 23 | format_refund_log, |
24 | 24 | format_transaction_log, |
25 | | - format_transfer_log, |
26 | 25 | ) |
27 | 26 | from app.core.myeclpay.models_myeclpay import Store, WalletDevice |
28 | 27 | from app.core.myeclpay.types_myeclpay import ( |
@@ -737,10 +736,18 @@ async def delete_store( |
737 | 736 | wallet_id=store.wallet_id, |
738 | 737 | db=db, |
739 | 738 | ) |
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: |
741 | 748 | raise HTTPException( |
742 | 749 | 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", |
744 | 751 | ) |
745 | 752 |
|
746 | 753 | sellers = await cruds_myeclpay.get_sellers_by_store_id( |
@@ -891,38 +898,32 @@ async def update_store_seller( |
891 | 898 | **The user must have the `can_manage_sellers` permission for this store** |
892 | 899 | """ |
893 | 900 |
|
894 | | - seller_admin = await cruds_myeclpay.get_seller( |
895 | | - user_id=user.id, |
| 901 | + store = await cruds_myeclpay.get_store( |
896 | 902 | store_id=store_id, |
897 | 903 | db=db, |
898 | 904 | ) |
899 | | - if seller_admin is None or not seller_admin.can_manage_sellers: |
| 905 | + if store is None: |
900 | 906 | 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", |
903 | 909 | ) |
904 | 910 |
|
905 | | - store = await cruds_myeclpay.get_store( |
| 911 | + seller_admin = await cruds_myeclpay.get_seller( |
| 912 | + user_id=user.id, |
906 | 913 | store_id=store_id, |
907 | 914 | db=db, |
908 | 915 | ) |
909 | | - if store is None: |
| 916 | + if seller_admin is None or not seller_admin.can_manage_sellers: |
910 | 917 | 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", |
913 | 920 | ) |
914 | 921 |
|
915 | 922 | structure = await cruds_myeclpay.get_structure_by_id( |
916 | 923 | structure_id=store.structure_id, |
917 | 924 | db=db, |
918 | 925 | ) |
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: |
926 | 927 | raise HTTPException( |
927 | 928 | status_code=400, |
928 | 929 | detail="User is the manager for this structure and cannot be updated as a seller", |
@@ -964,37 +965,32 @@ async def delete_store_seller( |
964 | 965 |
|
965 | 966 | **The user must have the `can_manage_sellers` permission for this store** |
966 | 967 | """ |
967 | | - seller_admin = await cruds_myeclpay.get_seller( |
968 | | - user_id=user.id, |
| 968 | + store = await cruds_myeclpay.get_store( |
969 | 969 | store_id=store_id, |
970 | 970 | db=db, |
971 | 971 | ) |
972 | | - if seller_admin is None or not seller_admin.can_manage_sellers: |
| 972 | + if store is None: |
973 | 973 | 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", |
976 | 976 | ) |
977 | 977 |
|
978 | | - store = await cruds_myeclpay.get_store( |
| 978 | + seller_admin = await cruds_myeclpay.get_seller( |
| 979 | + user_id=user.id, |
979 | 980 | store_id=store_id, |
980 | 981 | db=db, |
981 | 982 | ) |
982 | | - if store is None: |
| 983 | + if seller_admin is None or not seller_admin.can_manage_sellers: |
983 | 984 | 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", |
986 | 987 | ) |
987 | 988 |
|
988 | 989 | structure = await cruds_myeclpay.get_structure_by_id( |
989 | 990 | structure_id=store.structure_id, |
990 | 991 | db=db, |
991 | 992 | ) |
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: |
998 | 994 | raise HTTPException( |
999 | 995 | status_code=400, |
1000 | 996 | 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( |
1637 | 1633 | return history |
1638 | 1634 |
|
1639 | 1635 |
|
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 | | - |
1747 | 1636 | @router.post( |
1748 | 1637 | "/myeclpay/transfer/init", |
1749 | 1638 | response_model=schemas_payment.PaymentUrl, |
|
0 commit comments