Life-Pill / pharmacy-pos-main-backend

The Pharmacy POS System Backend is the server-side component of our comprehensive pharmacy management software. It is developed using Spring Boot, Java, Spring Security ,and Microservices Architecture
Apache License 2.0
5 stars 3 forks source link

New Endpoint for Daily Orders and Sales Summary by Branch and bug fixed #109

Closed PramithaMJ closed 5 months ago

PramithaMJ commented 5 months ago

Pull Request: New Endpoint for Daily Orders and Sales Summary by Branch

Title

Add Endpoint for Retrieving Daily Orders and Sales Summary by Branch

Description

This pull request introduces a new endpoint in our pharmacy POS system to provide daily orders and sales summary for a specific branch over the past year. The endpoint will allow the frontend and DevOps teams to retrieve the required data for analysis and reporting purposes.

Endpoint Details

Request Example

GET [/api/v1/sales-summary/daily?branchId=12345 HTTP/1.1](http://localhost:8079/lifepill/v1/branch-summary/sales-summary/daily)
Host: localhost:8079

Response Format

The response will be a JSON array of objects, where each object contains:

Example Response

{
  "code": 200,
  "message": "SUCCESS",
  "data": [
    { "date": "2024-05-28", "orders": 1, "sales": 600.0 },
    { "date": "2024-05-27", "orders": 1, "sales": 600.0 },
    { "date": "2024-05-26", "orders": 1, "sales": 600.0 },
    { "date": "2024-05-25", "orders": 3, "sales": 1800.0 }
  ]
}

Changes Made

1. New Endpoint Implementation

2. Service Layer

3. Data Layer

Code Snippets

Controller

@RestController
@RequestMapping("/api/v1/sales-summary")
public class SalesSummaryController {

    @Autowired
    private SalesSummaryService salesSummaryService;

    @GetMapping("/daily")
    public ResponseEntity<ApiResponse<List<DailySalesSummaryDTO>>> getDailySalesSummary(@RequestParam long branchId) {
        List<DailySalesSummaryDTO> dailySalesSummary = salesSummaryService.getDailySalesSummary(branchId);
        ApiResponse<List<DailySalesSummaryDTO>> response = new ApiResponse<>(200, "SUCCESS", dailySalesSummary);
        return ResponseEntity.ok(response);
    }
}

Service

@Service
public class SalesSummaryService {

    @Autowired
    private SalesRepository salesRepository;

    public List<DailySalesSummaryDTO> getDailySalesSummary(long branchId) {
        List<DailySalesSummary> dailySalesSummaries = salesRepository.findDailySalesSummaryByBranchId(branchId);
        return dailySalesSummaries.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    private DailySalesSummaryDTO convertToDTO(DailySalesSummary dailySalesSummary) {
        return new DailySalesSummaryDTO(dailySalesSummary.getDate(), dailySalesSummary.getOrders(), dailySalesSummary.getSales());
    }
}

Repository

public interface SalesRepository extends JpaRepository<Sale, Long> {

    @Query("SELECT new com.example.DailySalesSummary(s.date, COUNT(s.id), SUM(s.totalAmount)) " +
           "FROM Sale s WHERE s.branchId = :branchId GROUP BY s.date")
    List<DailySalesSummary> findDailySalesSummaryByBranchId(@Param("branchId") long branchId);
}

DTO

public class DailySalesSummaryDTO {

    private String date;
    private long orders;
    private double sales;

    public DailySalesSummaryDTO(String date, long orders, double sales) {
        this.date = date;
        this.orders = orders;
        this.sales = sales;
    }

    // Getters and setters
}

Testing

Issue

[Issue #107 ]: Request for New Endpoint: Daily Orders and Sales for a Branch [Issue #108 ]: Request for New Endpoint: Daily Orders and Sales for a Branch

Checklist

Conclusion

This pull request adds a valuable endpoint to our system, enabling frontend and DevOps teams to retrieve daily orders and sales summaries for specific branches efficiently. The implementation follows best practices and has been thoroughly tested to ensure reliability and performance.

Best regards,
Pramitha Jayasooriya, Backend Developer at LifePill, https://pramithamj.me

sonarcloud[bot] commented 5 months ago

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarCloud