$ git clone https://github.com/amruali/order-script.git
$ cd order-script
$ redis-server --bind 0.0.0.0 --protected-mode no
MONGO_DB_URI=mongodb+srv://username:password@hostname
REDIS_HOST=localhost
REDIS_PORT=6379
PORT=3000
$ docker build -t orderscript .
$ docker run -p 3000:3000 --env-file .env orderscript
@Schema()
export class Item extends Document {
@Prop({ required: true, unique: true })
name: string;
@Prop({ required: true, unique: true })
description: string;
@Prop({ required: true })
price: number;
@Prop({ default: true })
isActive: boolean;
}
@Schema({ timestamps: true })
export class Order extends Document {
@Prop({
type: String,
required: true
})
customerName: string;
@Prop({
type: [{
item: { type: Types.ObjectId, ref: 'Item', required: true },
quantity: { type: Number, required: true, min: 1 },
}],
required: true,
_id: false, // Disable automatic _id creation for the items array
})
items: { item: Types.ObjectId; quantity: number }[];
@Prop({ required: true })
totalAmount: number;
}
curl --location 'http://localhost:3000/items' \
--header 'Content-Type: application/json' \
--data '{
"name": "وجبة فرخة مشوية عالفحم",
"description": "وجبة فرخة مشوية عالفحم تقدم مع الأرز والطحينة و السلطة",
"price": 400,
"isActive": true
}'
## Request
$ curl --location 'http://localhost:3000/orders' \
--header 'Content-Type: application/json' \
--data '{
"customerName": "Hussein",
"items": [
{
"itemId": "67408874bb5763691de5822e",
"quantity": 1
}
]
}'
{
"customerName": "Hussein",
"items": [
{
"item": "67408874bb5763691de5822e",
"quantity": 1
}
],
"totalAmount": 400,
"_id": "6741e93817cc3e8b59a96309",
"createdAt": "2024-11-23T14:39:52.552Z",
"updatedAt": "2024-11-23T14:39:52.552Z",
"__v": 0
}
curl --location --request PUT 'http://localhost:3000/orders/6740ade0705941e0506f30b7' \
--header 'Content-Type: application/json' \
--data '{
"customerName": "Amr Ali",
"items": [
{
"itemId": "67408646bb5763691de5822e",
"quantity": 1
}
]
}'
Daily sales Report using mongo aggregation framework and redis for caching generated reports by endpoint hit
$ curl localhost:3000/orders/daily-report
$ curl localhost:3000/orders/daily-report?date=20-11-2024
{
"totalRevenue": 2400,
"totalOrders": 6,
"topSellingItems": [
{
"itemId": "67408874bb5763691de5822e",
"name": "وجبة فرخة مشوية عالفحم",
"quantitySold": 6,
"totalSales": 2400
}
]
}