# Image Processing System Implementation Prompt

## 1. Requirements Analysis

1.1. System Overview:
- Develop an asynchronous image processing system that accepts CSV files containing product and image information.
- The system should validate, process, and store image data, providing status updates to users.

1.2. Functional Requirements:
- CSV Upload API: Accept and validate CSV files, return a unique request ID.
- Status Check API: Allow users to query processing status using the request ID.
- Asynchronous Image Processing: Compress images to 50% of original quality.
- Data Storage: Save processed image data and product information in a database.
- Webhook Integration (Bonus): Implement a webhook flow to notify after processing all images.

1.3. Technical Stack:
- Backend: Node.js with TypeScript and Express
- Database: PostgreSQL with Prisma ORM
- Image Processing: Use a suitable Node.js library (e.g., Sharp)

1.4. CSV Format:
- Input CSV: Serial Number, Product Name, Input Image URLs (comma-separated)
- Output CSV: Serial Number, Product Name, Input Image URLs, Output Image URLs

## 2. System Design

2.1. High-Level Architecture:
- Web Server: Express.js application
- Database: PostgreSQL
- ORM: Prisma
- Queue System: Bull (Redis-based queue for Node.js)
- Image Processing Service: Separate worker process

2.2. Component Breakdown:
- API Layer: Express.js routes and controllers
- Service Layer: Business logic implementation
- Data Access Layer: Prisma ORM for database operations
- Queue Management: Bull for managing asynchronous tasks
- Image Processing Worker: Separate process for image compression
- Webhook Service: For sending notifications after processing

2.3. Database Schema (Prisma model):
```prisma
model Product {
  id          Int      @id @default(autoincrement())
  serialNumber Int
  name        String
  inputUrls   String[]
  outputUrls  String[]
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt
}

model ProcessingRequest {
  id        String   @id @default(uuid())
  status    String   // e.g., "PENDING", "PROCESSING", "COMPLETED", "FAILED"
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}
```

## 3. Implementation

3.1. Project Setup:
- Initialize a new Node.js project with TypeScript
- Set up Express.js server
- Configure Prisma with PostgreSQL
- Install necessary dependencies (csv-parser, bull, sharp, etc.)

3.2. API Development:
- Implement CSV Upload API:
  - Create route for file upload
  - Validate CSV format
  - Generate and return a unique request ID
  - Queue the processing task

- Implement Status Check API:
  - Create route for status check
  - Query database for processing status
  - Return status to the user

3.3. CSV Processing:
- Develop CSV parsing logic
- Validate CSV data structure and content

3.4. Image Processing:
- Implement image downloading functionality
- Create image compression logic (50% quality reduction)
- Develop logic to save processed images

3.5. Database Operations:
- Implement Prisma models for Product and ProcessingRequest
- Create database operations for storing and retrieving data

3.6. Asynchronous Processing:
- Set up Bull queue for managing processing tasks
- Implement worker process for asynchronous image processing

3.7. Webhook Integration (Bonus):
- Implement webhook sending functionality
- Trigger webhook after processing all images in a request