154 lines
3.3 KiB
JavaScript
154 lines
3.3 KiB
JavaScript
const express = require('express');
|
||
const { PrismaClient } = require('@prisma/client');
|
||
const { asyncHandler } = require('../middleware/errorHandler');
|
||
const { authenticateToken } = require('../middleware/auth');
|
||
|
||
const router = express.Router();
|
||
const prisma = new PrismaClient();
|
||
|
||
/**
|
||
* Dashboard istatistikleri
|
||
* GET /api/dashboard/stats
|
||
*/
|
||
router.get('/stats', [
|
||
authenticateToken
|
||
], asyncHandler(async (req, res) => {
|
||
const userId = req.user.id;
|
||
|
||
// Kullanıcının listelerini al
|
||
const userLists = await prisma.shoppingList.findMany({
|
||
where: {
|
||
OR: [
|
||
{ ownerId: userId },
|
||
{
|
||
members: {
|
||
some: {
|
||
userId: userId
|
||
}
|
||
}
|
||
}
|
||
],
|
||
isActive: true
|
||
},
|
||
include: {
|
||
items: true
|
||
}
|
||
});
|
||
|
||
// İstatistikleri hesapla
|
||
const totalLists = userLists.length;
|
||
const totalItems = userLists.reduce((sum, list) => sum + list.items.length, 0);
|
||
const completedItems = userLists.reduce((sum, list) =>
|
||
sum + list.items.filter(item => item.isPurchased).length, 0
|
||
);
|
||
const pendingItems = totalItems - completedItems;
|
||
|
||
// Bu ayki harcama
|
||
const currentMonth = new Date();
|
||
currentMonth.setDate(1);
|
||
currentMonth.setHours(0, 0, 0, 0);
|
||
|
||
const monthlySpending = await prisma.listItem.aggregate({
|
||
where: {
|
||
isPurchased: true,
|
||
purchasedAt: {
|
||
gte: currentMonth
|
||
},
|
||
list: {
|
||
OR: [
|
||
{ ownerId: userId },
|
||
{
|
||
members: {
|
||
some: {
|
||
userId: userId
|
||
}
|
||
}
|
||
}
|
||
],
|
||
isActive: true
|
||
}
|
||
},
|
||
_sum: {
|
||
price: true
|
||
}
|
||
});
|
||
|
||
res.json({
|
||
success: true,
|
||
data: {
|
||
totalLists,
|
||
totalItems,
|
||
completedItems,
|
||
pendingItems,
|
||
monthlySpending: monthlySpending._sum.price || 0,
|
||
completionRate: totalItems > 0 ? Math.round((completedItems / totalItems) * 100) : 0
|
||
}
|
||
});
|
||
}));
|
||
|
||
/**
|
||
* Son aktiviteler
|
||
* GET /api/dashboard/activity
|
||
*/
|
||
router.get('/activity', [
|
||
authenticateToken
|
||
], asyncHandler(async (req, res) => {
|
||
const userId = req.user.id;
|
||
const { limit = 10 } = req.query;
|
||
|
||
// Son eklenen ürünler
|
||
const recentItems = await prisma.listItem.findMany({
|
||
where: {
|
||
list: {
|
||
OR: [
|
||
{ ownerId: userId },
|
||
{
|
||
members: {
|
||
some: {
|
||
userId: userId
|
||
}
|
||
}
|
||
}
|
||
],
|
||
isActive: true
|
||
}
|
||
},
|
||
include: {
|
||
list: {
|
||
select: {
|
||
id: true,
|
||
name: true
|
||
}
|
||
},
|
||
product: {
|
||
select: {
|
||
id: true,
|
||
name: true
|
||
}
|
||
}
|
||
},
|
||
orderBy: {
|
||
createdAt: 'desc'
|
||
},
|
||
take: parseInt(limit)
|
||
});
|
||
|
||
// Aktiviteleri formatla
|
||
const activities = recentItems.map(item => ({
|
||
id: item.id,
|
||
type: item.isPurchased ? 'item_purchased' : 'item_added',
|
||
message: item.isPurchased
|
||
? `${item.product?.name || item.name} satın alındı`
|
||
: `${item.product?.name || item.name} listeye eklendi`,
|
||
listName: item.list.name,
|
||
listId: item.list.id,
|
||
createdAt: item.isPurchased ? item.purchasedAt : item.createdAt
|
||
}));
|
||
|
||
res.json({
|
||
success: true,
|
||
data: { activities }
|
||
});
|
||
}));
|
||
|
||
module.exports = router; |