We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Usermanager: shows wrong key
Usermanager API sidebar shows invoice API keys for most operations but it should be the admin key.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `lnbits/extensions/usermanager/views_api.py`
Content:
```
1 from http import HTTPStatus
2 
3 from fastapi import Query
4 from fastapi.params import Depends
5 from starlette.exceptions import HTTPException
6 
7 from lnbits.core import update_user_extension
8 from lnbits.core.crud import get_user
9 from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key
10 
11 from . import usermanager_ext
12 from .crud import (
13     create_usermanager_user,
14     create_usermanager_wallet,
15     delete_usermanager_user,
16     delete_usermanager_wallet,
17     get_usermanager_user,
18     get_usermanager_users,
19     get_usermanager_users_wallets,
20     get_usermanager_wallet,
21     get_usermanager_wallet_transactions,
22     get_usermanager_wallets,
23 )
24 from .models import CreateUserData, CreateUserWallet
25 
26 # Users
27 
28 
29 @usermanager_ext.get("/api/v1/users", status_code=HTTPStatus.OK)
30 async def api_usermanager_users(wallet: WalletTypeInfo = Depends(require_admin_key)):
31     user_id = wallet.wallet.user
32     return [user.dict() for user in await get_usermanager_users(user_id)]
33 
34 
35 @usermanager_ext.get("/api/v1/users/{user_id}", status_code=HTTPStatus.OK)
36 async def api_usermanager_user(user_id, wallet: WalletTypeInfo = Depends(get_key_type)):
37     user = await get_usermanager_user(user_id)
38     return user.dict()
39 
40 
41 @usermanager_ext.post("/api/v1/users", status_code=HTTPStatus.CREATED)
42 async def api_usermanager_users_create(
43     data: CreateUserData, wallet: WalletTypeInfo = Depends(get_key_type)
44 ):
45     user = await create_usermanager_user(data)
46     full = user.dict()
47     full["wallets"] = [
48         wallet.dict() for wallet in await get_usermanager_users_wallets(user.id)
49     ]
50     return full
51 
52 
53 @usermanager_ext.delete("/api/v1/users/{user_id}")
54 async def api_usermanager_users_delete(
55     user_id, wallet: WalletTypeInfo = Depends(require_admin_key)
56 ):
57     user = await get_usermanager_user(user_id)
58     if not user:
59         raise HTTPException(
60             status_code=HTTPStatus.NOT_FOUND, detail="User does not exist."
61         )
62     await delete_usermanager_user(user_id)
63     raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
64 
65 
66 # Activate Extension
67 
68 
69 @usermanager_ext.post("/api/v1/extensions")
70 async def api_usermanager_activate_extension(
71     extension: str = Query(...), userid: str = Query(...), active: bool = Query(...)
72 ):
73     user = await get_user(userid)
74     if not user:
75         raise HTTPException(
76             status_code=HTTPStatus.NOT_FOUND, detail="User does not exist."
77         )
78     await update_user_extension(user_id=userid, extension=extension, active=active)
79     return {"extension": "updated"}
80 
81 
82 # Wallets
83 
84 
85 @usermanager_ext.post("/api/v1/wallets")
86 async def api_usermanager_wallets_create(
87     data: CreateUserWallet, wallet: WalletTypeInfo = Depends(get_key_type)
88 ):
89     user = await create_usermanager_wallet(
90         user_id=data.user_id, wallet_name=data.wallet_name, admin_id=data.admin_id
91     )
92     return user.dict()
93 
94 
95 @usermanager_ext.get("/api/v1/wallets")
96 async def api_usermanager_wallets(wallet: WalletTypeInfo = Depends(require_admin_key)):
97     admin_id = wallet.wallet.user
98     return [wallet.dict() for wallet in await get_usermanager_wallets(admin_id)]
99 
100 
101 @usermanager_ext.get("/api/v1/transactions/{wallet_id}")
102 async def api_usermanager_wallet_transactions(
103     wallet_id, wallet: WalletTypeInfo = Depends(get_key_type)
104 ):
105     return await get_usermanager_wallet_transactions(wallet_id)
106 
107 
108 @usermanager_ext.get("/api/v1/wallets/{user_id}")
109 async def api_usermanager_users_wallets(
110     user_id, wallet: WalletTypeInfo = Depends(require_admin_key)
111 ):
112     return [
113         s_wallet.dict() for s_wallet in await get_usermanager_users_wallets(user_id)
114     ]
115 
116 
117 @usermanager_ext.delete("/api/v1/wallets/{wallet_id}")
118 async def api_usermanager_wallets_delete(
119     wallet_id, wallet: WalletTypeInfo = Depends(require_admin_key)
120 ):
121     get_wallet = await get_usermanager_wallet(wallet_id)
122     if not get_wallet:
123         raise HTTPException(
124             status_code=HTTPStatus.NOT_FOUND, detail="Wallet does not exist."
125         )
126     await delete_usermanager_wallet(wallet_id, get_wallet.user)
127     raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
128
```

Path: `lnbits/extensions/usermanager/crud.py`
Content:
```
1 from typing import List, Optional
2 
3 from lnbits.core.crud import (
4     create_account,
5     create_wallet,
6     delete_wallet,
7     get_payments,
8     get_user,
9 )
10 from lnbits.core.models import Payment
11 
12 from . import db
13 from .models import CreateUserData, Users, Wallets
14 
15 ### Users
16 
17 
18 async def create_usermanager_user(data: CreateUserData) -> Users:
19     account = await create_account()
20     user = await get_user(account.id)
21     assert user, "Newly created user couldn't be retrieved"
22 
23     wallet = await create_wallet(user_id=user.id, wallet_name=data.wallet_name)
24 
25     await db.execute(
26         """
27         INSERT INTO usermanager.users (id, name, admin, email, password)
28         VALUES (?, ?, ?, ?, ?)
29         """,
30         (user.id, data.user_name, data.admin_id, data.email, data.password),
31     )
32 
33     await db.execute(
34         """
35         INSERT INTO usermanager.wallets (id, admin, name, "user", adminkey, inkey)
36         VALUES (?, ?, ?, ?, ?, ?)
37         """,
38         (
39             wallet.id,
40             data.admin_id,
41             data.wallet_name,
42             user.id,
43             wallet.adminkey,
44             wallet.inkey,
45         ),
46     )
47 
48     user_created = await get_usermanager_user(user.id)
49     assert user_created, "Newly created user couldn't be retrieved"
50     return user_created
51 
52 
53 async def get_usermanager_user(user_id: str) -> Optional[Users]:
54     row = await db.fetchone("SELECT * FROM usermanager.users WHERE id = ?", (user_id,))
55     return Users(**row) if row else None
56 
57 
58 async def get_usermanager_users(user_id: str) -> List[Users]:
59     rows = await db.fetchall(
60         "SELECT * FROM usermanager.users WHERE admin = ?", (user_id,)
61     )
62 
63     return [Users(**row) for row in rows]
64 
65 
66 async def delete_usermanager_user(user_id: str) -> None:
67     wallets = await get_usermanager_wallets(user_id)
68     for wallet in wallets:
69         await delete_wallet(user_id=user_id, wallet_id=wallet.id)
70 
71     await db.execute("DELETE FROM usermanager.users WHERE id = ?", (user_id,))
72     await db.execute("""DELETE FROM usermanager.wallets WHERE "user" = ?""", (user_id,))
73 
74 
75 ### Wallets
76 
77 
78 async def create_usermanager_wallet(
79     user_id: str, wallet_name: str, admin_id: str
80 ) -> Wallets:
81     wallet = await create_wallet(user_id=user_id, wallet_name=wallet_name)
82     await db.execute(
83         """
84         INSERT INTO usermanager.wallets (id, admin, name, "user", adminkey, inkey)
85         VALUES (?, ?, ?, ?, ?, ?)
86         """,
87         (wallet.id, admin_id, wallet_name, user_id, wallet.adminkey, wallet.inkey),
88     )
89     wallet_created = await get_usermanager_wallet(wallet.id)
90     assert wallet_created, "Newly created wallet couldn't be retrieved"
91     return wallet_created
92 
93 
94 async def get_usermanager_wallet(wallet_id: str) -> Optional[Wallets]:
95     row = await db.fetchone(
96         "SELECT * FROM usermanager.wallets WHERE id = ?", (wallet_id,)
97     )
98     return Wallets(**row) if row else None
99 
100 
101 async def get_usermanager_wallets(admin_id: str) -> Optional[Wallets]:
102     rows = await db.fetchall(
103         "SELECT * FROM usermanager.wallets WHERE admin = ?", (admin_id,)
104     )
105     return [Wallets(**row) for row in rows]
106 
107 
108 async def get_usermanager_users_wallets(user_id: str) -> Optional[Wallets]:
109     rows = await db.fetchall(
110         """SELECT * FROM usermanager.wallets WHERE "user" = ?""", (user_id,)
111     )
112     return [Wallets(**row) for row in rows]
113 
114 
115 async def get_usermanager_wallet_transactions(wallet_id: str) -> Optional[Payment]:
116     return await get_payments(
117         wallet_id=wallet_id, complete=True, pending=False, outgoing=True, incoming=True
118     )
119 
120 
121 async def delete_usermanager_wallet(wallet_id: str, user_id: str) -> None:
122     await delete_wallet(user_id=user_id, wallet_id=wallet_id)
123     await db.execute("DELETE FROM usermanager.wallets WHERE id = ?", (wallet_id,))
124
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/lnbits/extensions/usermanager/crud.py b/lnbits/extensions/usermanager/crud.py
--- a/lnbits/extensions/usermanager/crud.py
+++ b/lnbits/extensions/usermanager/crud.py
@@ -63,10 +63,11 @@
     return [Users(**row) for row in rows]
 
 
-async def delete_usermanager_user(user_id: str) -> None:
-    wallets = await get_usermanager_wallets(user_id)
-    for wallet in wallets:
-        await delete_wallet(user_id=user_id, wallet_id=wallet.id)
+async def delete_usermanager_user(user_id: str, delete_core: bool = True) -> None:
+    if delete_core:
+        wallets = await get_usermanager_wallets(user_id)
+        for wallet in wallets:
+            await delete_wallet(user_id=user_id, wallet_id=wallet.id)
 
     await db.execute("DELETE FROM usermanager.users WHERE id = ?", (user_id,))
     await db.execute("""DELETE FROM usermanager.wallets WHERE "user" = ?""", (user_id,))
diff --git a/lnbits/extensions/usermanager/views_api.py b/lnbits/extensions/usermanager/views_api.py
--- a/lnbits/extensions/usermanager/views_api.py
+++ b/lnbits/extensions/usermanager/views_api.py
@@ -52,15 +52,17 @@
 
 @usermanager_ext.delete("/api/v1/users/{user_id}")
 async def api_usermanager_users_delete(
-    user_id, wallet: WalletTypeInfo = Depends(require_admin_key)
+    user_id,
+    delete_core: bool = Query(True),
+    wallet: WalletTypeInfo = Depends(require_admin_key),
 ):
     user = await get_usermanager_user(user_id)
     if not user:
         raise HTTPException(
             status_code=HTTPStatus.NOT_FOUND, detail="User does not exist."
         )
-    await delete_usermanager_user(user_id)
-    raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
+    await delete_usermanager_user(user_id, delete_core)
+    return "", HTTPStatus.NO_CONTENT
 
 
 # Activate Extension
@@ -124,4 +126,4 @@
             status_code=HTTPStatus.NOT_FOUND, detail="Wallet does not exist."
         )
     await delete_usermanager_wallet(wallet_id, get_wallet.user)
-    raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
+    return "", HTTPStatus.NO_CONTENT

{"golden_diff": "diff --git a/lnbits/extensions/usermanager/crud.py b/lnbits/extensions/usermanager/crud.py\n--- a/lnbits/extensions/usermanager/crud.py\n+++ b/lnbits/extensions/usermanager/crud.py\n@@ -63,10 +63,11 @@\n     return [Users(**row) for row in rows]\n \n \n-async def delete_usermanager_user(user_id: str) -> None:\n-    wallets = await get_usermanager_wallets(user_id)\n-    for wallet in wallets:\n-        await delete_wallet(user_id=user_id, wallet_id=wallet.id)\n+async def delete_usermanager_user(user_id: str, delete_core: bool = True) -> None:\n+    if delete_core:\n+        wallets = await get_usermanager_wallets(user_id)\n+        for wallet in wallets:\n+            await delete_wallet(user_id=user_id, wallet_id=wallet.id)\n \n     await db.execute(\"DELETE FROM usermanager.users WHERE id = ?\", (user_id,))\n     await db.execute(\"\"\"DELETE FROM usermanager.wallets WHERE \"user\" = ?\"\"\", (user_id,))\ndiff --git a/lnbits/extensions/usermanager/views_api.py b/lnbits/extensions/usermanager/views_api.py\n--- a/lnbits/extensions/usermanager/views_api.py\n+++ b/lnbits/extensions/usermanager/views_api.py\n@@ -52,15 +52,17 @@\n \n @usermanager_ext.delete(\"/api/v1/users/{user_id}\")\n async def api_usermanager_users_delete(\n-    user_id, wallet: WalletTypeInfo = Depends(require_admin_key)\n+    user_id,\n+    delete_core: bool = Query(True),\n+    wallet: WalletTypeInfo = Depends(require_admin_key),\n ):\n     user = await get_usermanager_user(user_id)\n     if not user:\n         raise HTTPException(\n             status_code=HTTPStatus.NOT_FOUND, detail=\"User does not exist.\"\n         )\n-    await delete_usermanager_user(user_id)\n-    raise HTTPException(status_code=HTTPStatus.NO_CONTENT)\n+    await delete_usermanager_user(user_id, delete_core)\n+    return \"\", HTTPStatus.NO_CONTENT\n \n \n # Activate Extension\n@@ -124,4 +126,4 @@\n             status_code=HTTPStatus.NOT_FOUND, detail=\"Wallet does not exist.\"\n         )\n     await delete_usermanager_wallet(wallet_id, get_wallet.user)\n-    raise HTTPException(status_code=HTTPStatus.NO_CONTENT)\n+    return \"\", HTTPStatus.NO_CONTENT\n", "issue": "Usermanager: shows wrong key\nUsermanager API sidebar shows invoice API keys for most operations but it should be the admin key.\n", "before_files": [{"content": "from http import HTTPStatus\n\nfrom fastapi import Query\nfrom fastapi.params import Depends\nfrom starlette.exceptions import HTTPException\n\nfrom lnbits.core import update_user_extension\nfrom lnbits.core.crud import get_user\nfrom lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key\n\nfrom . import usermanager_ext\nfrom .crud import (\n    create_usermanager_user,\n    create_usermanager_wallet,\n    delete_usermanager_user,\n    delete_usermanager_wallet,\n    get_usermanager_user,\n    get_usermanager_users,\n    get_usermanager_users_wallets,\n    get_usermanager_wallet,\n    get_usermanager_wallet_transactions,\n    get_usermanager_wallets,\n)\nfrom .models import CreateUserData, CreateUserWallet\n\n# Users\n\n\n@usermanager_ext.get(\"/api/v1/users\", status_code=HTTPStatus.OK)\nasync def api_usermanager_users(wallet: WalletTypeInfo = Depends(require_admin_key)):\n    user_id = wallet.wallet.user\n    return [user.dict() for user in await get_usermanager_users(user_id)]\n\n\n@usermanager_ext.get(\"/api/v1/users/{user_id}\", status_code=HTTPStatus.OK)\nasync def api_usermanager_user(user_id, wallet: WalletTypeInfo = Depends(get_key_type)):\n    user = await get_usermanager_user(user_id)\n    return user.dict()\n\n\n@usermanager_ext.post(\"/api/v1/users\", status_code=HTTPStatus.CREATED)\nasync def api_usermanager_users_create(\n    data: CreateUserData, wallet: WalletTypeInfo = Depends(get_key_type)\n):\n    user = await create_usermanager_user(data)\n    full = user.dict()\n    full[\"wallets\"] = [\n        wallet.dict() for wallet in await get_usermanager_users_wallets(user.id)\n    ]\n    return full\n\n\n@usermanager_ext.delete(\"/api/v1/users/{user_id}\")\nasync def api_usermanager_users_delete(\n    user_id, wallet: WalletTypeInfo = Depends(require_admin_key)\n):\n    user = await get_usermanager_user(user_id)\n    if not user:\n        raise HTTPException(\n            status_code=HTTPStatus.NOT_FOUND, detail=\"User does not exist.\"\n        )\n    await delete_usermanager_user(user_id)\n    raise HTTPException(status_code=HTTPStatus.NO_CONTENT)\n\n\n# Activate Extension\n\n\n@usermanager_ext.post(\"/api/v1/extensions\")\nasync def api_usermanager_activate_extension(\n    extension: str = Query(...), userid: str = Query(...), active: bool = Query(...)\n):\n    user = await get_user(userid)\n    if not user:\n        raise HTTPException(\n            status_code=HTTPStatus.NOT_FOUND, detail=\"User does not exist.\"\n        )\n    await update_user_extension(user_id=userid, extension=extension, active=active)\n    return {\"extension\": \"updated\"}\n\n\n# Wallets\n\n\n@usermanager_ext.post(\"/api/v1/wallets\")\nasync def api_usermanager_wallets_create(\n    data: CreateUserWallet, wallet: WalletTypeInfo = Depends(get_key_type)\n):\n    user = await create_usermanager_wallet(\n        user_id=data.user_id, wallet_name=data.wallet_name, admin_id=data.admin_id\n    )\n    return user.dict()\n\n\n@usermanager_ext.get(\"/api/v1/wallets\")\nasync def api_usermanager_wallets(wallet: WalletTypeInfo = Depends(require_admin_key)):\n    admin_id = wallet.wallet.user\n    return [wallet.dict() for wallet in await get_usermanager_wallets(admin_id)]\n\n\n@usermanager_ext.get(\"/api/v1/transactions/{wallet_id}\")\nasync def api_usermanager_wallet_transactions(\n    wallet_id, wallet: WalletTypeInfo = Depends(get_key_type)\n):\n    return await get_usermanager_wallet_transactions(wallet_id)\n\n\n@usermanager_ext.get(\"/api/v1/wallets/{user_id}\")\nasync def api_usermanager_users_wallets(\n    user_id, wallet: WalletTypeInfo = Depends(require_admin_key)\n):\n    return [\n        s_wallet.dict() for s_wallet in await get_usermanager_users_wallets(user_id)\n    ]\n\n\n@usermanager_ext.delete(\"/api/v1/wallets/{wallet_id}\")\nasync def api_usermanager_wallets_delete(\n    wallet_id, wallet: WalletTypeInfo = Depends(require_admin_key)\n):\n    get_wallet = await get_usermanager_wallet(wallet_id)\n    if not get_wallet:\n        raise HTTPException(\n            status_code=HTTPStatus.NOT_FOUND, detail=\"Wallet does not exist.\"\n        )\n    await delete_usermanager_wallet(wallet_id, get_wallet.user)\n    raise HTTPException(status_code=HTTPStatus.NO_CONTENT)\n", "path": "lnbits/extensions/usermanager/views_api.py"}, {"content": "from typing import List, Optional\n\nfrom lnbits.core.crud import (\n    create_account,\n    create_wallet,\n    delete_wallet,\n    get_payments,\n    get_user,\n)\nfrom lnbits.core.models import Payment\n\nfrom . import db\nfrom .models import CreateUserData, Users, Wallets\n\n### Users\n\n\nasync def create_usermanager_user(data: CreateUserData) -> Users:\n    account = await create_account()\n    user = await get_user(account.id)\n    assert user, \"Newly created user couldn't be retrieved\"\n\n    wallet = await create_wallet(user_id=user.id, wallet_name=data.wallet_name)\n\n    await db.execute(\n        \"\"\"\n        INSERT INTO usermanager.users (id, name, admin, email, password)\n        VALUES (?, ?, ?, ?, ?)\n        \"\"\",\n        (user.id, data.user_name, data.admin_id, data.email, data.password),\n    )\n\n    await db.execute(\n        \"\"\"\n        INSERT INTO usermanager.wallets (id, admin, name, \"user\", adminkey, inkey)\n        VALUES (?, ?, ?, ?, ?, ?)\n        \"\"\",\n        (\n            wallet.id,\n            data.admin_id,\n            data.wallet_name,\n            user.id,\n            wallet.adminkey,\n            wallet.inkey,\n        ),\n    )\n\n    user_created = await get_usermanager_user(user.id)\n    assert user_created, \"Newly created user couldn't be retrieved\"\n    return user_created\n\n\nasync def get_usermanager_user(user_id: str) -> Optional[Users]:\n    row = await db.fetchone(\"SELECT * FROM usermanager.users WHERE id = ?\", (user_id,))\n    return Users(**row) if row else None\n\n\nasync def get_usermanager_users(user_id: str) -> List[Users]:\n    rows = await db.fetchall(\n        \"SELECT * FROM usermanager.users WHERE admin = ?\", (user_id,)\n    )\n\n    return [Users(**row) for row in rows]\n\n\nasync def delete_usermanager_user(user_id: str) -> None:\n    wallets = await get_usermanager_wallets(user_id)\n    for wallet in wallets:\n        await delete_wallet(user_id=user_id, wallet_id=wallet.id)\n\n    await db.execute(\"DELETE FROM usermanager.users WHERE id = ?\", (user_id,))\n    await db.execute(\"\"\"DELETE FROM usermanager.wallets WHERE \"user\" = ?\"\"\", (user_id,))\n\n\n### Wallets\n\n\nasync def create_usermanager_wallet(\n    user_id: str, wallet_name: str, admin_id: str\n) -> Wallets:\n    wallet = await create_wallet(user_id=user_id, wallet_name=wallet_name)\n    await db.execute(\n        \"\"\"\n        INSERT INTO usermanager.wallets (id, admin, name, \"user\", adminkey, inkey)\n        VALUES (?, ?, ?, ?, ?, ?)\n        \"\"\",\n        (wallet.id, admin_id, wallet_name, user_id, wallet.adminkey, wallet.inkey),\n    )\n    wallet_created = await get_usermanager_wallet(wallet.id)\n    assert wallet_created, \"Newly created wallet couldn't be retrieved\"\n    return wallet_created\n\n\nasync def get_usermanager_wallet(wallet_id: str) -> Optional[Wallets]:\n    row = await db.fetchone(\n        \"SELECT * FROM usermanager.wallets WHERE id = ?\", (wallet_id,)\n    )\n    return Wallets(**row) if row else None\n\n\nasync def get_usermanager_wallets(admin_id: str) -> Optional[Wallets]:\n    rows = await db.fetchall(\n        \"SELECT * FROM usermanager.wallets WHERE admin = ?\", (admin_id,)\n    )\n    return [Wallets(**row) for row in rows]\n\n\nasync def get_usermanager_users_wallets(user_id: str) -> Optional[Wallets]:\n    rows = await db.fetchall(\n        \"\"\"SELECT * FROM usermanager.wallets WHERE \"user\" = ?\"\"\", (user_id,)\n    )\n    return [Wallets(**row) for row in rows]\n\n\nasync def get_usermanager_wallet_transactions(wallet_id: str) -> Optional[Payment]:\n    return await get_payments(\n        wallet_id=wallet_id, complete=True, pending=False, outgoing=True, incoming=True\n    )\n\n\nasync def delete_usermanager_wallet(wallet_id: str, user_id: str) -> None:\n    await delete_wallet(user_id=user_id, wallet_id=wallet_id)\n    await db.execute(\"DELETE FROM usermanager.wallets WHERE id = ?\", (wallet_id,))\n", "path": "lnbits/extensions/usermanager/crud.py"}], "after_files": [{"content": "from http import HTTPStatus\n\nfrom fastapi import Query\nfrom fastapi.params import Depends\nfrom starlette.exceptions import HTTPException\n\nfrom lnbits.core import update_user_extension\nfrom lnbits.core.crud import get_user\nfrom lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key\n\nfrom . import usermanager_ext\nfrom .crud import (\n    create_usermanager_user,\n    create_usermanager_wallet,\n    delete_usermanager_user,\n    delete_usermanager_wallet,\n    get_usermanager_user,\n    get_usermanager_users,\n    get_usermanager_users_wallets,\n    get_usermanager_wallet,\n    get_usermanager_wallet_transactions,\n    get_usermanager_wallets,\n)\nfrom .models import CreateUserData, CreateUserWallet\n\n# Users\n\n\n@usermanager_ext.get(\"/api/v1/users\", status_code=HTTPStatus.OK)\nasync def api_usermanager_users(wallet: WalletTypeInfo = Depends(require_admin_key)):\n    user_id = wallet.wallet.user\n    return [user.dict() for user in await get_usermanager_users(user_id)]\n\n\n@usermanager_ext.get(\"/api/v1/users/{user_id}\", status_code=HTTPStatus.OK)\nasync def api_usermanager_user(user_id, wallet: WalletTypeInfo = Depends(get_key_type)):\n    user = await get_usermanager_user(user_id)\n    return user.dict()\n\n\n@usermanager_ext.post(\"/api/v1/users\", status_code=HTTPStatus.CREATED)\nasync def api_usermanager_users_create(\n    data: CreateUserData, wallet: WalletTypeInfo = Depends(get_key_type)\n):\n    user = await create_usermanager_user(data)\n    full = user.dict()\n    full[\"wallets\"] = [\n        wallet.dict() for wallet in await get_usermanager_users_wallets(user.id)\n    ]\n    return full\n\n\n@usermanager_ext.delete(\"/api/v1/users/{user_id}\")\nasync def api_usermanager_users_delete(\n    user_id,\n    delete_core: bool = Query(True),\n    wallet: WalletTypeInfo = Depends(require_admin_key),\n):\n    user = await get_usermanager_user(user_id)\n    if not user:\n        raise HTTPException(\n            status_code=HTTPStatus.NOT_FOUND, detail=\"User does not exist.\"\n        )\n    await delete_usermanager_user(user_id, delete_core)\n    return \"\", HTTPStatus.NO_CONTENT\n\n\n# Activate Extension\n\n\n@usermanager_ext.post(\"/api/v1/extensions\")\nasync def api_usermanager_activate_extension(\n    extension: str = Query(...), userid: str = Query(...), active: bool = Query(...)\n):\n    user = await get_user(userid)\n    if not user:\n        raise HTTPException(\n            status_code=HTTPStatus.NOT_FOUND, detail=\"User does not exist.\"\n        )\n    await update_user_extension(user_id=userid, extension=extension, active=active)\n    return {\"extension\": \"updated\"}\n\n\n# Wallets\n\n\n@usermanager_ext.post(\"/api/v1/wallets\")\nasync def api_usermanager_wallets_create(\n    data: CreateUserWallet, wallet: WalletTypeInfo = Depends(get_key_type)\n):\n    user = await create_usermanager_wallet(\n        user_id=data.user_id, wallet_name=data.wallet_name, admin_id=data.admin_id\n    )\n    return user.dict()\n\n\n@usermanager_ext.get(\"/api/v1/wallets\")\nasync def api_usermanager_wallets(wallet: WalletTypeInfo = Depends(require_admin_key)):\n    admin_id = wallet.wallet.user\n    return [wallet.dict() for wallet in await get_usermanager_wallets(admin_id)]\n\n\n@usermanager_ext.get(\"/api/v1/transactions/{wallet_id}\")\nasync def api_usermanager_wallet_transactions(\n    wallet_id, wallet: WalletTypeInfo = Depends(get_key_type)\n):\n    return await get_usermanager_wallet_transactions(wallet_id)\n\n\n@usermanager_ext.get(\"/api/v1/wallets/{user_id}\")\nasync def api_usermanager_users_wallets(\n    user_id, wallet: WalletTypeInfo = Depends(require_admin_key)\n):\n    return [\n        s_wallet.dict() for s_wallet in await get_usermanager_users_wallets(user_id)\n    ]\n\n\n@usermanager_ext.delete(\"/api/v1/wallets/{wallet_id}\")\nasync def api_usermanager_wallets_delete(\n    wallet_id, wallet: WalletTypeInfo = Depends(require_admin_key)\n):\n    get_wallet = await get_usermanager_wallet(wallet_id)\n    if not get_wallet:\n        raise HTTPException(\n            status_code=HTTPStatus.NOT_FOUND, detail=\"Wallet does not exist.\"\n        )\n    await delete_usermanager_wallet(wallet_id, get_wallet.user)\n    return \"\", HTTPStatus.NO_CONTENT\n", "path": "lnbits/extensions/usermanager/views_api.py"}, {"content": "from typing import List, Optional\n\nfrom lnbits.core.crud import (\n    create_account,\n    create_wallet,\n    delete_wallet,\n    get_payments,\n    get_user,\n)\nfrom lnbits.core.models import Payment\n\nfrom . import db\nfrom .models import CreateUserData, Users, Wallets\n\n### Users\n\n\nasync def create_usermanager_user(data: CreateUserData) -> Users:\n    account = await create_account()\n    user = await get_user(account.id)\n    assert user, \"Newly created user couldn't be retrieved\"\n\n    wallet = await create_wallet(user_id=user.id, wallet_name=data.wallet_name)\n\n    await db.execute(\n        \"\"\"\n        INSERT INTO usermanager.users (id, name, admin, email, password)\n        VALUES (?, ?, ?, ?, ?)\n        \"\"\",\n        (user.id, data.user_name, data.admin_id, data.email, data.password),\n    )\n\n    await db.execute(\n        \"\"\"\n        INSERT INTO usermanager.wallets (id, admin, name, \"user\", adminkey, inkey)\n        VALUES (?, ?, ?, ?, ?, ?)\n        \"\"\",\n        (\n            wallet.id,\n            data.admin_id,\n            data.wallet_name,\n            user.id,\n            wallet.adminkey,\n            wallet.inkey,\n        ),\n    )\n\n    user_created = await get_usermanager_user(user.id)\n    assert user_created, \"Newly created user couldn't be retrieved\"\n    return user_created\n\n\nasync def get_usermanager_user(user_id: str) -> Optional[Users]:\n    row = await db.fetchone(\"SELECT * FROM usermanager.users WHERE id = ?\", (user_id,))\n    return Users(**row) if row else None\n\n\nasync def get_usermanager_users(user_id: str) -> List[Users]:\n    rows = await db.fetchall(\n        \"SELECT * FROM usermanager.users WHERE admin = ?\", (user_id,)\n    )\n\n    return [Users(**row) for row in rows]\n\n\nasync def delete_usermanager_user(user_id: str, delete_core: bool = True) -> None:\n    if delete_core:\n        wallets = await get_usermanager_wallets(user_id)\n        for wallet in wallets:\n            await delete_wallet(user_id=user_id, wallet_id=wallet.id)\n\n    await db.execute(\"DELETE FROM usermanager.users WHERE id = ?\", (user_id,))\n    await db.execute(\"\"\"DELETE FROM usermanager.wallets WHERE \"user\" = ?\"\"\", (user_id,))\n\n\n### Wallets\n\n\nasync def create_usermanager_wallet(\n    user_id: str, wallet_name: str, admin_id: str\n) -> Wallets:\n    wallet = await create_wallet(user_id=user_id, wallet_name=wallet_name)\n    await db.execute(\n        \"\"\"\n        INSERT INTO usermanager.wallets (id, admin, name, \"user\", adminkey, inkey)\n        VALUES (?, ?, ?, ?, ?, ?)\n        \"\"\",\n        (wallet.id, admin_id, wallet_name, user_id, wallet.adminkey, wallet.inkey),\n    )\n    wallet_created = await get_usermanager_wallet(wallet.id)\n    assert wallet_created, \"Newly created wallet couldn't be retrieved\"\n    return wallet_created\n\n\nasync def get_usermanager_wallet(wallet_id: str) -> Optional[Wallets]:\n    row = await db.fetchone(\n        \"SELECT * FROM usermanager.wallets WHERE id = ?\", (wallet_id,)\n    )\n    return Wallets(**row) if row else None\n\n\nasync def get_usermanager_wallets(admin_id: str) -> Optional[Wallets]:\n    rows = await db.fetchall(\n        \"SELECT * FROM usermanager.wallets WHERE admin = ?\", (admin_id,)\n    )\n    return [Wallets(**row) for row in rows]\n\n\nasync def get_usermanager_users_wallets(user_id: str) -> Optional[Wallets]:\n    rows = await db.fetchall(\n        \"\"\"SELECT * FROM usermanager.wallets WHERE \"user\" = ?\"\"\", (user_id,)\n    )\n    return [Wallets(**row) for row in rows]\n\n\nasync def get_usermanager_wallet_transactions(wallet_id: str) -> Optional[Payment]:\n    return await get_payments(\n        wallet_id=wallet_id, complete=True, pending=False, outgoing=True, incoming=True\n    )\n\n\nasync def delete_usermanager_wallet(wallet_id: str, user_id: str) -> None:\n    await delete_wallet(user_id=user_id, wallet_id=wallet_id)\n    await db.execute(\"DELETE FROM usermanager.wallets WHERE id = ?\", (wallet_id,))\n", "path": "lnbits/extensions/usermanager/crud.py"}]}