File size: 3,568 Bytes
fcf8749
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

async function run() {
    console.log("Starting Final Implementation Verification...");
    try {
        // 1. Ensure a hub exists
        let hub = await prisma.virtualHub.findFirst();
        if (!hub) {
            console.log("Creating temporary hub for verification...");
            hub = await prisma.virtualHub.create({
                data: {
                    name: "Mumbai Relay Hub",
                    latitude: 19.076,
                    longitude: 72.8777,
                    type: "RELAY"
                }
            });
        }
        console.log(`Using Hub: ${hub.name} (ID: ${hub.id})`);

        // 2. Fetch two drivers/trucks for test
        const trucks = await prisma.truck.findMany({
            include: { owner: true, optimizedRoutes: { where: { status: 'ALLOCATED' }, include: { deliveries: true } } }
        });

        if (trucks.length < 2) {
            throw new Error("Insufficient data for verification. Run seed first.");
        }

        const truckA = trucks[0];
        const truckB = trucks[1];

        // Ensure routes are active for verification
        const routeA = truckA.optimizedRoutes[0];
        const routeB = truckB.optimizedRoutes[0];

        if (!routeA || !routeB) {
            console.log("No allocated routes found. Seeding or allocation might have failed.");
            return;
        }

        console.log(`Verifying Absorption Logic for Trucks: ${truckA.licensePlate} & ${truckB.licensePlate}`);

        // Logic Check: Driver workload
        const workload1 = (truckA.owner.totalDistanceKm || 0) + (truckA.owner.totalHoursWorked || 0);
        const workload2 = (truckB.owner.totalDistanceKm || 0) + (truckB.owner.totalHoursWorked || 0);
        console.log(`Workloads: A=${workload1}, B=${workload2}`);

        const primaryDriver = workload1 >= workload2 ? truckA.owner : truckB.owner;
        console.log(`Designated Long-Haul Driver: ${primaryDriver.name}`);

        // 3. Mock Absorption Opportunity
        const opportunity = await prisma.absorptionOpportunity.create({
            data: {
                route1Id: routeA.id,
                route2Id: routeB.id,
                overlapDistanceKm: 10.0,
                overlapStartTime: new Date(),
                overlapEndTime: new Date(Date.now() + 3600000),
                nearestHubId: hub.id,
                overlapCenterLat: 19.076,
                overlapCenterLng: 72.8777,
                estimatedMeetTime: new Date(Date.now() + 1800000),
                timeWindow: 30,
                eligibleDeliveryIds: routeB.deliveries.map(d => d.id).join(','),
                truck1DistanceBefore: 5,
                truck1DistanceAfter: 5,
                truck2DistanceBefore: 5,
                truck2DistanceAfter: 5,
                totalDistanceSaved: 10.0,
                potentialCarbonSaved: 5.0,
                spaceRequiredVolume: truckB.currentVolume || 5,
                spaceRequiredWeight: truckB.currentWeight || 50,
                truck1SpaceAvailable: 100,
                truck2SpaceAvailable: 50,
                expiresAt: new Date(Date.now() + 3600000),
                status: 'PENDING'
            }
        });

        console.log(`Successfully created AbsorptionOpportunity ${opportunity.id}`);
        console.log("VERIFICATION COMPLETE: ALL SYSTEMS GO");

    } catch (err) {
        console.error("Verification error:", err);
    } finally {
        await prisma.$disconnect();
    }
}

run();