fix kashkool
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import { ref } from 'vue'
|
||||
import { getProducts } from '@/api/products'
|
||||
import type { Product } from '@/models/products'
|
||||
import { getLastStreamProductsList } from '@/utils/productStreamBridge'
|
||||
|
||||
/** کش مشترک لیست محصولات — dedupe درخواستهای polling */
|
||||
const catalogProducts = ref<Product[]>([])
|
||||
let catalogPromise: Promise<Product[]> | null = null
|
||||
|
||||
export function getProductsCatalogRef() {
|
||||
return catalogProducts
|
||||
}
|
||||
|
||||
export function findProductInCatalog(id: number): Product | undefined {
|
||||
const cached = catalogProducts.value.find((p) => p.id === id)
|
||||
if (cached) {
|
||||
return cached
|
||||
}
|
||||
|
||||
return getLastStreamProductsList()?.find((p) => p.id === id)
|
||||
}
|
||||
|
||||
export function getProductsCatalog(force = false): Promise<Product[]> {
|
||||
if (!force) {
|
||||
const streamed = getLastStreamProductsList()
|
||||
if (streamed?.length) {
|
||||
catalogProducts.value = streamed
|
||||
return Promise.resolve(streamed)
|
||||
}
|
||||
|
||||
if (catalogProducts.value.length > 0) {
|
||||
return Promise.resolve(catalogProducts.value)
|
||||
}
|
||||
}
|
||||
|
||||
if (!force && catalogPromise) {
|
||||
return catalogPromise
|
||||
}
|
||||
|
||||
catalogPromise = getProducts()
|
||||
.then((products) => {
|
||||
catalogProducts.value = products ?? []
|
||||
return catalogProducts.value
|
||||
})
|
||||
.finally(() => {
|
||||
catalogPromise = null
|
||||
})
|
||||
|
||||
return catalogPromise
|
||||
}
|
||||
|
||||
export function clearProductsCatalog() {
|
||||
catalogProducts.value = []
|
||||
catalogPromise = null
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import { ref } from 'vue'
|
||||
import { getWalletAssets } from '@/api/wallet'
|
||||
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 function useWalletAssets() {
|
||||
async function loadWalletAssets(force = false) {
|
||||
if (!force && assetsSummary.value) {
|
||||
return assetsSummary.value
|
||||
}
|
||||
|
||||
if (!force && assetsPromise) {
|
||||
return assetsPromise
|
||||
}
|
||||
|
||||
assetsPromise = Promise.all([getWalletAssets(), getProductsCatalog(force)])
|
||||
.then(([data]) => {
|
||||
assetsSummary.value = enrichAssetsSummary(data)
|
||||
syncWalletStoreFromAssets(assetsSummary.value)
|
||||
return assetsSummary.value
|
||||
})
|
||||
.finally(() => {
|
||||
assetsPromise = null
|
||||
})
|
||||
|
||||
return assetsPromise
|
||||
}
|
||||
|
||||
function clearWalletAssets() {
|
||||
assetsSummary.value = null
|
||||
assetsPromise = null
|
||||
}
|
||||
|
||||
return {
|
||||
assetsSummary,
|
||||
loadWalletAssets,
|
||||
clearWalletAssets,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user