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)
|
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 => {
|
events.forEach(event => {
|
||||||
try {
|
try {
|
||||||
console.log('Processing stall event:', event)
|
|
||||||
const stallData = JSON.parse(event.content)
|
const stallData = JSON.parse(event.content)
|
||||||
console.log('Parsed stall data:', stallData)
|
const stallId = stallData.id
|
||||||
|
|
||||||
const stall: Stall = {
|
if (!stallGroups.has(stallId)) {
|
||||||
id: stallData.id, // Use the stall's unique ID from content, not the Nostr event ID
|
stallGroups.set(stallId, [])
|
||||||
pubkey: event.pubkey,
|
|
||||||
name: stallData.name,
|
|
||||||
description: stallData.description,
|
|
||||||
logo: stallData.logo,
|
|
||||||
categories: stallData.categories,
|
|
||||||
shipping: stallData.shipping
|
|
||||||
}
|
}
|
||||||
|
stallGroups.get(stallId)!.push({ event, stallData })
|
||||||
console.log('Created stall:', stall)
|
|
||||||
marketStore.addStall(stall)
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn('Failed to parse stall event:', 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) {
|
} catch (err) {
|
||||||
console.error('Error loading stalls:', err)
|
console.error('Error loading stalls:', err)
|
||||||
// Don't throw error, continue without stalls
|
// Don't throw error, continue without stalls
|
||||||
|
|
@ -226,41 +245,60 @@ export function useMarket() {
|
||||||
|
|
||||||
console.log('Found product events:', events.length)
|
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 => {
|
events.forEach(event => {
|
||||||
try {
|
try {
|
||||||
console.log('Processing product event:', event)
|
|
||||||
const productData = JSON.parse(event.content)
|
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)
|
if (!productGroups.has(productId)) {
|
||||||
console.log('Found stall for product:', stall)
|
productGroups.set(productId, [])
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
productGroups.get(productId)!.push({ event, productData })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn('Failed to parse product event:', 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) {
|
} catch (err) {
|
||||||
console.error('Error loading products:', err)
|
console.error('Error loading products:', err)
|
||||||
// Don't throw error, continue without products
|
// Don't throw error, continue without products
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue