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
This commit is contained in:
ekzyis 2024-07-04 02:48:20 +02:00
parent d60e26bfdf
commit d20e258649
2 changed files with 35 additions and 15 deletions

View File

@ -318,9 +318,10 @@ export default {
where: { where: {
userId: me.id userId: me.id
}, },
orderBy: { orderBy: [
createdAt: 'asc' { createdAt: 'desc' },
} { id: 'desc' }
]
}) })
} }
}, },
@ -513,7 +514,7 @@ export default {
await models.$transaction([ await models.$transaction([
models.wallet.delete({ where: { userId: me.id, id: Number(id) } }), 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 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 { } else {
txs.push( txs.push(
@ -677,11 +677,29 @@ async function upsertWallet (
create: walletData 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) await models.$transaction(txs)
return true return true
} }

View File

@ -30,6 +30,7 @@ export function useWallet (name) {
const { logger } = useWalletLogger(wallet) const { logger } = useWalletLogger(wallet)
const [config, saveConfig, clearConfig] = useConfig(wallet) const [config, saveConfig, clearConfig] = useConfig(wallet)
const _isConfigured = isConfigured({ ...wallet, config })
const sendPayment = useCallback(async (bolt11) => { const sendPayment = useCallback(async (bolt11) => {
const hash = bolt11Tags(bolt11).payment_hash const hash = bolt11Tags(bolt11).payment_hash
@ -54,32 +55,33 @@ export function useWallet (name) {
logger.info('wallet disabled') logger.info('wallet disabled')
}, [name, me, logger]) }, [name, me, logger])
const save = useCallback(async (config) => { const save = useCallback(async (newConfig) => {
try { try {
// validate should log custom INFO and OK message // validate should log custom INFO and OK message
// validate is optional since validation might happen during save on server // validate is optional since validation might happen during save on server
// TODO: add timeout // TODO: add timeout
await wallet.validate?.({ me, logger, ...config }) await wallet.validate?.({ me, logger, ...newConfig })
await saveConfig(config) await saveConfig(newConfig)
logger.ok('wallet attached') logger.ok(_isConfigured ? 'wallet updated' : 'wallet attached')
} catch (err) { } catch (err) {
const message = err.message || err.toString?.() const message = err.message || err.toString?.()
logger.error('failed to attach: ' + message) logger.error('failed to attach: ' + message)
throw err throw err
} }
}, [saveConfig, me, logger]) }, [_isConfigured, saveConfig, me, logger])
// delete is a reserved keyword // delete is a reserved keyword
const delete_ = useCallback(() => { const delete_ = useCallback(() => {
try { try {
clearConfig() clearConfig()
logger.ok('wallet detached') logger.ok('wallet detached')
disable()
} catch (err) { } catch (err) {
const message = err.message || err.toString?.() const message = err.message || err.toString?.()
logger.error(message) logger.error(message)
throw err throw err
} }
}, [clearConfig, logger]) }, [clearConfig, logger, disable])
return { return {
...wallet, ...wallet,
@ -89,7 +91,7 @@ export function useWallet (name) {
delete: delete_, delete: delete_,
enable, enable,
disable, disable,
isConfigured: isConfigured({ ...wallet, config }), isConfigured: _isConfigured,
status: config?.enabled || config?.priority ? Status.Enabled : Status.Initialized, status: config?.enabled || config?.priority ? Status.Enabled : Status.Initialized,
logger logger
} }