From d20e258649ae78f7de134bd7661092ab164b5b7d Mon Sep 17 00:00:00 2001 From: ekzyis Date: Thu, 4 Jul 2024 02:48:20 +0200 Subject: [PATCH] Consistent logs between local and server wallets * 'wallet attached' on create * 'wallet updated' on config updates * 'wallet enabled' and 'wallet disabled' if checkbox changed * 'wallet detached' on delete --- api/resolvers/wallet.js | 34 ++++++++++++++++++++++++++-------- components/wallet/index.js | 16 +++++++++------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/api/resolvers/wallet.js b/api/resolvers/wallet.js index 8b1c1331..c73e0dc7 100644 --- a/api/resolvers/wallet.js +++ b/api/resolvers/wallet.js @@ -318,9 +318,10 @@ export default { where: { userId: me.id }, - orderBy: { - createdAt: 'asc' - } + orderBy: [ + { createdAt: 'desc' }, + { id: 'desc' } + ] }) } }, @@ -513,7 +514,7 @@ export default { await models.$transaction([ models.wallet.delete({ where: { userId: me.id, id: Number(id) } }), - models.walletLog.create({ data: { userId: me.id, wallet: wallet.type, level: 'SUCCESS', message: 'wallet deleted' } }) + models.walletLog.create({ data: { userId: me.id, wallet: wallet.type, level: 'SUCCESS', message: 'wallet detached' } }) ]) return true @@ -663,8 +664,7 @@ async function upsertWallet ( } } } - }), - models.walletLog.create({ data: { userId: me.id, wallet: wallet.type, level: 'SUCCESS', message: 'wallet updated' } }) + }) ) } else { txs.push( @@ -677,11 +677,29 @@ async function upsertWallet ( create: walletData } } - }), - models.walletLog.create({ data: { userId: me.id, wallet: wallet.type, level: 'SUCCESS', message: 'wallet created' } }) + }) ) } + txs.push( + models.walletLog.createMany({ + data: { + userId: me.id, + wallet: wallet.type, + level: 'SUCCESS', + message: id ? 'wallet updated' : 'wallet attached' + } + }), + models.walletLog.create({ + data: { + userId: me.id, + wallet: wallet.type, + level: priority ? 'SUCCESS' : 'INFO', + message: priority ? 'wallet enabled' : 'wallet disabled' + } + }) + ) + await models.$transaction(txs) return true } diff --git a/components/wallet/index.js b/components/wallet/index.js index db2b7cf6..f9a87fbd 100644 --- a/components/wallet/index.js +++ b/components/wallet/index.js @@ -30,6 +30,7 @@ export function useWallet (name) { const { logger } = useWalletLogger(wallet) const [config, saveConfig, clearConfig] = useConfig(wallet) + const _isConfigured = isConfigured({ ...wallet, config }) const sendPayment = useCallback(async (bolt11) => { const hash = bolt11Tags(bolt11).payment_hash @@ -54,32 +55,33 @@ export function useWallet (name) { logger.info('wallet disabled') }, [name, me, logger]) - const save = useCallback(async (config) => { + const save = useCallback(async (newConfig) => { try { // validate should log custom INFO and OK message // validate is optional since validation might happen during save on server // TODO: add timeout - await wallet.validate?.({ me, logger, ...config }) - await saveConfig(config) - logger.ok('wallet attached') + await wallet.validate?.({ me, logger, ...newConfig }) + await saveConfig(newConfig) + logger.ok(_isConfigured ? 'wallet updated' : 'wallet attached') } catch (err) { const message = err.message || err.toString?.() logger.error('failed to attach: ' + message) throw err } - }, [saveConfig, me, logger]) + }, [_isConfigured, saveConfig, me, logger]) // delete is a reserved keyword const delete_ = useCallback(() => { try { clearConfig() logger.ok('wallet detached') + disable() } catch (err) { const message = err.message || err.toString?.() logger.error(message) throw err } - }, [clearConfig, logger]) + }, [clearConfig, logger, disable]) return { ...wallet, @@ -89,7 +91,7 @@ export function useWallet (name) { delete: delete_, enable, disable, - isConfigured: isConfigured({ ...wallet, config }), + isConfigured: _isConfigured, status: config?.enabled || config?.priority ? Status.Enabled : Status.Initialized, logger }