Fix fournisseur handling across catalog flows
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { ref } from 'vue'
|
||||
import { useToast } from './useToast'
|
||||
import { useApi } from './useApi'
|
||||
import { buildConstructeurRequestPayload, uniqueConstructeurIds } from '~/shared/constructeurUtils'
|
||||
import { useConstructeurs } from './useConstructeurs'
|
||||
|
||||
const products = ref([])
|
||||
const total = ref(0)
|
||||
@@ -26,6 +28,29 @@ const replaceInCache = (item) => {
|
||||
export function useProducts () {
|
||||
const { showError } = useToast()
|
||||
const { get, post, patch, delete: del } = useApi()
|
||||
const { ensureConstructeurs } = useConstructeurs()
|
||||
|
||||
const withResolvedConstructeurs = async (product) => {
|
||||
if (!product || typeof product !== 'object') {
|
||||
return product
|
||||
}
|
||||
const ids = uniqueConstructeurIds(
|
||||
product.constructeurIds,
|
||||
product.constructeurs,
|
||||
product.constructeur,
|
||||
)
|
||||
const hasConstructeurs =
|
||||
Array.isArray(product.constructeurs) && product.constructeurs.length > 0
|
||||
|
||||
if (ids.length && !hasConstructeurs) {
|
||||
const resolved = await ensureConstructeurs(ids)
|
||||
if (resolved.length) {
|
||||
product.constructeurs = resolved
|
||||
product.constructeurIds = ids
|
||||
}
|
||||
}
|
||||
return product
|
||||
}
|
||||
|
||||
const loadProducts = async (options = {}) => {
|
||||
if (loading.value) {
|
||||
@@ -47,7 +72,8 @@ export function useProducts () {
|
||||
const result = await get('/products?limit=100')
|
||||
if (result.success) {
|
||||
const items = Array.isArray(result.data?.items) ? result.data.items : []
|
||||
products.value = items
|
||||
const enrichedItems = await Promise.all(items.map((item) => withResolvedConstructeurs(item)))
|
||||
products.value = enrichedItems
|
||||
total.value = typeof result.data?.total === 'number' ? result.data.total : items.length
|
||||
loaded.value = true
|
||||
} else if (result.error) {
|
||||
@@ -67,12 +93,14 @@ export function useProducts () {
|
||||
}
|
||||
|
||||
const createProduct = async (payload) => {
|
||||
const normalizedPayload = buildConstructeurRequestPayload(payload)
|
||||
loading.value = true
|
||||
error.value = null
|
||||
try {
|
||||
const result = await post('/products', payload)
|
||||
const result = await post('/products', normalizedPayload)
|
||||
if (result.success && result.data) {
|
||||
const added = replaceInCache(result.data)
|
||||
const enriched = await withResolvedConstructeurs(result.data)
|
||||
const added = replaceInCache(enriched)
|
||||
if (added) {
|
||||
total.value += 1
|
||||
}
|
||||
@@ -93,12 +121,14 @@ export function useProducts () {
|
||||
}
|
||||
|
||||
const updateProduct = async (id, payload) => {
|
||||
const normalizedPayload = buildConstructeurRequestPayload(payload)
|
||||
loading.value = true
|
||||
error.value = null
|
||||
try {
|
||||
const result = await patch(`/products/${id}`, payload)
|
||||
const result = await patch(`/products/${id}`, normalizedPayload)
|
||||
if (result.success && result.data) {
|
||||
replaceInCache(result.data)
|
||||
const enriched = await withResolvedConstructeurs(result.data)
|
||||
replaceInCache(enriched)
|
||||
} else if (result.error) {
|
||||
error.value = result.error
|
||||
showError(result.error)
|
||||
@@ -141,9 +171,10 @@ export function useProducts () {
|
||||
}
|
||||
|
||||
const getProduct = async (id, options = {}) => {
|
||||
if (!options.force) {
|
||||
const shouldForce = !!options.force
|
||||
if (!shouldForce) {
|
||||
const cached = products.value.find((product) => product.id === id)
|
||||
if (cached) {
|
||||
if (cached && Array.isArray(cached.constructeurs) && cached.constructeurs.length > 0) {
|
||||
return { success: true, data: cached }
|
||||
}
|
||||
}
|
||||
@@ -151,7 +182,9 @@ export function useProducts () {
|
||||
try {
|
||||
const result = await get(`/products/${id}`)
|
||||
if (result.success && result.data) {
|
||||
replaceInCache(result.data)
|
||||
const enriched = await withResolvedConstructeurs(result.data)
|
||||
replaceInCache(enriched)
|
||||
return { success: true, data: enriched }
|
||||
}
|
||||
return result
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user