This is a coding challenge for Foodics Senior PHP Developer. The challenge is to build a web application that allows users manage their own restaurant stock.
-
Clone the repository
git clone https://github.com/nagi1/foodics-coding-challenge.git
-
Install the dependencies
composer install
-
Copy the
.env.example
file to.env
and update the database credentialscp .env.example .env
-
Generate the application key
php artisan key:generate
-
Run the migrations --seed
php artisan migrate
-
Product
-
Ingredient
-
Order
-
Product has many Ingredients
-
Order has many Products (items)
-
stock: represents the amount of the ingredient in the stock.
-
max_stock: represents the maximum level of the ingredient in the stock.
-
notified_at: represents the date of the last notification sent to the merchant.
-
unit: represents the unit of the ingredient (kg, g, etc...).
-
quantity: represents the amount of the ingredient used in the product.
-
weight: represents the weight of the ingredient used in the product.
-
unit: represents the unit of the ingredient used in the product.
-
User send
POST
API to/api/orders
with the order details. The order details contains the products and the quantity of each product. -
The
OrderController
validates the request json. -
The
OrderController
resolve theOrderService
and call thecreate
method. -
if the order service returned a status
NOT_ENOUGH_INGREDIENTS
theOrderController
will return a422
response with the messageNot enough ingredients
. -
OrderService
Will do the follwing: -
Check if it has enough ingredients to make the order.
-
Start a transaction. and calculate the price of the order.
-
create the order.
-
attach the products to the order.
-
Fire the
OrderCreated
event. -
The
OrderCreated
event will be handled triggerUpdateIngredientsStock
. -
The
UpdateIngredientsStock
will check if the ingredients stock is below 50% and will update the ingredients stock. -
Filter out ingredients that have been notified before. and send an email to the admin.
ResetNotifiedIngredientWhenGetRefiledJob
will reset thenotified_at
column of the ingredients that have been notified before and the stock is above 50%. This job will run every 5 minutes.
-
Run the tests
vendor/bin/pest
In a system that has three main models; Product, Ingredient, and Order.
A Burger (Product) may have several ingredients:
-
150g Beef
-
30g Cheese
-
20g Onion
The system keeps the stock of each of these ingredients stored in the database. You can use the following levels for seeding the database:
-
20kg Beef
-
5kg Cheese
-
1kg Onion
When a customer makes an order that includes a Burger. The system needs to update the stock of each of the ingredients so it reflects the amounts consumed. Also when any of the ingredients stock level reaches 50%, the system should send an email message to alert the merchant they need to buy more of this ingredient.
First, Write a controller action that:
-
Accepts the order details from the request payload.
-
Persists the Order in the database.
-
Updates the stock of the ingredients. Second, ensure that en email is sent once the level of any of the ingredients reach below 50%. Only a single email should be sent, further consumption of the same ingredient below 50% shouldn't trigger an email.
Finally, write several test cases that assert the order was correctly stored and the stock was correctly updated.
The incoming payload may look like this:
{ "products": [ { "product_id": 1, "quantity": 2 } ] }