diff --git a/crud.py b/crud.py index a7b4491..2c0c51d 100644 --- a/crud.py +++ b/crud.py @@ -17,6 +17,7 @@ IdentifierRanking, Nip5Settings, PublicDomain, + UserSetting, ) db = Database("ext_nostrnip5") @@ -304,14 +305,21 @@ async def delete_inferior_ranking(name: str, rank: int): ) -async def create_settings(settings: Nip5Settings): - assert settings.owner_id, "Missing owner ID." - await db.insert("nostrnip5.settings", settings) +async def create_settings(settings: UserSetting) -> UserSetting: + user_settings = await get_settings(settings.owner_id) + if not user_settings: + await db.insert("nostrnip5.settings", settings) + else: + await db.update("nostrnip5.settings", settings, "WHERE owner_id = :owner_id") + return settings async def get_settings(owner_id: str) -> Optional[Nip5Settings]: - return await db.fetchone( + user_settings = await db.fetchone( "SELECT * FROM nostrnip5.settings WHERE owner_id = :owner_id", {"owner_id": owner_id}, - Nip5Settings, + UserSetting, ) + if user_settings: + return user_settings.settings + return None diff --git a/migrations.py b/migrations.py index 2532b70..ddbb1b8 100644 --- a/migrations.py +++ b/migrations.py @@ -1,5 +1,3 @@ -import json - from lnbits.db import Database @@ -102,4 +100,3 @@ async def m007_add_cost_extra_column_to_addresses(db: Database): "ALTER TABLE nostrnip5.addresses ADD COLUMN " "reimburse_amount REAL NOT NULL DEFAULT 0" ) - diff --git a/models.py b/models.py index 2ace8ad..c758f1e 100644 --- a/models.py +++ b/models.py @@ -354,7 +354,11 @@ class AddressFilters(FilterModel): class Nip5Settings(BaseModel): - owner_id: Optional[str] = None cloudflare_access_token: Optional[str] = None lnaddress_api_admin_key: Optional[str] = "" lnaddress_api_endpoint: Optional[str] = "https://nostr.com" + + +class UserSetting(BaseModel): + owner_id: str + settings: Nip5Settings diff --git a/static/js/index.js b/static/js/index.js index b9a3ca0..41c6b08 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -1,5 +1,5 @@ const mapDomain = function (obj) { - obj.time = Quasar.utils.date.formatDate( + obj.time = Quasar.date.formatDate( new Date(obj.time * 1000), 'YYYY-MM-DD HH:mm' ) diff --git a/views_api.py b/views_api.py index 5ea2410..a6bb78b 100644 --- a/views_api.py +++ b/views_api.py @@ -54,6 +54,7 @@ Nip5Settings, RotateAddressData, UpdateAddressData, + UserSetting, ) from .services import ( activate_address, @@ -609,8 +610,8 @@ async def api_settings_create_or_update( user: User = Depends(check_user_exists), ): owner_id = owner_id_from_user_id("admin" if user.admin else user.id) - settings.owner_id = owner_id - await create_settings(settings) + user_settings = UserSetting(owner_id=owner_id, settings=settings) + await create_settings(user_settings) @nostrnip5_api_router.get("/api/v1/settings") @@ -619,6 +620,5 @@ async def api_get_settings( ) -> Nip5Settings: owner_id = owner_id_from_user_id("admin" if user.admin else user.id) nip5_settings = await get_settings(owner_id) - if not nip5_settings: - raise HTTPException(HTTPStatus.NOT_FOUND, "Settings for user not found.") - return nip5_settings + + return nip5_settings or Nip5Settings()