refactor: Enhance useMarket composable to group and process recent stall and product events
- Implement grouping of stall and product events by their unique IDs to retain only the most recent versions. - Update event processing logic to sort events by creation timestamp, ensuring the latest data is used for stalls and products. - Improve logging for better visibility into the processing of events and the creation of market entities.
This commit is contained in:
parent
9e02574c27
commit
1fcc3706be
1 changed files with 76 additions and 38 deletions
|
|
@ -175,29 +175,48 @@ export function useMarket() {
|
|||
|
||||
console.log('Found stall events:', events.length)
|
||||
|
||||
// Group events by stall ID and keep only the most recent version
|
||||
const stallGroups = new Map<string, any[]>()
|
||||
|
||||
events.forEach(event => {
|
||||
try {
|
||||
console.log('Processing stall event:', event)
|
||||
const stallData = JSON.parse(event.content)
|
||||
console.log('Parsed stall data:', stallData)
|
||||
const stallId = stallData.id
|
||||
|
||||
const stall: Stall = {
|
||||
id: stallData.id, // Use the stall's unique ID from content, not the Nostr event ID
|
||||
pubkey: event.pubkey,
|
||||
name: stallData.name,
|
||||
description: stallData.description,
|
||||
logo: stallData.logo,
|
||||
categories: stallData.categories,
|
||||
shipping: stallData.shipping
|
||||
if (!stallGroups.has(stallId)) {
|
||||
stallGroups.set(stallId, [])
|
||||
}
|
||||
|
||||
console.log('Created stall:', stall)
|
||||
marketStore.addStall(stall)
|
||||
stallGroups.get(stallId)!.push({ event, stallData })
|
||||
} catch (err) {
|
||||
console.warn('Failed to parse stall event:', err)
|
||||
}
|
||||
})
|
||||
|
||||
// Process each stall group, keeping only the most recent version
|
||||
stallGroups.forEach((stallEvents, stallId) => {
|
||||
// Sort by created_at timestamp (most recent first)
|
||||
stallEvents.sort((a, b) => b.event.created_at - a.event.created_at)
|
||||
|
||||
// Take the most recent version
|
||||
const { event, stallData } = stallEvents[0]
|
||||
|
||||
console.log('Processing most recent stall event:', event)
|
||||
console.log('Parsed stall data:', stallData)
|
||||
|
||||
const stall: Stall = {
|
||||
id: stallData.id, // Use the stall's unique ID from content, not the Nostr event ID
|
||||
pubkey: event.pubkey,
|
||||
name: stallData.name,
|
||||
description: stallData.description,
|
||||
logo: stallData.logo,
|
||||
categories: stallData.categories,
|
||||
shipping: stallData.shipping
|
||||
}
|
||||
|
||||
console.log('Created stall (most recent version):', stall)
|
||||
marketStore.addStall(stall)
|
||||
})
|
||||
|
||||
} catch (err) {
|
||||
console.error('Error loading stalls:', err)
|
||||
// Don't throw error, continue without stalls
|
||||
|
|
@ -226,41 +245,60 @@ export function useMarket() {
|
|||
|
||||
console.log('Found product events:', events.length)
|
||||
|
||||
// Group events by product ID and keep only the most recent version
|
||||
const productGroups = new Map<string, any[]>()
|
||||
|
||||
events.forEach(event => {
|
||||
try {
|
||||
console.log('Processing product event:', event)
|
||||
const productData = JSON.parse(event.content)
|
||||
console.log('Parsed product data:', productData)
|
||||
const productId = productData.id
|
||||
|
||||
const stall = marketStore.stalls.find(s => s.pubkey === event.pubkey)
|
||||
console.log('Found stall for product:', stall)
|
||||
|
||||
if (stall) {
|
||||
const product: Product = {
|
||||
id: productData.id, // Use the product's unique ID from content, not the Nostr event ID
|
||||
stall_id: stall.id,
|
||||
stallName: stall.name,
|
||||
name: productData.name,
|
||||
description: productData.description,
|
||||
price: productData.price,
|
||||
currency: productData.currency,
|
||||
quantity: productData.quantity,
|
||||
images: productData.images,
|
||||
categories: productData.categories,
|
||||
createdAt: event.created_at,
|
||||
updatedAt: event.created_at
|
||||
}
|
||||
|
||||
console.log('Created product:', product)
|
||||
marketStore.addProduct(product)
|
||||
} else {
|
||||
console.warn('No stall found for product pubkey:', event.pubkey)
|
||||
if (!productGroups.has(productId)) {
|
||||
productGroups.set(productId, [])
|
||||
}
|
||||
productGroups.get(productId)!.push({ event, productData })
|
||||
} catch (err) {
|
||||
console.warn('Failed to parse product event:', err)
|
||||
}
|
||||
})
|
||||
|
||||
// Process each product group, keeping only the most recent version
|
||||
productGroups.forEach((productEvents, productId) => {
|
||||
// Sort by created_at timestamp (most recent first)
|
||||
productEvents.sort((a, b) => b.event.created_at - a.event.created_at)
|
||||
|
||||
// Take the most recent version
|
||||
const { event, productData } = productEvents[0]
|
||||
|
||||
console.log('Processing most recent product event:', event)
|
||||
console.log('Parsed product data:', productData)
|
||||
|
||||
const stall = marketStore.stalls.find(s => s.pubkey === event.pubkey)
|
||||
console.log('Found stall for product:', stall)
|
||||
|
||||
if (stall) {
|
||||
const product: Product = {
|
||||
id: productData.id, // Use the product's unique ID from content, not the Nostr event ID
|
||||
stall_id: stall.id,
|
||||
stallName: stall.name,
|
||||
name: productData.name,
|
||||
description: productData.description,
|
||||
price: productData.price,
|
||||
currency: productData.currency,
|
||||
quantity: productData.quantity,
|
||||
images: productData.images,
|
||||
categories: productData.categories,
|
||||
createdAt: event.created_at,
|
||||
updatedAt: event.created_at
|
||||
}
|
||||
|
||||
console.log('Created product (most recent version):', product)
|
||||
marketStore.addProduct(product)
|
||||
} else {
|
||||
console.warn('No stall found for product pubkey:', event.pubkey)
|
||||
}
|
||||
})
|
||||
|
||||
} catch (err) {
|
||||
console.error('Error loading products:', err)
|
||||
// Don't throw error, continue without products
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue