File size: 4,746 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

const MOCK_BILLS = [
    { id: 'EWB-2024-001', vehicle: 'MH-12-AB-1234', from: 'Mumbai', to: 'Pune', dist: 148, driver: 'Rajesh Kumar', value: 45000, valid: '20 Feb, 18:00', validUntil: new Date(Date.now() + 86400000), status: 'Active' },
    { id: 'EWB-2024-002', vehicle: 'KA-05-CD-4567', from: 'Bangalore', to: 'Chennai', dist: 346, driver: 'Priya Sharma', value: 82000, valid: '21 Feb, 09:00', validUntil: new Date(Date.now() + 172800000), status: 'Active' },
    { id: 'EWB-2024-003', vehicle: 'DL-01-XY-9876', from: 'Delhi', to: 'Jaipur', dist: 282, driver: 'Amit Patel', value: 63000, valid: '19 Feb, 14:00', validUntil: new Date(Date.now() - 3600000), status: 'Expired' },
];

const getAllBills = async (req, res) => {
    try {
        const bills = await prisma.eWayBill.findMany({
            include: { driver: true }
        });

        const formattedBills = bills.map(bill => {
            const date = new Date(bill.validUntil);
            const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
            const formattedDate = `${date.getDate()} ${months[date.getMonth()]}, ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;

            return {
                id: bill.billNo,
                vehicle: bill.vehicleNo,
                from: bill.from,
                to: bill.to,
                dist: bill.distance,
                driver: bill.driver.name,
                value: bill.cargoValue,
                valid: formattedDate,
                validUntil: bill.validUntil,
                status: bill.status.split('_').map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join(' ')
            };
        });
        res.json(formattedBills);
    } catch (error) {
        console.warn('[Demo Mode] getAllBills using mock data');
        res.json(MOCK_BILLS);
    }
};

const createBill = async (req, res) => {
    try {
        const bill = await prisma.eWayBill.create({
            data: req.body
        });
        res.status(201).json(bill);
    } catch (error) {
        console.warn('[Demo Mode] createBill - DB unavailable');
        res.status(201).json({ id: `EWB-${Date.now()}`, ...req.body, status: 'ACTIVE', message: 'Demo mode' });
    }
};

// GET /api/eway-bills/stats - Get statistics for e-way bills
const getStats = async (req, res) => {
    try {
        const now = new Date();
        const twoDaysFromNow = new Date();
        twoDaysFromNow.setDate(twoDaysFromNow.getDate() + 2);

        const [totalActive, expireSoon, expired] = await Promise.all([
            // Total e-way bills (User requested total count from DB)
            prisma.eWayBill.count(),
            // Expire soon (valid_until - current_date < 2 days)
            prisma.eWayBill.count({
                where: {
                    validUntil: {
                        gte: now,
                        lt: twoDaysFromNow
                    },
                    status: 'ACTIVE'
                }
            }),
            // Expired (valid_until < current_date)
            prisma.eWayBill.count({
                where: {
                    validUntil: {
                        lt: now
                    }
                }
            })
        ]);

        res.json({ active: totalActive, expireSoon: expireSoon, expired: expired });
    } catch (error) {
        console.warn('[Demo Mode] getStats using mock data');
        res.json({ active: 3, expireSoon: 1, expired: 1 });
    }
};
const updateBill = async (req, res) => {
    try {
        const bill = await prisma.eWayBill.update({
            where: { billNo: req.params.id }, // Assuming billNo is the ID used in params, or use 'id' if that's the primary key. Schema says id is UUID, billNo is unique string. Let's check route param usage. Usually ID is safer. req.params.id matches 'id' field.
            data: req.body
        });
        res.json(bill);
    } catch (error) {
        console.warn('[Demo Mode] updateBill - DB unavailable');
        res.json({ id: req.params.id, ...req.body });
    }
};

const deleteBill = async (req, res) => {
    try {
        await prisma.eWayBill.delete({
            where: { billNo: req.params.id } // Frontend usually uses ID, but check what ID we send. In getAllBills we send 'id: bill.billNo'. So we should use billNo.
        });
        res.status(204).send();
    } catch (error) {
        console.warn('[Demo Mode] deleteBill - DB unavailable');
        res.status(204).send();
    }
};

module.exports = {
    getAllBills,
    createBill,
    getStats,
    updateBill,
    deleteBill
};