File size: 2,635 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
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

exports.getAllRequests = async (req, res) => {
    try {
        const requests = await prisma.shipment.findMany({
            where: {
                status: {
                    in: ['PENDING', 'AWAITING_DISPATCHER', 'DISPATCHER_APPROVED', 'DRIVER_NOTIFIED', 'IN_TRANSIT']
                }
            }
        });

        const formattedRequests = requests.map(req => ({
            id: `#AR-${req.id.slice(0, 4).toUpperCase()}`,
            type: req.cargoType || 'General Cargo',
            route: `${req.pickupLocation || 'Unknown'}${req.dropLocation || 'Unknown'}`,
            weight: req.cargoWeight ? `${req.cargoWeight}T` : 'N/A',
            priority: req.priority.charAt(0) + req.priority.slice(1).toLowerCase(),
            status: req.status.split('_').map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join(' '),
            pickup: req.pickupLocation,
            drop: req.dropLocation,
            color: req.priority === 'HIGH' ? 'text-red-500 bg-red-500/10 border-red-500/20' :
                req.priority === 'MEDIUM' ? 'text-orange-500 bg-orange-500/10 border-orange-500/20' :
                    'text-blue-500 bg-blue-500/10 border-blue-500/20'
        }));

        res.json(formattedRequests);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
};

exports.updateRequestStatus = async (req, res) => {
    try {
        const { status } = req.body;
        const request = await prisma.shipment.update({
            where: { id: req.params.id },
            data: { status }
        });
        res.json(request);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
};

exports.getRecommendedDrivers = async (req, res) => {
    try {
        const drivers = await prisma.user.findMany({
            where: { role: 'DRIVER', status: 'ON_DUTY' },
            take: 2,
            orderBy: { rating: 'desc' }
        });

        const recommendations = drivers.map((d, index) => ({
            id: d.id,
            name: d.name,
            initials: d.initials || d.name.split(' ').map(n => n[0]).join(''),
            plate: d.currentVehicleNo || 'N/A',
            rating: d.rating,
            dist: index === 0 ? '12 km' : '18 km',
            eta: index === 0 ? '22 mins' : '32 mins',
            profit: index === 0 ? '+₹2,400' : '+₹2,100',
            bestMatch: index === 0
        }));

        res.json(recommendations);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
};