Dashboard adoptation
This commit is contained in:
@@ -21,6 +21,7 @@ import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
@@ -9,8 +9,10 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/wallet")
|
||||
@@ -34,6 +36,20 @@ public class WalletController {
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/users")
|
||||
public ResponseEntity<List<Map<String, Object>>> getUsers() {
|
||||
List<User> users = userRepository.findAll();
|
||||
List<Map<String, Object>> userList = users.stream().map(user -> {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", user.getId());
|
||||
map.put("name", user.getName());
|
||||
map.put("mobileNumber", user.getMobileNumber());
|
||||
map.put("ritzTokenBalance", user.getRitzTokenBalance());
|
||||
return map;
|
||||
}).collect(Collectors.toList());
|
||||
return ResponseEntity.ok(userList);
|
||||
}
|
||||
|
||||
@GetMapping("/transactions/{userId}")
|
||||
public ResponseEntity<List<TokenTransaction>> getTransactions(@PathVariable Long userId) {
|
||||
return ResponseEntity.ok(tokenService.getTransactions(userId));
|
||||
|
||||
@@ -2,11 +2,22 @@ package com.rit.canteen.sales.repository;
|
||||
|
||||
import com.rit.canteen.sales.model.TokenTransaction;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface TokenTransactionRepository extends JpaRepository<TokenTransaction, Long> {
|
||||
List<TokenTransaction> findByUserIdOrderByTimestampDesc(Long userId);
|
||||
List<TokenTransaction> findAllByOrderByTimestampDesc();
|
||||
|
||||
@Query("SELECT SUM(t.amount) FROM TokenTransaction t WHERE t.type = :type")
|
||||
BigDecimal sumByType(@Param("type") TokenTransaction.TransactionType type);
|
||||
|
||||
@Query("SELECT SUM(t.amount) FROM TokenTransaction t WHERE t.type = :type AND t.timestamp >= :start AND t.timestamp <= :end")
|
||||
BigDecimal sumByTypeInRange(@Param("type") TokenTransaction.TransactionType type, @Param("start") LocalDateTime start, @Param("end") LocalDateTime end);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ public interface UserRepository extends JpaRepository<User, Long> {
|
||||
"OR u.mobileNumber LIKE CONCAT('%', :search, '%')")
|
||||
org.springframework.data.domain.Page<User> findByNameOrMobileContainingIgnoreCase(String search, org.springframework.data.domain.Pageable pageable);
|
||||
|
||||
@jakarta.persistence.Lock(jakarta.persistence.LockModeType.PESSIMISTIC_WRITE)
|
||||
@org.springframework.data.jpa.repository.Lock(jakarta.persistence.LockModeType.PESSIMISTIC_WRITE)
|
||||
@Query("SELECT u FROM User u WHERE u.id = :id")
|
||||
Optional<User> findByIdWithLock(@org.springframework.data.repository.query.Param("id") Long id);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.rit.canteen.sales.model.TrendingItem;
|
||||
import com.rit.canteen.sales.repository.OrderRepository;
|
||||
import com.rit.canteen.sales.repository.PurchaseOrderRepository;
|
||||
import com.rit.canteen.sales.repository.VendorRepository;
|
||||
import com.rit.canteen.sales.repository.TokenTransactionRepository;
|
||||
import com.rit.canteen.sales.model.ProcurementDashboardData;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -29,11 +30,15 @@ public class DashboardService {
|
||||
|
||||
@Autowired
|
||||
private VendorRepository vendorRepository;
|
||||
|
||||
@Autowired
|
||||
private TokenTransactionRepository tokenTransactionRepository;
|
||||
|
||||
public GeneralDashboardData getGeneralDashboardData(LocalDateTime from, LocalDateTime to) {
|
||||
if (from == null) from = LocalDate.now().atStartOfDay();
|
||||
if (to == null) to = LocalDate.now().atTime(LocalTime.MAX);
|
||||
|
||||
System.out.println("[DIAGNOSTIC] Final timestamp range for service logic: " + from + " to " + to);
|
||||
DashboardStats stats = getDashboardStats(from, to);
|
||||
|
||||
System.out.println("Fetching dashboard data for range: " + from + " to " + to);
|
||||
@@ -94,7 +99,9 @@ public class DashboardService {
|
||||
orderInsight.put("color", "bg-rose-50 text-rose-600 border-rose-100");
|
||||
insights.add(orderInsight);
|
||||
|
||||
BigDecimal avg = stats.getTotalSales() > 0 ? BigDecimal.valueOf(stats.getTotalSales()).divide(BigDecimal.valueOf(stats.getActiveOrders()), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO;
|
||||
BigDecimal avg = (stats.getTotalSales() > 0 && stats.getActiveOrders() > 0)
|
||||
? BigDecimal.valueOf(stats.getTotalSales()).divide(BigDecimal.valueOf(stats.getActiveOrders()), 2, RoundingMode.HALF_UP)
|
||||
: BigDecimal.ZERO;
|
||||
Map<String, String> avgInsight = new HashMap<>();
|
||||
avgInsight.put("text", "₹" + avg + " average order value! Either everyone's hungry or just living large 🔥😋");
|
||||
avgInsight.put("color", "bg-emerald-50 text-emerald-600 border-emerald-100");
|
||||
@@ -127,17 +134,22 @@ public class DashboardService {
|
||||
LocalDateTime startOfYesterday = LocalDate.now().minusDays(1).atStartOfDay();
|
||||
LocalDateTime endOfYesterday = LocalDate.now().minusDays(1).atTime(LocalTime.MAX);
|
||||
|
||||
BigDecimal totalRevenueRaw = orderRepository.getTotalRevenue();
|
||||
System.out.println("[DIAGNOSTIC] Fetching Total Revenue...");
|
||||
BigDecimal totalRevenueRaw = tokenTransactionRepository.sumByType(com.rit.canteen.sales.model.TokenTransaction.TransactionType.TOPUP);
|
||||
System.out.println("[DIAGNOSTIC] Raw Total Revenue: " + totalRevenueRaw);
|
||||
long totalSales = totalRevenueRaw != null ? totalRevenueRaw.longValue() : 0;
|
||||
|
||||
BigDecimal periodRevenueRaw = orderRepository.getRevenuePerPeriod(from, to);
|
||||
System.out.println("[DIAGNOSTIC] Fetching Period Revenue for range: " + from + " to " + to);
|
||||
BigDecimal periodRevenueRaw = tokenTransactionRepository.sumByTypeInRange(com.rit.canteen.sales.model.TokenTransaction.TransactionType.TOPUP, from, to);
|
||||
System.out.println("[DIAGNOSTIC] Raw Period Revenue: " + periodRevenueRaw);
|
||||
long periodRevenue = periodRevenueRaw != null ? periodRevenueRaw.longValue() : 0;
|
||||
|
||||
int activeOrders = (int) orderRepository.countByCreatedAtBetween(from, to);
|
||||
int dailyCustomers = (int) orderRepository.countUniqueUsersInRange(from, to);
|
||||
|
||||
BigDecimal todayRevenue = orderRepository.getRevenuePerPeriod(startOfToday, endOfToday);
|
||||
BigDecimal yesterdayRevenue = orderRepository.getRevenuePerPeriod(startOfYesterday, endOfYesterday);
|
||||
BigDecimal todayRevenue = tokenTransactionRepository.sumByTypeInRange(com.rit.canteen.sales.model.TokenTransaction.TransactionType.TOPUP, startOfToday, endOfToday);
|
||||
BigDecimal yesterdayRevenue = tokenTransactionRepository.sumByTypeInRange(com.rit.canteen.sales.model.TokenTransaction.TransactionType.TOPUP, startOfYesterday, endOfYesterday);
|
||||
System.out.println("[DIAGNOSTIC] Today vs Yesterday: " + todayRevenue + " / " + yesterdayRevenue);
|
||||
|
||||
BigDecimal totalExpensesRaw = purchaseOrderRepository.getTotalPurchaseAmount();
|
||||
long totalExpenses = totalExpensesRaw != null ? totalExpensesRaw.longValue() : 0;
|
||||
|
||||
Reference in New Issue
Block a user