diff --git a/src/components/layout/Navbar.vue b/src/components/layout/Navbar.vue index 08ded0c..07fbba1 100644 --- a/src/components/layout/Navbar.vue +++ b/src/components/layout/Navbar.vue @@ -33,13 +33,12 @@ const { navigation: modularNavigation, userMenuItems } = useModularNavigation() const navigation = modularNavigation -// Compute total wallet balance -const totalBalance = computed(() => { - if (!auth.currentUser.value?.wallets) return 0 +// Get PaymentService from DI for centralized wallet balance +const paymentService = tryInjectService(SERVICE_TOKENS.PAYMENT_SERVICE) as any - return auth.currentUser.value.wallets.reduce((total, wallet) => { - return total + (wallet.balance_msat || 0) - }, 0) +// Use PaymentService for total wallet balance +const totalBalance = computed(() => { + return paymentService?.totalBalance || 0 }) // Try to get chat service from DI (may not be available if chat module not loaded) diff --git a/src/core/services/PaymentService.ts b/src/core/services/PaymentService.ts index c19c4bb..277fabd 100644 --- a/src/core/services/PaymentService.ts +++ b/src/core/services/PaymentService.ts @@ -59,6 +59,15 @@ export class PaymentService extends BaseService { 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 */ diff --git a/src/modules/market/components/CreateProductDialog.vue b/src/modules/market/components/CreateProductDialog.vue index d03cff1..de94285 100644 --- a/src/modules/market/components/CreateProductDialog.vue +++ b/src/modules/market/components/CreateProductDialog.vue @@ -232,6 +232,7 @@ const emit = defineEmits<{ // Services const nostrmarketAPI = injectService(SERVICE_TOKENS.NOSTRMARKET_API) as NostrmarketAPI +const paymentService = injectService(SERVICE_TOKENS.PAYMENT_SERVICE) as any const toast = useToast() // 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( - currentUser.wallets[0].adminkey, + adminKey, props.product.id, 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( - currentUser.wallets[0].adminkey, + adminKey, productData ) diff --git a/src/modules/market/components/CreateStoreDialog.vue b/src/modules/market/components/CreateStoreDialog.vue index 5a65074..613c005 100644 --- a/src/modules/market/components/CreateStoreDialog.vue +++ b/src/modules/market/components/CreateStoreDialog.vue @@ -269,6 +269,7 @@ const emit = defineEmits<{ // Services const nostrmarketAPI = injectService(SERVICE_TOKENS.NOSTRMARKET_API) as NostrmarketAPI +const paymentService = injectService(SERVICE_TOKENS.PAYMENT_SERVICE) as any const toast = useToast() // Local state @@ -342,8 +343,11 @@ const loadAvailableZones = async () => { const currentUser = auth.currentUser?.value if (!currentUser?.wallets?.length) return + const inkey = paymentService.getPreferredWalletInvoiceKey() + if (!inkey) return + try { - const zones = await nostrmarketAPI.getZones(currentUser.wallets[0].inkey) + const zones = await nostrmarketAPI.getZones(inkey) availableZones.value = zones // Auto-select the first available zone to make form valid @@ -370,9 +374,14 @@ const addNewZone = async () => { return } + const adminKey = paymentService.getPreferredWalletAdminKey() + if (!adminKey) { + throw new Error('No wallet admin key available') + } + try { const createdZone = await nostrmarketAPI.createZone( - currentUser.wallets[0].adminkey, + adminKey, { name: newZone.name!, currency: values.currency!, @@ -418,9 +427,14 @@ const createStore = async (formData: any) => { selectedZones.includes(zone.id) ) + const wallet = paymentService.getPreferredWallet() + if (!wallet) { + throw new Error('No wallet available') + } + const stallData: CreateStallRequest = { name, - wallet: currentUser.wallets[0].id, + wallet: wallet.id, currency, shipping_zones: selectedZoneData, 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( - currentUser.wallets[0].adminkey, + adminKey, stallData ) diff --git a/src/modules/market/components/MerchantStore.vue b/src/modules/market/components/MerchantStore.vue index b4c9b6c..854ca9a 100644 --- a/src/modules/market/components/MerchantStore.vue +++ b/src/modules/market/components/MerchantStore.vue @@ -353,6 +353,7 @@ import StoreCard from './StoreCard.vue' const router = useRouter() const marketStore = useMarketStore() const nostrmarketAPI = injectService(SERVICE_TOKENS.NOSTRMARKET_API) as NostrmarketAPI +const paymentService = injectService(SERVICE_TOKENS.PAYMENT_SERVICE) as any const toast = useToast() // Local state @@ -473,8 +474,15 @@ const loadStallsList = async () => { } isLoadingStalls.value = true + + const inkey = paymentService.getPreferredWalletInvoiceKey() + if (!inkey) { + console.error('No wallet invoice key available') + return + } + try { - const stalls = await nostrmarketAPI.getStalls(currentUser.wallets[0].inkey) + const stalls = await nostrmarketAPI.getStalls(inkey) userStalls.value = stalls || [] // If there are stalls but no active one selected, select the first @@ -496,9 +504,16 @@ const loadStallProducts = async () => { if (!currentUser?.wallets?.length) return isLoadingProducts.value = true + + const inkey = paymentService.getPreferredWalletInvoiceKey() + if (!inkey) { + console.error('No wallet invoice key available') + return + } + try { const products = await nostrmarketAPI.getProducts( - currentUser.wallets[0].inkey, + inkey, activeStall.value.id! ) stallProducts.value = products || [] diff --git a/src/modules/wallet/views/WalletPage.vue b/src/modules/wallet/views/WalletPage.vue index d63ed30..462f46d 100644 --- a/src/modules/wallet/views/WalletPage.vue +++ b/src/modules/wallet/views/WalletPage.vue @@ -28,13 +28,8 @@ const isGeneratingQR = ref(false) const transactions = computed(() => walletService?.transactions?.value || []) const isLoading = computed(() => walletService?.isLoading?.value || false) const error = computed(() => walletService?.error?.value) -const currentUser = computed(() => authService?.currentUser?.value) -const totalBalance = computed(() => { - if (!currentUser.value?.wallets) return 0 - return currentUser.value.wallets.reduce((total: number, wallet: any) => { - return total + (wallet.balance_msat || 0) - }, 0) -}) +// Use PaymentService for centralized balance calculation +const totalBalance = computed(() => paymentService?.totalBalance || 0) const payLinks = computed(() => walletService?.payLinks?.value || []) const firstPayLink = computed(() => payLinks.value[0] || null)