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:
parent
c064b0b40d
commit
e5db949aae
6 changed files with 69 additions and 21 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -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
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -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 || []
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue