Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 43 additions & 25 deletions src/services/AnalyticsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,31 +141,44 @@ export class AnalyticsService {

// Filter sessions by payment status and fulfillment status
let filteredSessions = []
for (const s of sessions.data) {
if (s.payment_status === 'paid' || s.status === 'complete' || s.status === 'completed') {
let includeSession = true
const fulfillmentMap = new Map()

// Apply fulfillment filtering if KV is available
if (options.kvNamespace) {
const fulfillmentKey = `order_fulfillment:${s.id}`
const fulfillmentData = await options.kvNamespace.get(fulfillmentKey)
const fulfillmentStatus = fulfillmentData ? JSON.parse(fulfillmentData) : { fulfilled: false }
const candidates = sessions.data.filter(s =>
s.payment_status === 'paid' || s.status === 'complete' || s.status === 'completed'
)

if (showFulfilled) {
if (!fulfillmentStatus.fulfilled) {
includeSession = false
}
} else {
if (fulfillmentStatus.fulfilled) {
includeSession = false
}
}
if (options.kvNamespace && candidates.length > 0) {
await Promise.all(candidates.map(async (s) => {
const fulfillmentKey = `order_fulfillment:${s.id}`
const fulfillmentData = await options.kvNamespace.get(fulfillmentKey)
if (fulfillmentData) {
fulfillmentMap.set(s.id, fulfillmentData)
}
}))
}

for (const s of candidates) {
let includeSession = true

if (includeSession) {
filteredSessions.push(s)
// Apply fulfillment filtering if KV is available
if (options.kvNamespace) {
const fulfillmentData = fulfillmentMap.get(s.id)
const fulfillmentStatus = fulfillmentData ? JSON.parse(fulfillmentData) : { fulfilled: false }

if (showFulfilled) {
if (!fulfillmentStatus.fulfilled) {
includeSession = false
}
} else {
if (fulfillmentStatus.fulfilled) {
includeSession = false
}
}
}

if (includeSession) {
filteredSessions.push(s)
}
}

// Oldest at top within the current page
Expand Down Expand Up @@ -199,9 +212,13 @@ export class AnalyticsService {
}

// Check fulfillment status from KV
const fulfillmentKey = `order_fulfillment:${s.id}`
const fulfillmentData = options.kvNamespace ? await options.kvNamespace.get(fulfillmentKey) : null
const fulfillmentStatus = fulfillmentData ? JSON.parse(fulfillmentData) : { fulfilled: false, fulfilledAt: null, fulfilledBy: null }
let fulfillmentStatus = { fulfilled: false, fulfilledAt: null, fulfilledBy: null }
if (options.kvNamespace) {
const fulfillmentData = fulfillmentMap.get(s.id)
if (fulfillmentData) {
fulfillmentStatus = JSON.parse(fulfillmentData)
}
}

return {
id: s.id,
Expand Down Expand Up @@ -256,9 +273,10 @@ export class AnalyticsService {
// Handle error case...
let fulfillmentStatus = { fulfilled: false, fulfilledAt: null, fulfilledBy: null }
if (options.kvNamespace) {
const fulfillmentKey = `order_fulfillment:${s.id}`
const fulfillmentData = await options.kvNamespace.get(fulfillmentKey)
fulfillmentStatus = fulfillmentData ? JSON.parse(fulfillmentData) : { fulfilled: false, fulfilledAt: null, fulfilledBy: null }
const fulfillmentData = fulfillmentMap.get(s.id)
if (fulfillmentData) {
fulfillmentStatus = JSON.parse(fulfillmentData)
}
}

return {
Expand Down