Enhances expense submission with user wallet

Updates expense submission to require a user wallet.

Retrieves wallet information from the authentication context
and includes it in the expense submission request to the backend.
This ensures that expenses are correctly associated with the user's
wallet, enabling accurate tracking and management of expenses.
Also adds error handling and user feedback.
This commit is contained in:
padreug 2025-11-07 16:29:50 +01:00
parent 9ed674d0f3
commit 00a99995c9
3 changed files with 47 additions and 59 deletions

View file

@ -197,6 +197,8 @@ import {
} from '@/components/ui/form'
import { DollarSign, X, ChevronLeft, Loader2 } from 'lucide-vue-next'
import { injectService, SERVICE_TOKENS } from '@/core/di-container'
import { useAuth } from '@/composables/useAuthService'
import { useToast } from '@/core/composables/useToast'
import type { ExpensesAPI } from '../services/ExpensesAPI'
import type { Account } from '../types'
import AccountSelector from './AccountSelector.vue'
@ -209,8 +211,10 @@ interface Emits {
const emit = defineEmits<Emits>()
// Inject services
// Inject services and composables
const expensesAPI = injectService<ExpensesAPI>(SERVICE_TOKENS.EXPENSES_API)
const { user } = useAuth()
const toast = useToast()
// Component state
const currentStep = ref(1)
@ -255,22 +259,33 @@ function handleAccountSelected(account: Account) {
const onSubmit = form.handleSubmit(async (values) => {
if (!selectedAccount.value) {
console.error('[AddExpense] No account selected')
toast.error('No account selected', { description: 'Please select an account first' })
return
}
// Get wallet key from first wallet (invoice key for submission)
const wallet = user.value?.wallets?.[0]
if (!wallet || !wallet.inkey) {
toast.error('No wallet available', { description: 'Please log in to submit expenses' })
return
}
isSubmitting.value = true
try {
await expensesAPI.submitExpense({
await expensesAPI.submitExpense(wallet.inkey, {
description: values.description,
amount: values.amount,
expense_account: selectedAccount.value.name,
is_equity: values.isEquity,
user_wallet: '', // Will be filled by backend from auth
user_wallet: wallet.id,
reference: values.reference,
currency: 'sats'
})
// Show success message
toast.success('Expense submitted', { description: 'Your expense is pending admin approval' })
// Reset form and close
resetForm()
selectedAccount.value = null
@ -281,6 +296,8 @@ const onSubmit = form.handleSubmit(async (values) => {
emit('close')
} catch (error) {
console.error('[AddExpense] Error submitting expense:', error)
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
toast.error('Submission failed', { description: errorMessage })
} finally {
isSubmitting.value = false
}