File size: 4,509 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const axios = require('axios');
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

async function verifyAll() {
    console.log("--- STARTING END-TO-END VERIFICATION ---");

    try {
        // 1. Get a Courier Company
        const company = await prisma.courierCompany.findFirst();
        let courierCompanyId;
        if (!company) {
            console.log("No courier company found. Creating one...");
            const c = await prisma.courierCompany.create({
                data: { name: "EcoLogiq Logistics", code: "ECO001" }
            });
            courierCompanyId = c.id;
        } else {
            courierCompanyId = company.id;
        }

        // Update all data to belong to this company for verification
        await prisma.user.updateMany({ data: { courierCompanyId } });
        await prisma.truck.updateMany({ data: { courierCompanyId, isAvailable: true, registrationStatus: 'APPROVED' } });
        await prisma.delivery.updateMany({ data: { courierCompanyId, status: 'PENDING' } });

        console.log(`Using Courier Company: ${courierCompanyId}`);

        // 2. Mock some PENDING deliveries
        console.log("Creating pending deliveries for allocation...");
        const dispatcher = await prisma.user.findFirst({ where: { role: 'DISPATCHER' } });
        const shipper = await prisma.user.findFirst({ where: { role: 'SHIPPER' } });

        await prisma.delivery.updateMany({
            where: { status: 'COMPLETED' },
            data: { status: 'PENDING' }
        });

        const delCount = await prisma.delivery.count({ where: { courierCompanyId, status: 'PENDING' } });
        const truckCount = await prisma.truck.count({ where: { courierCompanyId, isAvailable: true, registrationStatus: 'APPROVED' } });
        console.log(`Pending Deliveries found: ${delCount}`);
        console.log(`Available Trucks found: ${truckCount}`);

        // 3. Trigger Allocation
        console.log("Triggering Route Allocation Logic...");
        // Since I don't have a token easily here without auth logic, I'll call the controller logic directly if possible,
        // but for a clean test I'll just check if the DB reflects the changes after my manual logic.

        // Let's use the controller logic directly by mocking req/res
        const routeController = require('../controllers/routeController');
        const req = { body: { courierCompanyId } };
        const res = {
            status: (code) => ({
                json: (data) => {
                    console.log(`Allocation Response (${code}):`, data.message);
                }
            })
        };

        await routeController.allocateRoutes(req, res);

        // 4. Check if OptimizedRoute was created
        const routes = await prisma.optimizedRoute.findMany({
            where: { courierCompanyId },
            include: { deliveries: true }
        });
        console.log(`Routes created: ${routes.length}`);

        if (routes.length >= 2) {
            console.log("Triggering Proximity Check...");
            const synergyController = require('../controllers/synergyController');
            const truckA = await prisma.truck.findFirst({ where: { id: routes[0].truckId } });
            const hub = await prisma.virtualHub.findFirst();

            // Mock location for truck A and B to be near each other
            await prisma.truck.update({
                where: { id: routes[0].truckId },
                data: { currentLat: 19.076, currentLng: 72.8777 }
            });
            await prisma.truck.update({
                where: { id: routes[1].truckId },
                data: { currentLat: 19.077, currentLng: 72.8778 }
            });

            // Set routes to ACTIVE to pass filter
            await prisma.optimizedRoute.updateMany({ data: { status: 'ACTIVE' } });

            const ioMock = { emit: (event, data) => console.log(`Socket Event [${event}]:`, data) };
            const opportunity = await synergyController.detectAbsorptionOpportunity(truckA.id, 19.076, 72.8777, ioMock);

            if (opportunity) {
                console.log(`Opportunity Created: ${opportunity.id}`);
                console.log("VERIFICATION SUCCESSFUL");
            } else {
                console.log("Opportunity not detected. Check proximity logs.");
            }
        }

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

verifyAll();