Skip to content

An application for processing investor and investment data from CSV files. This API provides endpoints to import CSV data, list investors, and retrieve aggregate statistics.

Notifications You must be signed in to change notification settings

phyroxin/data-processor

Repository files navigation

Data Processor

An application for processing investor and investment data from CSV files. This API provides endpoints to import CSV data, list investors, and retrieve aggregate statistics.

Requirements

  • PHP >= 8.2
  • Composer
  • MySQL, PostgreSQL, or SQLite database
  • Queue driver (database, redis, or sync for development)

Quick Setup (All-in-one)

Note: Make sure to create a database first before running the setup command. Update your .env file with the database credentials if needed.

composer run setup

This will:

  • Install Composer dependencies
  • Copy .env.example to .env if it doesn't exist
  • Generate application key
  • Run migrations and seed tables with the first 10 records from the example

Running the Application

Development Mode

Run the development server with queue worker and log viewer:

composer run dev

This command runs:

  • Laravel development server (php artisan serve)
  • Queue worker (php artisan queue:listen)
  • Log viewer (php artisan pail)

Task decision details

  • CSV imports use a queued job for processing ProcessCsvImportJob.php
  • If a CSV contains duplicate investor_id and investment_data combinations they will be skipped

API Endpoints

Base URL: /api

Postman Collection: A ready-to-use Postman collection is available at Data_Processor_API.postman_collection.json. Import this file into Postman to quickly test all endpoints.

Investor Endpoints

List Investors

GET /api/investors

Query Parameters:

  • per_page (optional): Number of items per page (default: 15)
  • format (optional): Response format - json or csv (default: json)

Example:

GET /api/investors?per_page=10
GET /api/investors?format=csv

Response (JSON):

{
  "data": [
    {
      "id": 1,
      "investor_id": "1001",
      "name": "John Doe",
      "age": 30,
      "investments_sum_investment_amount": "50000.00"
    }
  ],
  "pagination": {
    "current_page": 1,
    "per_page": 15,
    "total": 10,
    "last_page": 1
  }
}

Export Investors as CSV

GET /api/investors?format=csv

Returns a CSV file with columns: investor_id, name, age, total_investment_amount

Import CSV File

POST /api/investors/import

Content-Type: multipart/form-data

Body:

  • file (file, required): CSV file
    • Allowed types: .csv, .txt
    • Max size: 10MB
    • Required columns: investor_id, name, age, investment_amount, investment_date
    • Date format: DD-MM-YYYY (e.g., 15-01-2024)

Example CSV Format:

investor_id,name,age,investment_amount,investment_date
1001,John Doe,30,50000.50,15-01-2024
1002,Jane Smith,45,75000.00,20-02-2024

Response:

{
  "message": "CSV import job queued successfully",
  "job_id": "550e8400-e29b-41d4-a716-446655440000"
}

Status Codes:

  • 202 Accepted: Job queued successfully
  • 422 Unprocessable Entity: Validation errors

Aggregate Endpoints

Get Average Age

GET /api/aggregates/average-age

Response:

{
  "average_age": 45.67
}

Get Average Investment Amount

GET /api/aggregates/average-investment-amount

Response:

{
  "average_investment_amount": 456789.12
}

Get Total Investments Count

GET /api/aggregates/total-investments

Response:

{
  "total_investments": 150
}

Testing

Run the test suite:

composer run test

Or directly:

php artisan test

Database Seeding

Seed the database with sample data from the first 10 rows of investors_with_dates.csv (located in the project root):

php artisan db:seed --class=InvestorsWithDatesSeeder

Project Structure

app/
├── Http/
│   ├── Controllers/
│   │   └── Api/
│   │       ├── AggregateController.php
│   │       └── InvestorController.php
│   └── Requests/
│       └── ImportCsvRequest.php
├── Jobs/
│   └── ProcessCsvImportJob.php
├── Models/
│   ├── Investor.php
│   └── Investment.php
└── Services/
    ├── AggregateService.php
    ├── CsvImportService.php
    ├── InvestorService.php
    └── InvestmentService.php

About

An application for processing investor and investment data from CSV files. This API provides endpoints to import CSV data, list investors, and retrieve aggregate statistics.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages