Refactor wallet balance handling and integrate PaymentService for centralized management

- Replaced direct wallet balance computation in Navbar and WalletPage with a centralized totalBalance property from PaymentService, improving code maintainability.
- Updated CreateProductDialog, CreateStoreDialog, and MerchantStore components to utilize PaymentService for retrieving wallet admin and invoice keys, enhancing consistency across the application.
- These changes streamline wallet management and improve the overall architecture of the wallet module.
This commit is contained in:
padreug 2025-09-17 20:23:46 +02:00
parent c064b0b40d
commit e5db949aae
6 changed files with 69 additions and 21 deletions

View file

@ -33,13 +33,12 @@ const { navigation: modularNavigation, userMenuItems } = useModularNavigation()
const navigation = modularNavigation const navigation = modularNavigation
// Compute total wallet balance // Get PaymentService from DI for centralized wallet balance
const totalBalance = computed(() => { const paymentService = tryInjectService(SERVICE_TOKENS.PAYMENT_SERVICE) as any
if (!auth.currentUser.value?.wallets) return 0
return auth.currentUser.value.wallets.reduce((total, wallet) => { // Use PaymentService for total wallet balance
return total + (wallet.balance_msat || 0) const totalBalance = computed(() => {
}, 0) return paymentService?.totalBalance || 0
}) })
// Try to get chat service from DI (may not be available if chat module not loaded) // Try to get chat service from DI (may not be available if chat module not loaded)

View file

@ -59,6 +59,15 @@ export class PaymentService extends BaseService {
return this.authService?.user?.value?.wallets || [] return this.authService?.user?.value?.wallets || []
} }
/**
* Get total balance across all user wallets in millisatoshis
*/
get totalBalance(): number {
return this.userWallets.reduce((total: number, wallet: any) => {
return total + (wallet.balance_msat || 0)
}, 0)
}
/** /**
* Check if user has any wallet with balance * Check if user has any wallet with balance
*/ */

View file

@ -232,6 +232,7 @@ const emit = defineEmits<{
// Services // Services
const nostrmarketAPI = injectService(SERVICE_TOKENS.NOSTRMARKET_API) as NostrmarketAPI const nostrmarketAPI = injectService(SERVICE_TOKENS.NOSTRMARKET_API) as NostrmarketAPI
const paymentService = injectService(SERVICE_TOKENS.PAYMENT_SERVICE) as any
const toast = useToast() const toast = useToast()
// Local state // Local state
@ -336,8 +337,13 @@ const updateProduct = async (formData: any) => {
} }
} }
const adminKey = paymentService.getPreferredWalletAdminKey()
if (!adminKey) {
throw new Error('No wallet admin key available')
}
const updatedProduct = await nostrmarketAPI.updateProduct( const updatedProduct = await nostrmarketAPI.updateProduct(
currentUser.wallets[0].adminkey, adminKey,
props.product.id, props.product.id,
productData productData
) )
@ -398,8 +404,13 @@ const createProduct = async (formData: any) => {
} }
} }
const adminKey = paymentService.getPreferredWalletAdminKey()
if (!adminKey) {
throw new Error('No wallet admin key available')
}
const newProduct = await nostrmarketAPI.createProduct( const newProduct = await nostrmarketAPI.createProduct(
currentUser.wallets[0].adminkey, adminKey,
productData productData
) )

View file

@ -269,6 +269,7 @@ const emit = defineEmits<{
// Services // Services
const nostrmarketAPI = injectService(SERVICE_TOKENS.NOSTRMARKET_API) as NostrmarketAPI const nostrmarketAPI = injectService(SERVICE_TOKENS.NOSTRMARKET_API) as NostrmarketAPI
const paymentService = injectService(SERVICE_TOKENS.PAYMENT_SERVICE) as any
const toast = useToast() const toast = useToast()
// Local state // Local state
@ -342,8 +343,11 @@ const loadAvailableZones = async () => {
const currentUser = auth.currentUser?.value const currentUser = auth.currentUser?.value
if (!currentUser?.wallets?.length) return if (!currentUser?.wallets?.length) return
const inkey = paymentService.getPreferredWalletInvoiceKey()
if (!inkey) return
try { try {
const zones = await nostrmarketAPI.getZones(currentUser.wallets[0].inkey) const zones = await nostrmarketAPI.getZones(inkey)
availableZones.value = zones availableZones.value = zones
// Auto-select the first available zone to make form valid // Auto-select the first available zone to make form valid
@ -370,9 +374,14 @@ const addNewZone = async () => {
return return
} }
const adminKey = paymentService.getPreferredWalletAdminKey()
if (!adminKey) {
throw new Error('No wallet admin key available')
}
try { try {
const createdZone = await nostrmarketAPI.createZone( const createdZone = await nostrmarketAPI.createZone(
currentUser.wallets[0].adminkey, adminKey,
{ {
name: newZone.name!, name: newZone.name!,
currency: values.currency!, currency: values.currency!,
@ -418,9 +427,14 @@ const createStore = async (formData: any) => {
selectedZones.includes(zone.id) selectedZones.includes(zone.id)
) )
const wallet = paymentService.getPreferredWallet()
if (!wallet) {
throw new Error('No wallet available')
}
const stallData: CreateStallRequest = { const stallData: CreateStallRequest = {
name, name,
wallet: currentUser.wallets[0].id, wallet: wallet.id,
currency, currency,
shipping_zones: selectedZoneData, shipping_zones: selectedZoneData,
config: { config: {
@ -428,8 +442,13 @@ const createStore = async (formData: any) => {
} }
} }
const adminKey = paymentService.getPreferredWalletAdminKey()
if (!adminKey) {
throw new Error('No wallet admin key available')
}
const newStall = await nostrmarketAPI.createStall( const newStall = await nostrmarketAPI.createStall(
currentUser.wallets[0].adminkey, adminKey,
stallData stallData
) )

View file

@ -353,6 +353,7 @@ import StoreCard from './StoreCard.vue'
const router = useRouter() const router = useRouter()
const marketStore = useMarketStore() const marketStore = useMarketStore()
const nostrmarketAPI = injectService(SERVICE_TOKENS.NOSTRMARKET_API) as NostrmarketAPI const nostrmarketAPI = injectService(SERVICE_TOKENS.NOSTRMARKET_API) as NostrmarketAPI
const paymentService = injectService(SERVICE_TOKENS.PAYMENT_SERVICE) as any
const toast = useToast() const toast = useToast()
// Local state // Local state
@ -473,8 +474,15 @@ const loadStallsList = async () => {
} }
isLoadingStalls.value = true isLoadingStalls.value = true
const inkey = paymentService.getPreferredWalletInvoiceKey()
if (!inkey) {
console.error('No wallet invoice key available')
return
}
try { try {
const stalls = await nostrmarketAPI.getStalls(currentUser.wallets[0].inkey) const stalls = await nostrmarketAPI.getStalls(inkey)
userStalls.value = stalls || [] userStalls.value = stalls || []
// If there are stalls but no active one selected, select the first // If there are stalls but no active one selected, select the first
@ -496,9 +504,16 @@ const loadStallProducts = async () => {
if (!currentUser?.wallets?.length) return if (!currentUser?.wallets?.length) return
isLoadingProducts.value = true isLoadingProducts.value = true
const inkey = paymentService.getPreferredWalletInvoiceKey()
if (!inkey) {
console.error('No wallet invoice key available')
return
}
try { try {
const products = await nostrmarketAPI.getProducts( const products = await nostrmarketAPI.getProducts(
currentUser.wallets[0].inkey, inkey,
activeStall.value.id! activeStall.value.id!
) )
stallProducts.value = products || [] stallProducts.value = products || []

View file

@ -28,13 +28,8 @@ const isGeneratingQR = ref(false)
const transactions = computed(() => walletService?.transactions?.value || []) const transactions = computed(() => walletService?.transactions?.value || [])
const isLoading = computed(() => walletService?.isLoading?.value || false) const isLoading = computed(() => walletService?.isLoading?.value || false)
const error = computed(() => walletService?.error?.value) const error = computed(() => walletService?.error?.value)
const currentUser = computed(() => authService?.currentUser?.value) // Use PaymentService for centralized balance calculation
const totalBalance = computed(() => { const totalBalance = computed(() => paymentService?.totalBalance || 0)
if (!currentUser.value?.wallets) return 0
return currentUser.value.wallets.reduce((total: number, wallet: any) => {
return total + (wallet.balance_msat || 0)
}, 0)
})
const payLinks = computed(() => walletService?.payLinks?.value || []) const payLinks = computed(() => walletService?.payLinks?.value || [])
const firstPayLink = computed(() => payLinks.value[0] || null) const firstPayLink = computed(() => payLinks.value[0] || null)