aboutsummaryrefslogtreecommitdiff
path: root/scripts/seed/generators/revenue.ts
blob: deea9e6b33ecb05c89a78faa6a6f0d4ccc562ee1 (plain) (blame)
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
import { uuid, randomFloat } from '../utils.js';
import type { EventData } from './events.js';

export interface RevenueConfig {
  eventName: string;
  minAmount: number;
  maxAmount: number;
  currency: string;
  weight: number;
}

export interface RevenueData {
  id: string;
  websiteId: string;
  sessionId: string;
  eventId: string;
  eventName: string;
  currency: string;
  revenue: number;
  createdAt: Date;
}

export function generateRevenue(event: EventData, config: RevenueConfig): RevenueData | null {
  if (event.eventName !== config.eventName) {
    return null;
  }

  if (Math.random() > config.weight) {
    return null;
  }

  const revenue = randomFloat(config.minAmount, config.maxAmount);

  return {
    id: uuid(),
    websiteId: event.websiteId,
    sessionId: event.sessionId,
    eventId: event.id,
    eventName: event.eventName!,
    currency: config.currency,
    revenue: Math.round(revenue * 100) / 100, // Round to 2 decimal places
    createdAt: event.createdAt,
  };
}

export function generateRevenueForEvents(
  events: EventData[],
  configs: RevenueConfig[],
): RevenueData[] {
  const revenueEntries: RevenueData[] = [];

  for (const event of events) {
    if (!event.eventName) continue;

    for (const config of configs) {
      const revenue = generateRevenue(event, config);
      if (revenue) {
        revenueEntries.push(revenue);
        break; // Only one revenue per event
      }
    }
  }

  return revenueEntries;
}