Repository navigation
Expand file tree
/
Copy pathfix-sessions.ts
More file actions
71 lines (57 loc) · 1.7 KB
/
Copy pathfix-sessions.ts
File metadata and controls
71 lines (57 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { generateObject } from 'ai';
import { z } from 'zod';
import { openai } from '@ai-sdk/openai';
import { writeFile, readFile } from 'fs/promises';
import dotenv from 'dotenv';
dotenv.config();
const filepath = './src/sessions.json';
const prices = [
9.99,
19.99,
29.99,
39.99,
49.99,
59.99,
69.99,
79.99,
89.99,
99.99,
109.99,
119.99,
129.99,
139.99,
149.99,
]
async function main() {
const existingSessions = JSON.parse(await readFile('./src/sessions-old.json', 'utf-8')) as any[];
const fixedSessions = existingSessions.map((session) => {
return {
...session,
events: session.events.flatMap((event) => {
if(event.event !== 'screen_view') {
delete event.path;
delete event.page_title;
}
if(event.event === 'add_to_cart') {
event.price = prices[Math.floor(Math.random() * prices.length)];
}
if(['purchase', 'remove_from_cart', 'checkout_success', 'checkout_failed'].includes(event.event)) {
return []
}
if(event.event === 'checkout') {
event.items = session.events.filter((e) => e.event === 'add_to_cart').length
event.total_price = session.events.filter((e) => e.event === 'add_to_cart').reduce((acc, curr) => acc + curr.price, 0)
const checkoutStatus = {
event: Math.random() > 0.15 ? 'checkout_success' : 'checkout_failed',
items: event.items,
total_price: event.total_price,
}
return [event, checkoutStatus]
}
return [event]
}).filter(Boolean)
}
});
await writeFile(filepath, JSON.stringify(fixedSessions, null, 2));
}
main();