added assets into stream
This commit is contained in:
@@ -1,12 +1,57 @@
|
||||
import { ref } from 'vue'
|
||||
import { getWalletAssets } from '@/api/wallet'
|
||||
import { getProductsCatalog } from '@/composables/useProductsCatalog'
|
||||
import { getProductsCatalog, getProductsCatalogRef } from '@/composables/useProductsCatalog'
|
||||
import { useWalletStore } from '@/store/walletStore'
|
||||
import type { WalletAssetsSummary } from '@/models/walletAssets'
|
||||
import { enrichInventoryCardsWithCatalog } from '@/utils/productAssets'
|
||||
|
||||
const assetsSummary = ref<WalletAssetsSummary | null>(null)
|
||||
let assetsPromise: Promise<WalletAssetsSummary> | null = null
|
||||
|
||||
function enrichAssetsSummary(data: WalletAssetsSummary): WalletAssetsSummary {
|
||||
const catalog = getProductsCatalogRef().value
|
||||
|
||||
return {
|
||||
...data,
|
||||
inventory_cards: enrichInventoryCardsWithCatalog(data.inventory_cards ?? [], catalog ?? []),
|
||||
count_products: enrichInventoryCardsWithCatalog(data.count_products ?? [], catalog ?? []),
|
||||
}
|
||||
}
|
||||
|
||||
function syncWalletStoreFromAssets(data: WalletAssetsSummary) {
|
||||
const walletStore = useWalletStore()
|
||||
if (!walletStore.walletInfo) {
|
||||
return
|
||||
}
|
||||
|
||||
const goldMetal = data.metal_totals?.find((metal) => metal.metal_id === 1) ?? data.metal_totals?.[0]
|
||||
|
||||
walletStore.walletInfo.cash = data.cash_wallet ?? walletStore.walletInfo.cash
|
||||
|
||||
if (goldMetal) {
|
||||
walletStore.walletInfo.gold = goldMetal.melted_weight ?? walletStore.walletInfo.gold
|
||||
walletStore.walletInfo.gold_sum = goldMetal.total_weight ?? walletStore.walletInfo.gold_sum
|
||||
}
|
||||
}
|
||||
|
||||
export async function applyWalletAssetsFromStream(payload: unknown) {
|
||||
if (!payload || typeof payload !== 'object') {
|
||||
return
|
||||
}
|
||||
|
||||
const data = payload as WalletAssetsSummary
|
||||
if (data.meta == null) {
|
||||
return
|
||||
}
|
||||
|
||||
if (!getProductsCatalogRef().value.length) {
|
||||
await getProductsCatalog()
|
||||
}
|
||||
|
||||
assetsSummary.value = enrichAssetsSummary(data)
|
||||
syncWalletStoreFromAssets(assetsSummary.value)
|
||||
}
|
||||
|
||||
export function useWalletAssets() {
|
||||
async function loadWalletAssets(force = false) {
|
||||
if (!force && assetsSummary.value) {
|
||||
@@ -17,14 +62,10 @@ export function useWalletAssets() {
|
||||
return assetsPromise
|
||||
}
|
||||
|
||||
// Reuses cached getProductsCatalog() — same catalog as chart offset logic
|
||||
assetsPromise = Promise.all([getWalletAssets(), getProductsCatalog(force)])
|
||||
.then(([data, catalog]) => {
|
||||
assetsSummary.value = {
|
||||
...data,
|
||||
inventory_cards: enrichInventoryCardsWithCatalog(data.inventory_cards ?? [], catalog ?? []),
|
||||
count_products: enrichInventoryCardsWithCatalog(data.count_products ?? [], catalog ?? []),
|
||||
}
|
||||
.then(([data]) => {
|
||||
assetsSummary.value = enrichAssetsSummary(data)
|
||||
syncWalletStoreFromAssets(assetsSummary.value)
|
||||
return assetsSummary.value
|
||||
})
|
||||
.finally(() => {
|
||||
|
||||
Reference in New Issue
Block a user