Senior Software Engineer PAY (Back-End) - Foodics
This is a Laravel-based order management system designed to handle orders, manage product stock levels, and notify merchants when stock is low. The application uses service classes, request validation, and resource classes to ensure clean and maintainable code.
- Order Creation: Customers can place orders containing multiple products.
- Stock Management: The system checks and updates product stock levels after an order.
- Low Stock Notification: Sends email notifications when stock levels drop below a threshold.
- Transactional Integrity: Uses database transactions to ensure data consistency.
- API Responses: Standardized JSON responses for success, errors, and paginated data.
Handles ingredient stock checks and low stock notifications.
Processes orders and links products, ensuring that stock levels are updated.
Provides success()
, errors()
, and paginate()
methods to standardize JSON responses.
Validates incoming requests to ensure they contain valid product IDs and quantities.
Handles HTTP requests related to orders and calls the OrderService
to process orders.
Transforms the Order
model into a standardized JSON structure for API responses.
Ensure you have the following installed:
- PHP (>= 8.2)
- Composer
- SQLite or MySQL
-
Clone the repository:
git clone https://github.com/egyjs/order-management-backend-app.git cd order-management-backend-app
-
Install dependencies:
composer install
-
Create the
.env
file:cp .env.example .env
Configure the
.env
file with your database and email settings. addMERCHANT_EMAIL
to the.env
file to set the email address to receive low-stock notifications. -
Generate the application key:
php artisan key:generate
-
Run migrations and seed the database:
php artisan migrate --seed
-
Run the application:
php artisan serve
Endpoint: POST /orders
Request Body:
{
"products":[
{
"product_id":1,
"qty":1
}
]
}
Response:
- Success (201):
{
"success": true,
"status": 201,
"message": "Order placed successfully.",
"data": {
"id": 1,
"created_at": "2025-01-15T01:57:18.000000Z",
"products": [
{
"id": 1,
"name": "Burger",
"qty": 1
}
]
}
}
- Error (422 - Validation Error):
{
"success": false,
"status": 422,
"message": "The selected products.0.product_id is invalid.",
"errors": {
"products.0.product_id": [
"The selected products.0.product_id is invalid."
]
}
}
- 500 Internal Server Error:
{
"success": false,
"status": 500,
"message": "An unexpected error occurred. Please try again later.",
"errors": {}
}
Configure the database in the .env
file:
for testing purposes, you can use the sqlite database
DB_CONNECTION=sqlite
Configure email settings for low-stock notifications:
MAIL_MAILER=smtp
MAIL_HOST=smtp.example.com
MAIL_PORT=587
MAIL_USERNAME=[email protected]
MAIL_PASSWORD=your-password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=[email protected]
MAIL_FROM_NAME="Order System"
To run tests, use:
php artisan test
Ensure that feature tests cover scenarios for order creation, stock management, and low-stock notifications.
output
❯ php artisan test
PASS Tests\Unit\Controllers\OrderControllerTest
✓ it creates an order successfully 0.42s
✓ it fails to create order due to exception 0.03s
PASS Tests\Unit\Models\IngredientTest
✓ ingredient has product relationship 0.04s
✓ it sends low stock notification 0.04s
✓ low stock notification not sent if already notified 0.03s
PASS Tests\Unit\Requests\StoreOrderRequestTest
✓ validation passes with valid data 0.03s
✓ validation fails with invalid data 0.02s
✓ validation fails when products are empty 0.02s
✓ validation fails without products key 0.02s
PASS Tests\Unit\Services\IngredientServiceTest
✓ has sufficient stock returns true when stock is enough 0.03s
✓ has sufficient stock returns false when stock is insufficient 0.03s
✓ has stock lower than half returns true when stock is below half minimum 0.03s
✓ has stock lower than half returns false when stock is above half minimum 0.02s
✓ notify low stock sends notification when stock is low 0.03s
✓ notify low stock does not send notification if already notified 0.02s
✓ update ingredients stock updates stock levels 0.13s
✓ update ingredients stock throws insufficient stock exception 0.03s
✓ update ingredients stock sends low stock notification 0.03s
PASS Tests\Unit\Services\OrderServiceTest
✓ process order successfully 0.04s
✓ process order throws product not found exception 0.03s
✓ process order throws insufficient stock exception 0.03s
✓ process order with multiple products 0.10s
PASS Tests\Feature\OrderFeatureTest
✓ order creation success 0.04s
✓ order creation fails for invalid product id 0.03s
✓ order creation fails due to insufficient stock 0.05s
✓ order creation fails for invalid quantity 0.03s
✓ low stock notification is sent 0.04s
✓ multiple products order success 0.11s
Tests: 28 passed (74 assertions)
Duration: 1.68s
curl --location 'http://localhost:8000/api/orders' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{
"products":[
{
"product_id":1,
"qty":1
}
]
}'
- Migration Errors: Ensure the database credentials in the
.env
file are correct. - Mail Errors: Check email credentials and make sure your SMTP server allows connections.
- Name: Abdulrahman Elzahaby
- Email: [email protected]