[API Node] Fix credits fetch condition (#3575)

This commit is contained in:
Christian Byrne
2025-04-23 09:33:44 +08:00
committed by GitHub
parent a01aa39423
commit e9723407d8
3 changed files with 60 additions and 39 deletions

View File

@@ -45,6 +45,7 @@ export const useFirebaseAuthStore = defineStore('firebaseAuth', () => {
const currentUser = ref<User | null>(null)
const isInitialized = ref(false)
const customerCreated = ref(false)
const isFetchingBalance = ref(false)
// Balance state
const balance = ref<GetCustomerBalanceResponse | null>(null)
@@ -95,33 +96,42 @@ export const useFirebaseAuthStore = defineStore('firebaseAuth', () => {
}
const fetchBalance = async (): Promise<GetCustomerBalanceResponse | null> => {
const token = await getIdToken()
if (!token) {
error.value = 'Cannot fetch balance: User not authenticated'
return null
}
const response = await fetch(`${API_BASE_URL}/customers/balance`, {
headers: {
Authorization: `Bearer ${token}`
}
})
if (!response.ok) {
if (response.status === 404) {
// Customer not found is expected for new users
isFetchingBalance.value = true
try {
const token = await getIdToken()
if (!token) {
error.value = 'Cannot fetch balance: User not authenticated'
isFetchingBalance.value = false
return null
}
const errorData = await response.json()
error.value = `Failed to fetch balance: ${errorData.message}`
return null
}
const balanceData = await response.json()
// Update the last balance update time
lastBalanceUpdateTime.value = new Date()
balance.value = balanceData
return balanceData
const response = await fetch(`${API_BASE_URL}/customers/balance`, {
headers: {
Authorization: `Bearer ${token}`
}
})
if (!response.ok) {
if (response.status === 404) {
// Customer not found is expected for new users
return null
}
const errorData = await response.json()
error.value = `Failed to fetch balance: ${errorData.message}`
return null
}
const balanceData = await response.json()
// Update the last balance update time
lastBalanceUpdateTime.value = new Date()
balance.value = balanceData
return balanceData
} catch (e) {
error.value = `Failed to fetch balance: ${e}`
return null
} finally {
isFetchingBalance.value = false
}
}
const createCustomer = async (
@@ -303,6 +313,7 @@ export const useFirebaseAuthStore = defineStore('firebaseAuth', () => {
isInitialized,
balance,
lastBalanceUpdateTime,
isFetchingBalance,
// Getters
isAuthenticated,