File size: 3,983 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const prisma = require('../config/database');

/**
 * GET /api/transactions/my-transactions - Earnings history
 */
const getMyTransactions = async (req, res) => {
    try {
        const transactions = await prisma.transaction.findMany({
            where: {
                driverId: req.user.id,
            },
            orderBy: {
                createdAt: 'desc',
            },
            include: {
                delivery: {
                    select: {
                        pickupLocation: true,
                        dropLocation: true,
                        cargoType: true,
                        completedAt: true,
                    }
                }
            }
        });

        res.status(200).json({
            success: true,
            data: transactions,
        });
    } catch (error) {
        console.error('Get transactions error:', error);
        res.status(500).json({
            success: false,
            message: 'Failed to fetch transactions',
        });
    }
};

/**
 * GET /api/transactions/weekly-summary - Weekly earnings + km driven
 */
const getWeeklySummary = async (req, res) => {
    try {
        // Get current driver performance data from User model
        const driver = await prisma.user.findUnique({
            where: { id: req.user.id },
            select: {
                weeklyEarnings: true,
                weeklyKmDriven: true,
                totalEarnings: true,
                totalDistanceKm: true,
                weekResetDate: true,
            }
        });

        // We can also aggregate from transactions for the last 7 days for more detail
        const sevenDaysAgo = new Date();
        sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7);

        const weeklyTransactions = await prisma.transaction.findMany({
            where: {
                driverId: req.user.id,
                createdAt: {
                    gte: sevenDaysAgo,
                }
            },
            orderBy: {
                createdAt: 'asc',
            }
        });

        // Group by day for a chart-friendly format
        const dailyEarnings = {};
        weeklyTransactions.forEach(t => {
            const date = t.createdAt.toISOString().split('T')[0];
            dailyEarnings[date] = (dailyEarnings[date] || 0) + t.amount;
        });

        res.status(200).json({
            success: true,
            data: {
                summary: {
                    earnings: driver.weeklyEarnings,
                    distance: driver.weeklyKmDriven,
                    totalEarnings: driver.totalEarnings,
                    totalDistance: driver.totalDistanceKm,
                    resetDate: driver.weekResetDate,
                },
                dailyBreakdown: dailyEarnings,
            }
        });
    } catch (error) {
        console.error('Get weekly summary error:', error);
        res.status(500).json({
            success: false,
            message: 'Failed to fetch weekly summary',
        });
    }
};

/**
 * GET /api/transactions/:id - Single transaction details
 */
const getTransaction = async (req, res) => {
    try {
        const { id } = req.params;

        const transaction = await prisma.transaction.findUnique({
            where: { id },
            include: {
                delivery: true,
            }
        });

        if (!transaction || transaction.driverId !== req.user.id) {
            return res.status(404).json({
                success: false,
                message: 'Transaction not found',
            });
        }

        res.status(200).json({
            success: true,
            data: transaction,
        });
    } catch (error) {
        console.error('Get transaction error:', error);
        res.status(500).json({
            success: false,
            message: 'Failed to fetch transaction details',
        });
    }
};

module.exports = {
    getMyTransactions,
    getWeeklySummary,
    getTransaction,
};