72 lines
2.0 KiB
TypeScript
72 lines
2.0 KiB
TypeScript
import type { Product } from '@/models/products'
|
|
import type { WalletInventoryCard, WalletMetalTotal } from '@/models/walletAssets'
|
|
import { resolveProductImageSrc } from '@/utils/productImage'
|
|
|
|
export function fundPageInventoryCards<T extends { quantity?: number }>(cards: T[]): T[] {
|
|
return cards.filter((card) => Number(card.quantity ?? 0) > 0)
|
|
}
|
|
|
|
export function resolveSummaryMetalWeight(
|
|
metal: { total_weight: number },
|
|
_cards?: Array<{ metal_id?: number; quantity?: number }>
|
|
): number {
|
|
return Number(metal.total_weight ?? 0)
|
|
}
|
|
|
|
export function enrichInventoryCardsWithCatalog<T extends WalletInventoryCard>(
|
|
cards: T[],
|
|
catalog: Product[]
|
|
): T[] {
|
|
return cards.map((card) => {
|
|
const fromCatalog = catalog.find((product) => product.id === card.product_id)
|
|
if (!fromCatalog) {
|
|
return card
|
|
}
|
|
|
|
return {
|
|
...card,
|
|
file: card.file ?? fromCatalog.file ?? null,
|
|
}
|
|
})
|
|
}
|
|
|
|
export function getProductDisplayImage(product?: {
|
|
group_type?: string | null
|
|
title?: string | null
|
|
file?: { src?: string | null } | null
|
|
img?: string | null
|
|
} | null): string {
|
|
return resolveProductImageSrc(product)
|
|
}
|
|
|
|
export function formatMetalTotal(
|
|
metal: WalletMetalTotal,
|
|
inventoryCards: WalletInventoryCard[]
|
|
): string {
|
|
return `${resolveSummaryMetalWeight(metal, inventoryCards)}${metal.unit}`
|
|
}
|
|
|
|
export function resolveAssetsRialValue(
|
|
summary: {
|
|
total_rial_value?: number
|
|
assets_rial_value?: number
|
|
cash_wallet?: number
|
|
} | null | undefined,
|
|
liveGoldPrice = 0
|
|
): number {
|
|
const total = Number(summary?.total_rial_value ?? 0)
|
|
if (total > 0) {
|
|
return total
|
|
}
|
|
|
|
const cash = Number(summary?.cash_wallet ?? 0)
|
|
const assetsRial = Number(summary?.assets_rial_value ?? 0)
|
|
if (assetsRial > 0) {
|
|
return cash + assetsRial
|
|
}
|
|
|
|
const metalWeight = (summary as { metal_totals?: Array<{ total_weight?: number }> } | null)
|
|
?.metal_totals?.[0]?.total_weight
|
|
return cash + Number(metalWeight ?? 0) * liveGoldPrice
|
|
}
|